blob: daf86fd8f2541570832a9f2150f6ffe041e00cc9 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
David Sehrc431b9d2018-03-02 12:01:51 -080029#include "base/quasi_atomic.h"
Vladimir Marko60584552015-09-03 13:35:12 +000030#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010031#include "base/transform_array_ref.h"
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +010032#include "art_method.h"
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010033#include "data_type.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "deoptimization_kind.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file.h"
36#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080037#include "dex/invoke_type.h"
David Sehr312f3b22018-03-19 08:39:26 -070038#include "dex/method_reference.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000039#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "handle.h"
41#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010042#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010043#include "locations.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000044#include "mirror/class.h"
Orion Hodson18259d72018-04-12 11:18:23 +010045#include "mirror/method_type.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010046#include "offsets.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010047#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048
49namespace art {
50
Vladimir Markoca6fff82017-10-03 14:49:14 +010051class ArenaStack;
David Brazdil1abb4192015-02-17 18:33:36 +000052class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000053class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070054class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010055class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010057class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000059class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000061class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000062class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000063class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000064class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000065class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070066class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010067class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010068class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010069class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010070class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000071class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010072class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000073class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074
Mathieu Chartier736b5602015-09-02 14:54:11 -070075namespace mirror {
76class DexCache;
77} // namespace mirror
78
Nicolas Geoffray818f2102014-02-18 16:43:35 +000079static const int kDefaultNumberOfBlocks = 8;
80static const int kDefaultNumberOfSuccessors = 2;
81static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010082static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010083static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000084static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000085
Roland Levillain5b5b9312016-03-22 14:57:31 +000086// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
87static constexpr int32_t kMaxIntShiftDistance = 0x1f;
88// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
89static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000090
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010091static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070092static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010093
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010094static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
95
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060096static constexpr uint32_t kNoDexPc = -1;
97
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010098inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
99 // For the purposes of the compiler, the dex files must actually be the same object
100 // if we want to safely treat them as the same. This is especially important for JIT
101 // as custom class loaders can open the same underlying file (or memory) multiple
102 // times and provide different class resolution but no two class loaders should ever
103 // use the same DexFile object - doing so is an unsupported hack that can lead to
104 // all sorts of weird failures.
105 return &lhs == &rhs;
106}
107
Dave Allison20dfc792014-06-16 20:44:29 -0700108enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700109 // All types.
110 kCondEQ, // ==
111 kCondNE, // !=
112 // Signed integers and floating-point numbers.
113 kCondLT, // <
114 kCondLE, // <=
115 kCondGT, // >
116 kCondGE, // >=
117 // Unsigned integers.
118 kCondB, // <
119 kCondBE, // <=
120 kCondA, // >
121 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100122 // First and last aliases.
123 kCondFirst = kCondEQ,
124 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700125};
126
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000127enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000128 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000129 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000130 kAnalysisFailThrowCatchLoop,
131 kAnalysisFailAmbiguousArrayOp,
132 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000133};
134
Andreas Gampe9186ced2016-12-12 14:28:21 -0800135template <typename T>
136static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
137 return static_cast<typename std::make_unsigned<T>::type>(x);
138}
139
Vladimir Markof9f64412015-09-02 14:05:49 +0100140class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100141 public:
142 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
143
144 void AddInstruction(HInstruction* instruction);
145 void RemoveInstruction(HInstruction* instruction);
146
David Brazdilc3d743f2015-04-22 13:40:50 +0100147 // Insert `instruction` before/after an existing instruction `cursor`.
148 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
149 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
150
Roland Levillain6b469232014-09-25 10:10:38 +0100151 // Return true if this list contains `instruction`.
152 bool Contains(HInstruction* instruction) const;
153
Roland Levillainccc07a92014-09-16 14:48:16 +0100154 // Return true if `instruction1` is found before `instruction2` in
155 // this instruction list and false otherwise. Abort if none
156 // of these instructions is found.
157 bool FoundBefore(const HInstruction* instruction1,
158 const HInstruction* instruction2) const;
159
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000160 bool IsEmpty() const { return first_instruction_ == nullptr; }
161 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
162
163 // Update the block of all instructions to be `block`.
164 void SetBlockOfInstructions(HBasicBlock* block) const;
165
166 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000167 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000168 void Add(const HInstructionList& instruction_list);
169
David Brazdil2d7352b2015-04-20 14:52:42 +0100170 // Return the number of instructions in the list. This is an expensive operation.
171 size_t CountSize() const;
172
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 private:
174 HInstruction* first_instruction_;
175 HInstruction* last_instruction_;
176
177 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000178 friend class HGraph;
179 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100180 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000181 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100182 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100183
184 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
185};
186
David Brazdil4833f5a2015-12-16 10:37:39 +0000187class ReferenceTypeInfo : ValueObject {
188 public:
189 typedef Handle<mirror::Class> TypeHandle;
190
Vladimir Markoa1de9182016-02-25 11:37:38 +0000191 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
192
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700193 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100194 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
195 }
196
Vladimir Markoa1de9182016-02-25 11:37:38 +0000197 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000198 return ReferenceTypeInfo(type_handle, is_exact);
199 }
200
201 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
202
Vladimir Markof39745e2016-01-26 12:16:55 +0000203 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return handle.GetReference() != nullptr;
205 }
206
Vladimir Marko456307a2016-04-19 14:12:13 +0000207 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000208 return IsValidHandle(type_handle_);
209 }
210
211 bool IsExact() const { return is_exact_; }
212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000214 DCHECK(IsValid());
215 return GetTypeHandle()->IsObjectClass();
216 }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return GetTypeHandle()->IsStringClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return GetTypeHandle()->IsInterface();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 return GetTypeHandle()->IsArrayClass();
236 }
237
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000239 DCHECK(IsValid());
240 return GetTypeHandle()->IsPrimitiveArray();
241 }
242
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000244 DCHECK(IsValid());
245 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
253 }
254
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700255 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000256 DCHECK(IsValid());
257 if (!IsExact()) return false;
258 if (!IsArrayClass()) return false;
259 if (!rti.IsArrayClass()) return false;
260 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
261 rti.GetTypeHandle()->GetComponentType());
262 }
263
264 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
265
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700266 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000267 DCHECK(IsValid());
268 DCHECK(rti.IsValid());
269 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700272 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000273 DCHECK(IsValid());
274 DCHECK(rti.IsValid());
275 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
276 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
277 }
278
279 // Returns true if the type information provide the same amount of details.
280 // Note that it does not mean that the instructions have the same actual type
281 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700282 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000283 if (!IsValid() && !rti.IsValid()) {
284 // Invalid types are equal.
285 return true;
286 }
287 if (!IsValid() || !rti.IsValid()) {
288 // One is valid, the other not.
289 return false;
290 }
291 return IsExact() == rti.IsExact()
292 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
293 }
294
295 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000296 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
297 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
298 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000299
300 // The class of the object.
301 TypeHandle type_handle_;
302 // Whether or not the type is exact or a superclass of the actual type.
303 // Whether or not we have any information about this type.
304 bool is_exact_;
305};
306
307std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
308
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000309// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100310class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000311 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100312 HGraph(ArenaAllocator* allocator,
313 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 const DexFile& dex_file,
315 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100317 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000319 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100320 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100321 : allocator_(allocator),
322 arena_stack_(arena_stack),
323 blocks_(allocator->Adapter(kArenaAllocBlockList)),
324 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
325 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700326 entry_block_(nullptr),
327 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100328 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100329 number_of_vregs_(0),
330 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000331 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400332 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000333 has_try_catch_(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),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000337 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000338 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100339 dex_file_(dex_file),
340 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100341 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800343 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000345 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100346 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
347 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
349 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000350 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100351 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000352 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700353 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100354 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100355 blocks_.reserve(kDefaultNumberOfBlocks);
356 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000357
David Brazdilbadd8262016-02-02 16:28:56 +0000358 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700359 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000360
Vladimir Markoca6fff82017-10-03 14:49:14 +0100361 ArenaAllocator* GetAllocator() const { return allocator_; }
362 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100363 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
364
David Brazdil69ba7b72015-06-23 18:27:30 +0100365 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000366 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100367
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000368 HBasicBlock* GetEntryBlock() const { return entry_block_; }
369 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100370 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000371
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000372 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
373 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000374
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100376
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100377 void ComputeDominanceInformation();
378 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000379 void ClearLoopInformation();
380 void FindBackEdges(ArenaBitVector* visited);
381 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100382 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100383 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000384
David Brazdil4833f5a2015-12-16 10:37:39 +0000385 // Analyze all natural loops in this graph. Returns a code specifying that it
386 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000387 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000388 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100389
David Brazdilffee3d32015-07-06 11:48:53 +0100390 // Iterate over blocks to compute try block membership. Needs reverse post
391 // order and loop information.
392 void ComputeTryBlockInformation();
393
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000394 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000395 // Returns the instruction to replace the invoke expression or null if the
396 // invoke is for a void method. Note that the caller is responsible for replacing
397 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000398 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000399
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000400 // Update the loop and try membership of `block`, which was spawned from `reference`.
401 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
402 // should be the new back edge.
403 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
404 HBasicBlock* reference,
405 bool replace_if_back_edge);
406
Mingyao Yang3584bce2015-05-19 16:01:59 -0700407 // Need to add a couple of blocks to test if the loop body is entered and
408 // put deoptimization instructions, etc.
409 void TransformLoopHeaderForBCE(HBasicBlock* header);
410
Aart Bikf8f5a162017-02-06 15:35:29 -0800411 // Adds a new loop directly after the loop with the given header and exit.
412 // Returns the new preheader.
413 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
414 HBasicBlock* body,
415 HBasicBlock* exit);
416
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000417 // Removes `block` from the graph. Assumes `block` has been disconnected from
418 // other blocks and has no instructions or phis.
419 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000420
David Brazdilfc6a86a2015-06-26 10:33:45 +0000421 // Splits the edge between `block` and `successor` while preserving the
422 // indices in the predecessor/successor lists. If there are multiple edges
423 // between the blocks, the lowest indices are used.
424 // Returns the new block which is empty and has the same dex pc as `successor`.
425 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
426
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100427 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100428 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000429
430 // Transform a loop into a format with a single preheader.
431 //
432 // Each phi in the header should be split: original one in the header should only hold
433 // inputs reachable from the back edges and a single input from the preheader. The newly created
434 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
435 //
436 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
437 // that no longer have this property.
438 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
439
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100440 void SimplifyLoop(HBasicBlock* header);
441
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100443 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000444 return current_instruction_id_++;
445 }
446
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000447 int32_t GetCurrentInstructionId() const {
448 return current_instruction_id_;
449 }
450
451 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100452 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000453 current_instruction_id_ = id;
454 }
455
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100456 uint16_t GetMaximumNumberOfOutVRegs() const {
457 return maximum_number_of_out_vregs_;
458 }
459
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000460 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
461 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100462 }
463
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100464 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
465 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
466 }
467
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000468 void UpdateTemporariesVRegSlots(size_t slots) {
469 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100470 }
471
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000472 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100473 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000474 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100475 }
476
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100477 void SetNumberOfVRegs(uint16_t number_of_vregs) {
478 number_of_vregs_ = number_of_vregs;
479 }
480
481 uint16_t GetNumberOfVRegs() const {
482 return number_of_vregs_;
483 }
484
485 void SetNumberOfInVRegs(uint16_t value) {
486 number_of_in_vregs_ = value;
487 }
488
David Brazdildee58d62016-04-07 09:54:26 +0000489 uint16_t GetNumberOfInVRegs() const {
490 return number_of_in_vregs_;
491 }
492
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100493 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100494 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100495 return number_of_vregs_ - number_of_in_vregs_;
496 }
497
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100498 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100499 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100500 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100501
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100502 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
503 DCHECK(GetReversePostOrder()[0] == entry_block_);
504 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
505 }
506
507 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
508 return ReverseRange(GetReversePostOrder());
509 }
510
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100511 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100512 return linear_order_;
513 }
514
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100515 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
516 return ReverseRange(GetLinearOrder());
517 }
518
Mark Mendell1152c922015-04-24 17:06:35 -0400519 bool HasBoundsChecks() const {
520 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800521 }
522
Mark Mendell1152c922015-04-24 17:06:35 -0400523 void SetHasBoundsChecks(bool value) {
524 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800525 }
526
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000527 bool IsDebuggable() const { return debuggable_; }
528
David Brazdil8d5b8b22015-03-24 10:51:52 +0000529 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 // already, it is created and inserted into the graph. This method is only for
531 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100532 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000533
534 // TODO: This is problematic for the consistency of reference type propagation
535 // because it can be created anytime after the pass and thus it will be left
536 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600537 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000538
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600539 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
540 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000541 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600542 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
543 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000544 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600545 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
546 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000547 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600548 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
549 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000550 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000551
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100552 HCurrentMethod* GetCurrentMethod();
553
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100554 const DexFile& GetDexFile() const {
555 return dex_file_;
556 }
557
558 uint32_t GetMethodIdx() const {
559 return method_idx_;
560 }
561
Igor Murashkind01745e2017-04-05 16:40:31 -0700562 // Get the method name (without the signature), e.g. "<init>"
563 const char* GetMethodName() const;
564
565 // Get the pretty method name (class + name + optionally signature).
566 std::string PrettyMethod(bool with_signature = true) const;
567
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100568 InvokeType GetInvokeType() const {
569 return invoke_type_;
570 }
571
Mark Mendellc4701932015-04-10 13:18:51 -0400572 InstructionSet GetInstructionSet() const {
573 return instruction_set_;
574 }
575
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000576 bool IsCompilingOsr() const { return osr_; }
577
Mingyao Yang063fc772016-08-02 11:02:54 -0700578 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
579 return cha_single_implementation_list_;
580 }
581
582 void AddCHASingleImplementationDependency(ArtMethod* method) {
583 cha_single_implementation_list_.insert(method);
584 }
585
586 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800587 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700588 }
589
David Brazdil77a48ae2015-09-15 12:34:04 +0000590 bool HasTryCatch() const { return has_try_catch_; }
591 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100592
Aart Bikb13c65b2017-03-21 20:14:07 -0700593 bool HasSIMD() const { return has_simd_; }
594 void SetHasSIMD(bool value) { has_simd_ = value; }
595
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800596 bool HasLoops() const { return has_loops_; }
597 void SetHasLoops(bool value) { has_loops_ = value; }
598
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000599 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
600 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
601
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100602 ArtMethod* GetArtMethod() const { return art_method_; }
603 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
604
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100605 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500606 // The instruction has been inserted into the graph, either as a constant, or
607 // before cursor.
608 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
609
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000610 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
611
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800612 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
613 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
614 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
615
David Brazdil2d7352b2015-04-20 14:52:42 +0100616 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000617 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100618 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000619
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000620 template <class InstructionType, typename ValueType>
621 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600622 ArenaSafeMap<ValueType, InstructionType*>* cache,
623 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 // Try to find an existing constant of the given value.
625 InstructionType* constant = nullptr;
626 auto cached_constant = cache->find(value);
627 if (cached_constant != cache->end()) {
628 constant = cached_constant->second;
629 }
630
631 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100632 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000633 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100634 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000635 cache->Overwrite(value, constant);
636 InsertConstant(constant);
637 }
638 return constant;
639 }
640
David Brazdil8d5b8b22015-03-24 10:51:52 +0000641 void InsertConstant(HConstant* instruction);
642
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000643 // Cache a float constant into the graph. This method should only be
644 // called by the SsaBuilder when creating "equivalent" instructions.
645 void CacheFloatConstant(HFloatConstant* constant);
646
647 // See CacheFloatConstant comment.
648 void CacheDoubleConstant(HDoubleConstant* constant);
649
Vladimir Markoca6fff82017-10-03 14:49:14 +0100650 ArenaAllocator* const allocator_;
651 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000652
653 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100654 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000655
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100656 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100657 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000658
Aart Bik281c6812016-08-26 11:31:48 -0700659 // List of blocks to perform a linear order tree traversal. Unlike the reverse
660 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100661 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100662
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000663 HBasicBlock* entry_block_;
664 HBasicBlock* exit_block_;
665
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100666 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100667 uint16_t maximum_number_of_out_vregs_;
668
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100669 // The number of virtual registers in this method. Contains the parameters.
670 uint16_t number_of_vregs_;
671
672 // The number of virtual registers used by parameters of this method.
673 uint16_t number_of_in_vregs_;
674
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000675 // Number of vreg size slots that the temporaries use (used in baseline compiler).
676 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100677
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800678 // Flag whether there are bounds checks in the graph. We can skip
679 // BCE if it's false. It's only best effort to keep it up to date in
680 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400681 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800682
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800683 // Flag whether there are try/catch blocks in the graph. We will skip
684 // try/catch-related passes if it's false. It's only best effort to keep
685 // it up to date in the presence of code elimination so there might be
686 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000687 bool has_try_catch_;
688
Aart Bikb13c65b2017-03-21 20:14:07 -0700689 // Flag whether SIMD instructions appear in the graph. If true, the
690 // code generators may have to be more careful spilling the wider
691 // contents of SIMD registers.
692 bool has_simd_;
693
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800694 // Flag whether there are any loops in the graph. We can skip loop
695 // optimization if it's false. It's only best effort to keep it up
696 // to date in the presence of code elimination so there might be false
697 // positives.
698 bool has_loops_;
699
700 // Flag whether there are any irreducible loops in the graph. It's only
701 // best effort to keep it up to date in the presence of code elimination
702 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000703 bool has_irreducible_loops_;
704
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000705 // Indicates whether the graph should be compiled in a way that
706 // ensures full debuggability. If false, we can apply more
707 // aggressive optimizations that may limit the level of debugging.
708 const bool debuggable_;
709
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000710 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000711 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000712
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100713 // The dex file from which the method is from.
714 const DexFile& dex_file_;
715
716 // The method index in the dex file.
717 const uint32_t method_idx_;
718
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100719 // If inlined, this encodes how the callee is being invoked.
720 const InvokeType invoke_type_;
721
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100722 // Whether the graph has been transformed to SSA form. Only used
723 // in debug mode to ensure we are not using properties only valid
724 // for non-SSA form (like the number of temporaries).
725 bool in_ssa_form_;
726
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800727 // Number of CHA guards in the graph. Used to short-circuit the
728 // CHA guard optimization pass when there is no CHA guard left.
729 uint32_t number_of_cha_guards_;
730
Mathieu Chartiere401d142015-04-22 13:56:20 -0700731 const InstructionSet instruction_set_;
732
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000733 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000734 HNullConstant* cached_null_constant_;
735 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000736 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000737 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000738 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000739
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100740 HCurrentMethod* cached_current_method_;
741
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100742 // The ArtMethod this graph is for. Note that for AOT, it may be null,
743 // for example for methods whose declaring class could not be resolved
744 // (such as when the superclass could not be found).
745 ArtMethod* art_method_;
746
David Brazdil4833f5a2015-12-16 10:37:39 +0000747 // Keep the RTI of inexact Object to avoid having to pass stack handle
748 // collection pointer to passes which may create NullConstant.
749 ReferenceTypeInfo inexact_object_rti_;
750
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000751 // Whether we are compiling this graph for on stack replacement: this will
752 // make all loops seen as irreducible and emit special stack maps to mark
753 // compiled code entries which the interpreter can directly jump to.
754 const bool osr_;
755
Mingyao Yang063fc772016-08-02 11:02:54 -0700756 // List of methods that are assumed to have single implementation.
757 ArenaSet<ArtMethod*> cha_single_implementation_list_;
758
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000759 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100760 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000761 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000762 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000763 DISALLOW_COPY_AND_ASSIGN(HGraph);
764};
765
Vladimir Markof9f64412015-09-02 14:05:49 +0100766class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767 public:
768 HLoopInformation(HBasicBlock* header, HGraph* graph)
769 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100770 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000771 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100772 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100773 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100774 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100775 blocks_(graph->GetAllocator(),
776 graph->GetBlocks().size(),
777 true,
778 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100779 back_edges_.reserve(kDefaultNumberOfBackEdges);
780 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000782 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100783 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000784
785 void Dump(std::ostream& os);
786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 HBasicBlock* GetHeader() const {
788 return header_;
789 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791 void SetHeader(HBasicBlock* block) {
792 header_ = block;
793 }
794
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100795 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
796 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
797 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
798
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000799 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100800 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000801 }
802
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100805 }
806
David Brazdil46e2a392015-03-16 17:31:52 +0000807 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100808 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 }
810
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000811 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100812 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000813 }
814
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100815 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100816
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100817 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100818 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100819 }
820
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100821 // Returns the lifetime position of the back edge that has the
822 // greatest lifetime position.
823 size_t GetLifetimeEnd() const;
824
825 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100826 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100827 }
828
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000829 // Finds blocks that are part of this loop.
830 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100831
Artem Serov7f4aff62017-06-21 17:02:18 +0100832 // Updates blocks population of the loop and all of its outer' ones recursively after the
833 // population of the inner loop is updated.
834 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
835
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836 // Returns whether this loop information contains `block`.
837 // Note that this loop information *must* be populated before entering this function.
838 bool Contains(const HBasicBlock& block) const;
839
840 // Returns whether this loop information is an inner loop of `other`.
841 // Note that `other` *must* be populated before entering this function.
842 bool IsIn(const HLoopInformation& other) const;
843
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800844 // Returns true if instruction is not defined within this loop.
845 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700846
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100847 const ArenaBitVector& GetBlocks() const { return blocks_; }
848
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000849 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000850 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000851
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000852 void ClearAllBlocks() {
853 blocks_.ClearAllBits();
854 }
855
David Brazdil3f4a5222016-05-06 12:46:21 +0100856 bool HasBackEdgeNotDominatedByHeader() const;
857
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100858 bool IsPopulated() const {
859 return blocks_.GetHighestBitSet() != -1;
860 }
861
Anton Shaminf89381f2016-05-16 16:44:13 +0600862 bool DominatesAllBackEdges(HBasicBlock* block);
863
David Sehrc757dec2016-11-04 15:48:34 -0700864 bool HasExitEdge() const;
865
Artem Serov7f4aff62017-06-21 17:02:18 +0100866 // Resets back edge and blocks-in-loop data.
867 void ResetBasicBlockData() {
868 back_edges_.clear();
869 ClearAllBlocks();
870 }
871
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000872 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100873 // Internal recursive implementation of `Populate`.
874 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100875 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100876
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000877 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100878 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000879 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100880 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100881 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100882 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000883
884 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
885};
886
David Brazdilec16f792015-08-19 15:04:01 +0100887// Stores try/catch information for basic blocks.
888// Note that HGraph is constructed so that catch blocks cannot simultaneously
889// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100890class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100891 public:
892 // Try block information constructor.
893 explicit TryCatchInformation(const HTryBoundary& try_entry)
894 : try_entry_(&try_entry),
895 catch_dex_file_(nullptr),
896 catch_type_index_(DexFile::kDexNoIndex16) {
897 DCHECK(try_entry_ != nullptr);
898 }
899
900 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800901 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100902 : try_entry_(nullptr),
903 catch_dex_file_(&dex_file),
904 catch_type_index_(catch_type_index) {}
905
906 bool IsTryBlock() const { return try_entry_ != nullptr; }
907
908 const HTryBoundary& GetTryEntry() const {
909 DCHECK(IsTryBlock());
910 return *try_entry_;
911 }
912
913 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
914
915 bool IsCatchAllTypeIndex() const {
916 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800917 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100918 }
919
Andreas Gampea5b09a62016-11-17 15:21:22 -0800920 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100921 DCHECK(IsCatchBlock());
922 return catch_type_index_;
923 }
924
925 const DexFile& GetCatchDexFile() const {
926 DCHECK(IsCatchBlock());
927 return *catch_dex_file_;
928 }
929
930 private:
931 // One of possibly several TryBoundary instructions entering the block's try.
932 // Only set for try blocks.
933 const HTryBoundary* try_entry_;
934
935 // Exception type information. Only set for catch blocks.
936 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800937 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100938};
939
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100940static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100941static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100942
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000943// A block in a method. Contains the list of instructions represented
944// as a double linked list. Each block knows its predecessors and
945// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100946
Vladimir Markof9f64412015-09-02 14:05:49 +0100947class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700949 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000950 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100951 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
952 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000953 loop_information_(nullptr),
954 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100955 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100956 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100957 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100958 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000959 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000960 try_catch_information_(nullptr) {
961 predecessors_.reserve(kDefaultNumberOfPredecessors);
962 successors_.reserve(kDefaultNumberOfSuccessors);
963 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
964 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000965
Vladimir Marko60584552015-09-03 13:35:12 +0000966 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100967 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968 }
969
Vladimir Marko60584552015-09-03 13:35:12 +0000970 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100971 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972 }
973
David Brazdild26a4112015-11-10 11:07:31 +0000974 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
975 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
976
Vladimir Marko60584552015-09-03 13:35:12 +0000977 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
978 return ContainsElement(successors_, block, start_from);
979 }
980
981 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100982 return dominated_blocks_;
983 }
984
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100985 bool IsEntryBlock() const {
986 return graph_->GetEntryBlock() == this;
987 }
988
989 bool IsExitBlock() const {
990 return graph_->GetExitBlock() == this;
991 }
992
David Brazdil46e2a392015-03-16 17:31:52 +0000993 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200994 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700995 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000996 bool IsSingleTryBoundary() const;
997
998 // Returns true if this block emits nothing but a jump.
999 bool IsSingleJump() const {
1000 HLoopInformation* loop_info = GetLoopInformation();
1001 return (IsSingleGoto() || IsSingleTryBoundary())
1002 // Back edges generate a suspend check.
1003 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1004 }
David Brazdil46e2a392015-03-16 17:31:52 +00001005
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001006 void AddBackEdge(HBasicBlock* back_edge) {
1007 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001008 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001009 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001010 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001011 loop_information_->AddBackEdge(back_edge);
1012 }
1013
Artem Serov7f4aff62017-06-21 17:02:18 +01001014 // Registers a back edge; if the block was not a loop header before the call associates a newly
1015 // created loop info with it.
1016 //
1017 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1018 // info for all blocks during back edges recalculation.
1019 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1020 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1021 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1022 }
1023 loop_information_->AddBackEdge(back_edge);
1024 }
1025
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001026 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001028
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001029 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001030 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001031 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001032
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001033 HBasicBlock* GetDominator() const { return dominator_; }
1034 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001035 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1036
1037 void RemoveDominatedBlock(HBasicBlock* block) {
1038 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001039 }
Vladimir Marko60584552015-09-03 13:35:12 +00001040
1041 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1042 ReplaceElement(dominated_blocks_, existing, new_block);
1043 }
1044
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001045 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001046
1047 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001048 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001049 }
1050
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001051 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1052 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001053 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001054 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001055 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1056 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001057
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001058 HInstruction* GetFirstInstructionDisregardMoves() const;
1059
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001060 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001061 successors_.push_back(block);
1062 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001063 }
1064
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001065 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1066 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001067 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001068 new_block->predecessors_.push_back(this);
1069 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001070 }
1071
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001072 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1073 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001074 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001075 new_block->successors_.push_back(this);
1076 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 }
1078
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001079 // Insert `this` between `predecessor` and `successor. This method
1080 // preserves the indicies, and will update the first edge found between
1081 // `predecessor` and `successor`.
1082 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1083 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001084 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001085 successor->predecessors_[predecessor_index] = this;
1086 predecessor->successors_[successor_index] = this;
1087 successors_.push_back(successor);
1088 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001089 }
1090
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001091 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001092 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001093 }
1094
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001095 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001096 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001097 }
1098
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001099 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001100 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001101 }
1102
1103 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001104 predecessors_.push_back(block);
1105 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001106 }
1107
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001108 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001109 DCHECK_EQ(predecessors_.size(), 2u);
1110 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001111 }
1112
David Brazdil769c9e52015-04-27 13:54:09 +01001113 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001114 DCHECK_EQ(successors_.size(), 2u);
1115 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001116 }
1117
David Brazdilfc6a86a2015-06-26 10:33:45 +00001118 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001119 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001120 }
1121
David Brazdilfc6a86a2015-06-26 10:33:45 +00001122 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001123 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001124 }
1125
David Brazdilfc6a86a2015-06-26 10:33:45 +00001126 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001127 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001128 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001129 }
1130
1131 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001132 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001133 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001134 }
1135
1136 // Returns whether the first occurrence of `predecessor` in the list of
1137 // predecessors is at index `idx`.
1138 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001139 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001140 return GetPredecessorIndexOf(predecessor) == idx;
1141 }
1142
David Brazdild7558da2015-09-22 13:04:14 +01001143 // Create a new block between this block and its predecessors. The new block
1144 // is added to the graph, all predecessor edges are relinked to it and an edge
1145 // is created to `this`. Returns the new empty block. Reverse post order or
1146 // loop and try/catch information are not updated.
1147 HBasicBlock* CreateImmediateDominator();
1148
David Brazdilfc6a86a2015-06-26 10:33:45 +00001149 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001150 // created, latter block. Note that this method will add the block to the
1151 // graph, create a Goto at the end of the former block and will create an edge
1152 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001153 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001154 HBasicBlock* SplitBefore(HInstruction* cursor);
1155
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001156 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001157 // created block. Note that this method just updates raw block information,
1158 // like predecessors, successors, dominators, and instruction list. It does not
1159 // update the graph, reverse post order, loop information, nor make sure the
1160 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001161 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1162
1163 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1164 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001165
1166 // Merge `other` at the end of `this`. Successors and dominated blocks of
1167 // `other` are changed to be successors and dominated blocks of `this`. Note
1168 // that this method does not update the graph, reverse post order, loop
1169 // information, nor make sure the blocks are consistent (for example ending
1170 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001171 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001172
1173 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1174 // of `this` are moved to `other`.
1175 // Note that this method does not update the graph, reverse post order, loop
1176 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001177 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001178 void ReplaceWith(HBasicBlock* other);
1179
Aart Bik6b69e0a2017-01-11 10:20:43 -08001180 // Merges the instructions of `other` at the end of `this`.
1181 void MergeInstructionsWith(HBasicBlock* other);
1182
David Brazdil2d7352b2015-04-20 14:52:42 +01001183 // Merge `other` at the end of `this`. This method updates loops, reverse post
1184 // order, links to predecessors, successors, dominators and deletes the block
1185 // from the graph. The two blocks must be successive, i.e. `this` the only
1186 // predecessor of `other` and vice versa.
1187 void MergeWith(HBasicBlock* other);
1188
1189 // Disconnects `this` from all its predecessors, successors and dominator,
1190 // removes it from all loops it is included in and eventually from the graph.
1191 // The block must not dominate any other block. Predecessors and successors
1192 // are safely updated.
1193 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001194
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001195 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001196 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001197 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001198 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001199 // Replace phi `initial` with `replacement` within this block.
1200 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001201 // Replace instruction `initial` with `replacement` within this block.
1202 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1203 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001204 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001205 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001206 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1207 // instruction list. With 'ensure_safety' set to true, it verifies that the
1208 // instruction is not in use and removes it from the use lists of its inputs.
1209 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1210 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001211 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001212
1213 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001214 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001215 }
1216
Roland Levillain6b879dd2014-09-22 17:13:44 +01001217 bool IsLoopPreHeaderFirstPredecessor() const {
1218 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001219 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001220 }
1221
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001222 bool IsFirstPredecessorBackEdge() const {
1223 DCHECK(IsLoopHeader());
1224 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1225 }
1226
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001227 HLoopInformation* GetLoopInformation() const {
1228 return loop_information_;
1229 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001231 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001232 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001233 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001234 void SetInLoop(HLoopInformation* info) {
1235 if (IsLoopHeader()) {
1236 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001237 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001238 loop_information_ = info;
1239 } else if (loop_information_->Contains(*info->GetHeader())) {
1240 // Block is currently part of an outer loop. Make it part of this inner loop.
1241 // Note that a non loop header having a loop information means this loop information
1242 // has already been populated
1243 loop_information_ = info;
1244 } else {
1245 // Block is part of an inner loop. Do not update the loop information.
1246 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1247 // at this point, because this method is being called while populating `info`.
1248 }
1249 }
1250
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001251 // Raw update of the loop information.
1252 void SetLoopInformation(HLoopInformation* info) {
1253 loop_information_ = info;
1254 }
1255
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001256 bool IsInLoop() const { return loop_information_ != nullptr; }
1257
David Brazdilec16f792015-08-19 15:04:01 +01001258 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1259
1260 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1261 try_catch_information_ = try_catch_information;
1262 }
1263
1264 bool IsTryBlock() const {
1265 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1266 }
1267
1268 bool IsCatchBlock() const {
1269 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1270 }
David Brazdilffee3d32015-07-06 11:48:53 +01001271
1272 // Returns the try entry that this block's successors should have. They will
1273 // be in the same try, unless the block ends in a try boundary. In that case,
1274 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001275 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001276
Aart Bik75ff2c92018-04-21 01:28:11 +00001277 bool HasThrowingInstructions() const;
1278
David Brazdila4b8c212015-05-07 09:59:30 +01001279 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001280 bool Dominates(HBasicBlock* block) const;
1281
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001282 size_t GetLifetimeStart() const { return lifetime_start_; }
1283 size_t GetLifetimeEnd() const { return lifetime_end_; }
1284
1285 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1286 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1287
David Brazdil8d5b8b22015-03-24 10:51:52 +00001288 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001289 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001290 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001291 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001292 bool HasSinglePhi() const;
1293
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001294 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001295 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001296 ArenaVector<HBasicBlock*> predecessors_;
1297 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001298 HInstructionList instructions_;
1299 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001300 HLoopInformation* loop_information_;
1301 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001302 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001303 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001304 // The dex program counter of the first instruction of this block.
1305 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001306 size_t lifetime_start_;
1307 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001308 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001309
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001310 friend class HGraph;
1311 friend class HInstruction;
1312
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001313 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1314};
1315
David Brazdilb2bd1c52015-03-25 11:17:37 +00001316// Iterates over the LoopInformation of all loops which contain 'block'
1317// from the innermost to the outermost.
1318class HLoopInformationOutwardIterator : public ValueObject {
1319 public:
1320 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1321 : current_(block.GetLoopInformation()) {}
1322
1323 bool Done() const { return current_ == nullptr; }
1324
1325 void Advance() {
1326 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001327 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001328 }
1329
1330 HLoopInformation* Current() const {
1331 DCHECK(!Done());
1332 return current_;
1333 }
1334
1335 private:
1336 HLoopInformation* current_;
1337
1338 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1339};
1340
Alexandre Ramesef20f712015-06-09 10:29:30 +01001341#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001342 M(Above, Condition) \
1343 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001344 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001345 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001346 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001347 M(ArrayGet, Instruction) \
1348 M(ArrayLength, Instruction) \
1349 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001350 M(Below, Condition) \
1351 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001352 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001353 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001354 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001355 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001356 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001357 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001358 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001359 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001360 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001361 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001362 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001363 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001364 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001365 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001366 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001368 M(Exit, Instruction) \
1369 M(FloatConstant, Constant) \
1370 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001371 M(GreaterThan, Condition) \
1372 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001373 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001374 M(InstanceFieldGet, Instruction) \
1375 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001376 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001377 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001378 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001379 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001380 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001381 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001382 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001383 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001384 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001385 M(LessThan, Condition) \
1386 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001387 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001388 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001389 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001390 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001391 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001393 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001394 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001395 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001396 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001397 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001398 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001399 M(Neg, UnaryOperation) \
1400 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001401 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001402 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001403 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001404 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001405 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001406 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001407 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001408 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001409 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001410 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001411 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001412 M(Return, Instruction) \
1413 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001414 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001415 M(Shl, BinaryOperation) \
1416 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001417 M(StaticFieldGet, Instruction) \
1418 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001419 M(UnresolvedInstanceFieldGet, Instruction) \
1420 M(UnresolvedInstanceFieldSet, Instruction) \
1421 M(UnresolvedStaticFieldGet, Instruction) \
1422 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001423 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001425 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001426 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001427 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001428 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001429 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001430 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001431 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001432 M(VecExtractScalar, VecUnaryOperation) \
1433 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001434 M(VecCnv, VecUnaryOperation) \
1435 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001436 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001437 M(VecNot, VecUnaryOperation) \
1438 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001439 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001440 M(VecSub, VecBinaryOperation) \
1441 M(VecMul, VecBinaryOperation) \
1442 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001443 M(VecMin, VecBinaryOperation) \
1444 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(VecAnd, VecBinaryOperation) \
1446 M(VecAndNot, VecBinaryOperation) \
1447 M(VecOr, VecBinaryOperation) \
1448 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001449 M(VecSaturationAdd, VecBinaryOperation) \
1450 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001451 M(VecShl, VecBinaryOperation) \
1452 M(VecShr, VecBinaryOperation) \
1453 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001454 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001455 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001456 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001457 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001458 M(VecLoad, VecMemoryOperation) \
1459 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001460
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001461/*
1462 * Instructions, shared across several (not all) architectures.
1463 */
1464#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1465#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1466#else
1467#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001468 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001469 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001470 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001471 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001472#endif
1473
Alexandre Ramesef20f712015-06-09 10:29:30 +01001474#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1475
1476#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1477
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001478#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001480#else
1481#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1482 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001483 M(MipsPackedSwitch, Instruction) \
1484 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001485#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001486
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001487#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1488
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001489#ifndef ART_ENABLE_CODEGEN_x86
1490#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1491#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001492#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1493 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001494 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001495 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001496 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001497#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001498
1499#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1500
1501#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1502 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001503 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001504 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1505 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001506 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001507 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001508 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1509 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1510
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001511#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1512 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001513 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001514 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001515 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001516 M(Invoke, Instruction) \
1517 M(VecOperation, Instruction) \
1518 M(VecUnaryOperation, VecOperation) \
1519 M(VecBinaryOperation, VecOperation) \
1520 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001521
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001522#define FOR_EACH_INSTRUCTION(M) \
1523 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1524 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1525
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001526#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001527FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1528#undef FORWARD_DECLARATION
1529
Vladimir Marko372f10e2016-05-17 16:30:10 +01001530#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001531 private: \
1532 H##type& operator=(const H##type&) = delete; \
1533 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001534 const char* DebugName() const override { return #type; } \
1535 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001536 DCHECK(IsClonable()); \
1537 return new (arena) H##type(*this->As##type()); \
1538 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001539 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001540
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001541#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001542 private: \
1543 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001544 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001545
Artem Serovcced8ba2017-07-19 18:18:09 +01001546#define DEFAULT_COPY_CONSTRUCTOR(type) \
1547 explicit H##type(const H##type& other) = default;
1548
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001549template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001550class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1551 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001552 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001553 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001554 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001555 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001556 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001557 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001558 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001559
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001560 private:
David Brazdiled596192015-01-23 10:39:45 +00001561 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001562 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001563
1564 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001565 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001566
Vladimir Marko46817b82016-03-29 12:21:58 +01001567 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001568
1569 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1570};
1571
David Brazdiled596192015-01-23 10:39:45 +00001572template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001573using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001574
David Brazdil1abb4192015-02-17 18:33:36 +00001575// This class is used by HEnvironment and HInstruction classes to record the
1576// instructions they use and pointers to the corresponding HUseListNodes kept
1577// by the used instructions.
1578template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001579class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001580 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001581 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1582 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001583
Vladimir Marko46817b82016-03-29 12:21:58 +01001584 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1585 : HUserRecord(old_record.instruction_, before_use_node) {}
1586 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1587 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001588 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001589 }
1590
1591 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001592 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1593 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001594
1595 private:
1596 // Instruction used by the user.
1597 HInstruction* instruction_;
1598
Vladimir Marko46817b82016-03-29 12:21:58 +01001599 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1600 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001601};
1602
Vladimir Markoe9004912016-06-16 16:50:52 +01001603// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1604// This is used for HInstruction::GetInputs() to return a container wrapper providing
1605// HInstruction* values even though the underlying container has HUserRecord<>s.
1606struct HInputExtractor {
1607 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1608 return record.GetInstruction();
1609 }
1610 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1611 return record.GetInstruction();
1612 }
1613};
1614
1615using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1616using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001620 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 * For write/read dependences on fields/arrays, the dependence analysis uses
1622 * type disambiguation (e.g. a float field write cannot modify the value of an
1623 * integer field read) and the access type (e.g. a reference array write cannot
1624 * modify the value of a reference field read [although it may modify the
1625 * reference fetch prior to reading the field, which is represented by its own
1626 * write/read dependence]). The analysis makes conservative points-to
1627 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1628 * the same, and any reference read depends on any reference read without
1629 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001630 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001631 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1632 * alive across the point where garbage collection might happen.
1633 *
1634 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1635 *
1636 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1637 * those instructions from the compiler perspective (between this instruction and the next one
1638 * in the IR).
1639 *
1640 * Note: Instructions which can cause GC only on a fatal slow path do not need
1641 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1642 * one. However the execution may return to compiled code if there is a catch block in the
1643 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1644 * set.
1645 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 * The internal representation uses 38-bit and is described in the table below.
1647 * The first line indicates the side effect, and for field/array accesses the
1648 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001649 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 * The two numbered lines below indicate the bit position in the bitfield (read
1651 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001652 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1654 * +-------------+---------+---------+--------------+---------+---------+
1655 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1656 * | 3 |333333322|222222221| 1 |111111110|000000000|
1657 * | 7 |654321098|765432109| 8 |765432109|876543210|
1658 *
1659 * Note that, to ease the implementation, 'changes' bits are least significant
1660 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001661 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001662class SideEffects : public ValueObject {
1663 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001664 SideEffects() : flags_(0) {}
1665
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001666 static SideEffects None() {
1667 return SideEffects(0);
1668 }
1669
1670 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 return SideEffects(kAllChangeBits | kAllDependOnBits);
1672 }
1673
1674 static SideEffects AllChanges() {
1675 return SideEffects(kAllChangeBits);
1676 }
1677
1678 static SideEffects AllDependencies() {
1679 return SideEffects(kAllDependOnBits);
1680 }
1681
1682 static SideEffects AllExceptGCDependency() {
1683 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1684 }
1685
1686 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001687 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001688 }
1689
Aart Bik34c3ba92015-07-20 14:08:59 -07001690 static SideEffects AllWrites() {
1691 return SideEffects(kAllWrites);
1692 }
1693
1694 static SideEffects AllReads() {
1695 return SideEffects(kAllReads);
1696 }
1697
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001698 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001699 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001701 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001702 }
1703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001704 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001705 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001706 }
1707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001709 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001711 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001712 }
1713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001715 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001716 }
1717
Artem Serovd1aa7d02018-06-22 11:35:46 +01001718 // Returns whether GC might happen across this instruction from the compiler perspective so
1719 // the next instruction in the IR would see that.
1720 //
1721 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001722 static SideEffects CanTriggerGC() {
1723 return SideEffects(1ULL << kCanTriggerGCBit);
1724 }
1725
Artem Serovd1aa7d02018-06-22 11:35:46 +01001726 // Returns whether the instruction must not be alive across a GC point.
1727 //
1728 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 static SideEffects DependsOnGC() {
1730 return SideEffects(1ULL << kDependsOnGCBit);
1731 }
1732
Aart Bik854a02b2015-07-14 16:07:00 -07001733 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001734 SideEffects Union(SideEffects other) const {
1735 return SideEffects(flags_ | other.flags_);
1736 }
1737
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001738 SideEffects Exclusion(SideEffects other) const {
1739 return SideEffects(flags_ & ~other.flags_);
1740 }
1741
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001742 void Add(SideEffects other) {
1743 flags_ |= other.flags_;
1744 }
1745
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001746 bool Includes(SideEffects other) const {
1747 return (other.flags_ & flags_) == other.flags_;
1748 }
1749
1750 bool HasSideEffects() const {
1751 return (flags_ & kAllChangeBits);
1752 }
1753
1754 bool HasDependencies() const {
1755 return (flags_ & kAllDependOnBits);
1756 }
1757
1758 // Returns true if there are no side effects or dependencies.
1759 bool DoesNothing() const {
1760 return flags_ == 0;
1761 }
1762
Aart Bik854a02b2015-07-14 16:07:00 -07001763 // Returns true if something is written.
1764 bool DoesAnyWrite() const {
1765 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001766 }
1767
Aart Bik854a02b2015-07-14 16:07:00 -07001768 // Returns true if something is read.
1769 bool DoesAnyRead() const {
1770 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001771 }
1772
Aart Bik854a02b2015-07-14 16:07:00 -07001773 // Returns true if potentially everything is written and read
1774 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001775 bool DoesAllReadWrite() const {
1776 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1777 }
1778
Aart Bik854a02b2015-07-14 16:07:00 -07001779 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001780 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001781 }
1782
Roland Levillain0d5a2812015-11-13 10:07:31 +00001783 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001784 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001785 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1786 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001787 }
1788
1789 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001791 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001792 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001793 for (int s = kLastBit; s >= 0; s--) {
1794 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1795 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1796 // This is a bit for the GC side effect.
1797 if (current_bit_is_set) {
1798 flags += "GC";
1799 }
Aart Bik854a02b2015-07-14 16:07:00 -07001800 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001801 } else {
1802 // This is a bit for the array/field analysis.
1803 // The underscore character stands for the 'can trigger GC' bit.
1804 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1805 if (current_bit_is_set) {
1806 flags += kDebug[s];
1807 }
1808 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1809 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1810 flags += "|";
1811 }
1812 }
Aart Bik854a02b2015-07-14 16:07:00 -07001813 }
1814 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001815 }
1816
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001817 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001818
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001819 private:
1820 static constexpr int kFieldArrayAnalysisBits = 9;
1821
1822 static constexpr int kFieldWriteOffset = 0;
1823 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1824 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1825 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1826
1827 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1828
1829 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1830 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1831 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1832 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1833
1834 static constexpr int kLastBit = kDependsOnGCBit;
1835 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1836
1837 // Aliases.
1838
1839 static_assert(kChangeBits == kDependOnBits,
1840 "the 'change' bits should match the 'depend on' bits.");
1841
1842 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1843 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1844 static constexpr uint64_t kAllWrites =
1845 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1846 static constexpr uint64_t kAllReads =
1847 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001848
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001849 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001850 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001851 int shift;
1852 switch (type) {
1853 case DataType::Type::kReference: shift = 0; break;
1854 case DataType::Type::kBool: shift = 1; break;
1855 case DataType::Type::kInt8: shift = 2; break;
1856 case DataType::Type::kUint16: shift = 3; break;
1857 case DataType::Type::kInt16: shift = 4; break;
1858 case DataType::Type::kInt32: shift = 5; break;
1859 case DataType::Type::kInt64: shift = 6; break;
1860 case DataType::Type::kFloat32: shift = 7; break;
1861 case DataType::Type::kFloat64: shift = 8; break;
1862 default:
1863 LOG(FATAL) << "Unexpected data type " << type;
1864 UNREACHABLE();
1865 }
Aart Bik854a02b2015-07-14 16:07:00 -07001866 DCHECK_LE(kFieldWriteOffset, shift);
1867 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001868 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001869 }
1870
1871 // Private constructor on direct flags value.
1872 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1873
1874 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001875};
1876
David Brazdiled596192015-01-23 10:39:45 +00001877// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001878class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001879 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001880 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001881 size_t number_of_vregs,
1882 ArtMethod* method,
1883 uint32_t dex_pc,
1884 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001885 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1886 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001887 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001888 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001889 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001890 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001891 }
1892
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001893 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1894 const HEnvironment& to_copy,
1895 HInstruction* holder)
1896 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001897 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001898 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001899 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001900 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001901
Vladimir Markoec32f642017-06-02 10:51:55 +01001902 void AllocateLocations() {
1903 DCHECK(locations_.empty());
1904 locations_.resize(vregs_.size());
1905 }
1906
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001907 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001908 if (parent_ != nullptr) {
1909 parent_->SetAndCopyParentChain(allocator, parent);
1910 } else {
1911 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1912 parent_->CopyFrom(parent);
1913 if (parent->GetParent() != nullptr) {
1914 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1915 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001916 }
David Brazdiled596192015-01-23 10:39:45 +00001917 }
1918
Vladimir Marko69d310e2017-10-09 14:12:23 +01001919 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001920 void CopyFrom(HEnvironment* environment);
1921
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001922 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1923 // input to the loop phi instead. This is for inserting instructions that
1924 // require an environment (like HDeoptimization) in the loop pre-header.
1925 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001926
1927 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001928 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001929 }
1930
1931 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001932 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001933 }
1934
David Brazdil1abb4192015-02-17 18:33:36 +00001935 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001936
Artem Serovca210e32017-12-15 13:43:20 +00001937 // Replaces the input at the position 'index' with the replacement; the replacement and old
1938 // input instructions' env_uses_ lists are adjusted. The function works similar to
1939 // HInstruction::ReplaceInput.
1940 void ReplaceInput(HInstruction* replacement, size_t index);
1941
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001942 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001943
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001944 HEnvironment* GetParent() const { return parent_; }
1945
1946 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001947 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001948 }
1949
1950 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001951 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001952 }
1953
1954 uint32_t GetDexPc() const {
1955 return dex_pc_;
1956 }
1957
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001958 ArtMethod* GetMethod() const {
1959 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001960 }
1961
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001962 HInstruction* GetHolder() const {
1963 return holder_;
1964 }
1965
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001966
1967 bool IsFromInlinedInvoke() const {
1968 return GetParent() != nullptr;
1969 }
1970
David Brazdiled596192015-01-23 10:39:45 +00001971 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001972 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1973 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001974 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001975 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001976 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001977
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001978 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001979 HInstruction* const holder_;
1980
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001981 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001982
1983 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1984};
1985
Vladimir Markof9f64412015-09-02 14:05:49 +01001986class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001987 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301988#define DECLARE_KIND(type, super) k##type,
1989 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001990 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301991 kLastInstructionKind
1992 };
1993#undef DECLARE_KIND
1994
1995 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001996 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1997
1998 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001999 : previous_(nullptr),
2000 next_(nullptr),
2001 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002002 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002003 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002004 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002005 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002007 locations_(nullptr),
2008 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002009 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002010 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002011 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302012 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002013 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002014 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2015 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002016
Dave Allison20dfc792014-06-16 20:44:29 -07002017 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002018
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002019
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002020 HInstruction* GetNext() const { return next_; }
2021 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002022
Calin Juravle77520bc2015-01-12 18:45:46 +00002023 HInstruction* GetNextDisregardingMoves() const;
2024 HInstruction* GetPreviousDisregardingMoves() const;
2025
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002026 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002027 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002028 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002029 bool IsInBlock() const { return block_ != nullptr; }
2030 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002031 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2032 bool IsIrreducibleLoopHeaderPhi() const {
2033 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2034 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002035
Vladimir Marko372f10e2016-05-17 16:30:10 +01002036 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2037
2038 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2039 // One virtual method is enough, just const_cast<> and then re-add the const.
2040 return ArrayRef<const HUserRecord<HInstruction*>>(
2041 const_cast<HInstruction*>(this)->GetInputRecords());
2042 }
2043
Vladimir Markoe9004912016-06-16 16:50:52 +01002044 HInputsRef GetInputs() {
2045 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002046 }
2047
Vladimir Markoe9004912016-06-16 16:50:52 +01002048 HConstInputsRef GetInputs() const {
2049 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002050 }
2051
2052 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002053 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002054
Aart Bik2767f4b2016-10-28 15:03:53 -07002055 bool HasInput(HInstruction* input) const {
2056 for (const HInstruction* i : GetInputs()) {
2057 if (i == input) {
2058 return true;
2059 }
2060 }
2061 return false;
2062 }
2063
Vladimir Marko372f10e2016-05-17 16:30:10 +01002064 void SetRawInputAt(size_t index, HInstruction* input) {
2065 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2066 }
2067
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002068 virtual void Accept(HGraphVisitor* visitor) = 0;
2069 virtual const char* DebugName() const = 0;
2070
Vladimir Markobd785672018-05-03 17:09:09 +01002071 DataType::Type GetType() const {
2072 return TypeField::Decode(GetPackedFields());
2073 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002074
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002075 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002076
2077 uint32_t GetDexPc() const { return dex_pc_; }
2078
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002079 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002080
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002081 // Can the instruction throw?
2082 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2083 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002084 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002085
2086 // Does the instruction always throw an exception unconditionally?
2087 virtual bool AlwaysThrows() const { return false; }
2088
David Brazdilec16f792015-08-19 15:04:01 +01002089 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002090
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002091 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002092 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093
Calin Juravle10e244f2015-01-26 18:54:32 +00002094 // Does not apply for all instructions, but having this at top level greatly
2095 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002096 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002097 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002098 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002099 return true;
2100 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002101
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002102 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002103 return false;
2104 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002105
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002106 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2107 // with it. Otherwise return null.
2108 HNullCheck* GetImplicitNullCheck() const {
2109 // Find the first previous instruction which is not a move.
2110 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2111 if (first_prev_not_move != nullptr &&
2112 first_prev_not_move->IsNullCheck() &&
2113 first_prev_not_move->IsEmittedAtUseSite()) {
2114 return first_prev_not_move->AsNullCheck();
2115 }
2116 return nullptr;
2117 }
2118
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002119 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002121 }
2122
Calin Juravle2e768302015-07-28 14:41:11 +00002123 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002124
Calin Juravle61d544b2015-02-23 16:46:57 +00002125 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002126 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002127 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002128 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002129 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002131 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002132 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002133 // Note: fixup_end remains valid across push_front().
2134 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2135 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002136 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002137 uses_.push_front(*new_node);
2138 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002139 }
2140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002142 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002143 // Note: env_fixup_end remains valid across push_front().
2144 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2145 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002146 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002147 env_uses_.push_front(*new_node);
2148 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002149 }
2150
David Brazdil1abb4192015-02-17 18:33:36 +00002151 void RemoveAsUserOfInput(size_t input) {
2152 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002153 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2154 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2155 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002156 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002157
Vladimir Marko372f10e2016-05-17 16:30:10 +01002158 void RemoveAsUserOfAllInputs() {
2159 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2160 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2161 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2162 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2163 }
2164 }
2165
David Brazdil1abb4192015-02-17 18:33:36 +00002166 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2167 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002168
Vladimir Marko46817b82016-03-29 12:21:58 +01002169 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2170 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2171 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002172 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002173 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002174 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002175
Aart Bikcc42be02016-10-20 16:14:16 -07002176 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002177 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002178 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002179 !CanThrow() &&
2180 !IsSuspendCheck() &&
2181 !IsControlFlow() &&
2182 !IsNativeDebugInfo() &&
2183 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002184 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002185 !IsMemoryBarrier() &&
2186 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002187 }
2188
Aart Bikcc42be02016-10-20 16:14:16 -07002189 bool IsDeadAndRemovable() const {
2190 return IsRemovable() && !HasUses();
2191 }
2192
Roland Levillain6c82d402014-10-13 16:10:27 +01002193 // Does this instruction strictly dominate `other_instruction`?
2194 // Returns false if this instruction and `other_instruction` are the same.
2195 // Aborts if this instruction and `other_instruction` are both phis.
2196 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002197
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002198 int GetId() const { return id_; }
2199 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002200
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002201 int GetSsaIndex() const { return ssa_index_; }
2202 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2203 bool HasSsaIndex() const { return ssa_index_ != -1; }
2204
2205 bool HasEnvironment() const { return environment_ != nullptr; }
2206 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002207 // Set the `environment_` field. Raw because this method does not
2208 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002209 void SetRawEnvironment(HEnvironment* environment) {
2210 DCHECK(environment_ == nullptr);
2211 DCHECK_EQ(environment->GetHolder(), this);
2212 environment_ = environment;
2213 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002214
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002215 void InsertRawEnvironment(HEnvironment* environment) {
2216 DCHECK(environment_ != nullptr);
2217 DCHECK_EQ(environment->GetHolder(), this);
2218 DCHECK(environment->GetParent() == nullptr);
2219 environment->parent_ = environment_;
2220 environment_ = environment;
2221 }
2222
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002223 void RemoveEnvironment();
2224
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002225 // Set the environment of this instruction, copying it from `environment`. While
2226 // copying, the uses lists are being updated.
2227 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002228 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002229 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002230 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002231 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002232 if (environment->GetParent() != nullptr) {
2233 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2234 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002235 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002236
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002237 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2238 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002239 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002240 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002241 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002242 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002243 if (environment->GetParent() != nullptr) {
2244 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2245 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002246 }
2247
Nicolas Geoffray39468442014-09-02 15:17:15 +01002248 // Returns the number of entries in the environment. Typically, that is the
2249 // number of dex registers in a method. It could be more in case of inlining.
2250 size_t EnvironmentSize() const;
2251
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002252 LocationSummary* GetLocations() const { return locations_; }
2253 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002254
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002255 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002256 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002257 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002258 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002259
Alexandre Rames188d4312015-04-09 18:30:21 +01002260 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2261 // uses of this instruction by `other` are *not* updated.
2262 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2263 ReplaceWith(other);
2264 other->ReplaceInput(this, use_index);
2265 }
2266
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002267 // Move `this` instruction before `cursor`
2268 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002269
Vladimir Markofb337ea2015-11-25 15:25:10 +00002270 // Move `this` before its first user and out of any loops. If there is no
2271 // out-of-loop user that dominates all other users, move the instruction
2272 // to the end of the out-of-loop common dominator of the user's blocks.
2273 //
2274 // This can be used only on non-throwing instructions with no side effects that
2275 // have at least one use but no environment uses.
2276 void MoveBeforeFirstUserAndOutOfLoops();
2277
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002278#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002279 bool Is##type() const;
2280
2281 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2282#undef INSTRUCTION_TYPE_CHECK
2283
2284#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002285 const H##type* As##type() const; \
2286 H##type* As##type();
2287
Vladimir Markoa90dd512018-05-04 15:04:45 +01002288 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2289#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290
Artem Serovcced8ba2017-07-19 18:18:09 +01002291 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2292 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2293 // the instruction then the behaviour of this function is undefined.
2294 //
2295 // Note: It is semantically valid to create a clone of the instruction only until
2296 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2297 // copied.
2298 //
2299 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2300 // 'explicit HInstruction(const HInstruction& other)' for details.
2301 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2302 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2303 DebugName() << " " << GetId();
2304 UNREACHABLE();
2305 }
2306
2307 // Return whether instruction can be cloned (copied).
2308 virtual bool IsClonable() const { return false; }
2309
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002310 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002311 // TODO: this method is used by LICM and GVN with possibly different
2312 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313 virtual bool CanBeMoved() const { return false; }
2314
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002315 // Returns whether any data encoded in the two instructions is equal.
2316 // This method does not look at the inputs. Both instructions must be
2317 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002318 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002319 return false;
2320 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002321
2322 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002323 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002324 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002325 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002326
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002327 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2328 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2329 // the virtual function because the __attribute__((__pure__)) doesn't really
2330 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302331 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002332
2333 virtual size_t ComputeHashCode() const {
2334 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002335 for (const HInstruction* input : GetInputs()) {
2336 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002337 }
2338 return result;
2339 }
2340
2341 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002342 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002343 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002344
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002345 size_t GetLifetimePosition() const { return lifetime_position_; }
2346 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2347 LiveInterval* GetLiveInterval() const { return live_interval_; }
2348 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2349 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2350
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002351 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2352
2353 // Returns whether the code generation of the instruction will require to have access
2354 // to the current method. Such instructions are:
2355 // (1): Instructions that require an environment, as calling the runtime requires
2356 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002357 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2358 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002359 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002360 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002361 }
2362
Vladimir Markodc151b22015-10-15 18:02:30 +01002363 // Returns whether the code generation of the instruction will require to have access
2364 // to the dex cache of the current method's declaring class via the current method.
2365 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002366
Mark Mendellc4701932015-04-10 13:18:51 -04002367 // Does this instruction have any use in an environment before
2368 // control flow hits 'other'?
2369 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2370
2371 // Remove all references to environment uses of this instruction.
2372 // The caller must ensure that this is safe to do.
2373 void RemoveEnvironmentUsers();
2374
Vladimir Markoa1de9182016-02-25 11:37:38 +00002375 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2376 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002377
David Brazdil1abb4192015-02-17 18:33:36 +00002378 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002379 // If set, the machine code for this instruction is assumed to be generated by
2380 // its users. Used by liveness analysis to compute use positions accordingly.
2381 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2382 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302383 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2384 static constexpr size_t kFieldInstructionKindSize =
2385 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002386 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302387 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002388 static constexpr size_t kFieldTypeSize =
2389 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2390 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002391 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2392
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302393 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2394 "Too many generic packed fields");
2395
Vladimir Markobd785672018-05-03 17:09:09 +01002396 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2397
Vladimir Marko372f10e2016-05-17 16:30:10 +01002398 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2399 return GetInputRecords()[i];
2400 }
2401
2402 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2403 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2404 input_records[index] = input;
2405 }
David Brazdil1abb4192015-02-17 18:33:36 +00002406
Vladimir Markoa1de9182016-02-25 11:37:38 +00002407 uint32_t GetPackedFields() const {
2408 return packed_fields_;
2409 }
2410
2411 template <size_t flag>
2412 bool GetPackedFlag() const {
2413 return (packed_fields_ & (1u << flag)) != 0u;
2414 }
2415
2416 template <size_t flag>
2417 void SetPackedFlag(bool value = true) {
2418 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2419 }
2420
2421 template <typename BitFieldType>
2422 typename BitFieldType::value_type GetPackedField() const {
2423 return BitFieldType::Decode(packed_fields_);
2424 }
2425
2426 template <typename BitFieldType>
2427 void SetPackedField(typename BitFieldType::value_type value) {
2428 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2429 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2430 }
2431
Artem Serovcced8ba2017-07-19 18:18:09 +01002432 // Copy construction for the instruction (used for Clone function).
2433 //
2434 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2435 // prepare_for_register_allocator are not copied (set to default values).
2436 //
2437 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2438 // fine for most of them. However for some of the instructions a custom copy constructor must be
2439 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2440 // for copying them).
2441 explicit HInstruction(const HInstruction& other)
2442 : previous_(nullptr),
2443 next_(nullptr),
2444 block_(nullptr),
2445 dex_pc_(other.dex_pc_),
2446 id_(-1),
2447 ssa_index_(-1),
2448 packed_fields_(other.packed_fields_),
2449 environment_(nullptr),
2450 locations_(nullptr),
2451 live_interval_(nullptr),
2452 lifetime_position_(kNoLifetime),
2453 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302454 reference_type_handle_(other.reference_type_handle_) {
2455 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302458 using InstructionKindField =
2459 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2460
Vladimir Marko46817b82016-03-29 12:21:58 +01002461 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2462 auto before_use_node = uses_.before_begin();
2463 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2464 HInstruction* user = use_node->GetUser();
2465 size_t input_index = use_node->GetIndex();
2466 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2467 before_use_node = use_node;
2468 }
2469 }
2470
2471 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2472 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2473 if (next != uses_.end()) {
2474 HInstruction* next_user = next->GetUser();
2475 size_t next_index = next->GetIndex();
2476 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2477 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2478 }
2479 }
2480
2481 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2482 auto before_env_use_node = env_uses_.before_begin();
2483 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2484 HEnvironment* user = env_use_node->GetUser();
2485 size_t input_index = env_use_node->GetIndex();
2486 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2487 before_env_use_node = env_use_node;
2488 }
2489 }
2490
2491 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2492 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2493 if (next != env_uses_.end()) {
2494 HEnvironment* next_user = next->GetUser();
2495 size_t next_index = next->GetIndex();
2496 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2497 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2498 }
2499 }
David Brazdil1abb4192015-02-17 18:33:36 +00002500
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002501 HInstruction* previous_;
2502 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002503 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002504 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002505
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002506 // An instruction gets an id when it is added to the graph.
2507 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002508 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002509 int id_;
2510
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002511 // When doing liveness analysis, instructions that have uses get an SSA index.
2512 int ssa_index_;
2513
Vladimir Markoa1de9182016-02-25 11:37:38 +00002514 // Packed fields.
2515 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002516
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002517 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002518 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002519
2520 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002521 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002522
Nicolas Geoffray39468442014-09-02 15:17:15 +01002523 // The environment associated with this instruction. Not null if the instruction
2524 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002525 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002527 // Set by the code generator.
2528 LocationSummary* locations_;
2529
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002530 // Set by the liveness analysis.
2531 LiveInterval* live_interval_;
2532
2533 // Set by the liveness analysis, this is the position in a linear
2534 // order of blocks where this instruction's live interval start.
2535 size_t lifetime_position_;
2536
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002537 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002538
Vladimir Markoa1de9182016-02-25 11:37:38 +00002539 // The reference handle part of the reference type info.
2540 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002541 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002542 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002543
David Brazdil1abb4192015-02-17 18:33:36 +00002544 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002545 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002546 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002547 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002548 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002549};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002550std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002551
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002552// Iterates over the instructions, while preserving the next instruction
2553// in case the current instruction gets removed from the list by the user
2554// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002555class HInstructionIterator : public ValueObject {
2556 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002557 explicit HInstructionIterator(const HInstructionList& instructions)
2558 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002559 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002560 }
2561
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002562 bool Done() const { return instruction_ == nullptr; }
2563 HInstruction* Current() const { return instruction_; }
2564 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002566 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002567 }
2568
2569 private:
2570 HInstruction* instruction_;
2571 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002572
2573 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002574};
2575
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002576// Iterates over the instructions without saving the next instruction,
2577// therefore handling changes in the graph potentially made by the user
2578// of this iterator.
2579class HInstructionIteratorHandleChanges : public ValueObject {
2580 public:
2581 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2582 : instruction_(instructions.first_instruction_) {
2583 }
2584
2585 bool Done() const { return instruction_ == nullptr; }
2586 HInstruction* Current() const { return instruction_; }
2587 void Advance() {
2588 instruction_ = instruction_->GetNext();
2589 }
2590
2591 private:
2592 HInstruction* instruction_;
2593
2594 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2595};
2596
2597
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002598class HBackwardInstructionIterator : public ValueObject {
2599 public:
2600 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2601 : instruction_(instructions.last_instruction_) {
2602 next_ = Done() ? nullptr : instruction_->GetPrevious();
2603 }
2604
2605 bool Done() const { return instruction_ == nullptr; }
2606 HInstruction* Current() const { return instruction_; }
2607 void Advance() {
2608 instruction_ = next_;
2609 next_ = Done() ? nullptr : instruction_->GetPrevious();
2610 }
2611
2612 private:
2613 HInstruction* instruction_;
2614 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002615
2616 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002617};
2618
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002619class HVariableInputSizeInstruction : public HInstruction {
2620 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002621 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002622 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002623 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2624 }
2625
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002626 void AddInput(HInstruction* input);
2627 void InsertInputAt(size_t index, HInstruction* input);
2628 void RemoveInputAt(size_t index);
2629
Igor Murashkind01745e2017-04-05 16:40:31 -07002630 // Removes all the inputs.
2631 // Also removes this instructions from each input's use list
2632 // (for non-environment uses only).
2633 void RemoveAllInputs();
2634
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002635 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302636 HVariableInputSizeInstruction(InstructionKind inst_kind,
2637 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002638 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002639 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002640 size_t number_of_inputs,
2641 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302642 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002643 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002644 HVariableInputSizeInstruction(InstructionKind inst_kind,
2645 DataType::Type type,
2646 SideEffects side_effects,
2647 uint32_t dex_pc,
2648 ArenaAllocator* allocator,
2649 size_t number_of_inputs,
2650 ArenaAllocKind kind)
2651 : HInstruction(inst_kind, type, side_effects, dex_pc),
2652 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002653
Artem Serovcced8ba2017-07-19 18:18:09 +01002654 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002655
Artem Serovcced8ba2017-07-19 18:18:09 +01002656 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002657};
2658
Vladimir Markof9f64412015-09-02 14:05:49 +01002659template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002660class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002662 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302663 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002664 HExpression<N>(InstructionKind kind,
2665 DataType::Type type,
2666 SideEffects side_effects,
2667 uint32_t dex_pc)
2668 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2669 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002670
Vladimir Marko372f10e2016-05-17 16:30:10 +01002671 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002672 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002673 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002674 }
2675
Artem Serovcced8ba2017-07-19 18:18:09 +01002676 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002677 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002678
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002679 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002680 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002681
2682 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683};
2684
Vladimir Markobd785672018-05-03 17:09:09 +01002685// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002686template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002687class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002688 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002689 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690
Vladimir Markobd785672018-05-03 17:09:09 +01002691 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002692
Vladimir Marko372f10e2016-05-17 16:30:10 +01002693 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002694 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002695 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002696 }
2697
Artem Serovcced8ba2017-07-19 18:18:09 +01002698 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002699 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002700
Vladimir Markof9f64412015-09-02 14:05:49 +01002701 private:
2702 friend class SsaBuilder;
2703};
2704
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002705// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2706// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002707class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002708 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002709 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002710 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302711 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002713 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002714
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002715 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002716
Artem Serovcced8ba2017-07-19 18:18:09 +01002717 protected:
2718 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002719};
2720
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002721// Represents dex's RETURN opcodes. A HReturn is a control flow
2722// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002723class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002724 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002725 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002726 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002727 SetRawInputAt(0, value);
2728 }
2729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002730 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002731
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002732 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002733
Artem Serovcced8ba2017-07-19 18:18:09 +01002734 protected:
2735 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002736};
2737
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002738class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002739 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002740 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002741 uint32_t reg_number,
2742 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002744 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002745 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302746 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002747 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002748 SideEffects::None(),
2749 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002750 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002751 number_of_inputs,
2752 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002753 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002754 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002755 // Phis are constructed live and marked dead if conflicting or unused.
2756 // Individual steps of SsaBuilder should assume that if a phi has been
2757 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2758 SetPackedFlag<kFlagIsLive>(true);
2759 SetPackedFlag<kFlagCanBeNull>(true);
2760 }
2761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002762 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002763
David Brazdildee58d62016-04-07 09:54:26 +00002764 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002765 static DataType::Type ToPhiType(DataType::Type type) {
2766 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002767 }
2768
2769 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002772 // Make sure that only valid type changes occur. The following are allowed:
2773 // (1) int -> float/ref (primitive type propagation),
2774 // (2) long -> double (primitive type propagation).
2775 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2777 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2778 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002779 SetPackedField<TypeField>(new_type);
2780 }
2781
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002782 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002783 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2784
2785 uint32_t GetRegNumber() const { return reg_number_; }
2786
2787 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2788 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2789 bool IsDead() const { return !IsLive(); }
2790 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2791
Vladimir Markoe9004912016-06-16 16:50:52 +01002792 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002793 return other != nullptr
2794 && other->IsPhi()
2795 && other->AsPhi()->GetBlock() == GetBlock()
2796 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2797 }
2798
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002799 bool HasEquivalentPhi() const {
2800 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2801 return true;
2802 }
2803 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2804 return true;
2805 }
2806 return false;
2807 }
2808
David Brazdildee58d62016-04-07 09:54:26 +00002809 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2810 // An equivalent phi is a phi having the same dex register and type.
2811 // It assumes that phis with the same dex register are adjacent.
2812 HPhi* GetNextEquivalentPhiWithSameType() {
2813 HInstruction* next = GetNext();
2814 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2815 if (next->GetType() == GetType()) {
2816 return next->AsPhi();
2817 }
2818 next = next->GetNext();
2819 }
2820 return nullptr;
2821 }
2822
2823 DECLARE_INSTRUCTION(Phi);
2824
Artem Serovcced8ba2017-07-19 18:18:09 +01002825 protected:
2826 DEFAULT_COPY_CONSTRUCTOR(Phi);
2827
David Brazdildee58d62016-04-07 09:54:26 +00002828 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002829 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002830 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2831 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2832 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002833
David Brazdildee58d62016-04-07 09:54:26 +00002834 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002835};
2836
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002837// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002838// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002839// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002840class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002841 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302842 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002843 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302844 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002845
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002846 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002847
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002848 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002849
Artem Serovcced8ba2017-07-19 18:18:09 +01002850 protected:
2851 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002852};
2853
2854// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002855class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002856 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302857 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002858 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302859 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002860
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002861 bool IsClonable() const override { return true; }
2862 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002863
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002864 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002865 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002866 }
2867
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002868 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002869
Artem Serovcced8ba2017-07-19 18:18:09 +01002870 protected:
2871 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002872};
2873
Roland Levillain9867bc72015-08-05 10:21:34 +01002874class HConstant : public HExpression<0> {
2875 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302876 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2877 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2878 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002880 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002881
Roland Levillain1a653882016-03-18 18:05:57 +00002882 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002883 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002884 // Is this constant 0 in the arithmetic sense?
2885 virtual bool IsArithmeticZero() const { return false; }
2886 // Is this constant a 0-bit pattern?
2887 virtual bool IsZeroBitPattern() const { return false; }
2888 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002889 virtual bool IsOne() const { return false; }
2890
David Brazdil77a48ae2015-09-15 12:34:04 +00002891 virtual uint64_t GetValueAsUint64() const = 0;
2892
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002893 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002894
Artem Serovcced8ba2017-07-19 18:18:09 +01002895 protected:
2896 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002897};
2898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002899class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002900 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002901 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002902 return true;
2903 }
2904
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002905 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002907 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002908
Roland Levillain1a653882016-03-18 18:05:57 +00002909 // The null constant representation is a 0-bit pattern.
2910 virtual bool IsZeroBitPattern() const { return true; }
2911
Roland Levillain9867bc72015-08-05 10:21:34 +01002912 DECLARE_INSTRUCTION(NullConstant);
2913
Artem Serovcced8ba2017-07-19 18:18:09 +01002914 protected:
2915 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2916
Roland Levillain9867bc72015-08-05 10:21:34 +01002917 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302919 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2920 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002921
2922 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002923};
2924
2925// Constants of the type int. Those can be from Dex instructions, or
2926// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002927class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002928 public:
2929 int32_t GetValue() const { return value_; }
2930
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002931 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002932 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2933 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002934
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002935 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002936 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002937 return other->AsIntConstant()->value_ == value_;
2938 }
2939
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002940 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002942 bool IsMinusOne() const override { return GetValue() == -1; }
2943 bool IsArithmeticZero() const override { return GetValue() == 0; }
2944 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2945 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002946
Roland Levillain1a653882016-03-18 18:05:57 +00002947 // Integer constants are used to encode Boolean values as well,
2948 // where 1 means true and 0 means false.
2949 bool IsTrue() const { return GetValue() == 1; }
2950 bool IsFalse() const { return GetValue() == 0; }
2951
Roland Levillain9867bc72015-08-05 10:21:34 +01002952 DECLARE_INSTRUCTION(IntConstant);
2953
Artem Serovcced8ba2017-07-19 18:18:09 +01002954 protected:
2955 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2956
Roland Levillain9867bc72015-08-05 10:21:34 +01002957 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002958 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302959 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2960 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002961 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302962 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2963 value_(value ? 1 : 0) {
2964 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002965
2966 const int32_t value_;
2967
2968 friend class HGraph;
2969 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2970 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002971};
2972
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002973class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002974 public:
2975 int64_t GetValue() const { return value_; }
2976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002977 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002979 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002980 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002981 return other->AsLongConstant()->value_ == value_;
2982 }
2983
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002984 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002985
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002986 bool IsMinusOne() const override { return GetValue() == -1; }
2987 bool IsArithmeticZero() const override { return GetValue() == 0; }
2988 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2989 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002990
2991 DECLARE_INSTRUCTION(LongConstant);
2992
Artem Serovcced8ba2017-07-19 18:18:09 +01002993 protected:
2994 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2995
Roland Levillain9867bc72015-08-05 10:21:34 +01002996 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002997 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302998 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
2999 value_(value) {
3000 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003001
3002 const int64_t value_;
3003
3004 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003005};
Dave Allison20dfc792014-06-16 20:44:29 -07003006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003007class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003008 public:
3009 float GetValue() const { return value_; }
3010
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003011 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003012 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3013 }
3014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003015 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003016 DCHECK(other->IsFloatConstant()) << other->DebugName();
3017 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3018 }
3019
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003020 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003022 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003023 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3024 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003025 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003026 return std::fpclassify(value_) == FP_ZERO;
3027 }
3028 bool IsArithmeticPositiveZero() const {
3029 return IsArithmeticZero() && !std::signbit(value_);
3030 }
3031 bool IsArithmeticNegativeZero() const {
3032 return IsArithmeticZero() && std::signbit(value_);
3033 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003034 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003035 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003036 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003037 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003038 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3039 }
3040 bool IsNaN() const {
3041 return std::isnan(value_);
3042 }
3043
3044 DECLARE_INSTRUCTION(FloatConstant);
3045
Artem Serovcced8ba2017-07-19 18:18:09 +01003046 protected:
3047 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3048
Roland Levillain31dd3d62016-02-16 12:21:02 +00003049 private:
3050 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303051 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3052 value_(value) {
3053 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003054 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303055 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3056 value_(bit_cast<float, int32_t>(value)) {
3057 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003058
3059 const float value_;
3060
3061 // Only the SsaBuilder and HGraph can create floating-point constants.
3062 friend class SsaBuilder;
3063 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064};
3065
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003066class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003067 public:
3068 double GetValue() const { return value_; }
3069
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003070 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003072 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003073 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3074 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3075 }
3076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003077 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003078
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003079 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003080 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3081 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003082 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003083 return std::fpclassify(value_) == FP_ZERO;
3084 }
3085 bool IsArithmeticPositiveZero() const {
3086 return IsArithmeticZero() && !std::signbit(value_);
3087 }
3088 bool IsArithmeticNegativeZero() const {
3089 return IsArithmeticZero() && std::signbit(value_);
3090 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003091 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003092 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003093 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003094 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003095 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3096 }
3097 bool IsNaN() const {
3098 return std::isnan(value_);
3099 }
3100
3101 DECLARE_INSTRUCTION(DoubleConstant);
3102
Artem Serovcced8ba2017-07-19 18:18:09 +01003103 protected:
3104 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3105
Roland Levillain31dd3d62016-02-16 12:21:02 +00003106 private:
3107 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303108 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3109 value_(value) {
3110 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003111 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303112 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3113 value_(bit_cast<double, int64_t>(value)) {
3114 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003115
3116 const double value_;
3117
3118 // Only the SsaBuilder and HGraph can create floating-point constants.
3119 friend class SsaBuilder;
3120 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003121};
3122
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003123// Conditional branch. A block ending with an HIf instruction must have
3124// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003125class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003127 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003128 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003129 SetRawInputAt(0, input);
3130 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003132 bool IsClonable() const override { return true; }
3133 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003134
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003135 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003136 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003137 }
3138
3139 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003140 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003141 }
3142
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003143 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003144
Artem Serovcced8ba2017-07-19 18:18:09 +01003145 protected:
3146 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003147};
3148
David Brazdilfc6a86a2015-06-26 10:33:45 +00003149
3150// Abstract instruction which marks the beginning and/or end of a try block and
3151// links it to the respective exception handlers. Behaves the same as a Goto in
3152// non-exceptional control flow.
3153// Normal-flow successor is stored at index zero, exception handlers under
3154// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003155class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003156 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003157 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003158 kEntry,
3159 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003160 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003161 };
3162
Artem Serovd1aa7d02018-06-22 11:35:46 +01003163 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3164 // across the catch block entering edges as GC might happen during throwing an exception.
3165 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3166 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003167 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003168 : HExpression(kTryBoundary,
3169 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3170 : SideEffects::None(),
3171 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003172 SetPackedField<BoundaryKindField>(kind);
3173 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003175 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003176
3177 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003178 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003179
David Brazdild26a4112015-11-10 11:07:31 +00003180 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3181 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3182 }
3183
David Brazdilfc6a86a2015-06-26 10:33:45 +00003184 // Returns whether `handler` is among its exception handlers (non-zero index
3185 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003186 bool HasExceptionHandler(const HBasicBlock& handler) const {
3187 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003188 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003189 }
3190
3191 // If not present already, adds `handler` to its block's list of exception
3192 // handlers.
3193 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003194 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003195 GetBlock()->AddSuccessor(handler);
3196 }
3197 }
3198
Vladimir Markoa1de9182016-02-25 11:37:38 +00003199 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3200 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003201
David Brazdilffee3d32015-07-06 11:48:53 +01003202 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3203
David Brazdilfc6a86a2015-06-26 10:33:45 +00003204 DECLARE_INSTRUCTION(TryBoundary);
3205
Artem Serovcced8ba2017-07-19 18:18:09 +01003206 protected:
3207 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3208
David Brazdilfc6a86a2015-06-26 10:33:45 +00003209 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003210 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3211 static constexpr size_t kFieldBoundaryKindSize =
3212 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3213 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3214 kFieldBoundaryKind + kFieldBoundaryKindSize;
3215 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3216 "Too many packed fields.");
3217 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003218};
3219
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003220// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003221class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003222 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003223 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3224 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003225 HDeoptimize(ArenaAllocator* allocator,
3226 HInstruction* cond,
3227 DeoptimizationKind kind,
3228 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003229 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303230 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003231 SideEffects::All(),
3232 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003233 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003234 /* number_of_inputs */ 1,
3235 kArenaAllocMisc) {
3236 SetPackedFlag<kFieldCanBeMoved>(false);
3237 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003238 SetRawInputAt(0, cond);
3239 }
3240
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003241 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003242
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003243 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3244 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3245 // instead of `guard`.
3246 // We set CanTriggerGC to prevent any intermediate address to be live
3247 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003248 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003249 HInstruction* cond,
3250 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003251 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003252 uint32_t dex_pc)
3253 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303254 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003255 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003256 SideEffects::CanTriggerGC(),
3257 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003258 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003259 /* number_of_inputs */ 2,
3260 kArenaAllocMisc) {
3261 SetPackedFlag<kFieldCanBeMoved>(true);
3262 SetPackedField<DeoptimizeKindField>(kind);
3263 SetRawInputAt(0, cond);
3264 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003265 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003267 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003268
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003269 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003270 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3271 }
3272
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003273 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003275 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003276
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003277 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279 bool GuardsAnInput() const {
3280 return InputCount() == 2;
3281 }
3282
3283 HInstruction* GuardedInput() const {
3284 DCHECK(GuardsAnInput());
3285 return InputAt(1);
3286 }
3287
3288 void RemoveGuard() {
3289 RemoveInputAt(1);
3290 }
3291
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003292 DECLARE_INSTRUCTION(Deoptimize);
3293
Artem Serovcced8ba2017-07-19 18:18:09 +01003294 protected:
3295 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3296
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003297 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003298 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3299 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3300 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003301 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003302 static constexpr size_t kNumberOfDeoptimizePackedBits =
3303 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3304 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3305 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003306 using DeoptimizeKindField =
3307 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003308};
3309
Mingyao Yang063fc772016-08-02 11:02:54 -07003310// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3311// The compiled code checks this flag value in a guard before devirtualized call and
3312// if it's true, starts to do deoptimization.
3313// It has a 4-byte slot on stack.
3314// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003315class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003316 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003317 // CHA guards are only optimized in a separate pass and it has no side effects
3318 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003319 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303320 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003321 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303322 SideEffects::None(),
3323 dex_pc,
3324 allocator,
3325 0,
3326 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003327 }
3328
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003329 // We do all CHA guard elimination/motion in a single pass, after which there is no
3330 // further guard elimination/motion since a guard might have been used for justification
3331 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3332 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003333 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003334
3335 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3336
Artem Serovcced8ba2017-07-19 18:18:09 +01003337 protected:
3338 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003339};
3340
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003341// Represents the ArtMethod that was passed as a first argument to
3342// the method. It is used by instructions that depend on it, like
3343// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003344class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003345 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003346 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303347 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3348 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003349
3350 DECLARE_INSTRUCTION(CurrentMethod);
3351
Artem Serovcced8ba2017-07-19 18:18:09 +01003352 protected:
3353 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003354};
3355
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003356// Fetches an ArtMethod from the virtual table or the interface method table
3357// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003358class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003359 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003360 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003361 kVTable,
3362 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003363 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003364 };
3365 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003367 TableKind kind,
3368 size_t index,
3369 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303370 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003371 index_(index) {
3372 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003373 SetRawInputAt(0, cls);
3374 }
3375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003376 bool IsClonable() const override { return true; }
3377 bool CanBeMoved() const override { return true; }
3378 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003379 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003380 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003381 }
3382
Vladimir Markoa1de9182016-02-25 11:37:38 +00003383 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003384 size_t GetIndex() const { return index_; }
3385
3386 DECLARE_INSTRUCTION(ClassTableGet);
3387
Artem Serovcced8ba2017-07-19 18:18:09 +01003388 protected:
3389 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3390
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003391 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003392 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003393 static constexpr size_t kFieldTableKindSize =
3394 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3395 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3396 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3397 "Too many packed fields.");
3398 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3399
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003400 // The index of the ArtMethod in the table.
3401 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003402};
3403
Mark Mendellfe57faa2015-09-18 09:26:15 -04003404// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3405// have one successor for each entry in the switch table, and the final successor
3406// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003407class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003408 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003409 HPackedSwitch(int32_t start_value,
3410 uint32_t num_entries,
3411 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003412 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003413 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003414 start_value_(start_value),
3415 num_entries_(num_entries) {
3416 SetRawInputAt(0, input);
3417 }
3418
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003419 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003420
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003421 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003422
3423 int32_t GetStartValue() const { return start_value_; }
3424
Vladimir Marko211c2112015-09-24 16:52:33 +01003425 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003426
3427 HBasicBlock* GetDefaultBlock() const {
3428 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003429 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003430 }
3431 DECLARE_INSTRUCTION(PackedSwitch);
3432
Artem Serovcced8ba2017-07-19 18:18:09 +01003433 protected:
3434 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3435
Mark Mendellfe57faa2015-09-18 09:26:15 -04003436 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003437 const int32_t start_value_;
3438 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003439};
3440
Roland Levillain88cb1752014-10-20 16:36:47 +01003441class HUnaryOperation : public HExpression<1> {
3442 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303443 HUnaryOperation(InstructionKind kind,
3444 DataType::Type result_type,
3445 HInstruction* input,
3446 uint32_t dex_pc = kNoDexPc)
3447 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003448 SetRawInputAt(0, input);
3449 }
3450
Artem Serovcced8ba2017-07-19 18:18:09 +01003451 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003452 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003453
Roland Levillain88cb1752014-10-20 16:36:47 +01003454 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003455 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003456
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003457 bool CanBeMoved() const override { return true; }
3458 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003459 return true;
3460 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003461
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 // Try to statically evaluate `this` and return a HConstant
3463 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003464 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003465 HConstant* TryStaticEvaluation() const;
3466
3467 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003468 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3469 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3471 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003472
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003473 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003474
Artem Serovcced8ba2017-07-19 18:18:09 +01003475 protected:
3476 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003477};
3478
Dave Allison20dfc792014-06-16 20:44:29 -07003479class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003480 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303481 HBinaryOperation(InstructionKind kind,
3482 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003483 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003484 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003485 SideEffects side_effects = SideEffects::None(),
3486 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303487 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003488 SetRawInputAt(0, left);
3489 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003490 }
3491
Artem Serovcced8ba2017-07-19 18:18:09 +01003492 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003493 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003494
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003495 HInstruction* GetLeft() const { return InputAt(0); }
3496 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003497 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003498
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003499 virtual bool IsCommutative() const { return false; }
3500
3501 // Put constant on the right.
3502 // Returns whether order is changed.
3503 bool OrderInputsWithConstantOnTheRight() {
3504 HInstruction* left = InputAt(0);
3505 HInstruction* right = InputAt(1);
3506 if (left->IsConstant() && !right->IsConstant()) {
3507 ReplaceInput(right, 0);
3508 ReplaceInput(left, 1);
3509 return true;
3510 }
3511 return false;
3512 }
3513
3514 // Order inputs by instruction id, but favor constant on the right side.
3515 // This helps GVN for commutative ops.
3516 void OrderInputs() {
3517 DCHECK(IsCommutative());
3518 HInstruction* left = InputAt(0);
3519 HInstruction* right = InputAt(1);
3520 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3521 return;
3522 }
3523 if (OrderInputsWithConstantOnTheRight()) {
3524 return;
3525 }
3526 // Order according to instruction id.
3527 if (left->GetId() > right->GetId()) {
3528 ReplaceInput(right, 0);
3529 ReplaceInput(left, 1);
3530 }
3531 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003532
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003533 bool CanBeMoved() const override { return true; }
3534 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003535 return true;
3536 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003537
Roland Levillain31dd3d62016-02-16 12:21:02 +00003538 // Try to statically evaluate `this` and return a HConstant
3539 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003540 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003541 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003542
3543 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3545 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003546 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3547 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003549 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3550 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003551 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3552 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003553 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3554 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003555 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003556 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3557 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003558
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003559 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003560 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003561 HConstant* GetConstantRight() const;
3562
3563 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003564 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003565 HInstruction* GetLeastConstantLeft() const;
3566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003567 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003568
Artem Serovcced8ba2017-07-19 18:18:09 +01003569 protected:
3570 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003571};
3572
Mark Mendellc4701932015-04-10 13:18:51 -04003573// The comparison bias applies for floating point operations and indicates how NaN
3574// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003575enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003576 kNoBias, // bias is not applicable (i.e. for long operation)
3577 kGtBias, // return 1 for NaN comparisons
3578 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003579 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003580};
3581
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3583
Dave Allison20dfc792014-06-16 20:44:29 -07003584class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003585 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303586 HCondition(InstructionKind kind,
3587 HInstruction* first,
3588 HInstruction* second,
3589 uint32_t dex_pc = kNoDexPc)
3590 : HBinaryOperation(kind,
3591 DataType::Type::kBool,
3592 first,
3593 second,
3594 SideEffects::None(),
3595 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003596 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3597 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003598
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003599 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003600 // `instruction`, and disregard moves in between.
3601 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003602
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003603 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003604
3605 virtual IfCondition GetCondition() const = 0;
3606
Mark Mendellc4701932015-04-10 13:18:51 -04003607 virtual IfCondition GetOppositeCondition() const = 0;
3608
Vladimir Markoa1de9182016-02-25 11:37:38 +00003609 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003610 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3611
Vladimir Markoa1de9182016-02-25 11:37:38 +00003612 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3613 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003614
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003615 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003616 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003617 }
3618
Roland Levillain4fa13f62015-07-06 18:11:54 +01003619 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003620 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003621 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003622 if (if_cond == kCondNE) {
3623 return true;
3624 } else if (if_cond == kCondEQ) {
3625 return false;
3626 }
3627 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003628 }
3629
3630 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003632 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003633 if (if_cond == kCondEQ) {
3634 return true;
3635 } else if (if_cond == kCondNE) {
3636 return false;
3637 }
3638 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003639 }
3640
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003643 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003644 static constexpr size_t kFieldComparisonBiasSize =
3645 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3646 static constexpr size_t kNumberOfConditionPackedBits =
3647 kFieldComparisonBias + kFieldComparisonBiasSize;
3648 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3649 using ComparisonBiasField =
3650 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3651
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 template <typename T>
3653 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3654
3655 template <typename T>
3656 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003657 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003658 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3659 // Handle the bias.
3660 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3661 }
3662
3663 // Return an integer constant containing the result of a condition evaluated at compile time.
3664 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3665 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3666 }
3667
Artem Serovcced8ba2017-07-19 18:18:09 +01003668 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003669};
3670
3671// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003672class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003674 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303675 : HCondition(kEqual, first, second, dex_pc) {
3676 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003677
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003678 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003679
Vladimir Marko9e23df52015-11-10 17:14:35 +00003680 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003681 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003682 return MakeConstantCondition(true, GetDexPc());
3683 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003684 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3686 }
3687 // In the following Evaluate methods, a HCompare instruction has
3688 // been merged into this HEqual instruction; evaluate it as
3689 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003690 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003691 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3692 GetDexPc());
3693 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003694 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3696 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003697 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003699 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003700
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003701 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003702
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003703 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003704 return kCondEQ;
3705 }
3706
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003707 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003708 return kCondNE;
3709 }
3710
Artem Serovcced8ba2017-07-19 18:18:09 +01003711 protected:
3712 DEFAULT_COPY_CONSTRUCTOR(Equal);
3713
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003714 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003715 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003716};
3717
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003718class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003719 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303720 HNotEqual(HInstruction* first, HInstruction* second,
3721 uint32_t dex_pc = kNoDexPc)
3722 : HCondition(kNotEqual, first, second, dex_pc) {
3723 }
Dave Allison20dfc792014-06-16 20:44:29 -07003724
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003725 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003726
Vladimir Marko9e23df52015-11-10 17:14:35 +00003727 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003729 return MakeConstantCondition(false, GetDexPc());
3730 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3733 }
3734 // In the following Evaluate methods, a HCompare instruction has
3735 // been merged into this HNotEqual instruction; evaluate it as
3736 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3739 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003740 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3742 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003743 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003744 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003745 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003746
Dave Allison20dfc792014-06-16 20:44:29 -07003747 DECLARE_INSTRUCTION(NotEqual);
3748
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003749 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003750 return kCondNE;
3751 }
3752
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003753 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003754 return kCondEQ;
3755 }
3756
Artem Serovcced8ba2017-07-19 18:18:09 +01003757 protected:
3758 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3759
Dave Allison20dfc792014-06-16 20:44:29 -07003760 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003761 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003762};
3763
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003764class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003765 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303766 HLessThan(HInstruction* first, HInstruction* second,
3767 uint32_t dex_pc = kNoDexPc)
3768 : HCondition(kLessThan, first, second, dex_pc) {
3769 }
Dave Allison20dfc792014-06-16 20:44:29 -07003770
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003771 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003772 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003773 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 // In the following Evaluate methods, a HCompare instruction has
3775 // been merged into this HLessThan instruction; evaluate it as
3776 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003777 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003778 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3779 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003780 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003781 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3782 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003783 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003785 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003786
Dave Allison20dfc792014-06-16 20:44:29 -07003787 DECLARE_INSTRUCTION(LessThan);
3788
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003789 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003790 return kCondLT;
3791 }
3792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003793 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003794 return kCondGE;
3795 }
3796
Artem Serovcced8ba2017-07-19 18:18:09 +01003797 protected:
3798 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3799
Dave Allison20dfc792014-06-16 20:44:29 -07003800 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003801 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003802};
3803
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003804class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003805 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303806 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3807 uint32_t dex_pc = kNoDexPc)
3808 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3809 }
Dave Allison20dfc792014-06-16 20:44:29 -07003810
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003811 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003812 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003813 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003814 // In the following Evaluate methods, a HCompare instruction has
3815 // been merged into this HLessThanOrEqual instruction; evaluate it as
3816 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003817 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003818 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3819 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003820 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3822 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003823 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003824 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003825 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003826
Dave Allison20dfc792014-06-16 20:44:29 -07003827 DECLARE_INSTRUCTION(LessThanOrEqual);
3828
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003829 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003830 return kCondLE;
3831 }
3832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003833 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003834 return kCondGT;
3835 }
3836
Artem Serovcced8ba2017-07-19 18:18:09 +01003837 protected:
3838 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3839
Dave Allison20dfc792014-06-16 20:44:29 -07003840 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003841 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003842};
3843
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003844class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003845 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003846 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303847 : HCondition(kGreaterThan, first, second, dex_pc) {
3848 }
Dave Allison20dfc792014-06-16 20:44:29 -07003849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003852 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003853 // In the following Evaluate methods, a HCompare instruction has
3854 // been merged into this HGreaterThan instruction; evaluate it as
3855 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003856 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3858 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003859 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003860 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3861 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003862 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003864 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003865
Dave Allison20dfc792014-06-16 20:44:29 -07003866 DECLARE_INSTRUCTION(GreaterThan);
3867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003868 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003869 return kCondGT;
3870 }
3871
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003873 return kCondLE;
3874 }
3875
Artem Serovcced8ba2017-07-19 18:18:09 +01003876 protected:
3877 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3878
Dave Allison20dfc792014-06-16 20:44:29 -07003879 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003880 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003881};
3882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003883class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003885 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303886 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3887 }
Dave Allison20dfc792014-06-16 20:44:29 -07003888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003889 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003890 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003891 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003892 // In the following Evaluate methods, a HCompare instruction has
3893 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3894 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003895 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003896 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3897 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003898 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003899 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3900 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003901 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003902 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003903 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003904
Dave Allison20dfc792014-06-16 20:44:29 -07003905 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3906
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003907 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003908 return kCondGE;
3909 }
3910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003912 return kCondLT;
3913 }
3914
Artem Serovcced8ba2017-07-19 18:18:09 +01003915 protected:
3916 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3917
Dave Allison20dfc792014-06-16 20:44:29 -07003918 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003919 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003920};
3921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003922class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003923 public:
3924 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303925 : HCondition(kBelow, first, second, dex_pc) {
3926 }
Aart Bike9f37602015-10-09 11:15:55 -07003927
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003928 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003929 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003930 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003931 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003932 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3933 }
3934 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003935 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003936 LOG(FATAL) << DebugName() << " is not defined for float values";
3937 UNREACHABLE();
3938 }
3939 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003940 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003941 LOG(FATAL) << DebugName() << " is not defined for double values";
3942 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003943 }
3944
3945 DECLARE_INSTRUCTION(Below);
3946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003947 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003948 return kCondB;
3949 }
3950
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003951 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003952 return kCondAE;
3953 }
3954
Artem Serovcced8ba2017-07-19 18:18:09 +01003955 protected:
3956 DEFAULT_COPY_CONSTRUCTOR(Below);
3957
Aart Bike9f37602015-10-09 11:15:55 -07003958 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003959 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003960 return MakeUnsigned(x) < MakeUnsigned(y);
3961 }
Aart Bike9f37602015-10-09 11:15:55 -07003962};
3963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003964class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003965 public:
3966 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303967 : HCondition(kBelowOrEqual, 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(BelowOrEqual);
3988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003989 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003990 return kCondBE;
3991 }
3992
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003993 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003994 return kCondA;
3995 }
3996
Artem Serovcced8ba2017-07-19 18:18:09 +01003997 protected:
3998 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
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 HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004007 public:
4008 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304009 : HCondition(kAbove, 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(Above);
4030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004031 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004032 return kCondA;
4033 }
4034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004035 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004036 return kCondBE;
4037 }
4038
Artem Serovcced8ba2017-07-19 18:18:09 +01004039 protected:
4040 DEFAULT_COPY_CONSTRUCTOR(Above);
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 HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004049 public:
4050 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304051 : HCondition(kAboveOrEqual, 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(AboveOrEqual);
4072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004073 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004074 return kCondAE;
4075 }
4076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004077 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004078 return kCondB;
4079 }
4080
Artem Serovcced8ba2017-07-19 18:18:09 +01004081 protected:
4082 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
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};
Dave Allison20dfc792014-06-16 20:44:29 -07004089
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004090// Instruction to check how two inputs compare to each other.
4091// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004092class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004093 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004094 // Note that `comparison_type` is the type of comparison performed
4095 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 // HCompare instruction (which is always DataType::Type::kInt).
4097 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004098 HInstruction* first,
4099 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004100 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004101 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304102 : HBinaryOperation(kCompare,
4103 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004104 first,
4105 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004106 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004107 dex_pc) {
4108 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4110 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004111 }
4112
Roland Levillain9867bc72015-08-05 10:21:34 +01004113 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004114 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4115
4116 template <typename T>
4117 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004118 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004119 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4120 // Handle the bias.
4121 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4122 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004124 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004125 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4126 // reach this code path when processing a freshly built HIR
4127 // graph. However HCompare integer instructions can be synthesized
4128 // by the instruction simplifier to implement IntegerCompare and
4129 // IntegerSignum intrinsics, so we have to handle this case.
4130 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004131 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004132 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004133 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4134 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004135 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004136 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4137 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004138 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004139 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004140 }
4141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004142 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 }
4145
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004147
Roland Levillain31dd3d62016-02-16 12:21:02 +00004148 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004149 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004150 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004152 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004153 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004154
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004155 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004156 // Comparisons do not require a runtime call in any back end.
4157 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004158 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004159
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004160 DECLARE_INSTRUCTION(Compare);
4161
Roland Levillain31dd3d62016-02-16 12:21:02 +00004162 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004163 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004164 static constexpr size_t kFieldComparisonBiasSize =
4165 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4166 static constexpr size_t kNumberOfComparePackedBits =
4167 kFieldComparisonBias + kFieldComparisonBiasSize;
4168 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4169 using ComparisonBiasField =
4170 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4171
Roland Levillain31dd3d62016-02-16 12:21:02 +00004172 // Return an integer constant containing the result of a comparison evaluated at compile time.
4173 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4174 DCHECK(value == -1 || value == 0 || value == 1) << value;
4175 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4176 }
4177
Artem Serovcced8ba2017-07-19 18:18:09 +01004178 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004179};
4180
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004181class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004182 public:
4183 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004184 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004185 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004186 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004187 bool finalizable,
4188 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304189 : HExpression(kNewInstance,
4190 DataType::Type::kReference,
4191 SideEffects::CanTriggerGC(),
4192 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004193 type_index_(type_index),
4194 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004195 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004196 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004197 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004198 }
4199
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004200 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004201
Andreas Gampea5b09a62016-11-17 15:21:22 -08004202 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004203 const DexFile& GetDexFile() const { return dex_file_; }
4204
4205 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004206 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004207
Mingyao Yang062157f2016-03-02 10:15:36 -08004208 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004209 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004210
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004211 bool NeedsChecks() const {
4212 return entrypoint_ == kQuickAllocObjectWithChecks;
4213 }
David Brazdil6de19382016-01-08 17:37:10 +00004214
Vladimir Markoa1de9182016-02-25 11:37:38 +00004215 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004217 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004218
4219 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4220
4221 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4222 entrypoint_ = entrypoint;
4223 }
4224
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004225 HLoadClass* GetLoadClass() const {
4226 HInstruction* input = InputAt(0);
4227 if (input->IsClinitCheck()) {
4228 input = input->InputAt(0);
4229 }
4230 DCHECK(input->IsLoadClass());
4231 return input->AsLoadClass();
4232 }
4233
David Brazdil6de19382016-01-08 17:37:10 +00004234 bool IsStringAlloc() const;
4235
4236 DECLARE_INSTRUCTION(NewInstance);
4237
Artem Serovcced8ba2017-07-19 18:18:09 +01004238 protected:
4239 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4240
David Brazdil6de19382016-01-08 17:37:10 +00004241 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004242 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004243 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4244 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4245 "Too many packed fields.");
4246
Andreas Gampea5b09a62016-11-17 15:21:22 -08004247 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004248 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004249 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004250};
4251
Agi Csaki05f20562015-08-19 14:58:14 -07004252enum IntrinsicNeedsEnvironmentOrCache {
4253 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4254 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004255};
4256
Aart Bik5d75afe2015-12-14 11:57:01 -08004257enum IntrinsicSideEffects {
4258 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4259 kReadSideEffects, // Intrinsic may read heap memory.
4260 kWriteSideEffects, // Intrinsic may write heap memory.
4261 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4262};
4263
4264enum IntrinsicExceptions {
4265 kNoThrow, // Intrinsic does not throw any exceptions.
4266 kCanThrow // Intrinsic may throw exceptions.
4267};
4268
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004269class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004270 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004271 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004272
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004273 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004274 SetRawInputAt(index, argument);
4275 }
4276
Roland Levillain3e3d7332015-04-28 11:00:54 +01004277 // Return the number of arguments. This number can be lower than
4278 // the number of inputs returned by InputCount(), as some invoke
4279 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4280 // inputs at the end of their list of inputs.
4281 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4282
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004283 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4284
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004285 InvokeType GetInvokeType() const {
4286 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004287 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004288
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004289 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004290 return intrinsic_;
4291 }
4292
Aart Bik5d75afe2015-12-14 11:57:01 -08004293 void SetIntrinsic(Intrinsics intrinsic,
4294 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4295 IntrinsicSideEffects side_effects,
4296 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004297
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004298 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004299 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004300 }
4301
Aart Bikff7d89c2016-11-07 08:49:28 -08004302 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004304 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004305
Aart Bika8b8e9b2018-01-09 11:01:02 -08004306 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004308 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004309
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004310 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004311
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004312 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004313 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4314 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004315
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004316 uint32_t* GetIntrinsicOptimizations() {
4317 return &intrinsic_optimizations_;
4318 }
4319
4320 const uint32_t* GetIntrinsicOptimizations() const {
4321 return &intrinsic_optimizations_;
4322 }
4323
4324 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4325
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004326 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004327 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004328
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004329 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004330
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004331 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004332 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4333 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004334 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004335 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004336 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4337 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004339 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004340
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304341 HInvoke(InstructionKind kind,
4342 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004343 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004344 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004345 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004346 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004347 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004348 ArtMethod* resolved_method,
4349 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004350 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304351 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004352 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004353 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4354 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004355 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004356 number_of_arguments + number_of_other_inputs,
4357 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004358 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004359 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004360 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004361 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004362 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004363 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004364 // Check mutator lock, constructors lack annotalysis support.
4365 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4366 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004367 }
4368
Artem Serovcced8ba2017-07-19 18:18:09 +01004369 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4370
Roland Levillain3e3d7332015-04-28 11:00:54 +01004371 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004372 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004373 const uint32_t dex_method_index_;
4374 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004375
4376 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4377 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004378};
4379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004380class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004381 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004382 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004383 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004384 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004385 uint32_t dex_pc,
4386 uint32_t dex_method_index,
4387 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304388 : HInvoke(kInvokeUnresolved,
4389 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004390 number_of_arguments,
4391 0u /* number_of_other_inputs */,
4392 return_type,
4393 dex_pc,
4394 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004395 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004396 invoke_type) {
4397 }
4398
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004399 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004400
Calin Juravle175dc732015-08-25 15:42:32 +01004401 DECLARE_INSTRUCTION(InvokeUnresolved);
4402
Artem Serovcced8ba2017-07-19 18:18:09 +01004403 protected:
4404 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004405};
4406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004407class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004408 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004409 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004410 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004411 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004412 uint32_t dex_pc,
4413 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304414 : HInvoke(kInvokePolymorphic,
4415 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004416 number_of_arguments,
4417 0u /* number_of_other_inputs */,
4418 return_type,
4419 dex_pc,
4420 dex_method_index,
4421 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304422 kVirtual) {
4423 }
Orion Hodsonac141392017-01-13 11:53:47 +00004424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004425 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004426
Orion Hodsonac141392017-01-13 11:53:47 +00004427 DECLARE_INSTRUCTION(InvokePolymorphic);
4428
Artem Serovcced8ba2017-07-19 18:18:09 +01004429 protected:
4430 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004431};
4432
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004433class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004434 public:
4435 HInvokeCustom(ArenaAllocator* allocator,
4436 uint32_t number_of_arguments,
4437 uint32_t call_site_index,
4438 DataType::Type return_type,
4439 uint32_t dex_pc)
4440 : HInvoke(kInvokeCustom,
4441 allocator,
4442 number_of_arguments,
4443 /* number_of_other_inputs */ 0u,
4444 return_type,
4445 dex_pc,
4446 /* dex_method_index */ dex::kDexNoIndex,
4447 /* resolved_method */ nullptr,
4448 kStatic),
4449 call_site_index_(call_site_index) {
4450 }
4451
4452 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4453
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004454 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004455
4456 DECLARE_INSTRUCTION(InvokeCustom);
4457
4458 protected:
4459 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4460
4461 private:
4462 uint32_t call_site_index_;
4463};
4464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004465class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004466 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004467 // Requirements of this method call regarding the class
4468 // initialization (clinit) check of its declaring class.
4469 enum class ClinitCheckRequirement {
4470 kNone, // Class already initialized.
4471 kExplicit, // Static call having explicit clinit check as last input.
4472 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004473 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004474 };
4475
Vladimir Marko58155012015-08-19 12:49:41 +00004476 // Determines how to load the target ArtMethod*.
4477 enum class MethodLoadKind {
4478 // Use a String init ArtMethod* loaded from Thread entrypoints.
4479 kStringInit,
4480
4481 // Use the method's own ArtMethod* loaded by the register allocator.
4482 kRecursive,
4483
Vladimir Marko65979462017-05-19 17:25:12 +01004484 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4485 // Used for boot image methods referenced by boot image code.
4486 kBootImageLinkTimePcRelative,
4487
Vladimir Markob066d432018-01-03 13:14:37 +00004488 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4489 // Used for app->boot calls with relocatable image.
4490 kBootImageRelRo,
4491
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004492 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004493 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004494 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004495
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004496 // Use ArtMethod* at a known address, embed the direct address in the code.
4497 // Used for for JIT-compiled calls.
4498 kJitDirectAddress,
4499
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004500 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4501 // used when other kinds are unimplemented on a particular architecture.
4502 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004503 };
4504
4505 // Determines the location of the code pointer.
4506 enum class CodePtrLocation {
4507 // Recursive call, use local PC-relative call instruction.
4508 kCallSelf,
4509
Vladimir Marko58155012015-08-19 12:49:41 +00004510 // Use code pointer from the ArtMethod*.
4511 // Used when we don't know the target code. This is also the last-resort-kind used when
4512 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4513 kCallArtMethod,
4514 };
4515
4516 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004517 MethodLoadKind method_load_kind;
4518 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004519 // The method load data holds
4520 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4521 // Note that there are multiple string init methods, each having its own offset.
4522 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004523 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004524 };
4525
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004526 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004527 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004528 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004529 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004530 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004531 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004532 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004533 InvokeType invoke_type,
4534 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004535 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304536 : HInvoke(kInvokeStaticOrDirect,
4537 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004538 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004539 // There is potentially one extra argument for the HCurrentMethod node, and
4540 // potentially one other if the clinit check is explicit, and potentially
4541 // one other if the method is a string factory.
4542 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004543 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004544 return_type,
4545 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004546 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004547 resolved_method,
4548 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004549 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004550 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004551 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4552 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004554 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004555
Vladimir Markodc151b22015-10-15 18:02:30 +01004556 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004557 bool had_current_method_input = HasCurrentMethodInput();
4558 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4559
4560 // Using the current method is the default and once we find a better
4561 // method load kind, we should not go back to using the current method.
4562 DCHECK(had_current_method_input || !needs_current_method_input);
4563
4564 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004565 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4566 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004567 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004568 dispatch_info_ = dispatch_info;
4569 }
4570
Aart Bik6daebeb2017-04-03 14:35:41 -07004571 DispatchInfo GetDispatchInfo() const {
4572 return dispatch_info_;
4573 }
4574
Vladimir Markoc53c0792015-11-19 15:48:33 +00004575 void AddSpecialInput(HInstruction* input) {
4576 // We allow only one special input.
4577 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4578 DCHECK(InputCount() == GetSpecialInputIndex() ||
4579 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4580 InsertInputAt(GetSpecialInputIndex(), input);
4581 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004582
Vladimir Marko372f10e2016-05-17 16:30:10 +01004583 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004584 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004585 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4586 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4587 DCHECK(!input_records.empty());
4588 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4589 HInstruction* last_input = input_records.back().GetInstruction();
4590 // Note: `last_input` may be null during arguments setup.
4591 if (last_input != nullptr) {
4592 // `last_input` is the last input of a static invoke marked as having
4593 // an explicit clinit check. It must either be:
4594 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4595 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4596 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4597 }
4598 }
4599 return input_records;
4600 }
4601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004602 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004603 // We access the method via the dex cache so we can't do an implicit null check.
4604 // TODO: for intrinsics we can generate implicit null checks.
4605 return false;
4606 }
4607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004608 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004609 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004610 }
4611
Vladimir Markoc53c0792015-11-19 15:48:33 +00004612 // Get the index of the special input, if any.
4613 //
David Brazdil6de19382016-01-08 17:37:10 +00004614 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4615 // method pointer; otherwise there may be one platform-specific special input,
4616 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004617 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004618 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004619
Vladimir Marko58155012015-08-19 12:49:41 +00004620 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4621 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4622 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004623 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004624 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004625 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004626 bool HasPcRelativeMethodLoadKind() const {
4627 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004628 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004629 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004630 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004631 bool HasCurrentMethodInput() const {
4632 // This function can be called only after the invoke has been fully initialized by the builder.
4633 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004634 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004635 return true;
4636 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004637 DCHECK(InputCount() == GetSpecialInputIndex() ||
4638 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004639 return false;
4640 }
4641 }
Vladimir Marko58155012015-08-19 12:49:41 +00004642
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004643 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004644 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004645 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004646 }
4647
4648 uint64_t GetMethodAddress() const {
4649 DCHECK(HasMethodAddress());
4650 return dispatch_info_.method_load_data;
4651 }
4652
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004653 const DexFile& GetDexFileForPcRelativeDexCache() const;
4654
Vladimir Markoa1de9182016-02-25 11:37:38 +00004655 ClinitCheckRequirement GetClinitCheckRequirement() const {
4656 return GetPackedField<ClinitCheckRequirementField>();
4657 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004658
Roland Levillain4c0eb422015-04-24 16:43:49 +01004659 // Is this instruction a call to a static method?
4660 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004661 return GetInvokeType() == kStatic;
4662 }
4663
4664 MethodReference GetTargetMethod() const {
4665 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004666 }
4667
Vladimir Markofbb184a2015-11-13 14:47:00 +00004668 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4669 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4670 // instruction; only relevant for static calls with explicit clinit check.
4671 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004672 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004673 size_t last_input_index = inputs_.size() - 1u;
4674 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004675 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004676 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004677 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004678 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004679 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004680 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004681 }
4682
4683 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004684 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004685 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004686 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004687 }
4688
4689 // Is this a call to a static method whose declaring class has an
4690 // implicit intialization check requirement?
4691 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004692 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004693 }
4694
Vladimir Markob554b5a2015-11-06 12:57:55 +00004695 // Does this method load kind need the current method as an input?
4696 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004697 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004698 }
4699
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004700 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004701
Artem Serovcced8ba2017-07-19 18:18:09 +01004702 protected:
4703 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4704
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004705 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004706 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004707 static constexpr size_t kFieldClinitCheckRequirementSize =
4708 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4709 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4710 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4711 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4712 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004713 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4714 kFieldClinitCheckRequirement,
4715 kFieldClinitCheckRequirementSize>;
4716
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004717 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004718 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004719 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004720};
Vladimir Markof64242a2015-12-01 14:58:23 +00004721std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004722std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004724class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004725 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004726 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004727 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004728 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004729 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004730 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004731 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004732 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304733 : HInvoke(kInvokeVirtual,
4734 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004735 number_of_arguments,
4736 0u,
4737 return_type,
4738 dex_pc,
4739 dex_method_index,
4740 resolved_method,
4741 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304742 vtable_index_(vtable_index) {
4743 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004745 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004747 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004748 switch (GetIntrinsic()) {
4749 case Intrinsics::kThreadCurrentThread:
4750 case Intrinsics::kStringBufferAppend:
4751 case Intrinsics::kStringBufferToString:
4752 case Intrinsics::kStringBuilderAppend:
4753 case Intrinsics::kStringBuilderToString:
4754 return false;
4755 default:
4756 return HInvoke::CanBeNull();
4757 }
4758 }
4759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004760 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004762 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004763 }
4764
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004765 uint32_t GetVTableIndex() const { return vtable_index_; }
4766
4767 DECLARE_INSTRUCTION(InvokeVirtual);
4768
Artem Serovcced8ba2017-07-19 18:18:09 +01004769 protected:
4770 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4771
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004772 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004773 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004774 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004775};
4776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004777class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004778 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004779 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004780 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004781 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004782 uint32_t dex_pc,
4783 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004784 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004785 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304786 : HInvoke(kInvokeInterface,
4787 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004788 number_of_arguments,
4789 0u,
4790 return_type,
4791 dex_pc,
4792 dex_method_index,
4793 resolved_method,
4794 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304795 imt_index_(imt_index) {
4796 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004798 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004800 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004802 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 }
4804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004805 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004806 // The assembly stub currently needs it.
4807 return true;
4808 }
4809
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004810 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004811
4812 DECLARE_INSTRUCTION(InvokeInterface);
4813
Artem Serovcced8ba2017-07-19 18:18:09 +01004814 protected:
4815 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4816
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004817 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004818 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004819 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004820};
4821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004822class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004823 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304825 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004827 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004828
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004829 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004831 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004834 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004837 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004838 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4839 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004840 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4842 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004843
Roland Levillain88cb1752014-10-20 16:36:47 +01004844 DECLARE_INSTRUCTION(Neg);
4845
Artem Serovcced8ba2017-07-19 18:18:09 +01004846 protected:
4847 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004848};
4849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004850class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004851 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004852 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304853 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004854 SetRawInputAt(0, cls);
4855 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004856 }
4857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004858 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004859
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004860 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004861 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004862
Mingyao Yang0c365e62015-03-31 15:09:29 -07004863 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004864 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004866 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004867
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004868 HLoadClass* GetLoadClass() const {
4869 DCHECK(InputAt(0)->IsLoadClass());
4870 return InputAt(0)->AsLoadClass();
4871 }
4872
4873 HInstruction* GetLength() const {
4874 return InputAt(1);
4875 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004876
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877 DECLARE_INSTRUCTION(NewArray);
4878
Artem Serovcced8ba2017-07-19 18:18:09 +01004879 protected:
4880 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004881};
4882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004883class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004884 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004885 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 HInstruction* left,
4887 HInstruction* right,
4888 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304889 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4890 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004892 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004893
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004894 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004896 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 return GetBlock()->GetGraph()->GetIntConstant(
4898 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004899 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004900 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 return GetBlock()->GetGraph()->GetLongConstant(
4902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004903 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004904 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 return GetBlock()->GetGraph()->GetFloatConstant(
4906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4907 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004908 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004909 return GetBlock()->GetGraph()->GetDoubleConstant(
4910 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4911 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004912
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004913 DECLARE_INSTRUCTION(Add);
4914
Artem Serovcced8ba2017-07-19 18:18:09 +01004915 protected:
4916 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004917};
4918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004919class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004920 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004921 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004922 HInstruction* left,
4923 HInstruction* right,
4924 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304925 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4926 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004928 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004930 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 return GetBlock()->GetGraph()->GetIntConstant(
4932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004933 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetLongConstant(
4936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004937 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004938 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004939 return GetBlock()->GetGraph()->GetFloatConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4941 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004942 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004943 return GetBlock()->GetGraph()->GetDoubleConstant(
4944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4945 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004947 DECLARE_INSTRUCTION(Sub);
4948
Artem Serovcced8ba2017-07-19 18:18:09 +01004949 protected:
4950 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951};
4952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004954 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 HInstruction* left,
4957 HInstruction* right,
4958 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304959 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4960 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004963
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004964 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 return GetBlock()->GetGraph()->GetIntConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetLongConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 return GetBlock()->GetGraph()->GetFloatConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 return GetBlock()->GetGraph()->GetDoubleConstant(
4980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4981 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004982
4983 DECLARE_INSTRUCTION(Mul);
4984
Artem Serovcced8ba2017-07-19 18:18:09 +01004985 protected:
4986 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004987};
4988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004989class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004990 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004992 HInstruction* left,
4993 HInstruction* right,
4994 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304995 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4996 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004997
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005000 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005001 // Our graph structure ensures we never have 0 for `y` during
5002 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005003 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005004 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005005 return (y == -1) ? -x : x / y;
5006 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005007
Roland Levillain31dd3d62016-02-16 12:21:02 +00005008 template <typename T>
5009 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005011 return x / y;
5012 }
5013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005014 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005016 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005017 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005019 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005020 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 return GetBlock()->GetGraph()->GetFloatConstant(
5024 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5025 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005026 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005027 return GetBlock()->GetGraph()->GetDoubleConstant(
5028 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005029 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005030
5031 DECLARE_INSTRUCTION(Div);
5032
Artem Serovcced8ba2017-07-19 18:18:09 +01005033 protected:
5034 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005035};
5036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005037class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005038 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005039 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005040 HInstruction* left,
5041 HInstruction* right,
5042 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305043 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5044 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005045
Roland Levillain9867bc72015-08-05 10:21:34 +01005046 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005047 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005048 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005049 // Our graph structure ensures we never have 0 for `y` during
5050 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005051 DCHECK_NE(y, 0);
5052 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5053 return (y == -1) ? 0 : x % y;
5054 }
5055
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 template <typename T>
5057 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005058 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005059 return std::fmod(x, y);
5060 }
5061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005062 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005065 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005069 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005070 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005071 return GetBlock()->GetGraph()->GetFloatConstant(
5072 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005074 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 return GetBlock()->GetGraph()->GetDoubleConstant(
5076 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5077 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005078
5079 DECLARE_INSTRUCTION(Rem);
5080
Artem Serovcced8ba2017-07-19 18:18:09 +01005081 protected:
5082 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005083};
5084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005086 public:
5087 HMin(DataType::Type result_type,
5088 HInstruction* left,
5089 HInstruction* right,
5090 uint32_t dex_pc)
5091 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005094
5095 // Evaluation for integral values.
5096 template <typename T> static T ComputeIntegral(T x, T y) {
5097 return (x <= y) ? x : y;
5098 }
5099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005101 return GetBlock()->GetGraph()->GetIntConstant(
5102 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005105 return GetBlock()->GetGraph()->GetLongConstant(
5106 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5107 }
5108 // TODO: Evaluation for floating-point values.
5109 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005110 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005111 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005112 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005113
5114 DECLARE_INSTRUCTION(Min);
5115
5116 protected:
5117 DEFAULT_COPY_CONSTRUCTOR(Min);
5118};
5119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005120class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005121 public:
5122 HMax(DataType::Type result_type,
5123 HInstruction* left,
5124 HInstruction* right,
5125 uint32_t dex_pc)
5126 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005129
5130 // Evaluation for integral values.
5131 template <typename T> static T ComputeIntegral(T x, T y) {
5132 return (x >= y) ? x : y;
5133 }
5134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005136 return GetBlock()->GetGraph()->GetIntConstant(
5137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005139 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005140 return GetBlock()->GetGraph()->GetLongConstant(
5141 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5142 }
5143 // TODO: Evaluation for floating-point values.
5144 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005145 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005146 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005147 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005148
5149 DECLARE_INSTRUCTION(Max);
5150
5151 protected:
5152 DEFAULT_COPY_CONSTRUCTOR(Max);
5153};
5154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005155class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005156 public:
5157 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5158 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5159
5160 // Evaluation for integral values.
5161 template <typename T> static T ComputeIntegral(T x) {
5162 return x < 0 ? -x : x;
5163 }
5164
5165 // Evaluation for floating-point values.
5166 // Note, as a "quality of implementation", rather than pure "spec compliance",
5167 // we require that Math.abs() clears the sign bit (but changes nothing else)
5168 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5169 // http://b/30758343
5170 template <typename T, typename S> static T ComputeFP(T x) {
5171 S bits = bit_cast<S, T>(x);
5172 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5173 }
5174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005176 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5177 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005179 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005182 return GetBlock()->GetGraph()->GetFloatConstant(
5183 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5184 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005186 return GetBlock()->GetGraph()->GetDoubleConstant(
5187 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5188 }
5189
5190 DECLARE_INSTRUCTION(Abs);
5191
5192 protected:
5193 DEFAULT_COPY_CONSTRUCTOR(Abs);
5194};
5195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005196class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005197 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005198 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005199 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5200 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005201 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005202 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005203 SetRawInputAt(0, value);
5204 }
5205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 bool IsClonable() const override { return true; }
5207 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005210 return true;
5211 }
5212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005213 bool NeedsEnvironment() const override { return true; }
5214 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005215
Calin Juravled0d48522014-11-04 16:40:20 +00005216 DECLARE_INSTRUCTION(DivZeroCheck);
5217
Artem Serovcced8ba2017-07-19 18:18:09 +01005218 protected:
5219 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005220};
5221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005222class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005223 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005225 HInstruction* value,
5226 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305228 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5230 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005231 }
5232
Roland Levillain5b5b9312016-03-22 14:57:31 +00005233 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005234 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005235 return value << (distance & max_shift_distance);
5236 }
5237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005239 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005240 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005241 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005242 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005243 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005244 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005245 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005246 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5249 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005250 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005251 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005253 LOG(FATAL) << DebugName() << " is not defined for float values";
5254 UNREACHABLE();
5255 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005257 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005258 LOG(FATAL) << DebugName() << " is not defined for double values";
5259 UNREACHABLE();
5260 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005261
5262 DECLARE_INSTRUCTION(Shl);
5263
Artem Serovcced8ba2017-07-19 18:18:09 +01005264 protected:
5265 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005266};
5267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005269 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005271 HInstruction* value,
5272 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305274 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5276 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005277 }
5278
Roland Levillain5b5b9312016-03-22 14:57:31 +00005279 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005280 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005281 return value >> (distance & max_shift_distance);
5282 }
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005286 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005288 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005289 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005290 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005292 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005293 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005294 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5295 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005296 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005298 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005299 LOG(FATAL) << DebugName() << " is not defined for float values";
5300 UNREACHABLE();
5301 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005303 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005304 LOG(FATAL) << DebugName() << " is not defined for double values";
5305 UNREACHABLE();
5306 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005307
5308 DECLARE_INSTRUCTION(Shr);
5309
Artem Serovcced8ba2017-07-19 18:18:09 +01005310 protected:
5311 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005312};
5313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005314class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005315 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005317 HInstruction* value,
5318 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005319 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305320 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005321 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5322 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005323 }
5324
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005326 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005327 typedef typename std::make_unsigned<T>::type V;
5328 V ux = static_cast<V>(value);
5329 return static_cast<T>(ux >> (distance & max_shift_distance));
5330 }
5331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005332 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005333 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005335 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005336 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005337 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005338 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005340 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005341 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5343 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005344 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005345 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005347 LOG(FATAL) << DebugName() << " is not defined for float values";
5348 UNREACHABLE();
5349 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005352 LOG(FATAL) << DebugName() << " is not defined for double values";
5353 UNREACHABLE();
5354 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005355
5356 DECLARE_INSTRUCTION(UShr);
5357
Artem Serovcced8ba2017-07-19 18:18:09 +01005358 protected:
5359 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005360};
5361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005362class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005363 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005364 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 HInstruction* left,
5366 HInstruction* right,
5367 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305368 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5369 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005370
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005371 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005372
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005373 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 return GetBlock()->GetGraph()->GetIntConstant(
5377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005378 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 return GetBlock()->GetGraph()->GetLongConstant(
5381 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005382 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005383 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005384 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005385 LOG(FATAL) << DebugName() << " is not defined for float values";
5386 UNREACHABLE();
5387 }
5388 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005390 LOG(FATAL) << DebugName() << " is not defined for double values";
5391 UNREACHABLE();
5392 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005393
5394 DECLARE_INSTRUCTION(And);
5395
Artem Serovcced8ba2017-07-19 18:18:09 +01005396 protected:
5397 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005398};
5399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005400class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005401 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005403 HInstruction* left,
5404 HInstruction* right,
5405 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305406 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5407 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005410
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005411 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 return GetBlock()->GetGraph()->GetIntConstant(
5415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005416 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 return GetBlock()->GetGraph()->GetLongConstant(
5419 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005420 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005421 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005422 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005423 LOG(FATAL) << DebugName() << " is not defined for float values";
5424 UNREACHABLE();
5425 }
5426 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005427 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005428 LOG(FATAL) << DebugName() << " is not defined for double values";
5429 UNREACHABLE();
5430 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005431
5432 DECLARE_INSTRUCTION(Or);
5433
Artem Serovcced8ba2017-07-19 18:18:09 +01005434 protected:
5435 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005436};
5437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005438class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005439 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005440 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005441 HInstruction* left,
5442 HInstruction* right,
5443 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305444 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5445 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005448
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005449 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005452 return GetBlock()->GetGraph()->GetIntConstant(
5453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005455 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005456 return GetBlock()->GetGraph()->GetLongConstant(
5457 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005458 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005459 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005460 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005461 LOG(FATAL) << DebugName() << " is not defined for float values";
5462 UNREACHABLE();
5463 }
5464 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005465 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005466 LOG(FATAL) << DebugName() << " is not defined for double values";
5467 UNREACHABLE();
5468 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005469
5470 DECLARE_INSTRUCTION(Xor);
5471
Artem Serovcced8ba2017-07-19 18:18:09 +01005472 protected:
5473 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005474};
5475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005476class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005477 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005478 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305479 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5481 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005482 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005483
Roland Levillain5b5b9312016-03-22 14:57:31 +00005484 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005485 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005486 typedef typename std::make_unsigned<T>::type V;
5487 V ux = static_cast<V>(value);
5488 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005489 return static_cast<T>(ux);
5490 } else {
5491 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005492 return static_cast<T>(ux >> (distance & max_shift_value)) |
5493 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005494 }
5495 }
5496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005497 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005498 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005499 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005500 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005501 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005502 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005503 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005504 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005505 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005506 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005507 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5508 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005509 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005510 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005511 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005512 LOG(FATAL) << DebugName() << " is not defined for float values";
5513 UNREACHABLE();
5514 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005515 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005517 LOG(FATAL) << DebugName() << " is not defined for double values";
5518 UNREACHABLE();
5519 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005520
5521 DECLARE_INSTRUCTION(Ror);
5522
Artem Serovcced8ba2017-07-19 18:18:09 +01005523 protected:
5524 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005525};
5526
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005527// The value of a parameter in this method. Its location depends on
5528// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005530 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005531 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005532 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005533 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305536 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005537 dex_file_(dex_file),
5538 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 index_(index) {
5540 SetPackedFlag<kFlagIsThis>(is_this);
5541 SetPackedFlag<kFlagCanBeNull>(!is_this);
5542 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005543
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005544 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005545 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005546 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005547 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005549 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005550 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005551
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005552 DECLARE_INSTRUCTION(ParameterValue);
5553
Artem Serovcced8ba2017-07-19 18:18:09 +01005554 protected:
5555 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5556
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005557 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005559 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005560 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5561 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5562 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5563 "Too many packed fields.");
5564
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005565 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005566 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005567 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005568 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005569 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005570};
5571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005573 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305575 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5576 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005578 bool CanBeMoved() const override { return true; }
5579 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005580 return true;
5581 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005582
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005583 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005586 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005587 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005588 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005589 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005590 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005591 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005592 LOG(FATAL) << DebugName() << " is not defined for float values";
5593 UNREACHABLE();
5594 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005595 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005596 LOG(FATAL) << DebugName() << " is not defined for double values";
5597 UNREACHABLE();
5598 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005599
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005600 DECLARE_INSTRUCTION(Not);
5601
Artem Serovcced8ba2017-07-19 18:18:09 +01005602 protected:
5603 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005604};
5605
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005606class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005608 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305609 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5610 }
David Brazdil66d126e2015-04-03 16:02:44 +01005611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612 bool CanBeMoved() const override { return true; }
5613 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005614 return true;
5615 }
5616
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005617 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005618 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005619 return !x;
5620 }
5621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005622 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005623 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005624 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005626 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005627 UNREACHABLE();
5628 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005630 LOG(FATAL) << DebugName() << " is not defined for float values";
5631 UNREACHABLE();
5632 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005633 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005634 LOG(FATAL) << DebugName() << " is not defined for double values";
5635 UNREACHABLE();
5636 }
David Brazdil66d126e2015-04-03 16:02:44 +01005637
5638 DECLARE_INSTRUCTION(BooleanNot);
5639
Artem Serovcced8ba2017-07-19 18:18:09 +01005640 protected:
5641 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005642};
5643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005645 public:
5646 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005647 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305648 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005649 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005650 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005652 }
5653
5654 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005655 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5656 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658 bool IsClonable() const override { return true; }
5659 bool CanBeMoved() const override { return true; }
5660 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005661 return true;
5662 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005663
Mark Mendelle82549b2015-05-06 10:55:34 -04005664 // Try to statically evaluate the conversion and return a HConstant
5665 // containing the result. If the input cannot be converted, return nullptr.
5666 HConstant* TryStaticEvaluation() const;
5667
Roland Levillaindff1f282014-11-05 14:15:05 +00005668 DECLARE_INSTRUCTION(TypeConversion);
5669
Artem Serovcced8ba2017-07-19 18:18:09 +01005670 protected:
5671 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005672};
5673
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005674static constexpr uint32_t kNoRegNumber = -1;
5675
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005676class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005677 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005678 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005679 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5680 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005681 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005682 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005683 SetRawInputAt(0, value);
5684 }
5685
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005686 bool IsClonable() const override { return true; }
5687 bool CanBeMoved() const override { return true; }
5688 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005689 return true;
5690 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005691
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005692 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005693
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005695
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005696 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005697
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005698 DECLARE_INSTRUCTION(NullCheck);
5699
Artem Serovcced8ba2017-07-19 18:18:09 +01005700 protected:
5701 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702};
5703
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005704// Embeds an ArtField and all the information required by the compiler. We cache
5705// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706class FieldInfo : public ValueObject {
5707 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005708 FieldInfo(ArtField* field,
5709 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005710 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005711 bool is_volatile,
5712 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005713 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005714 const DexFile& dex_file)
5715 : field_(field),
5716 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005717 field_type_(field_type),
5718 is_volatile_(is_volatile),
5719 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005720 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005721 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005722
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005723 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005724 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005726 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005727 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005728 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005729 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005730
5731 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005732 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005735 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005736 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005737 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005738 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005739};
5740
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005741class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 public:
5743 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005744 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005745 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005746 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005747 bool is_volatile,
5748 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005749 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005750 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005751 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305752 : HExpression(kInstanceFieldGet,
5753 field_type,
5754 SideEffects::FieldReadOfType(field_type, is_volatile),
5755 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005756 field_info_(field,
5757 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005758 field_type,
5759 is_volatile,
5760 field_idx,
5761 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005762 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005763 SetRawInputAt(0, value);
5764 }
5765
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005766 bool IsClonable() const override { return true; }
5767 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005769 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005770 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005771 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005772 }
5773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005774 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005775 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005776 }
5777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005778 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005779 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5780 }
5781
Calin Juravle52c48962014-12-16 17:02:57 +00005782 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005783 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005785 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005786
Vladimir Marko61b92282017-10-11 13:23:17 +01005787 void SetType(DataType::Type new_type) {
5788 DCHECK(DataType::IsIntegralType(GetType()));
5789 DCHECK(DataType::IsIntegralType(new_type));
5790 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5791 SetPackedField<TypeField>(new_type);
5792 }
5793
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005794 DECLARE_INSTRUCTION(InstanceFieldGet);
5795
Artem Serovcced8ba2017-07-19 18:18:09 +01005796 protected:
5797 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5798
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005799 private:
5800 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005801};
5802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005803class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804 public:
5805 HInstanceFieldSet(HInstruction* object,
5806 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005807 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005809 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005810 bool is_volatile,
5811 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005812 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005813 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005814 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005815 : HExpression(kInstanceFieldSet,
5816 SideEffects::FieldWriteOfType(field_type, is_volatile),
5817 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005818 field_info_(field,
5819 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005820 field_type,
5821 is_volatile,
5822 field_idx,
5823 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005824 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005825 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826 SetRawInputAt(0, object);
5827 SetRawInputAt(1, value);
5828 }
5829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005830 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005831
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005832 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005833 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005834 }
5835
Calin Juravle52c48962014-12-16 17:02:57 +00005836 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005837 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005839 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005840 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5842 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005843
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005844 DECLARE_INSTRUCTION(InstanceFieldSet);
5845
Artem Serovcced8ba2017-07-19 18:18:09 +01005846 protected:
5847 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5848
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005849 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005850 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5851 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5852 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5853 "Too many packed fields.");
5854
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005855 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005856};
5857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005858class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005859 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005860 HArrayGet(HInstruction* array,
5861 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005863 uint32_t dex_pc)
5864 : HArrayGet(array,
5865 index,
5866 type,
5867 SideEffects::ArrayReadOfType(type),
5868 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305869 /* is_string_char_at */ false) {
5870 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005871
5872 HArrayGet(HInstruction* array,
5873 HInstruction* index,
5874 DataType::Type type,
5875 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005876 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005877 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305878 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005879 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005880 SetRawInputAt(0, array);
5881 SetRawInputAt(1, index);
5882 }
5883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005884 bool IsClonable() const override { return true; }
5885 bool CanBeMoved() const override { return true; }
5886 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005887 return true;
5888 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005889 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005890 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005891 // Currently, unless the array is the result of NewArray, the array access is always
5892 // preceded by some form of null NullCheck necessary for the bounds check, usually
5893 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5894 // dynamic BCE. There are cases when these could be removed to produce better code.
5895 // If we ever add optimizations to do so we should allow an implicit check here
5896 // (as long as the address falls in the first page).
5897 //
5898 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5899 // a = cond ? new int[1] : null;
5900 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005901 return false;
5902 }
5903
David Brazdil4833f5a2015-12-16 10:37:39 +00005904 bool IsEquivalentOf(HArrayGet* other) const {
5905 bool result = (GetDexPc() == other->GetDexPc());
5906 if (kIsDebugBuild && result) {
5907 DCHECK_EQ(GetBlock(), other->GetBlock());
5908 DCHECK_EQ(GetArray(), other->GetArray());
5909 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005910 if (DataType::IsIntOrLongType(GetType())) {
5911 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005912 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5914 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005915 }
5916 }
5917 return result;
5918 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005919
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005920 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5921
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005922 HInstruction* GetArray() const { return InputAt(0); }
5923 HInstruction* GetIndex() const { return InputAt(1); }
5924
Vladimir Marko61b92282017-10-11 13:23:17 +01005925 void SetType(DataType::Type new_type) {
5926 DCHECK(DataType::IsIntegralType(GetType()));
5927 DCHECK(DataType::IsIntegralType(new_type));
5928 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5929 SetPackedField<TypeField>(new_type);
5930 }
5931
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 DECLARE_INSTRUCTION(ArrayGet);
5933
Artem Serovcced8ba2017-07-19 18:18:09 +01005934 protected:
5935 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5936
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005938 // We treat a String as an array, creating the HArrayGet from String.charAt()
5939 // intrinsic in the instruction simplifier. We can always determine whether
5940 // a particular HArrayGet is actually a String.charAt() by looking at the type
5941 // of the input but that requires holding the mutator lock, so we prefer to use
5942 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005943 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005944 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5945 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5946 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005947};
5948
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005949class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950 public:
5951 HArraySet(HInstruction* array,
5952 HInstruction* index,
5953 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005955 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005956 : HArraySet(array,
5957 index,
5958 value,
5959 expected_component_type,
5960 // Make a best guess for side effects now, may be refined during SSA building.
5961 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305962 dex_pc) {
5963 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005964
5965 HArraySet(HInstruction* array,
5966 HInstruction* index,
5967 HInstruction* value,
5968 DataType::Type expected_component_type,
5969 SideEffects side_effects,
5970 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005971 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005973 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 SetPackedFlag<kFlagValueCanBeNull>(true);
5975 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976 SetRawInputAt(0, array);
5977 SetRawInputAt(1, index);
5978 SetRawInputAt(2, value);
5979 }
5980
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005981 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005983 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005984 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005985 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986 }
5987
Mingyao Yang81014cb2015-06-02 03:16:27 -07005988 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005989 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005990
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005991 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005992 // TODO: Same as for ArrayGet.
5993 return false;
5994 }
5995
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005996 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005997 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005998 }
5999
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006000 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006002 }
6003
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006004 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006006 }
6007
Vladimir Markoa1de9182016-02-25 11:37:38 +00006008 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6009 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6010 bool StaticTypeOfArrayIsObjectArray() const {
6011 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6012 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006013
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006014 HInstruction* GetArray() const { return InputAt(0); }
6015 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006016 HInstruction* GetValue() const { return InputAt(2); }
6017
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006019 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6020 }
6021
6022 static DataType::Type GetComponentType(DataType::Type value_type,
6023 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006024 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006025 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006026 // be correct, we also check what is the value type. If it is a floating
6027 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006028 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006029 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006030 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006031 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006033 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006034 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006035 }
6036
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006037 static SideEffects ComputeSideEffects(DataType::Type type) {
6038 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006039 }
6040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6042 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6043 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006044 }
6045
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046 DECLARE_INSTRUCTION(ArraySet);
6047
Artem Serovcced8ba2017-07-19 18:18:09 +01006048 protected:
6049 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6050
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6053 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006055 static constexpr size_t kFlagNeedsTypeCheck =
6056 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6057 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006058 // Cached information for the reference_type_info_ so that codegen
6059 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006060 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6061 static constexpr size_t kNumberOfArraySetPackedBits =
6062 kFlagStaticTypeOfArrayIsObjectArray + 1;
6063 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6064 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066};
6067
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006068class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006070 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306071 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006072 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006073 // Note that arrays do not change length, so the instruction does not
6074 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006075 SetRawInputAt(0, array);
6076 }
6077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006078 bool IsClonable() const override { return true; }
6079 bool CanBeMoved() const override { return true; }
6080 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006081 return true;
6082 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006083 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006084 return obj == InputAt(0);
6085 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006086
Vladimir Markodce016e2016-04-28 13:10:02 +01006087 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6088
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006089 DECLARE_INSTRUCTION(ArrayLength);
6090
Artem Serovcced8ba2017-07-19 18:18:09 +01006091 protected:
6092 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6093
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006094 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006095 // We treat a String as an array, creating the HArrayLength from String.length()
6096 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6097 // determine whether a particular HArrayLength is actually a String.length() by
6098 // looking at the type of the input but that requires holding the mutator lock, so
6099 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006100 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006101 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6102 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6103 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006104};
6105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006106class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006108 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006109 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6110 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006111 HBoundsCheck(HInstruction* index,
6112 HInstruction* length,
6113 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006114 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006115 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006116 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006117 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006118 SetRawInputAt(0, index);
6119 SetRawInputAt(1, length);
6120 }
6121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006122 bool IsClonable() const override { return true; }
6123 bool CanBeMoved() const override { return true; }
6124 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006125 return true;
6126 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006128 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006129
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006130 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006131
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006132 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006133
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006134 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006135
6136 DECLARE_INSTRUCTION(BoundsCheck);
6137
Artem Serovcced8ba2017-07-19 18:18:09 +01006138 protected:
6139 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6140
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006141 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006142 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006143};
6144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006145class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006146 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006147 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006148 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306149 slow_path_(nullptr) {
6150 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006152 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006153
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006154 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006155 return true;
6156 }
6157
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006158 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6159 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006160
6161 DECLARE_INSTRUCTION(SuspendCheck);
6162
Artem Serovcced8ba2017-07-19 18:18:09 +01006163 protected:
6164 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6165
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006166 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006167 // Only used for code generation, in order to share the same slow path between back edges
6168 // of a same loop.
6169 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006170};
6171
David Srbecky0cf44932015-12-09 14:09:59 +00006172// Pseudo-instruction which provides the native debugger with mapping information.
6173// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006174class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006175 public:
6176 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006177 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306178 }
David Srbecky0cf44932015-12-09 14:09:59 +00006179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006180 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006181 return true;
6182 }
6183
6184 DECLARE_INSTRUCTION(NativeDebugInfo);
6185
Artem Serovcced8ba2017-07-19 18:18:09 +01006186 protected:
6187 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006188};
6189
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006190/**
6191 * Instruction to load a Class object.
6192 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006193class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006194 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006195 // Determines how to load the Class.
6196 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006197 // We cannot load this class. See HSharpening::SharpenLoadClass.
6198 kInvalid = -1,
6199
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006200 // Use the Class* from the method's own ArtMethod*.
6201 kReferrersClass,
6202
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006203 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006204 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006205 kBootImageLinkTimePcRelative,
6206
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006207 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006208 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006209 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006210
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006211 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006212 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006213 kBssEntry,
6214
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006215 // Use a known boot image Class* address, embedded in the code by the codegen.
6216 // Used for boot image classes referenced by apps in JIT-compiled code.
6217 kJitBootImageAddress,
6218
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006219 // Load from the root table associated with the JIT compiled method.
6220 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006221
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006222 // Load using a simple runtime call. This is the fall-back load kind when
6223 // the codegen is unable to use another appropriate kind.
6224 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006225
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006226 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006227 };
6228
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006229 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006230 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006231 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006232 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006233 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006234 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006235 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006236 : HInstruction(kLoadClass,
6237 DataType::Type::kReference,
6238 SideEffectsForArchRuntimeCalls(),
6239 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006240 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006241 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006242 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006243 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006244 // Referrers class should not need access check. We never inline unverified
6245 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 DCHECK(!is_referrers_class || !needs_access_check);
6247
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006248 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006249 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006251 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006252 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006253 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006254 }
6255
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006256 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006257
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006258 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006259
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006260 LoadKind GetLoadKind() const {
6261 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006262 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006263
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006264 bool HasPcRelativeLoadKind() const {
6265 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6266 GetLoadKind() == LoadKind::kBootImageRelRo ||
6267 GetLoadKind() == LoadKind::kBssEntry;
6268 }
6269
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006270 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006271
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006272 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006274 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006276 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006278 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006279 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006280 }
6281
Calin Juravle0ba218d2015-05-19 18:46:01 +01006282 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006283 // The entrypoint the code generator is going to call does not do
6284 // clinit of the class.
6285 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006286 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006287 }
6288
6289 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006290 return NeedsAccessCheck() ||
6291 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006292 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006293 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006294 }
6295
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006296 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006297 return NeedsAccessCheck() ||
6298 MustGenerateClinitCheck() ||
6299 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006300 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006301 GetLoadKind() == LoadKind::kBssEntry) &&
6302 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006303 }
6304
Calin Juravleacf735c2015-02-12 15:25:22 +00006305 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006306 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6307 // Note: The is_exact flag from the return value should not be used.
6308 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6309 } else {
6310 return ReferenceTypeInfo::CreateInvalid();
6311 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006312 }
6313
Vladimir Marko175e7862018-03-27 09:03:13 +00006314 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6315 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6316 DCHECK(klass_ != nullptr);
6317 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006318 }
6319
Andreas Gampea5b09a62016-11-17 15:21:22 -08006320 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006321 const DexFile& GetDexFile() const { return dex_file_; }
6322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006323 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006324 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006325 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006326
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006327 static SideEffects SideEffectsForArchRuntimeCalls() {
6328 return SideEffects::CanTriggerGC();
6329 }
6330
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006331 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006333 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006334 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006335
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006336 bool MustResolveTypeOnSlowPath() const {
6337 // Check that this instruction has a slow path.
6338 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6339 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6340 return GetLoadKind() == LoadKind::kBssEntry;
6341 }
6342
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006343 void MarkInBootImage() {
6344 SetPackedFlag<kFlagIsInBootImage>(true);
6345 }
6346
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006347 void AddSpecialInput(HInstruction* special_input);
6348
6349 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006350 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006351 return ArrayRef<HUserRecord<HInstruction*>>(
6352 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6353 }
6354
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006355 Handle<mirror::Class> GetClass() const {
6356 return klass_;
6357 }
6358
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006359 DECLARE_INSTRUCTION(LoadClass);
6360
Artem Serovcced8ba2017-07-19 18:18:09 +01006361 protected:
6362 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6363
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006364 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006365 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006366 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006367 // Whether this instruction must generate the initialization check.
6368 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006369 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006370 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6371 static constexpr size_t kFieldLoadKindSize =
6372 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006373 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6374 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006375 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006376 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6377
6378 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006379 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006380 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006381 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006382 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006383 }
6384
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006385 void SetLoadKindInternal(LoadKind load_kind);
6386
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006387 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006388 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006389 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006392 // A type index and dex file where the class can be accessed. The dex file can be:
6393 // - The compiling method's dex file if the class is defined there too.
6394 // - The compiling method's dex file if the class is referenced there.
6395 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006396 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006397 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006399
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006400 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006401};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006402std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6403
6404// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006405inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6406 // The load kind should be determined before inserting the instruction to the graph.
6407 DCHECK(GetBlock() == nullptr);
6408 DCHECK(GetEnvironment() == nullptr);
6409 SetPackedField<LoadKindField>(load_kind);
6410 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6411 special_input_ = HUserRecord<HInstruction*>(nullptr);
6412 }
6413 if (!NeedsEnvironment()) {
6414 SetSideEffects(SideEffects::None());
6415 }
6416}
6417
6418// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006419inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006420 // The special input is used for PC-relative loads on some architectures,
6421 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006422 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006423 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006424 GetLoadKind() == LoadKind::kBssEntry ||
6425 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006426 DCHECK(special_input_.GetInstruction() == nullptr);
6427 special_input_ = HUserRecord<HInstruction*>(special_input);
6428 special_input->AddUseAt(this, 0);
6429}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006431class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006432 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006433 // Determines how to load the String.
6434 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006435 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006436 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006437 kBootImageLinkTimePcRelative,
6438
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006439 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006440 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006441 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006442
Vladimir Markoaad75c62016-10-03 08:46:48 +00006443 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006444 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006445 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006446
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006447 // Use a known boot image String* address, embedded in the code by the codegen.
6448 // Used for boot image strings referenced by apps in JIT-compiled code.
6449 kJitBootImageAddress,
6450
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006451 // Load from the root table associated with the JIT compiled method.
6452 kJitTableAddress,
6453
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006454 // Load using a simple runtime call. This is the fall-back load kind when
6455 // the codegen is unable to use another appropriate kind.
6456 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006457
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006458 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006459 };
6460
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006461 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006462 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006463 const DexFile& dex_file,
6464 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006465 : HInstruction(kLoadString,
6466 DataType::Type::kReference,
6467 SideEffectsForArchRuntimeCalls(),
6468 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006469 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006470 string_index_(string_index),
6471 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006472 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006473 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006475 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006476
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006477 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006479 LoadKind GetLoadKind() const {
6480 return GetPackedField<LoadKindField>();
6481 }
6482
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006483 bool HasPcRelativeLoadKind() const {
6484 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6485 GetLoadKind() == LoadKind::kBootImageRelRo ||
6486 GetLoadKind() == LoadKind::kBssEntry;
6487 }
6488
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006489 const DexFile& GetDexFile() const {
6490 return dex_file_;
6491 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006492
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006493 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006494 return string_index_;
6495 }
6496
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006497 Handle<mirror::String> GetString() const {
6498 return string_;
6499 }
6500
6501 void SetString(Handle<mirror::String> str) {
6502 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006503 }
6504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006505 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006506
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006507 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006509 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006510
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006511 // Will call the runtime if we need to load the string through
6512 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006513 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006514 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006515 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006516 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006517 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006518 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006519 return false;
6520 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006521 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006523
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006524 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006525 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006528 bool CanBeNull() const override { return false; }
6529 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006530
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006531 static SideEffects SideEffectsForArchRuntimeCalls() {
6532 return SideEffects::CanTriggerGC();
6533 }
6534
Vladimir Marko372f10e2016-05-17 16:30:10 +01006535 void AddSpecialInput(HInstruction* special_input);
6536
6537 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006538 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006539 return ArrayRef<HUserRecord<HInstruction*>>(
6540 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006541 }
6542
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006543 DECLARE_INSTRUCTION(LoadString);
6544
Artem Serovcced8ba2017-07-19 18:18:09 +01006545 protected:
6546 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6547
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006548 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006549 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006550 static constexpr size_t kFieldLoadKindSize =
6551 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6552 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006553 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006554 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006555
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006556 void SetLoadKindInternal(LoadKind load_kind);
6557
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006558 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006559 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006560 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006561 HUserRecord<HInstruction*> special_input_;
6562
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006563 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006564 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006565
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006566 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006567};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006568std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6569
6570// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006571inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6572 // The load kind should be determined before inserting the instruction to the graph.
6573 DCHECK(GetBlock() == nullptr);
6574 DCHECK(GetEnvironment() == nullptr);
6575 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6576 SetPackedField<LoadKindField>(load_kind);
6577 if (load_kind != LoadKind::kRuntimeCall) {
6578 special_input_ = HUserRecord<HInstruction*>(nullptr);
6579 }
6580 if (!NeedsEnvironment()) {
6581 SetSideEffects(SideEffects::None());
6582 }
6583}
6584
6585// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006587 // The special input is used for PC-relative loads on some architectures,
6588 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006590 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006591 GetLoadKind() == LoadKind::kBssEntry ||
6592 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006593 // HLoadString::GetInputRecords() returns an empty array at this point,
6594 // so use the GetInputRecords() from the base class to set the input record.
6595 DCHECK(special_input_.GetInstruction() == nullptr);
6596 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006597 special_input->AddUseAt(this, 0);
6598}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006599
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006600class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006601 public:
6602 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006603 uint16_t method_handle_idx,
6604 const DexFile& dex_file,
6605 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006606 : HInstruction(kLoadMethodHandle,
6607 DataType::Type::kReference,
6608 SideEffectsForArchRuntimeCalls(),
6609 dex_pc),
6610 special_input_(HUserRecord<HInstruction*>(current_method)),
6611 method_handle_idx_(method_handle_idx),
6612 dex_file_(dex_file) {
6613 }
6614
6615 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006616 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006617 return ArrayRef<HUserRecord<HInstruction*>>(
6618 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6619 }
6620
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006621 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006622
6623 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6624
6625 const DexFile& GetDexFile() const { return dex_file_; }
6626
6627 static SideEffects SideEffectsForArchRuntimeCalls() {
6628 return SideEffects::CanTriggerGC();
6629 }
6630
6631 DECLARE_INSTRUCTION(LoadMethodHandle);
6632
6633 protected:
6634 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6635
6636 private:
6637 // The special input is the HCurrentMethod for kRuntimeCall.
6638 HUserRecord<HInstruction*> special_input_;
6639
6640 const uint16_t method_handle_idx_;
6641 const DexFile& dex_file_;
6642};
6643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006644class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006645 public:
6646 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006647 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006648 const DexFile& dex_file,
6649 uint32_t dex_pc)
6650 : HInstruction(kLoadMethodType,
6651 DataType::Type::kReference,
6652 SideEffectsForArchRuntimeCalls(),
6653 dex_pc),
6654 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006655 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006656 dex_file_(dex_file) {
6657 }
6658
6659 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006660 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006661 return ArrayRef<HUserRecord<HInstruction*>>(
6662 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6663 }
6664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006665 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006666
Orion Hodson06d10a72018-05-14 08:53:38 +01006667 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006668
6669 const DexFile& GetDexFile() const { return dex_file_; }
6670
6671 static SideEffects SideEffectsForArchRuntimeCalls() {
6672 return SideEffects::CanTriggerGC();
6673 }
6674
6675 DECLARE_INSTRUCTION(LoadMethodType);
6676
6677 protected:
6678 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6679
6680 private:
6681 // The special input is the HCurrentMethod for kRuntimeCall.
6682 HUserRecord<HInstruction*> special_input_;
6683
Orion Hodson06d10a72018-05-14 08:53:38 +01006684 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006685 const DexFile& dex_file_;
6686};
6687
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006688/**
6689 * Performs an initialization check on its Class object input.
6690 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006691class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006692 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006693 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006694 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306695 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006697 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006698 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006699 SetRawInputAt(0, constant);
6700 }
Artem Serova6e26142018-06-19 14:55:17 +01006701 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006702 bool CanBeMoved() const override { return true; }
6703 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006704 return true;
6705 }
6706
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006707 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006708 // May call runtime to initialize the class.
6709 return true;
6710 }
6711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006712 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006713
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006714 HLoadClass* GetLoadClass() const {
6715 DCHECK(InputAt(0)->IsLoadClass());
6716 return InputAt(0)->AsLoadClass();
6717 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006718
6719 DECLARE_INSTRUCTION(ClinitCheck);
6720
Artem Serovcced8ba2017-07-19 18:18:09 +01006721
6722 protected:
6723 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006724};
6725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006726class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006727 public:
6728 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006729 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006731 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006732 bool is_volatile,
6733 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006734 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006735 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006736 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306737 : HExpression(kStaticFieldGet,
6738 field_type,
6739 SideEffects::FieldReadOfType(field_type, is_volatile),
6740 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006741 field_info_(field,
6742 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006743 field_type,
6744 is_volatile,
6745 field_idx,
6746 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006747 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006748 SetRawInputAt(0, cls);
6749 }
6750
Calin Juravle52c48962014-12-16 17:02:57 +00006751
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006752 bool IsClonable() const override { return true; }
6753 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006754
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006755 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006756 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006757 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006758 }
6759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006760 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006761 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6762 }
6763
Calin Juravle52c48962014-12-16 17:02:57 +00006764 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006765 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006766 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006767 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768
Vladimir Marko61b92282017-10-11 13:23:17 +01006769 void SetType(DataType::Type new_type) {
6770 DCHECK(DataType::IsIntegralType(GetType()));
6771 DCHECK(DataType::IsIntegralType(new_type));
6772 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6773 SetPackedField<TypeField>(new_type);
6774 }
6775
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006776 DECLARE_INSTRUCTION(StaticFieldGet);
6777
Artem Serovcced8ba2017-07-19 18:18:09 +01006778 protected:
6779 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6780
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006781 private:
6782 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006783};
6784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006785class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786 public:
6787 HStaticFieldSet(HInstruction* cls,
6788 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006789 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006790 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006791 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006792 bool is_volatile,
6793 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006794 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006795 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006796 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006797 : HExpression(kStaticFieldSet,
6798 SideEffects::FieldWriteOfType(field_type, is_volatile),
6799 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006800 field_info_(field,
6801 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006802 field_type,
6803 is_volatile,
6804 field_idx,
6805 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006806 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006807 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808 SetRawInputAt(0, cls);
6809 SetRawInputAt(1, value);
6810 }
6811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006812 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006813 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006815 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006816 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006817
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006818 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006819 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6820 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006821
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006822 DECLARE_INSTRUCTION(StaticFieldSet);
6823
Artem Serovcced8ba2017-07-19 18:18:09 +01006824 protected:
6825 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6826
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006827 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006828 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6829 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6830 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6831 "Too many packed fields.");
6832
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006833 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006834};
6835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006836class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006837 public:
6838 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006839 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006840 uint32_t field_index,
6841 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306842 : HExpression(kUnresolvedInstanceFieldGet,
6843 field_type,
6844 SideEffects::AllExceptGCDependency(),
6845 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006846 field_index_(field_index) {
6847 SetRawInputAt(0, obj);
6848 }
6849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006850 bool IsClonable() const override { return true; }
6851 bool NeedsEnvironment() const override { return true; }
6852 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006853
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006854 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006855 uint32_t GetFieldIndex() const { return field_index_; }
6856
6857 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6858
Artem Serovcced8ba2017-07-19 18:18:09 +01006859 protected:
6860 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6861
Calin Juravlee460d1d2015-09-29 04:52:17 +01006862 private:
6863 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006864};
6865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006866class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 public:
6868 HUnresolvedInstanceFieldSet(HInstruction* obj,
6869 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006871 uint32_t field_index,
6872 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006873 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006875 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006876 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006877 SetRawInputAt(0, obj);
6878 SetRawInputAt(1, value);
6879 }
6880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006881 bool IsClonable() const override { return true; }
6882 bool NeedsEnvironment() const override { return true; }
6883 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006885 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006886 uint32_t GetFieldIndex() const { return field_index_; }
6887
6888 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6889
Artem Serovcced8ba2017-07-19 18:18:09 +01006890 protected:
6891 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6892
Calin Juravlee460d1d2015-09-29 04:52:17 +01006893 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006894 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6895 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006896 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006897 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6898 kFieldFieldType + kFieldFieldTypeSize;
6899 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6900 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006901 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006902
Calin Juravlee460d1d2015-09-29 04:52:17 +01006903 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006904};
6905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006906class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006907 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006908 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006909 uint32_t field_index,
6910 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306911 : HExpression(kUnresolvedStaticFieldGet,
6912 field_type,
6913 SideEffects::AllExceptGCDependency(),
6914 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006915 field_index_(field_index) {
6916 }
6917
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006918 bool IsClonable() const override { return true; }
6919 bool NeedsEnvironment() const override { return true; }
6920 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006922 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006923 uint32_t GetFieldIndex() const { return field_index_; }
6924
6925 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6926
Artem Serovcced8ba2017-07-19 18:18:09 +01006927 protected:
6928 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6929
Calin Juravlee460d1d2015-09-29 04:52:17 +01006930 private:
6931 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006932};
6933
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006934class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006935 public:
6936 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006937 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006938 uint32_t field_index,
6939 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006940 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006941 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006942 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006943 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006944 SetRawInputAt(0, value);
6945 }
6946
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006947 bool IsClonable() const override { return true; }
6948 bool NeedsEnvironment() const override { return true; }
6949 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006951 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006952 uint32_t GetFieldIndex() const { return field_index_; }
6953
6954 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6955
Artem Serovcced8ba2017-07-19 18:18:09 +01006956 protected:
6957 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6958
Calin Juravlee460d1d2015-09-29 04:52:17 +01006959 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006960 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6961 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006962 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006963 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6964 kFieldFieldType + kFieldFieldTypeSize;
6965 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6966 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006967 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006968
Calin Juravlee460d1d2015-09-29 04:52:17 +01006969 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006970};
6971
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006972// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006973class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006974 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006975 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306976 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6977 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006978
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006979 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01006980
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006981 DECLARE_INSTRUCTION(LoadException);
6982
Artem Serovcced8ba2017-07-19 18:18:09 +01006983 protected:
6984 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006985};
6986
David Brazdilcb1c0552015-08-04 16:22:25 +01006987// Implicit part of move-exception which clears thread-local exception storage.
6988// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006989class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006990 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006991 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006992 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306993 }
David Brazdilcb1c0552015-08-04 16:22:25 +01006994
6995 DECLARE_INSTRUCTION(ClearException);
6996
Artem Serovcced8ba2017-07-19 18:18:09 +01006997 protected:
6998 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01006999};
7000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007001class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007002 public:
7003 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007004 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007005 SetRawInputAt(0, exception);
7006 }
7007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007008 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007010 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007012 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007014 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007015
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007016 DECLARE_INSTRUCTION(Throw);
7017
Artem Serovcced8ba2017-07-19 18:18:09 +01007018 protected:
7019 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007020};
7021
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007022/**
7023 * Implementation strategies for the code generator of a HInstanceOf
7024 * or `HCheckCast`.
7025 */
7026enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007027 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007028 kExactCheck, // Can do a single class compare.
7029 kClassHierarchyCheck, // Can just walk the super class chain.
7030 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7031 kInterfaceCheck, // No optimization yet when checking against an interface.
7032 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007033 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007034 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007035 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007036};
7037
Roland Levillain86503782016-02-11 19:07:30 +00007038std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7039
Vladimir Marko175e7862018-03-27 09:03:13 +00007040// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7041// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7042class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007043 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007044 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007045 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007046 HInstruction* object,
7047 HInstruction* target_class_or_null,
7048 TypeCheckKind check_kind,
7049 Handle<mirror::Class> klass,
7050 uint32_t dex_pc,
7051 ArenaAllocator* allocator,
7052 HIntConstant* bitstring_path_to_root,
7053 HIntConstant* bitstring_mask,
7054 SideEffects side_effects)
7055 : HVariableInputSizeInstruction(
7056 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007057 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007058 side_effects,
7059 dex_pc,
7060 allocator,
7061 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7062 kArenaAllocTypeCheckInputs),
7063 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007064 SetPackedField<TypeCheckKindField>(check_kind);
7065 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007066 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007067 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007068 SetRawInputAt(1, target_class_or_null);
7069 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7070 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7071 if (check_kind == TypeCheckKind::kBitstringCheck) {
7072 DCHECK(target_class_or_null->IsNullConstant());
7073 SetRawInputAt(2, bitstring_path_to_root);
7074 SetRawInputAt(3, bitstring_mask);
7075 } else {
7076 DCHECK(target_class_or_null->IsLoadClass());
7077 }
Vladimir Marko87584542017-12-12 17:47:52 +00007078 }
7079
7080 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007081 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007082 HInstruction* load_class = InputAt(1);
7083 DCHECK(load_class->IsLoadClass());
7084 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007085 }
7086
Vladimir Marko175e7862018-03-27 09:03:13 +00007087 uint32_t GetBitstringPathToRoot() const {
7088 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7089 HInstruction* path_to_root = InputAt(2);
7090 DCHECK(path_to_root->IsIntConstant());
7091 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7092 }
7093
7094 uint32_t GetBitstringMask() const {
7095 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7096 HInstruction* mask = InputAt(3);
7097 DCHECK(mask->IsIntConstant());
7098 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7099 }
7100
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007101 bool IsClonable() const override { return true; }
7102 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007104 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007105 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7106 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007107 }
7108
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007109 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7110 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7111 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7112 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7113
Vladimir Marko175e7862018-03-27 09:03:13 +00007114 ReferenceTypeInfo GetTargetClassRTI() {
7115 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7116 // Note: The is_exact flag from the return value should not be used.
7117 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7118 } else {
7119 return ReferenceTypeInfo::CreateInvalid();
7120 }
7121 }
7122
7123 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7124 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7125 DCHECK(klass_ != nullptr);
7126 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7127 }
7128
7129 Handle<mirror::Class> GetClass() const {
7130 return klass_;
7131 }
7132
7133 protected:
7134 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7135
7136 private:
7137 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7138 static constexpr size_t kFieldTypeCheckKindSize =
7139 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7140 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7141 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7142 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7143 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7144 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7145
7146 Handle<mirror::Class> klass_;
7147};
7148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007149class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007150 public:
7151 HInstanceOf(HInstruction* object,
7152 HInstruction* target_class_or_null,
7153 TypeCheckKind check_kind,
7154 Handle<mirror::Class> klass,
7155 uint32_t dex_pc,
7156 ArenaAllocator* allocator,
7157 HIntConstant* bitstring_path_to_root,
7158 HIntConstant* bitstring_mask)
7159 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007160 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007161 object,
7162 target_class_or_null,
7163 check_kind,
7164 klass,
7165 dex_pc,
7166 allocator,
7167 bitstring_path_to_root,
7168 bitstring_mask,
7169 SideEffectsForArchRuntimeCalls(check_kind)) {}
7170
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007171 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007173 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007174 return CanCallRuntime(GetTypeCheckKind());
7175 }
7176
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007177 static bool CanCallRuntime(TypeCheckKind check_kind) {
7178 // Mips currently does runtime calls for any other checks.
7179 return check_kind != TypeCheckKind::kExactCheck;
7180 }
7181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007182 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007183 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007184 }
7185
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007186 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007187
Artem Serovcced8ba2017-07-19 18:18:09 +01007188 protected:
7189 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007190};
7191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007192class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007193 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007194 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307195 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007196 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7197 SetPackedFlag<kFlagUpperCanBeNull>(true);
7198 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007199 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007200 SetRawInputAt(0, input);
7201 }
7202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007203 bool InstructionDataEquals(const HInstruction* other) const override;
7204 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007205
David Brazdilf5552582015-12-27 13:36:12 +00007206 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007207 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007208 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007209 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007210
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007211 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007212 DCHECK(GetUpperCanBeNull() || !can_be_null);
7213 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007214 }
7215
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007216 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007217
Calin Juravleb1498f62015-02-16 13:13:29 +00007218 DECLARE_INSTRUCTION(BoundType);
7219
Artem Serovcced8ba2017-07-19 18:18:09 +01007220 protected:
7221 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7222
Calin Juravleb1498f62015-02-16 13:13:29 +00007223 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007224 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7225 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007226 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007227 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7228 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7229 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7230
Calin Juravleb1498f62015-02-16 13:13:29 +00007231 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007232 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7233 // It is used to bound the type in cases like:
7234 // if (x instanceof ClassX) {
7235 // // uper_bound_ will be ClassX
7236 // }
David Brazdilf5552582015-12-27 13:36:12 +00007237 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007238};
7239
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007240class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007241 public:
7242 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007243 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007244 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007245 Handle<mirror::Class> klass,
7246 uint32_t dex_pc,
7247 ArenaAllocator* allocator,
7248 HIntConstant* bitstring_path_to_root,
7249 HIntConstant* bitstring_mask)
7250 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007251 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007252 object,
7253 target_class_or_null,
7254 check_kind,
7255 klass,
7256 dex_pc,
7257 allocator,
7258 bitstring_path_to_root,
7259 bitstring_mask,
7260 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007262 bool IsClonable() const override { return true; }
7263 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007264 // Instruction may throw a CheckCastError.
7265 return true;
7266 }
7267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007268 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007269
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007270 DECLARE_INSTRUCTION(CheckCast);
7271
Artem Serovcced8ba2017-07-19 18:18:09 +01007272 protected:
7273 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007274};
7275
Andreas Gampe26de38b2016-07-27 17:53:11 -07007276/**
7277 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7278 * @details We define the combined barrier types that are actually required
7279 * by the Java Memory Model, rather than using exactly the terminology from
7280 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7281 * primitives. Note that the JSR-133 cookbook generally does not deal with
7282 * store atomicity issues, and the recipes there are not always entirely sufficient.
7283 * The current recipe is as follows:
7284 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7285 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7286 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7287 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7288 * class has final fields.
7289 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7290 * store-to-memory instructions. Only generated together with non-temporal stores.
7291 */
7292enum MemBarrierKind {
7293 kAnyStore,
7294 kLoadAny,
7295 kStoreStore,
7296 kAnyAny,
7297 kNTStoreStore,
7298 kLastBarrierKind = kNTStoreStore
7299};
7300std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7301
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007302class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007303 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007304 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007305 : HExpression(kMemoryBarrier,
7306 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7307 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007308 SetPackedField<BarrierKindField>(barrier_kind);
7309 }
Calin Juravle27df7582015-04-17 19:12:31 +01007310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007311 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007312
Vladimir Markoa1de9182016-02-25 11:37:38 +00007313 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007314
7315 DECLARE_INSTRUCTION(MemoryBarrier);
7316
Artem Serovcced8ba2017-07-19 18:18:09 +01007317 protected:
7318 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7319
Calin Juravle27df7582015-04-17 19:12:31 +01007320 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007321 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7322 static constexpr size_t kFieldBarrierKindSize =
7323 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7324 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7325 kFieldBarrierKind + kFieldBarrierKindSize;
7326 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7327 "Too many packed fields.");
7328 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007329};
7330
Igor Murashkind01745e2017-04-05 16:40:31 -07007331// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7332// the specified object(s), with respect to any subsequent store that might "publish"
7333// (i.e. make visible) the specified object to another thread.
7334//
7335// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7336// for all final fields (that were set) at the end of the invoked constructor.
7337//
7338// The constructor fence models the freeze actions for the final fields of an object
7339// being constructed (semantically at the end of the constructor). Constructor fences
7340// have a per-object affinity; two separate objects being constructed get two separate
7341// constructor fences.
7342//
7343// (Note: that if calling a super-constructor or forwarding to another constructor,
7344// the freezes would happen at the end of *that* constructor being invoked).
7345//
7346// The memory model guarantees that when the object being constructed is "published" after
7347// constructor completion (i.e. escapes the current thread via a store), then any final field
7348// writes must be observable on other threads (once they observe that publication).
7349//
7350// Further, anything written before the freeze, and read by dereferencing through the final field,
7351// must also be visible (so final object field could itself have an object with non-final fields;
7352// yet the freeze must also extend to them).
7353//
7354// Constructor example:
7355//
7356// class HasFinal {
7357// final int field; Optimizing IR for <init>()V:
7358// HasFinal() {
7359// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7360// // freeze(this.field); HConstructorFence(this)
7361// } HReturn
7362// }
7363//
7364// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7365// already-initialized classes; in that case the allocation must act as a "default-initializer"
7366// of the object which effectively writes the class pointer "final field".
7367//
7368// For example, we can model default-initialiation as roughly the equivalent of the following:
7369//
7370// class Object {
7371// private final Class header;
7372// }
7373//
7374// Java code: Optimizing IR:
7375//
7376// T new_instance<T>() {
7377// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7378// obj.header = T.class; // header write is done by above call.
7379// // freeze(obj.header) HConstructorFence(obj)
7380// return (T)obj;
7381// }
7382//
7383// See also:
7384// * CompilerDriver::RequiresConstructorBarrier
7385// * QuasiAtomic::ThreadFenceForConstructor
7386//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007387class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007388 // A fence has variable inputs because the inputs can be removed
7389 // after prepare_for_register_allocation phase.
7390 // (TODO: In the future a fence could freeze multiple objects
7391 // after merging two fences together.)
7392 public:
7393 // `fence_object` is the reference that needs to be protected for correct publication.
7394 //
7395 // It makes sense in the following situations:
7396 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7397 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7398 //
7399 // After construction the `fence_object` becomes the 0th input.
7400 // This is not an input in a real sense, but just a convenient place to stash the information
7401 // about the associated object.
7402 HConstructorFence(HInstruction* fence_object,
7403 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007404 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007405 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7406 // constraints described in the class header. We claim that these SideEffects constraints
7407 // enforce a superset of the real constraints.
7408 //
7409 // The ordering described above is conservatively modeled with SideEffects as follows:
7410 //
7411 // * To prevent reordering of the publication stores:
7412 // ----> "Reads of objects" is the initial SideEffect.
7413 // * For every primitive final field store in the constructor:
7414 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7415 // * If there are any stores to reference final fields in the constructor:
7416 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7417 // that are reachable from `fence_object` also need to be prevented for reordering
7418 // (and we do not want to do alias analysis to figure out what those stores are).
7419 //
7420 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7421 // even more conservative approach than the one described above, and prevents all of the
7422 // above reordering without analyzing any of the instructions in the constructor.
7423 //
7424 // If in a later phase we discover that there are no writes to reference final fields,
7425 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307426 : HVariableInputSizeInstruction(kConstructorFence,
7427 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007428 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007429 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007430 /* number_of_inputs */ 1,
7431 kArenaAllocConstructorFenceInputs) {
7432 DCHECK(fence_object != nullptr);
7433 SetRawInputAt(0, fence_object);
7434 }
7435
7436 // The object associated with this constructor fence.
7437 //
7438 // (Note: This will be null after the prepare_for_register_allocation phase,
7439 // as all constructor fence inputs are removed there).
7440 HInstruction* GetFenceObject() const {
7441 return InputAt(0);
7442 }
7443
7444 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7445 // - Delete `fence_use` from `this`'s use list.
7446 // - Delete `this` from `fence_use`'s inputs list.
7447 // - If the `fence_use` is dead, remove it from the graph.
7448 //
7449 // A fence is considered dead once it no longer has any uses
7450 // and all of the inputs are dead.
7451 //
7452 // This must *not* be called during/after prepare_for_register_allocation,
7453 // because that removes all the inputs to the fences but the fence is actually
7454 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007455 //
7456 // Returns how many HConstructorFence instructions were removed from graph.
7457 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007458
Igor Murashkindd018df2017-08-09 10:38:31 -07007459 // Combine all inputs of `this` and `other` instruction and remove
7460 // `other` from the graph.
7461 //
7462 // Inputs are unique after the merge.
7463 //
7464 // Requirement: `this` must not be the same as `other.
7465 void Merge(HConstructorFence* other);
7466
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007467 // Check if this constructor fence is protecting
7468 // an HNewInstance or HNewArray that is also the immediate
7469 // predecessor of `this`.
7470 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007471 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7472 // to be one of the inputs of `this`.
7473 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007474 // Returns the associated HNewArray or HNewInstance,
7475 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007476 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007477
Igor Murashkind01745e2017-04-05 16:40:31 -07007478 DECLARE_INSTRUCTION(ConstructorFence);
7479
Artem Serovcced8ba2017-07-19 18:18:09 +01007480 protected:
7481 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007482};
7483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007484class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007485 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007486 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007487 kEnter,
7488 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007489 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007490 };
7491
7492 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007493 : HExpression(kMonitorOperation,
7494 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7495 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007496 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007497 SetRawInputAt(0, object);
7498 }
7499
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007500 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007501 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007502
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007503 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007504 // Verifier guarantees that monitor-exit cannot throw.
7505 // This is important because it allows the HGraphBuilder to remove
7506 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7507 return IsEnter();
7508 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007509
Vladimir Markoa1de9182016-02-25 11:37:38 +00007510 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7511 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007512
7513 DECLARE_INSTRUCTION(MonitorOperation);
7514
Artem Serovcced8ba2017-07-19 18:18:09 +01007515 protected:
7516 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7517
Calin Juravle52c48962014-12-16 17:02:57 +00007518 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007519 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7520 static constexpr size_t kFieldOperationKindSize =
7521 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7522 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7523 kFieldOperationKind + kFieldOperationKindSize;
7524 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7525 "Too many packed fields.");
7526 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007527};
7528
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007529class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007530 public:
7531 HSelect(HInstruction* condition,
7532 HInstruction* true_value,
7533 HInstruction* false_value,
7534 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307535 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007536 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7537
7538 // First input must be `true_value` or `false_value` to allow codegens to
7539 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7540 // that architectures which implement HSelect as a conditional move also
7541 // will not need to invert the condition.
7542 SetRawInputAt(0, false_value);
7543 SetRawInputAt(1, true_value);
7544 SetRawInputAt(2, condition);
7545 }
7546
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007547 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007548 HInstruction* GetFalseValue() const { return InputAt(0); }
7549 HInstruction* GetTrueValue() const { return InputAt(1); }
7550 HInstruction* GetCondition() const { return InputAt(2); }
7551
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007552 bool CanBeMoved() const override { return true; }
7553 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007554 return true;
7555 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007556
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007557 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007558 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7559 }
7560
7561 DECLARE_INSTRUCTION(Select);
7562
Artem Serovcced8ba2017-07-19 18:18:09 +01007563 protected:
7564 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007565};
7566
Vladimir Markof9f64412015-09-02 14:05:49 +01007567class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007568 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007569 MoveOperands(Location source,
7570 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007571 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007572 HInstruction* instruction)
7573 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007574
7575 Location GetSource() const { return source_; }
7576 Location GetDestination() const { return destination_; }
7577
7578 void SetSource(Location value) { source_ = value; }
7579 void SetDestination(Location value) { destination_ = value; }
7580
7581 // The parallel move resolver marks moves as "in-progress" by clearing the
7582 // destination (but not the source).
7583 Location MarkPending() {
7584 DCHECK(!IsPending());
7585 Location dest = destination_;
7586 destination_ = Location::NoLocation();
7587 return dest;
7588 }
7589
7590 void ClearPending(Location dest) {
7591 DCHECK(IsPending());
7592 destination_ = dest;
7593 }
7594
7595 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007596 DCHECK(source_.IsValid() || destination_.IsInvalid());
7597 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007598 }
7599
7600 // True if this blocks a move from the given location.
7601 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007602 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007603 }
7604
7605 // A move is redundant if it's been eliminated, if its source and
7606 // destination are the same, or if its destination is unneeded.
7607 bool IsRedundant() const {
7608 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7609 }
7610
7611 // We clear both operands to indicate move that's been eliminated.
7612 void Eliminate() {
7613 source_ = destination_ = Location::NoLocation();
7614 }
7615
7616 bool IsEliminated() const {
7617 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7618 return source_.IsInvalid();
7619 }
7620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007621 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007622
Nicolas Geoffray90218252015-04-15 11:56:51 +01007623 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007624 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007625 }
7626
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007627 HInstruction* GetInstruction() const { return instruction_; }
7628
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007629 private:
7630 Location source_;
7631 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007632 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007633 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007634 // The instruction this move is assocatied with. Null when this move is
7635 // for moving an input in the expected locations of user (including a phi user).
7636 // This is only used in debug mode, to ensure we do not connect interval siblings
7637 // in the same parallel move.
7638 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007639};
7640
Roland Levillainc9285912015-12-18 10:38:42 +00007641std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7642
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007643static constexpr size_t kDefaultNumberOfMoves = 4;
7644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007645class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007646 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007647 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007648 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007649 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007650 moves_.reserve(kDefaultNumberOfMoves);
7651 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007652
Nicolas Geoffray90218252015-04-15 11:56:51 +01007653 void AddMove(Location source,
7654 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007655 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007656 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007657 DCHECK(source.IsValid());
7658 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007659 if (kIsDebugBuild) {
7660 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007661 for (const MoveOperands& move : moves_) {
7662 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007663 // Special case the situation where the move is for the spill slot
7664 // of the instruction.
7665 if ((GetPrevious() == instruction)
7666 || ((GetPrevious() == nullptr)
7667 && instruction->IsPhi()
7668 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007669 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007670 << "Doing parallel moves for the same instruction.";
7671 } else {
7672 DCHECK(false) << "Doing parallel moves for the same instruction.";
7673 }
7674 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007675 }
7676 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007677 for (const MoveOperands& move : moves_) {
7678 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007679 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007680 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007681 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007682 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007683 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007684 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007685 }
7686
Vladimir Marko225b6462015-09-28 12:17:40 +01007687 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007688 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007689 }
7690
Vladimir Marko225b6462015-09-28 12:17:40 +01007691 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007692
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007693 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007694
Artem Serovcced8ba2017-07-19 18:18:09 +01007695 protected:
7696 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7697
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007698 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007699 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007700};
7701
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007702// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7703// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7704// never used across anything that can trigger GC.
7705// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7706// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007707class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007708 public:
7709 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307710 : HExpression(kIntermediateAddress,
7711 DataType::Type::kInt32,
7712 SideEffects::DependsOnGC(),
7713 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007714 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7715 DataType::Size(DataType::Type::kReference))
7716 << "kPrimInt and kPrimNot have different sizes.";
7717 SetRawInputAt(0, base_address);
7718 SetRawInputAt(1, offset);
7719 }
7720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007721 bool IsClonable() const override { return true; }
7722 bool CanBeMoved() const override { return true; }
7723 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007724 return true;
7725 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007726 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007727
7728 HInstruction* GetBaseAddress() const { return InputAt(0); }
7729 HInstruction* GetOffset() const { return InputAt(1); }
7730
7731 DECLARE_INSTRUCTION(IntermediateAddress);
7732
Artem Serovcced8ba2017-07-19 18:18:09 +01007733 protected:
7734 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007735};
7736
7737
Mark Mendell0616ae02015-04-17 12:49:27 -04007738} // namespace art
7739
Aart Bikf8f5a162017-02-06 15:35:29 -08007740#include "nodes_vector.h"
7741
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007742#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7743#include "nodes_shared.h"
7744#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007745#ifdef ART_ENABLE_CODEGEN_mips
7746#include "nodes_mips.h"
7747#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007748#ifdef ART_ENABLE_CODEGEN_x86
7749#include "nodes_x86.h"
7750#endif
7751
7752namespace art {
7753
Igor Murashkin6ef45672017-08-08 13:59:55 -07007754class OptimizingCompilerStats;
7755
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007756class HGraphVisitor : public ValueObject {
7757 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007758 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7759 : stats_(stats),
7760 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007761 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007763 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007764 virtual void VisitBasicBlock(HBasicBlock* block);
7765
Roland Levillain633021e2014-10-01 14:12:25 +01007766 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007767 void VisitInsertionOrder();
7768
Roland Levillain633021e2014-10-01 14:12:25 +01007769 // Visit the graph following dominator tree reverse post-order.
7770 void VisitReversePostOrder();
7771
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007772 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007773
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007774 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007775#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007776 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7777
7778 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7779
7780#undef DECLARE_VISIT_INSTRUCTION
7781
Igor Murashkin6ef45672017-08-08 13:59:55 -07007782 protected:
7783 OptimizingCompilerStats* stats_;
7784
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007785 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007786 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007787
7788 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7789};
7790
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007791class HGraphDelegateVisitor : public HGraphVisitor {
7792 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007793 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7794 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007795 virtual ~HGraphDelegateVisitor() {}
7796
7797 // Visit functions that delegate to to super class.
7798#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007799 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007800
7801 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7802
7803#undef DECLARE_VISIT_INSTRUCTION
7804
7805 private:
7806 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7807};
7808
Artem Serovcced8ba2017-07-19 18:18:09 +01007809// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7810// and remove the old instruction.
7811HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7812
7813// Create a clone for each clonable instructions/phis and replace the original with the clone.
7814//
7815// Used for testing individual instruction cloner.
7816class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7817 public:
7818 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007819 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007821 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007822 if (instruction->IsClonable()) {
7823 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007824 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007825 }
7826 }
7827
Artem Serov7f4aff62017-06-21 17:02:18 +01007828 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007829
7830 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007831 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007832
7833 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7834};
7835
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007836// Iterator over the blocks that art part of the loop. Includes blocks part
7837// of an inner loop. The order in which the blocks are iterated is on their
7838// block id.
7839class HBlocksInLoopIterator : public ValueObject {
7840 public:
7841 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7842 : blocks_in_loop_(info.GetBlocks()),
7843 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7844 index_(0) {
7845 if (!blocks_in_loop_.IsBitSet(index_)) {
7846 Advance();
7847 }
7848 }
7849
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007850 bool Done() const { return index_ == blocks_.size(); }
7851 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007852 void Advance() {
7853 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007854 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007855 if (blocks_in_loop_.IsBitSet(index_)) {
7856 break;
7857 }
7858 }
7859 }
7860
7861 private:
7862 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007863 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007864 size_t index_;
7865
7866 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7867};
7868
Mingyao Yang3584bce2015-05-19 16:01:59 -07007869// Iterator over the blocks that art part of the loop. Includes blocks part
7870// of an inner loop. The order in which the blocks are iterated is reverse
7871// post order.
7872class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7873 public:
7874 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7875 : blocks_in_loop_(info.GetBlocks()),
7876 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7877 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007878 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007879 Advance();
7880 }
7881 }
7882
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007883 bool Done() const { return index_ == blocks_.size(); }
7884 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007885 void Advance() {
7886 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007887 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7888 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007889 break;
7890 }
7891 }
7892 }
7893
7894 private:
7895 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007896 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007897 size_t index_;
7898
7899 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7900};
7901
Aart Bikf3e61ee2017-04-12 17:09:20 -07007902// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007903inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007904 if (constant->IsIntConstant()) {
7905 return constant->AsIntConstant()->GetValue();
7906 } else if (constant->IsLongConstant()) {
7907 return constant->AsLongConstant()->GetValue();
7908 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007909 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007910 return 0;
7911 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007912}
7913
Aart Bikf3e61ee2017-04-12 17:09:20 -07007914// Returns true iff instruction is an integral constant (and sets value on success).
7915inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7916 if (instruction->IsIntConstant()) {
7917 *value = instruction->AsIntConstant()->GetValue();
7918 return true;
7919 } else if (instruction->IsLongConstant()) {
7920 *value = instruction->AsLongConstant()->GetValue();
7921 return true;
7922 } else if (instruction->IsNullConstant()) {
7923 *value = 0;
7924 return true;
7925 }
7926 return false;
7927}
7928
Aart Bik0148de42017-09-05 09:25:01 -07007929// Returns true iff instruction is the given integral constant.
7930inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7931 int64_t val = 0;
7932 return IsInt64AndGet(instruction, &val) && val == value;
7933}
7934
7935// Returns true iff instruction is a zero bit pattern.
7936inline bool IsZeroBitPattern(HInstruction* instruction) {
7937 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7938}
7939
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007940// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007941#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007942 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7943 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7944#undef INSTRUCTION_TYPE_CHECK
7945
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007946// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007947#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7948 std::is_base_of<BaseType, H##type>::value,
7949#define INSTRUCTION_TYPE_CHECK(type, super) \
7950 inline bool HInstruction::Is##type() const { \
7951 DCHECK_LT(GetKind(), kLastInstructionKind); \
7952 using BaseType = H##type; \
7953 static constexpr bool results[] = { \
7954 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7955 }; \
7956 return results[static_cast<size_t>(GetKind())]; \
7957 }
7958
7959 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7960#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007961#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007962
7963#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007964 inline const H##type* HInstruction::As##type() const { \
7965 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7966 } \
7967 inline H##type* HInstruction::As##type() { \
7968 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7969 }
7970
Vladimir Markoa90dd512018-05-04 15:04:45 +01007971 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7972#undef INSTRUCTION_TYPE_CAST
7973
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007974
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007975// Create space in `blocks` for adding `number_of_new_blocks` entries
7976// starting at location `at`. Blocks after `at` are moved accordingly.
7977inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7978 size_t number_of_new_blocks,
7979 size_t after) {
7980 DCHECK_LT(after, blocks->size());
7981 size_t old_size = blocks->size();
7982 size_t new_size = old_size + number_of_new_blocks;
7983 blocks->resize(new_size);
7984 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7985}
7986
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007987/*
7988 * Hunt "under the hood" of array lengths (leading to array references),
7989 * null checks (also leading to array references), and new arrays
7990 * (leading to the actual length). This makes it more likely related
7991 * instructions become actually comparable.
7992 */
7993inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7994 while (instruction->IsArrayLength() ||
7995 instruction->IsNullCheck() ||
7996 instruction->IsNewArray()) {
7997 instruction = instruction->IsNewArray()
7998 ? instruction->AsNewArray()->GetLength()
7999 : instruction->InputAt(0);
8000 }
8001 return instruction;
8002}
8003
Artem Serov21c7e6f2017-07-27 16:04:42 +01008004void RemoveEnvironmentUses(HInstruction* instruction);
8005bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8006void ResetEnvironmentInputRecords(HInstruction* instruction);
8007
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008008} // namespace art
8009
8010#endif // ART_COMPILER_OPTIMIZING_NODES_H_