blob: 610852257b412fa92249eec6102058bfbfd14f73 [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,
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100132 kAnalysisFailIrreducibleLoopAndStringInit,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000133 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000134};
135
Andreas Gampe9186ced2016-12-12 14:28:21 -0800136template <typename T>
137static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
138 return static_cast<typename std::make_unsigned<T>::type>(x);
139}
140
Vladimir Markof9f64412015-09-02 14:05:49 +0100141class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100142 public:
143 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
144
145 void AddInstruction(HInstruction* instruction);
146 void RemoveInstruction(HInstruction* instruction);
147
David Brazdilc3d743f2015-04-22 13:40:50 +0100148 // Insert `instruction` before/after an existing instruction `cursor`.
149 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
150 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
151
Roland Levillain6b469232014-09-25 10:10:38 +0100152 // Return true if this list contains `instruction`.
153 bool Contains(HInstruction* instruction) const;
154
Roland Levillainccc07a92014-09-16 14:48:16 +0100155 // Return true if `instruction1` is found before `instruction2` in
156 // this instruction list and false otherwise. Abort if none
157 // of these instructions is found.
158 bool FoundBefore(const HInstruction* instruction1,
159 const HInstruction* instruction2) const;
160
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 bool IsEmpty() const { return first_instruction_ == nullptr; }
162 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
163
164 // Update the block of all instructions to be `block`.
165 void SetBlockOfInstructions(HBasicBlock* block) const;
166
167 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000168 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000169 void Add(const HInstructionList& instruction_list);
170
David Brazdil2d7352b2015-04-20 14:52:42 +0100171 // Return the number of instructions in the list. This is an expensive operation.
172 size_t CountSize() const;
173
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100174 private:
175 HInstruction* first_instruction_;
176 HInstruction* last_instruction_;
177
178 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000179 friend class HGraph;
180 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100181 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000182 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100183 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100184
185 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
186};
187
David Brazdil4833f5a2015-12-16 10:37:39 +0000188class ReferenceTypeInfo : ValueObject {
189 public:
190 typedef Handle<mirror::Class> TypeHandle;
191
Vladimir Markoa1de9182016-02-25 11:37:38 +0000192 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
193
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700194 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100195 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
196 }
197
Vladimir Markoa1de9182016-02-25 11:37:38 +0000198 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000199 return ReferenceTypeInfo(type_handle, is_exact);
200 }
201
202 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
203
Vladimir Markof39745e2016-01-26 12:16:55 +0000204 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000205 return handle.GetReference() != nullptr;
206 }
207
Vladimir Marko456307a2016-04-19 14:12:13 +0000208 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 return IsValidHandle(type_handle_);
210 }
211
212 bool IsExact() const { return is_exact_; }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsObjectClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return GetTypeHandle()->IsStringClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsInterface();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsArrayClass();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
247 }
248
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700249 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000250 DCHECK(IsValid());
251 if (!IsExact()) return false;
252 if (!IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
254 }
255
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700256 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000257 DCHECK(IsValid());
258 if (!IsExact()) return false;
259 if (!IsArrayClass()) return false;
260 if (!rti.IsArrayClass()) return false;
261 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
262 rti.GetTypeHandle()->GetComponentType());
263 }
264
265 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
266
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000268 DCHECK(IsValid());
269 DCHECK(rti.IsValid());
270 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
271 }
272
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700273 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000274 DCHECK(IsValid());
275 DCHECK(rti.IsValid());
276 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
277 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
278 }
279
280 // Returns true if the type information provide the same amount of details.
281 // Note that it does not mean that the instructions have the same actual type
282 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700283 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000284 if (!IsValid() && !rti.IsValid()) {
285 // Invalid types are equal.
286 return true;
287 }
288 if (!IsValid() || !rti.IsValid()) {
289 // One is valid, the other not.
290 return false;
291 }
292 return IsExact() == rti.IsExact()
293 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
294 }
295
296 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000297 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
298 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
299 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000300
301 // The class of the object.
302 TypeHandle type_handle_;
303 // Whether or not the type is exact or a superclass of the actual type.
304 // Whether or not we have any information about this type.
305 bool is_exact_;
306};
307
308std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
309
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000310// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100311class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000312 public:
Vladimir Markoca6fff82017-10-03 14:49:14 +0100313 HGraph(ArenaAllocator* allocator,
314 ArenaStack* arena_stack,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 const DexFile& dex_file,
316 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100318 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100319 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000320 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100321 int start_instruction_id = 0)
Vladimir Markoca6fff82017-10-03 14:49:14 +0100322 : allocator_(allocator),
323 arena_stack_(arena_stack),
324 blocks_(allocator->Adapter(kArenaAllocBlockList)),
325 reverse_post_order_(allocator->Adapter(kArenaAllocReversePostOrder)),
326 linear_order_(allocator->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700327 entry_block_(nullptr),
328 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100329 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100330 number_of_vregs_(0),
331 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000332 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400333 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000334 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700335 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800336 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000337 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000338 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100340 dex_file_(dex_file),
341 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100342 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100343 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800344 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000346 cached_null_constant_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100347 cached_int_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
348 cached_float_constants_(std::less<int32_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
349 cached_long_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
350 cached_double_constants_(std::less<int64_t>(), allocator->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000351 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100352 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000353 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700354 osr_(osr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100355 cha_single_implementation_list_(allocator->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 blocks_.reserve(kDefaultNumberOfBlocks);
357 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000358
David Brazdilbadd8262016-02-02 16:28:56 +0000359 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700360 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000361
Vladimir Markoca6fff82017-10-03 14:49:14 +0100362 ArenaAllocator* GetAllocator() const { return allocator_; }
363 ArenaStack* GetArenaStack() const { return arena_stack_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100364 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
365
David Brazdil69ba7b72015-06-23 18:27:30 +0100366 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000367 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100368
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000369 HBasicBlock* GetEntryBlock() const { return entry_block_; }
370 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100371 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000372
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000373 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
374 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000375
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100377
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100378 void ComputeDominanceInformation();
379 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000380 void ClearLoopInformation();
381 void FindBackEdges(ArenaBitVector* visited);
382 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100383 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100384 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000385
David Brazdil4833f5a2015-12-16 10:37:39 +0000386 // Analyze all natural loops in this graph. Returns a code specifying that it
387 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000388 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000389 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100390
David Brazdilffee3d32015-07-06 11:48:53 +0100391 // Iterate over blocks to compute try block membership. Needs reverse post
392 // order and loop information.
393 void ComputeTryBlockInformation();
394
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000396 // Returns the instruction to replace the invoke expression or null if the
397 // invoke is for a void method. Note that the caller is responsible for replacing
398 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000399 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000400
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000401 // Update the loop and try membership of `block`, which was spawned from `reference`.
402 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
403 // should be the new back edge.
404 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
405 HBasicBlock* reference,
406 bool replace_if_back_edge);
407
Mingyao Yang3584bce2015-05-19 16:01:59 -0700408 // Need to add a couple of blocks to test if the loop body is entered and
409 // put deoptimization instructions, etc.
410 void TransformLoopHeaderForBCE(HBasicBlock* header);
411
Aart Bikf8f5a162017-02-06 15:35:29 -0800412 // Adds a new loop directly after the loop with the given header and exit.
413 // Returns the new preheader.
414 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
415 HBasicBlock* body,
416 HBasicBlock* exit);
417
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000418 // Removes `block` from the graph. Assumes `block` has been disconnected from
419 // other blocks and has no instructions or phis.
420 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000421
David Brazdilfc6a86a2015-06-26 10:33:45 +0000422 // Splits the edge between `block` and `successor` while preserving the
423 // indices in the predecessor/successor lists. If there are multiple edges
424 // between the blocks, the lowest indices are used.
425 // Returns the new block which is empty and has the same dex pc as `successor`.
426 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
427
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100428 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100429 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Artem Serov09faaea2017-12-07 14:36:01 +0000430
431 // Transform a loop into a format with a single preheader.
432 //
433 // Each phi in the header should be split: original one in the header should only hold
434 // inputs reachable from the back edges and a single input from the preheader. The newly created
435 // phi in the preheader should collate the inputs from the original multiple incoming blocks.
436 //
437 // Loops in the graph typically have a single preheader, so this method is used to "repair" loops
438 // that no longer have this property.
439 void TransformLoopToSinglePreheaderFormat(HBasicBlock* header);
440
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100441 void SimplifyLoop(HBasicBlock* header);
442
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000443 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100444 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000445 return current_instruction_id_++;
446 }
447
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000448 int32_t GetCurrentInstructionId() const {
449 return current_instruction_id_;
450 }
451
452 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100453 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000454 current_instruction_id_ = id;
455 }
456
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100457 uint16_t GetMaximumNumberOfOutVRegs() const {
458 return maximum_number_of_out_vregs_;
459 }
460
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000461 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
462 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100463 }
464
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
466 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
467 }
468
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000469 void UpdateTemporariesVRegSlots(size_t slots) {
470 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100471 }
472
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000473 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100474 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000475 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100476 }
477
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100478 void SetNumberOfVRegs(uint16_t number_of_vregs) {
479 number_of_vregs_ = number_of_vregs;
480 }
481
482 uint16_t GetNumberOfVRegs() const {
483 return number_of_vregs_;
484 }
485
486 void SetNumberOfInVRegs(uint16_t value) {
487 number_of_in_vregs_ = value;
488 }
489
David Brazdildee58d62016-04-07 09:54:26 +0000490 uint16_t GetNumberOfInVRegs() const {
491 return number_of_in_vregs_;
492 }
493
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100494 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100495 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100496 return number_of_vregs_ - number_of_in_vregs_;
497 }
498
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100499 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100500 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100501 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100502
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100503 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
504 DCHECK(GetReversePostOrder()[0] == entry_block_);
505 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
506 }
507
508 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
509 return ReverseRange(GetReversePostOrder());
510 }
511
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100512 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100513 return linear_order_;
514 }
515
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100516 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
517 return ReverseRange(GetLinearOrder());
518 }
519
Mark Mendell1152c922015-04-24 17:06:35 -0400520 bool HasBoundsChecks() const {
521 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800522 }
523
Mark Mendell1152c922015-04-24 17:06:35 -0400524 void SetHasBoundsChecks(bool value) {
525 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800526 }
527
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000528 bool IsDebuggable() const { return debuggable_; }
529
David Brazdil8d5b8b22015-03-24 10:51:52 +0000530 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 // already, it is created and inserted into the graph. This method is only for
532 // integral types.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100533 HConstant* GetConstant(DataType::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000534
535 // TODO: This is problematic for the consistency of reference type propagation
536 // because it can be created anytime after the pass and thus it will be left
537 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600538 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000539
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600540 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
541 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000542 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600543 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
544 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000545 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600546 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
547 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000548 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600549 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
550 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000551 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000552
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100553 HCurrentMethod* GetCurrentMethod();
554
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100555 const DexFile& GetDexFile() const {
556 return dex_file_;
557 }
558
559 uint32_t GetMethodIdx() const {
560 return method_idx_;
561 }
562
Igor Murashkind01745e2017-04-05 16:40:31 -0700563 // Get the method name (without the signature), e.g. "<init>"
564 const char* GetMethodName() const;
565
566 // Get the pretty method name (class + name + optionally signature).
567 std::string PrettyMethod(bool with_signature = true) const;
568
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100569 InvokeType GetInvokeType() const {
570 return invoke_type_;
571 }
572
Mark Mendellc4701932015-04-10 13:18:51 -0400573 InstructionSet GetInstructionSet() const {
574 return instruction_set_;
575 }
576
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000577 bool IsCompilingOsr() const { return osr_; }
578
Mingyao Yang063fc772016-08-02 11:02:54 -0700579 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
580 return cha_single_implementation_list_;
581 }
582
583 void AddCHASingleImplementationDependency(ArtMethod* method) {
584 cha_single_implementation_list_.insert(method);
585 }
586
587 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800588 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700589 }
590
David Brazdil77a48ae2015-09-15 12:34:04 +0000591 bool HasTryCatch() const { return has_try_catch_; }
592 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100593
Aart Bikb13c65b2017-03-21 20:14:07 -0700594 bool HasSIMD() const { return has_simd_; }
595 void SetHasSIMD(bool value) { has_simd_ = value; }
596
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800597 bool HasLoops() const { return has_loops_; }
598 void SetHasLoops(bool value) { has_loops_ = value; }
599
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000600 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
601 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
602
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100603 ArtMethod* GetArtMethod() const { return art_method_; }
604 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
605
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100606 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500607 // The instruction has been inserted into the graph, either as a constant, or
608 // before cursor.
609 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
610
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000611 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
612
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800613 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
614 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
615 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
616
David Brazdil2d7352b2015-04-20 14:52:42 +0100617 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000618 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100619 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 template <class InstructionType, typename ValueType>
622 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600623 ArenaSafeMap<ValueType, InstructionType*>* cache,
624 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000625 // Try to find an existing constant of the given value.
626 InstructionType* constant = nullptr;
627 auto cached_constant = cache->find(value);
628 if (cached_constant != cache->end()) {
629 constant = cached_constant->second;
630 }
631
632 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100633 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000634 if (constant == nullptr || constant->GetBlock() == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +0100635 constant = new (allocator_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000636 cache->Overwrite(value, constant);
637 InsertConstant(constant);
638 }
639 return constant;
640 }
641
David Brazdil8d5b8b22015-03-24 10:51:52 +0000642 void InsertConstant(HConstant* instruction);
643
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000644 // Cache a float constant into the graph. This method should only be
645 // called by the SsaBuilder when creating "equivalent" instructions.
646 void CacheFloatConstant(HFloatConstant* constant);
647
648 // See CacheFloatConstant comment.
649 void CacheDoubleConstant(HDoubleConstant* constant);
650
Vladimir Markoca6fff82017-10-03 14:49:14 +0100651 ArenaAllocator* const allocator_;
652 ArenaStack* const arena_stack_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000653
654 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100655 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000656
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100657 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100658 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000659
Aart Bik281c6812016-08-26 11:31:48 -0700660 // List of blocks to perform a linear order tree traversal. Unlike the reverse
661 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100662 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100663
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000664 HBasicBlock* entry_block_;
665 HBasicBlock* exit_block_;
666
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100667 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100668 uint16_t maximum_number_of_out_vregs_;
669
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100670 // The number of virtual registers in this method. Contains the parameters.
671 uint16_t number_of_vregs_;
672
673 // The number of virtual registers used by parameters of this method.
674 uint16_t number_of_in_vregs_;
675
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000676 // Number of vreg size slots that the temporaries use (used in baseline compiler).
677 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100678
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800679 // Flag whether there are bounds checks in the graph. We can skip
680 // BCE if it's false. It's only best effort to keep it up to date in
681 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400682 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800683
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800684 // Flag whether there are try/catch blocks in the graph. We will skip
685 // try/catch-related passes if it's false. It's only best effort to keep
686 // it up to date in the presence of code elimination so there might be
687 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000688 bool has_try_catch_;
689
Aart Bikb13c65b2017-03-21 20:14:07 -0700690 // Flag whether SIMD instructions appear in the graph. If true, the
691 // code generators may have to be more careful spilling the wider
692 // contents of SIMD registers.
693 bool has_simd_;
694
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800695 // Flag whether there are any loops in the graph. We can skip loop
696 // optimization if it's false. It's only best effort to keep it up
697 // to date in the presence of code elimination so there might be false
698 // positives.
699 bool has_loops_;
700
701 // Flag whether there are any irreducible loops in the graph. It's only
702 // best effort to keep it up to date in the presence of code elimination
703 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000704 bool has_irreducible_loops_;
705
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000706 // Indicates whether the graph should be compiled in a way that
707 // ensures full debuggability. If false, we can apply more
708 // aggressive optimizations that may limit the level of debugging.
709 const bool debuggable_;
710
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000711 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000712 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000713
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100714 // The dex file from which the method is from.
715 const DexFile& dex_file_;
716
717 // The method index in the dex file.
718 const uint32_t method_idx_;
719
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100720 // If inlined, this encodes how the callee is being invoked.
721 const InvokeType invoke_type_;
722
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100723 // Whether the graph has been transformed to SSA form. Only used
724 // in debug mode to ensure we are not using properties only valid
725 // for non-SSA form (like the number of temporaries).
726 bool in_ssa_form_;
727
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800728 // Number of CHA guards in the graph. Used to short-circuit the
729 // CHA guard optimization pass when there is no CHA guard left.
730 uint32_t number_of_cha_guards_;
731
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732 const InstructionSet instruction_set_;
733
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000734 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000735 HNullConstant* cached_null_constant_;
736 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000737 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000738 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000739 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000740
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100741 HCurrentMethod* cached_current_method_;
742
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100743 // The ArtMethod this graph is for. Note that for AOT, it may be null,
744 // for example for methods whose declaring class could not be resolved
745 // (such as when the superclass could not be found).
746 ArtMethod* art_method_;
747
David Brazdil4833f5a2015-12-16 10:37:39 +0000748 // Keep the RTI of inexact Object to avoid having to pass stack handle
749 // collection pointer to passes which may create NullConstant.
750 ReferenceTypeInfo inexact_object_rti_;
751
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000752 // Whether we are compiling this graph for on stack replacement: this will
753 // make all loops seen as irreducible and emit special stack maps to mark
754 // compiled code entries which the interpreter can directly jump to.
755 const bool osr_;
756
Mingyao Yang063fc772016-08-02 11:02:54 -0700757 // List of methods that are assumed to have single implementation.
758 ArenaSet<ArtMethod*> cha_single_implementation_list_;
759
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000760 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100761 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000762 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000763 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000764 DISALLOW_COPY_AND_ASSIGN(HGraph);
765};
766
Vladimir Markof9f64412015-09-02 14:05:49 +0100767class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768 public:
769 HLoopInformation(HBasicBlock* header, HGraph* graph)
770 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000772 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100773 contains_irreducible_loop_(false),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100774 back_edges_(graph->GetAllocator()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100775 // Make bit vector growable, as the number of blocks may change.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100776 blocks_(graph->GetAllocator(),
777 graph->GetBlocks().size(),
778 true,
779 kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 back_edges_.reserve(kDefaultNumberOfBackEdges);
781 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000783 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100784 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000785
786 void Dump(std::ostream& os);
787
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100788 HBasicBlock* GetHeader() const {
789 return header_;
790 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000791
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000792 void SetHeader(HBasicBlock* block) {
793 header_ = block;
794 }
795
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100796 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
797 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
798 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
799
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000800 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100801 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000802 }
803
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100804 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100805 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100806 }
807
David Brazdil46e2a392015-03-16 17:31:52 +0000808 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100809 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810 }
811
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000812 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100813 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000814 }
815
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100817
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100818 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100819 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100820 }
821
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100822 // Returns the lifetime position of the back edge that has the
823 // greatest lifetime position.
824 size_t GetLifetimeEnd() const;
825
826 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100827 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100828 }
829
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000830 // Finds blocks that are part of this loop.
831 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100832
Artem Serov7f4aff62017-06-21 17:02:18 +0100833 // Updates blocks population of the loop and all of its outer' ones recursively after the
834 // population of the inner loop is updated.
835 void PopulateInnerLoopUpwards(HLoopInformation* inner_loop);
836
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100837 // Returns whether this loop information contains `block`.
838 // Note that this loop information *must* be populated before entering this function.
839 bool Contains(const HBasicBlock& block) const;
840
841 // Returns whether this loop information is an inner loop of `other`.
842 // Note that `other` *must* be populated before entering this function.
843 bool IsIn(const HLoopInformation& other) const;
844
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800845 // Returns true if instruction is not defined within this loop.
846 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700847
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 const ArenaBitVector& GetBlocks() const { return blocks_; }
849
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000850 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000851 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000852
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000853 void ClearAllBlocks() {
854 blocks_.ClearAllBits();
855 }
856
David Brazdil3f4a5222016-05-06 12:46:21 +0100857 bool HasBackEdgeNotDominatedByHeader() const;
858
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100859 bool IsPopulated() const {
860 return blocks_.GetHighestBitSet() != -1;
861 }
862
Anton Shaminf89381f2016-05-16 16:44:13 +0600863 bool DominatesAllBackEdges(HBasicBlock* block);
864
David Sehrc757dec2016-11-04 15:48:34 -0700865 bool HasExitEdge() const;
866
Artem Serov7f4aff62017-06-21 17:02:18 +0100867 // Resets back edge and blocks-in-loop data.
868 void ResetBasicBlockData() {
869 back_edges_.clear();
870 ClearAllBlocks();
871 }
872
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000873 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100874 // Internal recursive implementation of `Populate`.
875 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100876 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100877
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000878 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100879 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000880 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100881 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100882 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100883 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000884
885 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
886};
887
David Brazdilec16f792015-08-19 15:04:01 +0100888// Stores try/catch information for basic blocks.
889// Note that HGraph is constructed so that catch blocks cannot simultaneously
890// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100891class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100892 public:
893 // Try block information constructor.
894 explicit TryCatchInformation(const HTryBoundary& try_entry)
895 : try_entry_(&try_entry),
896 catch_dex_file_(nullptr),
897 catch_type_index_(DexFile::kDexNoIndex16) {
898 DCHECK(try_entry_ != nullptr);
899 }
900
901 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800902 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100903 : try_entry_(nullptr),
904 catch_dex_file_(&dex_file),
905 catch_type_index_(catch_type_index) {}
906
907 bool IsTryBlock() const { return try_entry_ != nullptr; }
908
909 const HTryBoundary& GetTryEntry() const {
910 DCHECK(IsTryBlock());
911 return *try_entry_;
912 }
913
914 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
915
916 bool IsCatchAllTypeIndex() const {
917 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800918 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100919 }
920
Andreas Gampea5b09a62016-11-17 15:21:22 -0800921 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100922 DCHECK(IsCatchBlock());
923 return catch_type_index_;
924 }
925
926 const DexFile& GetCatchDexFile() const {
927 DCHECK(IsCatchBlock());
928 return *catch_dex_file_;
929 }
930
931 private:
932 // One of possibly several TryBoundary instructions entering the block's try.
933 // Only set for try blocks.
934 const HTryBoundary* try_entry_;
935
936 // Exception type information. Only set for catch blocks.
937 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800938 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100939};
940
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100941static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100942static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944// A block in a method. Contains the list of instructions represented
945// as a double linked list. Each block knows its predecessors and
946// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100947
Vladimir Markof9f64412015-09-02 14:05:49 +0100948class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000949 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700950 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000951 : graph_(graph),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100952 predecessors_(graph->GetAllocator()->Adapter(kArenaAllocPredecessors)),
953 successors_(graph->GetAllocator()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000954 loop_information_(nullptr),
955 dominator_(nullptr),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100956 dominated_blocks_(graph->GetAllocator()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100957 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100958 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100959 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000960 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000961 try_catch_information_(nullptr) {
962 predecessors_.reserve(kDefaultNumberOfPredecessors);
963 successors_.reserve(kDefaultNumberOfSuccessors);
964 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
965 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000966
Vladimir Marko60584552015-09-03 13:35:12 +0000967 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969 }
970
Vladimir Marko60584552015-09-03 13:35:12 +0000971 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100972 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973 }
974
David Brazdild26a4112015-11-10 11:07:31 +0000975 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
976 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
977
Vladimir Marko60584552015-09-03 13:35:12 +0000978 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
979 return ContainsElement(successors_, block, start_from);
980 }
981
982 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100983 return dominated_blocks_;
984 }
985
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100986 bool IsEntryBlock() const {
987 return graph_->GetEntryBlock() == this;
988 }
989
990 bool IsExitBlock() const {
991 return graph_->GetExitBlock() == this;
992 }
993
David Brazdil46e2a392015-03-16 17:31:52 +0000994 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200995 bool IsSingleReturn() const;
Mingyao Yang46721ef2017-10-05 14:45:17 -0700996 bool IsSingleReturnOrReturnVoidAllowingPhis() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000997 bool IsSingleTryBoundary() const;
998
999 // Returns true if this block emits nothing but a jump.
1000 bool IsSingleJump() const {
1001 HLoopInformation* loop_info = GetLoopInformation();
1002 return (IsSingleGoto() || IsSingleTryBoundary())
1003 // Back edges generate a suspend check.
1004 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
1005 }
David Brazdil46e2a392015-03-16 17:31:52 +00001006
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001007 void AddBackEdge(HBasicBlock* back_edge) {
1008 if (loop_information_ == nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001009 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001010 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001011 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001012 loop_information_->AddBackEdge(back_edge);
1013 }
1014
Artem Serov7f4aff62017-06-21 17:02:18 +01001015 // Registers a back edge; if the block was not a loop header before the call associates a newly
1016 // created loop info with it.
1017 //
1018 // Used in SuperblockCloner to preserve LoopInformation object instead of reseting loop
1019 // info for all blocks during back edges recalculation.
1020 void AddBackEdgeWhileUpdating(HBasicBlock* back_edge) {
1021 if (loop_information_ == nullptr || loop_information_->GetHeader() != this) {
1022 loop_information_ = new (graph_->GetAllocator()) HLoopInformation(this, graph_);
1023 }
1024 loop_information_->AddBackEdge(back_edge);
1025 }
1026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001027 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001028 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001029
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001030 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001031 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001032 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001033
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001034 HBasicBlock* GetDominator() const { return dominator_; }
1035 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +00001036 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
1037
1038 void RemoveDominatedBlock(HBasicBlock* block) {
1039 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +01001040 }
Vladimir Marko60584552015-09-03 13:35:12 +00001041
1042 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
1043 ReplaceElement(dominated_blocks_, existing, new_block);
1044 }
1045
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001046 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001047
1048 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001049 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001050 }
1051
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001052 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1053 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001054 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001055 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001056 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1057 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001058
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001059 HInstruction* GetFirstInstructionDisregardMoves() const;
1060
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001061 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001062 successors_.push_back(block);
1063 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001064 }
1065
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001066 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1067 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001069 new_block->predecessors_.push_back(this);
1070 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001071 }
1072
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001073 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1074 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001075 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001076 new_block->successors_.push_back(this);
1077 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001078 }
1079
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001080 // Insert `this` between `predecessor` and `successor. This method
1081 // preserves the indicies, and will update the first edge found between
1082 // `predecessor` and `successor`.
1083 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1084 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001085 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001086 successor->predecessors_[predecessor_index] = this;
1087 predecessor->successors_[successor_index] = this;
1088 successors_.push_back(successor);
1089 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001090 }
1091
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001092 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001093 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001094 }
1095
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001096 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001097 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 }
1099
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001100 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001101 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001102 }
1103
1104 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001105 predecessors_.push_back(block);
1106 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001107 }
1108
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001109 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001110 DCHECK_EQ(predecessors_.size(), 2u);
1111 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001112 }
1113
David Brazdil769c9e52015-04-27 13:54:09 +01001114 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001115 DCHECK_EQ(successors_.size(), 2u);
1116 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001117 }
1118
David Brazdilfc6a86a2015-06-26 10:33:45 +00001119 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001120 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001121 }
1122
David Brazdilfc6a86a2015-06-26 10:33:45 +00001123 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001124 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001125 }
1126
David Brazdilfc6a86a2015-06-26 10:33:45 +00001127 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001128 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001129 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001130 }
1131
1132 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001133 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001134 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001135 }
1136
1137 // Returns whether the first occurrence of `predecessor` in the list of
1138 // predecessors is at index `idx`.
1139 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001140 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001141 return GetPredecessorIndexOf(predecessor) == idx;
1142 }
1143
David Brazdild7558da2015-09-22 13:04:14 +01001144 // Create a new block between this block and its predecessors. The new block
1145 // is added to the graph, all predecessor edges are relinked to it and an edge
1146 // is created to `this`. Returns the new empty block. Reverse post order or
1147 // loop and try/catch information are not updated.
1148 HBasicBlock* CreateImmediateDominator();
1149
David Brazdilfc6a86a2015-06-26 10:33:45 +00001150 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001151 // created, latter block. Note that this method will add the block to the
1152 // graph, create a Goto at the end of the former block and will create an edge
1153 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001154 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001155 HBasicBlock* SplitBefore(HInstruction* cursor);
1156
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001157 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001158 // created block. Note that this method just updates raw block information,
1159 // like predecessors, successors, dominators, and instruction list. It does not
1160 // update the graph, reverse post order, loop information, nor make sure the
1161 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001162 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1163
1164 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1165 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001166
1167 // Merge `other` at the end of `this`. Successors and dominated blocks of
1168 // `other` are changed to be successors and dominated blocks of `this`. Note
1169 // that this method does not update the graph, reverse post order, loop
1170 // information, nor make sure the blocks are consistent (for example ending
1171 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001172 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173
1174 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1175 // of `this` are moved to `other`.
1176 // Note that this method does not update the graph, reverse post order, loop
1177 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001178 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001179 void ReplaceWith(HBasicBlock* other);
1180
Aart Bik6b69e0a2017-01-11 10:20:43 -08001181 // Merges the instructions of `other` at the end of `this`.
1182 void MergeInstructionsWith(HBasicBlock* other);
1183
David Brazdil2d7352b2015-04-20 14:52:42 +01001184 // Merge `other` at the end of `this`. This method updates loops, reverse post
1185 // order, links to predecessors, successors, dominators and deletes the block
1186 // from the graph. The two blocks must be successive, i.e. `this` the only
1187 // predecessor of `other` and vice versa.
1188 void MergeWith(HBasicBlock* other);
1189
1190 // Disconnects `this` from all its predecessors, successors and dominator,
1191 // removes it from all loops it is included in and eventually from the graph.
1192 // The block must not dominate any other block. Predecessors and successors
1193 // are safely updated.
1194 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001195
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001196 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001197 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001198 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001199 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Artem Serovcced8ba2017-07-19 18:18:09 +01001200 // Replace phi `initial` with `replacement` within this block.
1201 void ReplaceAndRemovePhiWith(HPhi* initial, HPhi* replacement);
Roland Levillainccc07a92014-09-16 14:48:16 +01001202 // Replace instruction `initial` with `replacement` within this block.
1203 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1204 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001205 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001206 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001207 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1208 // instruction list. With 'ensure_safety' set to true, it verifies that the
1209 // instruction is not in use and removes it from the use lists of its inputs.
1210 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1211 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001212 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001213
1214 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001215 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001216 }
1217
Roland Levillain6b879dd2014-09-22 17:13:44 +01001218 bool IsLoopPreHeaderFirstPredecessor() const {
1219 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001220 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001221 }
1222
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001223 bool IsFirstPredecessorBackEdge() const {
1224 DCHECK(IsLoopHeader());
1225 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1226 }
1227
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001228 HLoopInformation* GetLoopInformation() const {
1229 return loop_information_;
1230 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001231
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001232 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001234 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001235 void SetInLoop(HLoopInformation* info) {
1236 if (IsLoopHeader()) {
1237 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001238 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001239 loop_information_ = info;
1240 } else if (loop_information_->Contains(*info->GetHeader())) {
1241 // Block is currently part of an outer loop. Make it part of this inner loop.
1242 // Note that a non loop header having a loop information means this loop information
1243 // has already been populated
1244 loop_information_ = info;
1245 } else {
1246 // Block is part of an inner loop. Do not update the loop information.
1247 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1248 // at this point, because this method is being called while populating `info`.
1249 }
1250 }
1251
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001252 // Raw update of the loop information.
1253 void SetLoopInformation(HLoopInformation* info) {
1254 loop_information_ = info;
1255 }
1256
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001257 bool IsInLoop() const { return loop_information_ != nullptr; }
1258
David Brazdilec16f792015-08-19 15:04:01 +01001259 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1260
1261 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1262 try_catch_information_ = try_catch_information;
1263 }
1264
1265 bool IsTryBlock() const {
1266 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1267 }
1268
1269 bool IsCatchBlock() const {
1270 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1271 }
David Brazdilffee3d32015-07-06 11:48:53 +01001272
1273 // Returns the try entry that this block's successors should have. They will
1274 // be in the same try, unless the block ends in a try boundary. In that case,
1275 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001276 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001277
Aart Bik75ff2c92018-04-21 01:28:11 +00001278 bool HasThrowingInstructions() const;
1279
David Brazdila4b8c212015-05-07 09:59:30 +01001280 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001281 bool Dominates(HBasicBlock* block) const;
1282
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001283 size_t GetLifetimeStart() const { return lifetime_start_; }
1284 size_t GetLifetimeEnd() const { return lifetime_end_; }
1285
1286 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1287 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1288
David Brazdil8d5b8b22015-03-24 10:51:52 +00001289 bool EndsWithControlFlowInstruction() const;
Aart Bik4dc09e72018-05-11 14:40:31 -07001290 bool EndsWithReturn() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001291 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001292 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001293 bool HasSinglePhi() const;
1294
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001295 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001296 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001297 ArenaVector<HBasicBlock*> predecessors_;
1298 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001299 HInstructionList instructions_;
1300 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001301 HLoopInformation* loop_information_;
1302 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001303 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001304 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001305 // The dex program counter of the first instruction of this block.
1306 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001307 size_t lifetime_start_;
1308 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001309 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001310
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311 friend class HGraph;
1312 friend class HInstruction;
1313
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001314 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1315};
1316
David Brazdilb2bd1c52015-03-25 11:17:37 +00001317// Iterates over the LoopInformation of all loops which contain 'block'
1318// from the innermost to the outermost.
1319class HLoopInformationOutwardIterator : public ValueObject {
1320 public:
1321 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1322 : current_(block.GetLoopInformation()) {}
1323
1324 bool Done() const { return current_ == nullptr; }
1325
1326 void Advance() {
1327 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001328 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001329 }
1330
1331 HLoopInformation* Current() const {
1332 DCHECK(!Done());
1333 return current_;
1334 }
1335
1336 private:
1337 HLoopInformation* current_;
1338
1339 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1340};
1341
Alexandre Ramesef20f712015-06-09 10:29:30 +01001342#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001343 M(Above, Condition) \
1344 M(AboveOrEqual, Condition) \
Aart Bik3dad3412018-02-28 12:01:46 -08001345 M(Abs, UnaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001347 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(ArrayGet, Instruction) \
1349 M(ArrayLength, Instruction) \
1350 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001351 M(Below, Condition) \
1352 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001353 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001355 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001356 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001357 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001358 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001359 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001360 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001361 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001362 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001363 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001364 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001365 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001366 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001367 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001369 M(Exit, Instruction) \
1370 M(FloatConstant, Constant) \
1371 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001372 M(GreaterThan, Condition) \
1373 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001374 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001375 M(InstanceFieldGet, Instruction) \
1376 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001377 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001378 M(IntConstant, Constant) \
xueliang.zhonge0eb4832017-10-30 13:43:14 +00001379 M(IntermediateAddress, Instruction) \
Calin Juravle175dc732015-08-25 15:42:32 +01001380 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001381 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001382 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001383 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001384 M(InvokePolymorphic, Invoke) \
Orion Hodson4c8e12e2018-05-18 08:33:20 +01001385 M(InvokeCustom, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001386 M(LessThan, Condition) \
1387 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001388 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001389 M(LoadException, Instruction) \
Orion Hodsondbaa5c72018-05-10 08:22:46 +01001390 M(LoadMethodHandle, Instruction) \
Orion Hodson18259d72018-04-12 11:18:23 +01001391 M(LoadMethodType, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001392 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(LongConstant, Constant) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001394 M(Max, Instruction) \
Calin Juravle27df7582015-04-17 19:12:31 +01001395 M(MemoryBarrier, Instruction) \
Aart Bik1f8d51b2018-02-15 10:42:37 -08001396 M(Min, BinaryOperation) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001397 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001398 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001399 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001400 M(Neg, UnaryOperation) \
1401 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001403 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001404 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001405 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001406 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001407 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001408 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001409 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001410 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001412 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001413 M(Return, Instruction) \
1414 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001415 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001416 M(Shl, BinaryOperation) \
1417 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001418 M(StaticFieldGet, Instruction) \
1419 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001420 M(UnresolvedInstanceFieldGet, Instruction) \
1421 M(UnresolvedInstanceFieldSet, Instruction) \
1422 M(UnresolvedStaticFieldGet, Instruction) \
1423 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001424 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001425 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001426 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001427 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001428 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001429 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001430 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001431 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001432 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001433 M(VecExtractScalar, VecUnaryOperation) \
1434 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001435 M(VecCnv, VecUnaryOperation) \
1436 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001437 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001438 M(VecNot, VecUnaryOperation) \
1439 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001440 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001441 M(VecSub, VecBinaryOperation) \
1442 M(VecMul, VecBinaryOperation) \
1443 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001444 M(VecMin, VecBinaryOperation) \
1445 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001446 M(VecAnd, VecBinaryOperation) \
1447 M(VecAndNot, VecBinaryOperation) \
1448 M(VecOr, VecBinaryOperation) \
1449 M(VecXor, VecBinaryOperation) \
Aart Bik29aa0822018-03-08 11:28:00 -08001450 M(VecSaturationAdd, VecBinaryOperation) \
1451 M(VecSaturationSub, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001452 M(VecShl, VecBinaryOperation) \
1453 M(VecShr, VecBinaryOperation) \
1454 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001455 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001456 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikdbbac8f2017-09-01 13:06:08 -07001457 M(VecSADAccumulate, VecOperation) \
Artem Serovaaac0e32018-08-07 00:52:22 +01001458 M(VecDotProd, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001459 M(VecLoad, VecMemoryOperation) \
1460 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001461
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001462/*
1463 * Instructions, shared across several (not all) architectures.
1464 */
1465#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1466#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1467#else
1468#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001469 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001470 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001471 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001472 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001473#endif
1474
Alexandre Ramesef20f712015-06-09 10:29:30 +01001475#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1476
1477#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1478
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001479#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001480#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001481#else
1482#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1483 M(MipsComputeBaseMethodAddress, Instruction) \
Lena Djokica2901602017-09-21 13:50:52 +02001484 M(MipsPackedSwitch, Instruction) \
1485 M(IntermediateArrayAddressIndex, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001486#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001487
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001488#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1489
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001490#ifndef ART_ENABLE_CODEGEN_x86
1491#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1492#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001493#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1494 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001495 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001496 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001497 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001498#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001499
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301500#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
1501#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M) \
1502 M(X86AndNot, Instruction) \
1503 M(X86MaskOrResetLeastSetBit, Instruction)
1504#else
1505#define FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
1506#endif
1507
Alexandre Ramesef20f712015-06-09 10:29:30 +01001508#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1509
1510#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1511 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001512 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001513 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1514 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001515 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001516 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001517 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05301518 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M) \
1519 FOR_EACH_CONCRETE_INSTRUCTION_X86_COMMON(M)
Alexandre Ramesef20f712015-06-09 10:29:30 +01001520
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001521#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1522 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001523 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001524 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001525 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001526 M(Invoke, Instruction) \
1527 M(VecOperation, Instruction) \
1528 M(VecUnaryOperation, VecOperation) \
1529 M(VecBinaryOperation, VecOperation) \
1530 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001531
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001532#define FOR_EACH_INSTRUCTION(M) \
1533 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1534 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1535
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001536#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001537FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1538#undef FORWARD_DECLARATION
1539
Vladimir Marko372f10e2016-05-17 16:30:10 +01001540#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001541 private: \
1542 H##type& operator=(const H##type&) = delete; \
1543 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001544 const char* DebugName() const override { return #type; } \
1545 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001546 DCHECK(IsClonable()); \
1547 return new (arena) H##type(*this->As##type()); \
1548 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001549 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001550
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001551#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001552 private: \
1553 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001554 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001555
Artem Serovcced8ba2017-07-19 18:18:09 +01001556#define DEFAULT_COPY_CONSTRUCTOR(type) \
1557 explicit H##type(const H##type& other) = default;
1558
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001559template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001560class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1561 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001563 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001564 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001565 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001566 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001567 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001568 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001569
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001570 private:
David Brazdiled596192015-01-23 10:39:45 +00001571 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001572 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001573
1574 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001575 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001576
Vladimir Marko46817b82016-03-29 12:21:58 +01001577 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001578
1579 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1580};
1581
David Brazdiled596192015-01-23 10:39:45 +00001582template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001583using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001584
David Brazdil1abb4192015-02-17 18:33:36 +00001585// This class is used by HEnvironment and HInstruction classes to record the
1586// instructions they use and pointers to the corresponding HUseListNodes kept
1587// by the used instructions.
1588template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001589class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001590 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001591 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1592 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001593
Vladimir Marko46817b82016-03-29 12:21:58 +01001594 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1595 : HUserRecord(old_record.instruction_, before_use_node) {}
1596 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1597 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001598 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001599 }
1600
1601 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001602 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1603 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001604
1605 private:
1606 // Instruction used by the user.
1607 HInstruction* instruction_;
1608
Vladimir Marko46817b82016-03-29 12:21:58 +01001609 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1610 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001611};
1612
Vladimir Markoe9004912016-06-16 16:50:52 +01001613// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1614// This is used for HInstruction::GetInputs() to return a container wrapper providing
1615// HInstruction* values even though the underlying container has HUserRecord<>s.
1616struct HInputExtractor {
1617 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1618 return record.GetInstruction();
1619 }
1620 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1621 return record.GetInstruction();
1622 }
1623};
1624
1625using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1626using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1627
Aart Bik854a02b2015-07-14 16:07:00 -07001628/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001630 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 * For write/read dependences on fields/arrays, the dependence analysis uses
1632 * type disambiguation (e.g. a float field write cannot modify the value of an
1633 * integer field read) and the access type (e.g. a reference array write cannot
1634 * modify the value of a reference field read [although it may modify the
1635 * reference fetch prior to reading the field, which is represented by its own
1636 * write/read dependence]). The analysis makes conservative points-to
1637 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1638 * the same, and any reference read depends on any reference read without
1639 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001640 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001641 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1642 * alive across the point where garbage collection might happen.
1643 *
1644 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1645 *
1646 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1647 * those instructions from the compiler perspective (between this instruction and the next one
1648 * in the IR).
1649 *
1650 * Note: Instructions which can cause GC only on a fatal slow path do not need
1651 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1652 * one. However the execution may return to compiled code if there is a catch block in the
1653 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1654 * set.
1655 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 * The internal representation uses 38-bit and is described in the table below.
1657 * The first line indicates the side effect, and for field/array accesses the
1658 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001660 * The two numbered lines below indicate the bit position in the bitfield (read
1661 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001662 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1664 * +-------------+---------+---------+--------------+---------+---------+
1665 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1666 * | 3 |333333322|222222221| 1 |111111110|000000000|
1667 * | 7 |654321098|765432109| 8 |765432109|876543210|
1668 *
1669 * Note that, to ease the implementation, 'changes' bits are least significant
1670 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001671 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001672class SideEffects : public ValueObject {
1673 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001674 SideEffects() : flags_(0) {}
1675
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001676 static SideEffects None() {
1677 return SideEffects(0);
1678 }
1679
1680 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 return SideEffects(kAllChangeBits | kAllDependOnBits);
1682 }
1683
1684 static SideEffects AllChanges() {
1685 return SideEffects(kAllChangeBits);
1686 }
1687
1688 static SideEffects AllDependencies() {
1689 return SideEffects(kAllDependOnBits);
1690 }
1691
1692 static SideEffects AllExceptGCDependency() {
1693 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1694 }
1695
1696 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001697 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001698 }
1699
Aart Bik34c3ba92015-07-20 14:08:59 -07001700 static SideEffects AllWrites() {
1701 return SideEffects(kAllWrites);
1702 }
1703
1704 static SideEffects AllReads() {
1705 return SideEffects(kAllReads);
1706 }
1707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001708 static SideEffects FieldWriteOfType(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, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001712 }
1713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001714 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001715 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001716 }
1717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001718 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001719 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001721 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001722 }
1723
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001724 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001725 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001726 }
1727
Artem Serovd1aa7d02018-06-22 11:35:46 +01001728 // Returns whether GC might happen across this instruction from the compiler perspective so
1729 // the next instruction in the IR would see that.
1730 //
1731 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001732 static SideEffects CanTriggerGC() {
1733 return SideEffects(1ULL << kCanTriggerGCBit);
1734 }
1735
Artem Serovd1aa7d02018-06-22 11:35:46 +01001736 // Returns whether the instruction must not be alive across a GC point.
1737 //
1738 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001739 static SideEffects DependsOnGC() {
1740 return SideEffects(1ULL << kDependsOnGCBit);
1741 }
1742
Aart Bik854a02b2015-07-14 16:07:00 -07001743 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001744 SideEffects Union(SideEffects other) const {
1745 return SideEffects(flags_ | other.flags_);
1746 }
1747
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001748 SideEffects Exclusion(SideEffects other) const {
1749 return SideEffects(flags_ & ~other.flags_);
1750 }
1751
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001752 void Add(SideEffects other) {
1753 flags_ |= other.flags_;
1754 }
1755
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001756 bool Includes(SideEffects other) const {
1757 return (other.flags_ & flags_) == other.flags_;
1758 }
1759
1760 bool HasSideEffects() const {
1761 return (flags_ & kAllChangeBits);
1762 }
1763
1764 bool HasDependencies() const {
1765 return (flags_ & kAllDependOnBits);
1766 }
1767
1768 // Returns true if there are no side effects or dependencies.
1769 bool DoesNothing() const {
1770 return flags_ == 0;
1771 }
1772
Aart Bik854a02b2015-07-14 16:07:00 -07001773 // Returns true if something is written.
1774 bool DoesAnyWrite() const {
1775 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001776 }
1777
Aart Bik854a02b2015-07-14 16:07:00 -07001778 // Returns true if something is read.
1779 bool DoesAnyRead() const {
1780 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001781 }
1782
Aart Bik854a02b2015-07-14 16:07:00 -07001783 // Returns true if potentially everything is written and read
1784 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001785 bool DoesAllReadWrite() const {
1786 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1787 }
1788
Aart Bik854a02b2015-07-14 16:07:00 -07001789 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001790 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001791 }
1792
Roland Levillain0d5a2812015-11-13 10:07:31 +00001793 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001794 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001795 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1796 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001797 }
1798
1799 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001800 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001801 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001802 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001803 for (int s = kLastBit; s >= 0; s--) {
1804 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1805 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1806 // This is a bit for the GC side effect.
1807 if (current_bit_is_set) {
1808 flags += "GC";
1809 }
Aart Bik854a02b2015-07-14 16:07:00 -07001810 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001811 } else {
1812 // This is a bit for the array/field analysis.
1813 // The underscore character stands for the 'can trigger GC' bit.
1814 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1815 if (current_bit_is_set) {
1816 flags += kDebug[s];
1817 }
1818 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1819 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1820 flags += "|";
1821 }
1822 }
Aart Bik854a02b2015-07-14 16:07:00 -07001823 }
1824 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001825 }
1826
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001827 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001828
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001829 private:
1830 static constexpr int kFieldArrayAnalysisBits = 9;
1831
1832 static constexpr int kFieldWriteOffset = 0;
1833 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1834 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1835 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1836
1837 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1838
1839 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1840 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1841 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1842 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1843
1844 static constexpr int kLastBit = kDependsOnGCBit;
1845 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1846
1847 // Aliases.
1848
1849 static_assert(kChangeBits == kDependOnBits,
1850 "the 'change' bits should match the 'depend on' bits.");
1851
1852 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1853 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1854 static constexpr uint64_t kAllWrites =
1855 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1856 static constexpr uint64_t kAllReads =
1857 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001858
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001859 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001861 int shift;
1862 switch (type) {
1863 case DataType::Type::kReference: shift = 0; break;
1864 case DataType::Type::kBool: shift = 1; break;
1865 case DataType::Type::kInt8: shift = 2; break;
1866 case DataType::Type::kUint16: shift = 3; break;
1867 case DataType::Type::kInt16: shift = 4; break;
1868 case DataType::Type::kInt32: shift = 5; break;
1869 case DataType::Type::kInt64: shift = 6; break;
1870 case DataType::Type::kFloat32: shift = 7; break;
1871 case DataType::Type::kFloat64: shift = 8; break;
1872 default:
1873 LOG(FATAL) << "Unexpected data type " << type;
1874 UNREACHABLE();
1875 }
Aart Bik854a02b2015-07-14 16:07:00 -07001876 DCHECK_LE(kFieldWriteOffset, shift);
1877 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001878 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001879 }
1880
1881 // Private constructor on direct flags value.
1882 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1883
1884 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001885};
1886
David Brazdiled596192015-01-23 10:39:45 +00001887// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001888class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001889 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001890 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001891 size_t number_of_vregs,
1892 ArtMethod* method,
1893 uint32_t dex_pc,
1894 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001895 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1896 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001897 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001898 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001899 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001900 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001901 }
1902
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001903 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1904 const HEnvironment& to_copy,
1905 HInstruction* holder)
1906 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001907 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001908 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001909 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001910 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001911
Vladimir Markoec32f642017-06-02 10:51:55 +01001912 void AllocateLocations() {
1913 DCHECK(locations_.empty());
1914 locations_.resize(vregs_.size());
1915 }
1916
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001917 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001918 if (parent_ != nullptr) {
1919 parent_->SetAndCopyParentChain(allocator, parent);
1920 } else {
1921 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1922 parent_->CopyFrom(parent);
1923 if (parent->GetParent() != nullptr) {
1924 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1925 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001926 }
David Brazdiled596192015-01-23 10:39:45 +00001927 }
1928
Vladimir Marko69d310e2017-10-09 14:12:23 +01001929 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001930 void CopyFrom(HEnvironment* environment);
1931
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001932 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1933 // input to the loop phi instead. This is for inserting instructions that
1934 // require an environment (like HDeoptimization) in the loop pre-header.
1935 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001936
1937 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001938 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001939 }
1940
1941 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001942 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001943 }
1944
David Brazdil1abb4192015-02-17 18:33:36 +00001945 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001946
Artem Serovca210e32017-12-15 13:43:20 +00001947 // Replaces the input at the position 'index' with the replacement; the replacement and old
1948 // input instructions' env_uses_ lists are adjusted. The function works similar to
1949 // HInstruction::ReplaceInput.
1950 void ReplaceInput(HInstruction* replacement, size_t index);
1951
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001952 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001953
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001954 HEnvironment* GetParent() const { return parent_; }
1955
1956 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001957 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001958 }
1959
1960 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001961 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001962 }
1963
1964 uint32_t GetDexPc() const {
1965 return dex_pc_;
1966 }
1967
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001968 ArtMethod* GetMethod() const {
1969 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001970 }
1971
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001972 HInstruction* GetHolder() const {
1973 return holder_;
1974 }
1975
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001976
1977 bool IsFromInlinedInvoke() const {
1978 return GetParent() != nullptr;
1979 }
1980
David Brazdiled596192015-01-23 10:39:45 +00001981 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001982 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1983 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001984 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001985 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001986 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001987
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001988 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001989 HInstruction* const holder_;
1990
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001991 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001992
1993 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1994};
1995
Vladimir Markof9f64412015-09-02 14:05:49 +01001996class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001997 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301998#define DECLARE_KIND(type, super) k##type,
1999 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01002000 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302001 kLastInstructionKind
2002 };
2003#undef DECLARE_KIND
2004
2005 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01002006 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
2007
2008 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002009 : previous_(nullptr),
2010 next_(nullptr),
2011 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002012 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002013 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002014 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002015 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002017 locations_(nullptr),
2018 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002019 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002020 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002021 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302022 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002023 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002024 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2025 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002026
Dave Allison20dfc792014-06-16 20:44:29 -07002027 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002028
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002029
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002030 HInstruction* GetNext() const { return next_; }
2031 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002032
Calin Juravle77520bc2015-01-12 18:45:46 +00002033 HInstruction* GetNextDisregardingMoves() const;
2034 HInstruction* GetPreviousDisregardingMoves() const;
2035
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002036 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002037 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002038 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002039 bool IsInBlock() const { return block_ != nullptr; }
2040 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002041 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2042 bool IsIrreducibleLoopHeaderPhi() const {
2043 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2044 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002045
Vladimir Marko372f10e2016-05-17 16:30:10 +01002046 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2047
2048 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2049 // One virtual method is enough, just const_cast<> and then re-add the const.
2050 return ArrayRef<const HUserRecord<HInstruction*>>(
2051 const_cast<HInstruction*>(this)->GetInputRecords());
2052 }
2053
Vladimir Markoe9004912016-06-16 16:50:52 +01002054 HInputsRef GetInputs() {
2055 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002056 }
2057
Vladimir Markoe9004912016-06-16 16:50:52 +01002058 HConstInputsRef GetInputs() const {
2059 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002060 }
2061
2062 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002063 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002064
Aart Bik2767f4b2016-10-28 15:03:53 -07002065 bool HasInput(HInstruction* input) const {
2066 for (const HInstruction* i : GetInputs()) {
2067 if (i == input) {
2068 return true;
2069 }
2070 }
2071 return false;
2072 }
2073
Vladimir Marko372f10e2016-05-17 16:30:10 +01002074 void SetRawInputAt(size_t index, HInstruction* input) {
2075 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2076 }
2077
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002078 virtual void Accept(HGraphVisitor* visitor) = 0;
2079 virtual const char* DebugName() const = 0;
2080
Vladimir Markobd785672018-05-03 17:09:09 +01002081 DataType::Type GetType() const {
2082 return TypeField::Decode(GetPackedFields());
2083 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002084
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002085 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002086
2087 uint32_t GetDexPc() const { return dex_pc_; }
2088
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002089 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002090
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002091 // Can the instruction throw?
2092 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2093 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002094 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002095
2096 // Does the instruction always throw an exception unconditionally?
2097 virtual bool AlwaysThrows() const { return false; }
2098
David Brazdilec16f792015-08-19 15:04:01 +01002099 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002100
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002101 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002102 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103
Calin Juravle10e244f2015-01-26 18:54:32 +00002104 // Does not apply for all instructions, but having this at top level greatly
2105 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002106 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002107 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002109 return true;
2110 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002111
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002112 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002113 return false;
2114 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002115
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002116 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2117 // with it. Otherwise return null.
2118 HNullCheck* GetImplicitNullCheck() const {
2119 // Find the first previous instruction which is not a move.
2120 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2121 if (first_prev_not_move != nullptr &&
2122 first_prev_not_move->IsNullCheck() &&
2123 first_prev_not_move->IsEmittedAtUseSite()) {
2124 return first_prev_not_move->AsNullCheck();
2125 }
2126 return nullptr;
2127 }
2128
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002129 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002130 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002131 }
2132
Calin Juravle2e768302015-07-28 14:41:11 +00002133 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002134
Calin Juravle61d544b2015-02-23 16:46:57 +00002135 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002137 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002138 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002139 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002141 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002142 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002143 // Note: fixup_end remains valid across push_front().
2144 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2145 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002146 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002147 uses_.push_front(*new_node);
2148 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002149 }
2150
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002151 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002152 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002153 // Note: env_fixup_end remains valid across push_front().
2154 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2155 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002156 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002157 env_uses_.push_front(*new_node);
2158 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002159 }
2160
David Brazdil1abb4192015-02-17 18:33:36 +00002161 void RemoveAsUserOfInput(size_t input) {
2162 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002163 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2164 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2165 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002166 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002167
Vladimir Marko372f10e2016-05-17 16:30:10 +01002168 void RemoveAsUserOfAllInputs() {
2169 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2170 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2171 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2172 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2173 }
2174 }
2175
David Brazdil1abb4192015-02-17 18:33:36 +00002176 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2177 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002178
Vladimir Marko46817b82016-03-29 12:21:58 +01002179 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2180 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2181 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002182 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002183 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002184 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185
Aart Bikcc42be02016-10-20 16:14:16 -07002186 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002187 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002188 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002189 !CanThrow() &&
2190 !IsSuspendCheck() &&
2191 !IsControlFlow() &&
2192 !IsNativeDebugInfo() &&
2193 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002194 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002195 !IsMemoryBarrier() &&
2196 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002197 }
2198
Aart Bikcc42be02016-10-20 16:14:16 -07002199 bool IsDeadAndRemovable() const {
2200 return IsRemovable() && !HasUses();
2201 }
2202
Roland Levillain6c82d402014-10-13 16:10:27 +01002203 // Does this instruction strictly dominate `other_instruction`?
2204 // Returns false if this instruction and `other_instruction` are the same.
2205 // Aborts if this instruction and `other_instruction` are both phis.
2206 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002207
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002208 int GetId() const { return id_; }
2209 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002210
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002211 int GetSsaIndex() const { return ssa_index_; }
2212 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2213 bool HasSsaIndex() const { return ssa_index_ != -1; }
2214
2215 bool HasEnvironment() const { return environment_ != nullptr; }
2216 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002217 // Set the `environment_` field. Raw because this method does not
2218 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002219 void SetRawEnvironment(HEnvironment* environment) {
2220 DCHECK(environment_ == nullptr);
2221 DCHECK_EQ(environment->GetHolder(), this);
2222 environment_ = environment;
2223 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002224
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002225 void InsertRawEnvironment(HEnvironment* environment) {
2226 DCHECK(environment_ != nullptr);
2227 DCHECK_EQ(environment->GetHolder(), this);
2228 DCHECK(environment->GetParent() == nullptr);
2229 environment->parent_ = environment_;
2230 environment_ = environment;
2231 }
2232
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002233 void RemoveEnvironment();
2234
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002235 // Set the environment of this instruction, copying it from `environment`. While
2236 // copying, the uses lists are being updated.
2237 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002238 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002239 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002240 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002241 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002242 if (environment->GetParent() != nullptr) {
2243 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2244 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002245 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002246
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002247 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2248 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002249 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002250 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002251 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002252 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002253 if (environment->GetParent() != nullptr) {
2254 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2255 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002256 }
2257
Nicolas Geoffray39468442014-09-02 15:17:15 +01002258 // Returns the number of entries in the environment. Typically, that is the
2259 // number of dex registers in a method. It could be more in case of inlining.
2260 size_t EnvironmentSize() const;
2261
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002262 LocationSummary* GetLocations() const { return locations_; }
2263 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002264
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002265 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002266 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002267 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002268 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002269
Alexandre Rames188d4312015-04-09 18:30:21 +01002270 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2271 // uses of this instruction by `other` are *not* updated.
2272 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2273 ReplaceWith(other);
2274 other->ReplaceInput(this, use_index);
2275 }
2276
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002277 // Move `this` instruction before `cursor`
2278 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002279
Vladimir Markofb337ea2015-11-25 15:25:10 +00002280 // Move `this` before its first user and out of any loops. If there is no
2281 // out-of-loop user that dominates all other users, move the instruction
2282 // to the end of the out-of-loop common dominator of the user's blocks.
2283 //
2284 // This can be used only on non-throwing instructions with no side effects that
2285 // have at least one use but no environment uses.
2286 void MoveBeforeFirstUserAndOutOfLoops();
2287
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002288#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002289 bool Is##type() const;
2290
2291 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2292#undef INSTRUCTION_TYPE_CHECK
2293
2294#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002295 const H##type* As##type() const; \
2296 H##type* As##type();
2297
Vladimir Markoa90dd512018-05-04 15:04:45 +01002298 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2299#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002300
Artem Serovcced8ba2017-07-19 18:18:09 +01002301 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2302 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2303 // the instruction then the behaviour of this function is undefined.
2304 //
2305 // Note: It is semantically valid to create a clone of the instruction only until
2306 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2307 // copied.
2308 //
2309 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2310 // 'explicit HInstruction(const HInstruction& other)' for details.
2311 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2312 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2313 DebugName() << " " << GetId();
2314 UNREACHABLE();
2315 }
2316
2317 // Return whether instruction can be cloned (copied).
2318 virtual bool IsClonable() const { return false; }
2319
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002320 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002321 // TODO: this method is used by LICM and GVN with possibly different
2322 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002323 virtual bool CanBeMoved() const { return false; }
2324
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325 // Returns whether any data encoded in the two instructions is equal.
2326 // This method does not look at the inputs. Both instructions must be
2327 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002328 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002329 return false;
2330 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002331
2332 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002333 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002334 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002335 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002336
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002337 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2338 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2339 // the virtual function because the __attribute__((__pure__)) doesn't really
2340 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302341 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002342
2343 virtual size_t ComputeHashCode() const {
2344 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002345 for (const HInstruction* input : GetInputs()) {
2346 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002347 }
2348 return result;
2349 }
2350
2351 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002352 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002353 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002354
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002355 size_t GetLifetimePosition() const { return lifetime_position_; }
2356 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2357 LiveInterval* GetLiveInterval() const { return live_interval_; }
2358 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2359 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2360
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002361 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2362
2363 // Returns whether the code generation of the instruction will require to have access
2364 // to the current method. Such instructions are:
2365 // (1): Instructions that require an environment, as calling the runtime requires
2366 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002367 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2368 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002369 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002370 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002371 }
2372
Vladimir Markodc151b22015-10-15 18:02:30 +01002373 // Returns whether the code generation of the instruction will require to have access
2374 // to the dex cache of the current method's declaring class via the current method.
2375 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002376
Mark Mendellc4701932015-04-10 13:18:51 -04002377 // Does this instruction have any use in an environment before
2378 // control flow hits 'other'?
2379 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2380
2381 // Remove all references to environment uses of this instruction.
2382 // The caller must ensure that this is safe to do.
2383 void RemoveEnvironmentUsers();
2384
Vladimir Markoa1de9182016-02-25 11:37:38 +00002385 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2386 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002387
David Brazdil1abb4192015-02-17 18:33:36 +00002388 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002389 // If set, the machine code for this instruction is assumed to be generated by
2390 // its users. Used by liveness analysis to compute use positions accordingly.
2391 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2392 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302393 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2394 static constexpr size_t kFieldInstructionKindSize =
2395 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002396 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302397 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002398 static constexpr size_t kFieldTypeSize =
2399 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2400 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002401 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2402
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302403 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2404 "Too many generic packed fields");
2405
Vladimir Markobd785672018-05-03 17:09:09 +01002406 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2407
Vladimir Marko372f10e2016-05-17 16:30:10 +01002408 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2409 return GetInputRecords()[i];
2410 }
2411
2412 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2413 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2414 input_records[index] = input;
2415 }
David Brazdil1abb4192015-02-17 18:33:36 +00002416
Vladimir Markoa1de9182016-02-25 11:37:38 +00002417 uint32_t GetPackedFields() const {
2418 return packed_fields_;
2419 }
2420
2421 template <size_t flag>
2422 bool GetPackedFlag() const {
2423 return (packed_fields_ & (1u << flag)) != 0u;
2424 }
2425
2426 template <size_t flag>
2427 void SetPackedFlag(bool value = true) {
2428 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2429 }
2430
2431 template <typename BitFieldType>
2432 typename BitFieldType::value_type GetPackedField() const {
2433 return BitFieldType::Decode(packed_fields_);
2434 }
2435
2436 template <typename BitFieldType>
2437 void SetPackedField(typename BitFieldType::value_type value) {
2438 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2439 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2440 }
2441
Artem Serovcced8ba2017-07-19 18:18:09 +01002442 // Copy construction for the instruction (used for Clone function).
2443 //
2444 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2445 // prepare_for_register_allocator are not copied (set to default values).
2446 //
2447 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2448 // fine for most of them. However for some of the instructions a custom copy constructor must be
2449 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2450 // for copying them).
2451 explicit HInstruction(const HInstruction& other)
2452 : previous_(nullptr),
2453 next_(nullptr),
2454 block_(nullptr),
2455 dex_pc_(other.dex_pc_),
2456 id_(-1),
2457 ssa_index_(-1),
2458 packed_fields_(other.packed_fields_),
2459 environment_(nullptr),
2460 locations_(nullptr),
2461 live_interval_(nullptr),
2462 lifetime_position_(kNoLifetime),
2463 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302464 reference_type_handle_(other.reference_type_handle_) {
2465 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002466
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002467 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302468 using InstructionKindField =
2469 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2470
Vladimir Marko46817b82016-03-29 12:21:58 +01002471 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2472 auto before_use_node = uses_.before_begin();
2473 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2474 HInstruction* user = use_node->GetUser();
2475 size_t input_index = use_node->GetIndex();
2476 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2477 before_use_node = use_node;
2478 }
2479 }
2480
2481 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2482 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2483 if (next != uses_.end()) {
2484 HInstruction* next_user = next->GetUser();
2485 size_t next_index = next->GetIndex();
2486 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2487 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2488 }
2489 }
2490
2491 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2492 auto before_env_use_node = env_uses_.before_begin();
2493 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2494 HEnvironment* user = env_use_node->GetUser();
2495 size_t input_index = env_use_node->GetIndex();
2496 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2497 before_env_use_node = env_use_node;
2498 }
2499 }
2500
2501 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2502 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2503 if (next != env_uses_.end()) {
2504 HEnvironment* next_user = next->GetUser();
2505 size_t next_index = next->GetIndex();
2506 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2507 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2508 }
2509 }
David Brazdil1abb4192015-02-17 18:33:36 +00002510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511 HInstruction* previous_;
2512 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002513 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002514 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002516 // An instruction gets an id when it is added to the graph.
2517 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002518 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002519 int id_;
2520
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002521 // When doing liveness analysis, instructions that have uses get an SSA index.
2522 int ssa_index_;
2523
Vladimir Markoa1de9182016-02-25 11:37:38 +00002524 // Packed fields.
2525 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002526
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002527 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002528 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002529
2530 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002531 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002532
Nicolas Geoffray39468442014-09-02 15:17:15 +01002533 // The environment associated with this instruction. Not null if the instruction
2534 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002535 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002536
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002537 // Set by the code generator.
2538 LocationSummary* locations_;
2539
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002540 // Set by the liveness analysis.
2541 LiveInterval* live_interval_;
2542
2543 // Set by the liveness analysis, this is the position in a linear
2544 // order of blocks where this instruction's live interval start.
2545 size_t lifetime_position_;
2546
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002547 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002548
Vladimir Markoa1de9182016-02-25 11:37:38 +00002549 // The reference handle part of the reference type info.
2550 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002551 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002552 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002553
David Brazdil1abb4192015-02-17 18:33:36 +00002554 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002555 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002556 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002557 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002558 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002560std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002561
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002562// Iterates over the instructions, while preserving the next instruction
2563// in case the current instruction gets removed from the list by the user
2564// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565class HInstructionIterator : public ValueObject {
2566 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002567 explicit HInstructionIterator(const HInstructionList& instructions)
2568 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002569 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002570 }
2571
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002572 bool Done() const { return instruction_ == nullptr; }
2573 HInstruction* Current() const { return instruction_; }
2574 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002576 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002577 }
2578
2579 private:
2580 HInstruction* instruction_;
2581 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002582
2583 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002584};
2585
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002586// Iterates over the instructions without saving the next instruction,
2587// therefore handling changes in the graph potentially made by the user
2588// of this iterator.
2589class HInstructionIteratorHandleChanges : public ValueObject {
2590 public:
2591 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2592 : instruction_(instructions.first_instruction_) {
2593 }
2594
2595 bool Done() const { return instruction_ == nullptr; }
2596 HInstruction* Current() const { return instruction_; }
2597 void Advance() {
2598 instruction_ = instruction_->GetNext();
2599 }
2600
2601 private:
2602 HInstruction* instruction_;
2603
2604 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2605};
2606
2607
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002608class HBackwardInstructionIterator : public ValueObject {
2609 public:
2610 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2611 : instruction_(instructions.last_instruction_) {
2612 next_ = Done() ? nullptr : instruction_->GetPrevious();
2613 }
2614
2615 bool Done() const { return instruction_ == nullptr; }
2616 HInstruction* Current() const { return instruction_; }
2617 void Advance() {
2618 instruction_ = next_;
2619 next_ = Done() ? nullptr : instruction_->GetPrevious();
2620 }
2621
2622 private:
2623 HInstruction* instruction_;
2624 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002625
2626 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002627};
2628
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002629class HVariableInputSizeInstruction : public HInstruction {
2630 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002631 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002632 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002633 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2634 }
2635
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002636 void AddInput(HInstruction* input);
2637 void InsertInputAt(size_t index, HInstruction* input);
2638 void RemoveInputAt(size_t index);
2639
Igor Murashkind01745e2017-04-05 16:40:31 -07002640 // Removes all the inputs.
2641 // Also removes this instructions from each input's use list
2642 // (for non-environment uses only).
2643 void RemoveAllInputs();
2644
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002645 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302646 HVariableInputSizeInstruction(InstructionKind inst_kind,
2647 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002648 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002649 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002650 size_t number_of_inputs,
2651 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302652 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002653 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002654 HVariableInputSizeInstruction(InstructionKind inst_kind,
2655 DataType::Type type,
2656 SideEffects side_effects,
2657 uint32_t dex_pc,
2658 ArenaAllocator* allocator,
2659 size_t number_of_inputs,
2660 ArenaAllocKind kind)
2661 : HInstruction(inst_kind, type, side_effects, dex_pc),
2662 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002663
Artem Serovcced8ba2017-07-19 18:18:09 +01002664 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002665
Artem Serovcced8ba2017-07-19 18:18:09 +01002666 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002667};
2668
Vladimir Markof9f64412015-09-02 14:05:49 +01002669template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002670class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002672 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302673 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002674 HExpression<N>(InstructionKind kind,
2675 DataType::Type type,
2676 SideEffects side_effects,
2677 uint32_t dex_pc)
2678 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2679 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680
Vladimir Marko372f10e2016-05-17 16:30:10 +01002681 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002682 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002683 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002684 }
2685
Artem Serovcced8ba2017-07-19 18:18:09 +01002686 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002687 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002688
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002689 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002690 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002691
2692 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002693};
2694
Vladimir Markobd785672018-05-03 17:09:09 +01002695// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002696template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002697class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002698 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002699 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002700
Vladimir Markobd785672018-05-03 17:09:09 +01002701 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002702
Vladimir Marko372f10e2016-05-17 16:30:10 +01002703 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002704 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002705 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002706 }
2707
Artem Serovcced8ba2017-07-19 18:18:09 +01002708 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002709 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002710
Vladimir Markof9f64412015-09-02 14:05:49 +01002711 private:
2712 friend class SsaBuilder;
2713};
2714
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002715// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2716// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002717class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002718 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002719 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002720 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302721 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002723 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002724
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002725 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002726
Artem Serovcced8ba2017-07-19 18:18:09 +01002727 protected:
2728 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002729};
2730
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002731// Represents dex's RETURN opcodes. A HReturn is a control flow
2732// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002733class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002735 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002736 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002737 SetRawInputAt(0, value);
2738 }
2739
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002740 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002741
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002742 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002743
Artem Serovcced8ba2017-07-19 18:18:09 +01002744 protected:
2745 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002746};
2747
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002748class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002749 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002750 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002751 uint32_t reg_number,
2752 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002754 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002755 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302756 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002757 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002758 SideEffects::None(),
2759 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002760 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002761 number_of_inputs,
2762 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002763 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002765 // Phis are constructed live and marked dead if conflicting or unused.
2766 // Individual steps of SsaBuilder should assume that if a phi has been
2767 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2768 SetPackedFlag<kFlagIsLive>(true);
2769 SetPackedFlag<kFlagCanBeNull>(true);
2770 }
2771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002772 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002773
David Brazdildee58d62016-04-07 09:54:26 +00002774 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002775 static DataType::Type ToPhiType(DataType::Type type) {
2776 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002777 }
2778
2779 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002781 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002782 // Make sure that only valid type changes occur. The following are allowed:
2783 // (1) int -> float/ref (primitive type propagation),
2784 // (2) long -> double (primitive type propagation).
2785 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2787 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2788 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002789 SetPackedField<TypeField>(new_type);
2790 }
2791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002792 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002793 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2794
2795 uint32_t GetRegNumber() const { return reg_number_; }
2796
2797 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2798 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2799 bool IsDead() const { return !IsLive(); }
2800 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2801
Vladimir Markoe9004912016-06-16 16:50:52 +01002802 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002803 return other != nullptr
2804 && other->IsPhi()
2805 && other->AsPhi()->GetBlock() == GetBlock()
2806 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2807 }
2808
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002809 bool HasEquivalentPhi() const {
2810 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2811 return true;
2812 }
2813 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2814 return true;
2815 }
2816 return false;
2817 }
2818
David Brazdildee58d62016-04-07 09:54:26 +00002819 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2820 // An equivalent phi is a phi having the same dex register and type.
2821 // It assumes that phis with the same dex register are adjacent.
2822 HPhi* GetNextEquivalentPhiWithSameType() {
2823 HInstruction* next = GetNext();
2824 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2825 if (next->GetType() == GetType()) {
2826 return next->AsPhi();
2827 }
2828 next = next->GetNext();
2829 }
2830 return nullptr;
2831 }
2832
2833 DECLARE_INSTRUCTION(Phi);
2834
Artem Serovcced8ba2017-07-19 18:18:09 +01002835 protected:
2836 DEFAULT_COPY_CONSTRUCTOR(Phi);
2837
David Brazdildee58d62016-04-07 09:54:26 +00002838 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002839 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002840 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2841 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2842 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002843
David Brazdildee58d62016-04-07 09:54:26 +00002844 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002845};
2846
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002847// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002848// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002849// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002850class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002851 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302852 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002853 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302854 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002856 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002858 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002859
Artem Serovcced8ba2017-07-19 18:18:09 +01002860 protected:
2861 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002862};
2863
2864// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002865class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002866 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302867 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002868 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302869 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002871 bool IsClonable() const override { return true; }
2872 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002873
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002874 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002875 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002876 }
2877
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002878 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002879
Artem Serovcced8ba2017-07-19 18:18:09 +01002880 protected:
2881 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002882};
2883
Roland Levillain9867bc72015-08-05 10:21:34 +01002884class HConstant : public HExpression<0> {
2885 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302886 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2887 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2888 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002890 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002891
Roland Levillain1a653882016-03-18 18:05:57 +00002892 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002893 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002894 // Is this constant 0 in the arithmetic sense?
2895 virtual bool IsArithmeticZero() const { return false; }
2896 // Is this constant a 0-bit pattern?
2897 virtual bool IsZeroBitPattern() const { return false; }
2898 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002899 virtual bool IsOne() const { return false; }
2900
David Brazdil77a48ae2015-09-15 12:34:04 +00002901 virtual uint64_t GetValueAsUint64() const = 0;
2902
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002903 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002904
Artem Serovcced8ba2017-07-19 18:18:09 +01002905 protected:
2906 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002907};
2908
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002909class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002910 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002911 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002912 return true;
2913 }
2914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002915 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002917 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002918
Roland Levillain1a653882016-03-18 18:05:57 +00002919 // The null constant representation is a 0-bit pattern.
2920 virtual bool IsZeroBitPattern() const { return true; }
2921
Roland Levillain9867bc72015-08-05 10:21:34 +01002922 DECLARE_INSTRUCTION(NullConstant);
2923
Artem Serovcced8ba2017-07-19 18:18:09 +01002924 protected:
2925 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2926
Roland Levillain9867bc72015-08-05 10:21:34 +01002927 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302929 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2930 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002931
2932 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002933};
2934
2935// Constants of the type int. Those can be from Dex instructions, or
2936// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002937class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002938 public:
2939 int32_t GetValue() const { return value_; }
2940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002942 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2943 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002944
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002945 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002946 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002947 return other->AsIntConstant()->value_ == value_;
2948 }
2949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002950 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002952 bool IsMinusOne() const override { return GetValue() == -1; }
2953 bool IsArithmeticZero() const override { return GetValue() == 0; }
2954 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2955 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002956
Roland Levillain1a653882016-03-18 18:05:57 +00002957 // Integer constants are used to encode Boolean values as well,
2958 // where 1 means true and 0 means false.
2959 bool IsTrue() const { return GetValue() == 1; }
2960 bool IsFalse() const { return GetValue() == 0; }
2961
Roland Levillain9867bc72015-08-05 10:21:34 +01002962 DECLARE_INSTRUCTION(IntConstant);
2963
Artem Serovcced8ba2017-07-19 18:18:09 +01002964 protected:
2965 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2966
Roland Levillain9867bc72015-08-05 10:21:34 +01002967 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002968 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302969 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2970 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002971 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302972 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2973 value_(value ? 1 : 0) {
2974 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002975
2976 const int32_t value_;
2977
2978 friend class HGraph;
2979 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2980 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002981};
2982
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002983class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002984 public:
2985 int64_t GetValue() const { return value_; }
2986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002989 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002990 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002991 return other->AsLongConstant()->value_ == value_;
2992 }
2993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002994 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002995
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002996 bool IsMinusOne() const override { return GetValue() == -1; }
2997 bool IsArithmeticZero() const override { return GetValue() == 0; }
2998 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2999 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01003000
3001 DECLARE_INSTRUCTION(LongConstant);
3002
Artem Serovcced8ba2017-07-19 18:18:09 +01003003 protected:
3004 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
3005
Roland Levillain9867bc72015-08-05 10:21:34 +01003006 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003007 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303008 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3009 value_(value) {
3010 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003011
3012 const int64_t value_;
3013
3014 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003015};
Dave Allison20dfc792014-06-16 20:44:29 -07003016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003017class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003018 public:
3019 float GetValue() const { return value_; }
3020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003021 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3023 }
3024
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003025 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003026 DCHECK(other->IsFloatConstant()) << other->DebugName();
3027 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3028 }
3029
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003030 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003032 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003033 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3034 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003036 return std::fpclassify(value_) == FP_ZERO;
3037 }
3038 bool IsArithmeticPositiveZero() const {
3039 return IsArithmeticZero() && !std::signbit(value_);
3040 }
3041 bool IsArithmeticNegativeZero() const {
3042 return IsArithmeticZero() && std::signbit(value_);
3043 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003044 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003045 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003046 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003047 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003048 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3049 }
3050 bool IsNaN() const {
3051 return std::isnan(value_);
3052 }
3053
3054 DECLARE_INSTRUCTION(FloatConstant);
3055
Artem Serovcced8ba2017-07-19 18:18:09 +01003056 protected:
3057 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3058
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059 private:
3060 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303061 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3062 value_(value) {
3063 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003064 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303065 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3066 value_(bit_cast<float, int32_t>(value)) {
3067 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068
3069 const float value_;
3070
3071 // Only the SsaBuilder and HGraph can create floating-point constants.
3072 friend class SsaBuilder;
3073 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074};
3075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003076class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003077 public:
3078 double GetValue() const { return value_; }
3079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003082 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003083 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3084 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3085 }
3086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003087 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003088
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003089 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003090 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3091 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003092 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003093 return std::fpclassify(value_) == FP_ZERO;
3094 }
3095 bool IsArithmeticPositiveZero() const {
3096 return IsArithmeticZero() && !std::signbit(value_);
3097 }
3098 bool IsArithmeticNegativeZero() const {
3099 return IsArithmeticZero() && std::signbit(value_);
3100 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003101 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003102 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003104 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3106 }
3107 bool IsNaN() const {
3108 return std::isnan(value_);
3109 }
3110
3111 DECLARE_INSTRUCTION(DoubleConstant);
3112
Artem Serovcced8ba2017-07-19 18:18:09 +01003113 protected:
3114 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3115
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116 private:
3117 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303118 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3119 value_(value) {
3120 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003121 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303122 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3123 value_(bit_cast<double, int64_t>(value)) {
3124 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125
3126 const double value_;
3127
3128 // Only the SsaBuilder and HGraph can create floating-point constants.
3129 friend class SsaBuilder;
3130 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131};
3132
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003133// Conditional branch. A block ending with an HIf instruction must have
3134// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003135class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003136 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003137 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003138 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139 SetRawInputAt(0, input);
3140 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003142 bool IsClonable() const override { return true; }
3143 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003144
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003145 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003146 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003147 }
3148
3149 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003150 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003151 }
3152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003153 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003154
Artem Serovcced8ba2017-07-19 18:18:09 +01003155 protected:
3156 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003157};
3158
David Brazdilfc6a86a2015-06-26 10:33:45 +00003159
3160// Abstract instruction which marks the beginning and/or end of a try block and
3161// links it to the respective exception handlers. Behaves the same as a Goto in
3162// non-exceptional control flow.
3163// Normal-flow successor is stored at index zero, exception handlers under
3164// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003165class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003166 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003167 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003168 kEntry,
3169 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003170 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003171 };
3172
Artem Serovd1aa7d02018-06-22 11:35:46 +01003173 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3174 // across the catch block entering edges as GC might happen during throwing an exception.
3175 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3176 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003177 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003178 : HExpression(kTryBoundary,
3179 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3180 : SideEffects::None(),
3181 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003182 SetPackedField<BoundaryKindField>(kind);
3183 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003184
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003185 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003186
3187 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003188 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003189
David Brazdild26a4112015-11-10 11:07:31 +00003190 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3191 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3192 }
3193
David Brazdilfc6a86a2015-06-26 10:33:45 +00003194 // Returns whether `handler` is among its exception handlers (non-zero index
3195 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003196 bool HasExceptionHandler(const HBasicBlock& handler) const {
3197 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003198 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003199 }
3200
3201 // If not present already, adds `handler` to its block's list of exception
3202 // handlers.
3203 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003204 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003205 GetBlock()->AddSuccessor(handler);
3206 }
3207 }
3208
Vladimir Markoa1de9182016-02-25 11:37:38 +00003209 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3210 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003211
David Brazdilffee3d32015-07-06 11:48:53 +01003212 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3213
David Brazdilfc6a86a2015-06-26 10:33:45 +00003214 DECLARE_INSTRUCTION(TryBoundary);
3215
Artem Serovcced8ba2017-07-19 18:18:09 +01003216 protected:
3217 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3218
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003220 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3221 static constexpr size_t kFieldBoundaryKindSize =
3222 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3223 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3224 kFieldBoundaryKind + kFieldBoundaryKindSize;
3225 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3226 "Too many packed fields.");
3227 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003228};
3229
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003230// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003231class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003232 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003233 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3234 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003235 HDeoptimize(ArenaAllocator* allocator,
3236 HInstruction* cond,
3237 DeoptimizationKind kind,
3238 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003239 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303240 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003241 SideEffects::All(),
3242 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003243 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003244 /* number_of_inputs */ 1,
3245 kArenaAllocMisc) {
3246 SetPackedFlag<kFieldCanBeMoved>(false);
3247 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003248 SetRawInputAt(0, cond);
3249 }
3250
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003251 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003252
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003253 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3254 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3255 // instead of `guard`.
3256 // We set CanTriggerGC to prevent any intermediate address to be live
3257 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003258 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003259 HInstruction* cond,
3260 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003261 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003262 uint32_t dex_pc)
3263 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303264 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003265 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003266 SideEffects::CanTriggerGC(),
3267 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003268 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003269 /* number_of_inputs */ 2,
3270 kArenaAllocMisc) {
3271 SetPackedFlag<kFieldCanBeMoved>(true);
3272 SetPackedField<DeoptimizeKindField>(kind);
3273 SetRawInputAt(0, cond);
3274 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003275 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003277 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003278
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003279 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003280 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3281 }
3282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003283 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003285 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003286
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003287 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003288
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003289 bool GuardsAnInput() const {
3290 return InputCount() == 2;
3291 }
3292
3293 HInstruction* GuardedInput() const {
3294 DCHECK(GuardsAnInput());
3295 return InputAt(1);
3296 }
3297
3298 void RemoveGuard() {
3299 RemoveInputAt(1);
3300 }
3301
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003302 DECLARE_INSTRUCTION(Deoptimize);
3303
Artem Serovcced8ba2017-07-19 18:18:09 +01003304 protected:
3305 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3306
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003307 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003308 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3309 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3310 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003311 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003312 static constexpr size_t kNumberOfDeoptimizePackedBits =
3313 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3314 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3315 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003316 using DeoptimizeKindField =
3317 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003318};
3319
Mingyao Yang063fc772016-08-02 11:02:54 -07003320// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3321// The compiled code checks this flag value in a guard before devirtualized call and
3322// if it's true, starts to do deoptimization.
3323// It has a 4-byte slot on stack.
3324// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003325class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003326 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003327 // CHA guards are only optimized in a separate pass and it has no side effects
3328 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003329 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303330 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003331 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303332 SideEffects::None(),
3333 dex_pc,
3334 allocator,
3335 0,
3336 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003337 }
3338
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003339 // We do all CHA guard elimination/motion in a single pass, after which there is no
3340 // further guard elimination/motion since a guard might have been used for justification
3341 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3342 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003343 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003344
3345 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3346
Artem Serovcced8ba2017-07-19 18:18:09 +01003347 protected:
3348 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003349};
3350
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003351// Represents the ArtMethod that was passed as a first argument to
3352// the method. It is used by instructions that depend on it, like
3353// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003354class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003355 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003356 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303357 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3358 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003359
3360 DECLARE_INSTRUCTION(CurrentMethod);
3361
Artem Serovcced8ba2017-07-19 18:18:09 +01003362 protected:
3363 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003364};
3365
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003366// Fetches an ArtMethod from the virtual table or the interface method table
3367// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003368class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003369 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003370 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003371 kVTable,
3372 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003373 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003374 };
3375 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003377 TableKind kind,
3378 size_t index,
3379 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303380 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003381 index_(index) {
3382 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003383 SetRawInputAt(0, cls);
3384 }
3385
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003386 bool IsClonable() const override { return true; }
3387 bool CanBeMoved() const override { return true; }
3388 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003389 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003390 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003391 }
3392
Vladimir Markoa1de9182016-02-25 11:37:38 +00003393 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003394 size_t GetIndex() const { return index_; }
3395
3396 DECLARE_INSTRUCTION(ClassTableGet);
3397
Artem Serovcced8ba2017-07-19 18:18:09 +01003398 protected:
3399 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3400
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003401 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003402 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003403 static constexpr size_t kFieldTableKindSize =
3404 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3405 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3406 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3407 "Too many packed fields.");
3408 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3409
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003410 // The index of the ArtMethod in the table.
3411 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003412};
3413
Mark Mendellfe57faa2015-09-18 09:26:15 -04003414// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3415// have one successor for each entry in the switch table, and the final successor
3416// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003417class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003418 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003419 HPackedSwitch(int32_t start_value,
3420 uint32_t num_entries,
3421 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003422 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003423 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003424 start_value_(start_value),
3425 num_entries_(num_entries) {
3426 SetRawInputAt(0, input);
3427 }
3428
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003429 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003430
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003431 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003432
3433 int32_t GetStartValue() const { return start_value_; }
3434
Vladimir Marko211c2112015-09-24 16:52:33 +01003435 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003436
3437 HBasicBlock* GetDefaultBlock() const {
3438 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003439 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003440 }
3441 DECLARE_INSTRUCTION(PackedSwitch);
3442
Artem Serovcced8ba2017-07-19 18:18:09 +01003443 protected:
3444 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3445
Mark Mendellfe57faa2015-09-18 09:26:15 -04003446 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003447 const int32_t start_value_;
3448 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003449};
3450
Roland Levillain88cb1752014-10-20 16:36:47 +01003451class HUnaryOperation : public HExpression<1> {
3452 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303453 HUnaryOperation(InstructionKind kind,
3454 DataType::Type result_type,
3455 HInstruction* input,
3456 uint32_t dex_pc = kNoDexPc)
3457 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003458 SetRawInputAt(0, input);
3459 }
3460
Artem Serovcced8ba2017-07-19 18:18:09 +01003461 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003462 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003463
Roland Levillain88cb1752014-10-20 16:36:47 +01003464 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003466
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003467 bool CanBeMoved() const override { return true; }
3468 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003469 return true;
3470 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003471
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 // Try to statically evaluate `this` and return a HConstant
3473 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003474 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003475 HConstant* TryStaticEvaluation() const;
3476
3477 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003478 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3479 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003480 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3481 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003482
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003483 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003484
Artem Serovcced8ba2017-07-19 18:18:09 +01003485 protected:
3486 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003487};
3488
Dave Allison20dfc792014-06-16 20:44:29 -07003489class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003490 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303491 HBinaryOperation(InstructionKind kind,
3492 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003493 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003494 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003495 SideEffects side_effects = SideEffects::None(),
3496 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303497 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003498 SetRawInputAt(0, left);
3499 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003500 }
3501
Artem Serovcced8ba2017-07-19 18:18:09 +01003502 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003503 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003504
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003505 HInstruction* GetLeft() const { return InputAt(0); }
3506 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003507 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003508
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003509 virtual bool IsCommutative() const { return false; }
3510
3511 // Put constant on the right.
3512 // Returns whether order is changed.
3513 bool OrderInputsWithConstantOnTheRight() {
3514 HInstruction* left = InputAt(0);
3515 HInstruction* right = InputAt(1);
3516 if (left->IsConstant() && !right->IsConstant()) {
3517 ReplaceInput(right, 0);
3518 ReplaceInput(left, 1);
3519 return true;
3520 }
3521 return false;
3522 }
3523
3524 // Order inputs by instruction id, but favor constant on the right side.
3525 // This helps GVN for commutative ops.
3526 void OrderInputs() {
3527 DCHECK(IsCommutative());
3528 HInstruction* left = InputAt(0);
3529 HInstruction* right = InputAt(1);
3530 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3531 return;
3532 }
3533 if (OrderInputsWithConstantOnTheRight()) {
3534 return;
3535 }
3536 // Order according to instruction id.
3537 if (left->GetId() > right->GetId()) {
3538 ReplaceInput(right, 0);
3539 ReplaceInput(left, 1);
3540 }
3541 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003542
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003543 bool CanBeMoved() const override { return true; }
3544 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003545 return true;
3546 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003547
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 // Try to statically evaluate `this` and return a HConstant
3549 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003550 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003551 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003552
3553 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3555 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003556 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3557 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003558 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003559 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3560 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003561 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3562 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003563 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3564 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3567 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003568
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003569 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003570 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003571 HConstant* GetConstantRight() const;
3572
3573 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003574 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003575 HInstruction* GetLeastConstantLeft() const;
3576
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003577 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003578
Artem Serovcced8ba2017-07-19 18:18:09 +01003579 protected:
3580 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003581};
3582
Mark Mendellc4701932015-04-10 13:18:51 -04003583// The comparison bias applies for floating point operations and indicates how NaN
3584// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003585enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003586 kNoBias, // bias is not applicable (i.e. for long operation)
3587 kGtBias, // return 1 for NaN comparisons
3588 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003589 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003590};
3591
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3593
Dave Allison20dfc792014-06-16 20:44:29 -07003594class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003595 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303596 HCondition(InstructionKind kind,
3597 HInstruction* first,
3598 HInstruction* second,
3599 uint32_t dex_pc = kNoDexPc)
3600 : HBinaryOperation(kind,
3601 DataType::Type::kBool,
3602 first,
3603 second,
3604 SideEffects::None(),
3605 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003606 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3607 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003608
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003609 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003610 // `instruction`, and disregard moves in between.
3611 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003612
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003613 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003614
3615 virtual IfCondition GetCondition() const = 0;
3616
Mark Mendellc4701932015-04-10 13:18:51 -04003617 virtual IfCondition GetOppositeCondition() const = 0;
3618
Vladimir Markoa1de9182016-02-25 11:37:38 +00003619 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003620 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3621
Vladimir Markoa1de9182016-02-25 11:37:38 +00003622 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3623 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003624
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003625 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003626 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003627 }
3628
Roland Levillain4fa13f62015-07-06 18:11:54 +01003629 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003631 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003632 if (if_cond == kCondNE) {
3633 return true;
3634 } else if (if_cond == kCondEQ) {
3635 return false;
3636 }
3637 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003638 }
3639
3640 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003642 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003643 if (if_cond == kCondEQ) {
3644 return true;
3645 } else if (if_cond == kCondNE) {
3646 return false;
3647 }
3648 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003649 }
3650
Roland Levillain31dd3d62016-02-16 12:21:02 +00003651 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003652 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003653 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003654 static constexpr size_t kFieldComparisonBiasSize =
3655 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3656 static constexpr size_t kNumberOfConditionPackedBits =
3657 kFieldComparisonBias + kFieldComparisonBiasSize;
3658 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3659 using ComparisonBiasField =
3660 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3661
Roland Levillain31dd3d62016-02-16 12:21:02 +00003662 template <typename T>
3663 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3664
3665 template <typename T>
3666 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3669 // Handle the bias.
3670 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3671 }
3672
3673 // Return an integer constant containing the result of a condition evaluated at compile time.
3674 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3675 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3676 }
3677
Artem Serovcced8ba2017-07-19 18:18:09 +01003678 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003679};
3680
3681// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003682class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003684 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303685 : HCondition(kEqual, first, second, dex_pc) {
3686 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003687
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003688 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003689
Vladimir Marko9e23df52015-11-10 17:14:35 +00003690 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003691 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 return MakeConstantCondition(true, GetDexPc());
3693 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003694 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3696 }
3697 // In the following Evaluate methods, a HCompare instruction has
3698 // been merged into this HEqual instruction; evaluate it as
3699 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003700 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003701 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3702 GetDexPc());
3703 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003704 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003705 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3706 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003707 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003708 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003709 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003710
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003711 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003713 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003714 return kCondEQ;
3715 }
3716
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003717 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003718 return kCondNE;
3719 }
3720
Artem Serovcced8ba2017-07-19 18:18:09 +01003721 protected:
3722 DEFAULT_COPY_CONSTRUCTOR(Equal);
3723
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003724 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003725 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003726};
3727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003728class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003729 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303730 HNotEqual(HInstruction* first, HInstruction* second,
3731 uint32_t dex_pc = kNoDexPc)
3732 : HCondition(kNotEqual, first, second, dex_pc) {
3733 }
Dave Allison20dfc792014-06-16 20:44:29 -07003734
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003735 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003736
Vladimir Marko9e23df52015-11-10 17:14:35 +00003737 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003738 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(false, GetDexPc());
3740 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003741 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3743 }
3744 // In the following Evaluate methods, a HCompare instruction has
3745 // been merged into this HNotEqual instruction; evaluate it as
3746 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003747 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3749 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003750 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003751 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3752 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003753 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003754 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003755 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003756
Dave Allison20dfc792014-06-16 20:44:29 -07003757 DECLARE_INSTRUCTION(NotEqual);
3758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003759 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003760 return kCondNE;
3761 }
3762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003763 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003764 return kCondEQ;
3765 }
3766
Artem Serovcced8ba2017-07-19 18:18:09 +01003767 protected:
3768 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3769
Dave Allison20dfc792014-06-16 20:44:29 -07003770 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003771 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003772};
3773
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003774class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003775 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303776 HLessThan(HInstruction* first, HInstruction* second,
3777 uint32_t dex_pc = kNoDexPc)
3778 : HCondition(kLessThan, first, second, dex_pc) {
3779 }
Dave Allison20dfc792014-06-16 20:44:29 -07003780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003783 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003784 // In the following Evaluate methods, a HCompare instruction has
3785 // been merged into this HLessThan instruction; evaluate it as
3786 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003787 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3789 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003790 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003791 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3792 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003793 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003794 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003795 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003796
Dave Allison20dfc792014-06-16 20:44:29 -07003797 DECLARE_INSTRUCTION(LessThan);
3798
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003799 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003800 return kCondLT;
3801 }
3802
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003803 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003804 return kCondGE;
3805 }
3806
Artem Serovcced8ba2017-07-19 18:18:09 +01003807 protected:
3808 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3809
Dave Allison20dfc792014-06-16 20:44:29 -07003810 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003811 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003812};
3813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003814class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003815 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303816 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3817 uint32_t dex_pc = kNoDexPc)
3818 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3819 }
Dave Allison20dfc792014-06-16 20:44:29 -07003820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003821 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003823 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003824 // In the following Evaluate methods, a HCompare instruction has
3825 // been merged into this HLessThanOrEqual instruction; evaluate it as
3826 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003827 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3829 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003830 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3832 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003833 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003834 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003835 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003836
Dave Allison20dfc792014-06-16 20:44:29 -07003837 DECLARE_INSTRUCTION(LessThanOrEqual);
3838
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003839 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003840 return kCondLE;
3841 }
3842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003843 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003844 return kCondGT;
3845 }
3846
Artem Serovcced8ba2017-07-19 18:18:09 +01003847 protected:
3848 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3849
Dave Allison20dfc792014-06-16 20:44:29 -07003850 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003851 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003852};
3853
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003854class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003856 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303857 : HCondition(kGreaterThan, first, second, dex_pc) {
3858 }
Dave Allison20dfc792014-06-16 20:44:29 -07003859
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003862 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 // In the following Evaluate methods, a HCompare instruction has
3864 // been merged into this HGreaterThan instruction; evaluate it as
3865 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3868 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003869 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3871 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003872 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003874 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003875
Dave Allison20dfc792014-06-16 20:44:29 -07003876 DECLARE_INSTRUCTION(GreaterThan);
3877
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003878 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003879 return kCondGT;
3880 }
3881
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003882 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003883 return kCondLE;
3884 }
3885
Artem Serovcced8ba2017-07-19 18:18:09 +01003886 protected:
3887 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3888
Dave Allison20dfc792014-06-16 20:44:29 -07003889 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003890 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003891};
3892
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003893class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003894 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003895 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303896 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3897 }
Dave Allison20dfc792014-06-16 20:44:29 -07003898
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003901 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003902 // In the following Evaluate methods, a HCompare instruction has
3903 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3904 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003906 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3907 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003908 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003909 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3910 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003911 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003912 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003913 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003914
Dave Allison20dfc792014-06-16 20:44:29 -07003915 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3916
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003917 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003918 return kCondGE;
3919 }
3920
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003921 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003922 return kCondLT;
3923 }
3924
Artem Serovcced8ba2017-07-19 18:18:09 +01003925 protected:
3926 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3927
Dave Allison20dfc792014-06-16 20:44:29 -07003928 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003929 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003930};
3931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003933 public:
3934 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303935 : HCondition(kBelow, first, second, dex_pc) {
3936 }
Aart Bike9f37602015-10-09 11:15:55 -07003937
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003939 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003940 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003942 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3943 }
3944 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003945 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003946 LOG(FATAL) << DebugName() << " is not defined for float values";
3947 UNREACHABLE();
3948 }
3949 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003950 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003951 LOG(FATAL) << DebugName() << " is not defined for double values";
3952 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003953 }
3954
3955 DECLARE_INSTRUCTION(Below);
3956
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003957 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003958 return kCondB;
3959 }
3960
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003961 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003962 return kCondAE;
3963 }
3964
Artem Serovcced8ba2017-07-19 18:18:09 +01003965 protected:
3966 DEFAULT_COPY_CONSTRUCTOR(Below);
3967
Aart Bike9f37602015-10-09 11:15:55 -07003968 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003969 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003970 return MakeUnsigned(x) < MakeUnsigned(y);
3971 }
Aart Bike9f37602015-10-09 11:15:55 -07003972};
3973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003974class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003975 public:
3976 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303977 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3978 }
Aart Bike9f37602015-10-09 11:15:55 -07003979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003981 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003982 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003983 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003984 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3985 }
3986 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003987 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003988 LOG(FATAL) << DebugName() << " is not defined for float values";
3989 UNREACHABLE();
3990 }
3991 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003992 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003993 LOG(FATAL) << DebugName() << " is not defined for double values";
3994 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003995 }
3996
3997 DECLARE_INSTRUCTION(BelowOrEqual);
3998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003999 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004000 return kCondBE;
4001 }
4002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004003 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004004 return kCondA;
4005 }
4006
Artem Serovcced8ba2017-07-19 18:18:09 +01004007 protected:
4008 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4009
Aart Bike9f37602015-10-09 11:15:55 -07004010 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004011 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004012 return MakeUnsigned(x) <= MakeUnsigned(y);
4013 }
Aart Bike9f37602015-10-09 11:15:55 -07004014};
4015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004016class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004017 public:
4018 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304019 : HCondition(kAbove, first, second, dex_pc) {
4020 }
Aart Bike9f37602015-10-09 11:15:55 -07004021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004022 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004023 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004024 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004025 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4027 }
4028 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004029 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004030 LOG(FATAL) << DebugName() << " is not defined for float values";
4031 UNREACHABLE();
4032 }
4033 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004034 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004035 LOG(FATAL) << DebugName() << " is not defined for double values";
4036 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004037 }
4038
4039 DECLARE_INSTRUCTION(Above);
4040
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004041 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004042 return kCondA;
4043 }
4044
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004045 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004046 return kCondBE;
4047 }
4048
Artem Serovcced8ba2017-07-19 18:18:09 +01004049 protected:
4050 DEFAULT_COPY_CONSTRUCTOR(Above);
4051
Aart Bike9f37602015-10-09 11:15:55 -07004052 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004053 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004054 return MakeUnsigned(x) > MakeUnsigned(y);
4055 }
Aart Bike9f37602015-10-09 11:15:55 -07004056};
4057
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004059 public:
4060 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304061 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4062 }
Aart Bike9f37602015-10-09 11:15:55 -07004063
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004064 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004065 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004066 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004067 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4069 }
4070 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004071 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004072 LOG(FATAL) << DebugName() << " is not defined for float values";
4073 UNREACHABLE();
4074 }
4075 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004076 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004077 LOG(FATAL) << DebugName() << " is not defined for double values";
4078 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004079 }
4080
4081 DECLARE_INSTRUCTION(AboveOrEqual);
4082
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004083 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004084 return kCondAE;
4085 }
4086
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004087 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004088 return kCondB;
4089 }
4090
Artem Serovcced8ba2017-07-19 18:18:09 +01004091 protected:
4092 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4093
Aart Bike9f37602015-10-09 11:15:55 -07004094 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004095 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004096 return MakeUnsigned(x) >= MakeUnsigned(y);
4097 }
Aart Bike9f37602015-10-09 11:15:55 -07004098};
Dave Allison20dfc792014-06-16 20:44:29 -07004099
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004100// Instruction to check how two inputs compare to each other.
4101// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004102class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004103 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004104 // Note that `comparison_type` is the type of comparison performed
4105 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004106 // HCompare instruction (which is always DataType::Type::kInt).
4107 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004108 HInstruction* first,
4109 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004110 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004111 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304112 : HBinaryOperation(kCompare,
4113 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004114 first,
4115 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004116 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004117 dex_pc) {
4118 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4120 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004121 }
4122
Roland Levillain9867bc72015-08-05 10:21:34 +01004123 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004124 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4125
4126 template <typename T>
4127 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004129 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4130 // Handle the bias.
4131 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4132 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004134 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004135 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4136 // reach this code path when processing a freshly built HIR
4137 // graph. However HCompare integer instructions can be synthesized
4138 // by the instruction simplifier to implement IntegerCompare and
4139 // IntegerSignum intrinsics, so we have to handle this case.
4140 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004141 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004142 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004143 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4144 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004145 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004146 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4147 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004148 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004150 }
4151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004152 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004153 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004154 }
4155
Vladimir Markoa1de9182016-02-25 11:37:38 +00004156 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004157
Roland Levillain31dd3d62016-02-16 12:21:02 +00004158 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004159 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004160 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004166 // Comparisons do not require a runtime call in any back end.
4167 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004168 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004169
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004170 DECLARE_INSTRUCTION(Compare);
4171
Roland Levillain31dd3d62016-02-16 12:21:02 +00004172 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004173 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004174 static constexpr size_t kFieldComparisonBiasSize =
4175 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4176 static constexpr size_t kNumberOfComparePackedBits =
4177 kFieldComparisonBias + kFieldComparisonBiasSize;
4178 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4179 using ComparisonBiasField =
4180 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4181
Roland Levillain31dd3d62016-02-16 12:21:02 +00004182 // Return an integer constant containing the result of a comparison evaluated at compile time.
4183 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4184 DCHECK(value == -1 || value == 0 || value == 1) << value;
4185 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4186 }
4187
Artem Serovcced8ba2017-07-19 18:18:09 +01004188 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004189};
4190
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004191class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004192 public:
4193 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004194 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004195 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004196 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004197 bool finalizable,
4198 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304199 : HExpression(kNewInstance,
4200 DataType::Type::kReference,
4201 SideEffects::CanTriggerGC(),
4202 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004203 type_index_(type_index),
4204 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004205 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004207 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004208 }
4209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004210 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004211
Andreas Gampea5b09a62016-11-17 15:21:22 -08004212 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004213 const DexFile& GetDexFile() const { return dex_file_; }
4214
4215 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004216 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004217
Mingyao Yang062157f2016-03-02 10:15:36 -08004218 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004219 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004220
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004221 bool NeedsChecks() const {
4222 return entrypoint_ == kQuickAllocObjectWithChecks;
4223 }
David Brazdil6de19382016-01-08 17:37:10 +00004224
Vladimir Markoa1de9182016-02-25 11:37:38 +00004225 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004227 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004228
4229 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4230
4231 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4232 entrypoint_ = entrypoint;
4233 }
4234
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004235 HLoadClass* GetLoadClass() const {
4236 HInstruction* input = InputAt(0);
4237 if (input->IsClinitCheck()) {
4238 input = input->InputAt(0);
4239 }
4240 DCHECK(input->IsLoadClass());
4241 return input->AsLoadClass();
4242 }
4243
David Brazdil6de19382016-01-08 17:37:10 +00004244 bool IsStringAlloc() const;
4245
4246 DECLARE_INSTRUCTION(NewInstance);
4247
Artem Serovcced8ba2017-07-19 18:18:09 +01004248 protected:
4249 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4250
David Brazdil6de19382016-01-08 17:37:10 +00004251 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004252 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004253 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4254 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4255 "Too many packed fields.");
4256
Andreas Gampea5b09a62016-11-17 15:21:22 -08004257 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004258 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004259 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004260};
4261
Agi Csaki05f20562015-08-19 14:58:14 -07004262enum IntrinsicNeedsEnvironmentOrCache {
4263 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4264 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004265};
4266
Aart Bik5d75afe2015-12-14 11:57:01 -08004267enum IntrinsicSideEffects {
4268 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4269 kReadSideEffects, // Intrinsic may read heap memory.
4270 kWriteSideEffects, // Intrinsic may write heap memory.
4271 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4272};
4273
4274enum IntrinsicExceptions {
4275 kNoThrow, // Intrinsic does not throw any exceptions.
4276 kCanThrow // Intrinsic may throw exceptions.
4277};
4278
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004279class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004280 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004281 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004282
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004283 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004284 SetRawInputAt(index, argument);
4285 }
4286
Roland Levillain3e3d7332015-04-28 11:00:54 +01004287 // Return the number of arguments. This number can be lower than
4288 // the number of inputs returned by InputCount(), as some invoke
4289 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4290 // inputs at the end of their list of inputs.
4291 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4292
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004293 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4294
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004295 InvokeType GetInvokeType() const {
4296 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004297 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004298
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004299 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004300 return intrinsic_;
4301 }
4302
Aart Bik5d75afe2015-12-14 11:57:01 -08004303 void SetIntrinsic(Intrinsics intrinsic,
4304 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4305 IntrinsicSideEffects side_effects,
4306 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004307
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004308 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004309 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004310 }
4311
Aart Bikff7d89c2016-11-07 08:49:28 -08004312 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004314 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004315
Aart Bika8b8e9b2018-01-09 11:01:02 -08004316 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004318 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004319
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004320 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004321
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004322 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004323 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4324 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004325
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004326 uint32_t* GetIntrinsicOptimizations() {
4327 return &intrinsic_optimizations_;
4328 }
4329
4330 const uint32_t* GetIntrinsicOptimizations() const {
4331 return &intrinsic_optimizations_;
4332 }
4333
4334 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4335
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004337 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004338
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004339 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004340
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004341 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004342 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4343 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004344 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004345 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004346 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4347 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004348 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004349 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004350
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304351 HInvoke(InstructionKind kind,
4352 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004353 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004354 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004356 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004357 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358 ArtMethod* resolved_method,
4359 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004360 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304361 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004362 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004363 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4364 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004365 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004366 number_of_arguments + number_of_other_inputs,
4367 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004368 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004369 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004370 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004371 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004372 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004373 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004374 // Check mutator lock, constructors lack annotalysis support.
4375 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4376 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004377 }
4378
Artem Serovcced8ba2017-07-19 18:18:09 +01004379 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4380
Roland Levillain3e3d7332015-04-28 11:00:54 +01004381 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004382 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004383 const uint32_t dex_method_index_;
4384 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004385
4386 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4387 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004388};
4389
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004390class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004391 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004392 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004393 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004395 uint32_t dex_pc,
4396 uint32_t dex_method_index,
4397 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304398 : HInvoke(kInvokeUnresolved,
4399 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004400 number_of_arguments,
4401 0u /* number_of_other_inputs */,
4402 return_type,
4403 dex_pc,
4404 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004405 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004406 invoke_type) {
4407 }
4408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004409 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004410
Calin Juravle175dc732015-08-25 15:42:32 +01004411 DECLARE_INSTRUCTION(InvokeUnresolved);
4412
Artem Serovcced8ba2017-07-19 18:18:09 +01004413 protected:
4414 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004415};
4416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004417class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004418 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004419 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004420 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004421 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004422 uint32_t dex_pc,
4423 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304424 : HInvoke(kInvokePolymorphic,
4425 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004426 number_of_arguments,
4427 0u /* number_of_other_inputs */,
4428 return_type,
4429 dex_pc,
4430 dex_method_index,
4431 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304432 kVirtual) {
4433 }
Orion Hodsonac141392017-01-13 11:53:47 +00004434
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004435 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004436
Orion Hodsonac141392017-01-13 11:53:47 +00004437 DECLARE_INSTRUCTION(InvokePolymorphic);
4438
Artem Serovcced8ba2017-07-19 18:18:09 +01004439 protected:
4440 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004441};
4442
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004443class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004444 public:
4445 HInvokeCustom(ArenaAllocator* allocator,
4446 uint32_t number_of_arguments,
4447 uint32_t call_site_index,
4448 DataType::Type return_type,
4449 uint32_t dex_pc)
4450 : HInvoke(kInvokeCustom,
4451 allocator,
4452 number_of_arguments,
4453 /* number_of_other_inputs */ 0u,
4454 return_type,
4455 dex_pc,
4456 /* dex_method_index */ dex::kDexNoIndex,
4457 /* resolved_method */ nullptr,
4458 kStatic),
4459 call_site_index_(call_site_index) {
4460 }
4461
4462 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4463
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004464 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004465
4466 DECLARE_INSTRUCTION(InvokeCustom);
4467
4468 protected:
4469 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4470
4471 private:
4472 uint32_t call_site_index_;
4473};
4474
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004475class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004476 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004477 // Requirements of this method call regarding the class
4478 // initialization (clinit) check of its declaring class.
4479 enum class ClinitCheckRequirement {
4480 kNone, // Class already initialized.
4481 kExplicit, // Static call having explicit clinit check as last input.
4482 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004483 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004484 };
4485
Vladimir Marko58155012015-08-19 12:49:41 +00004486 // Determines how to load the target ArtMethod*.
4487 enum class MethodLoadKind {
4488 // Use a String init ArtMethod* loaded from Thread entrypoints.
4489 kStringInit,
4490
4491 // Use the method's own ArtMethod* loaded by the register allocator.
4492 kRecursive,
4493
Vladimir Marko65979462017-05-19 17:25:12 +01004494 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4495 // Used for boot image methods referenced by boot image code.
4496 kBootImageLinkTimePcRelative,
4497
Vladimir Markob066d432018-01-03 13:14:37 +00004498 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4499 // Used for app->boot calls with relocatable image.
4500 kBootImageRelRo,
4501
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004502 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004503 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004504 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004505
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004506 // Use ArtMethod* at a known address, embed the direct address in the code.
4507 // Used for for JIT-compiled calls.
4508 kJitDirectAddress,
4509
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004510 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4511 // used when other kinds are unimplemented on a particular architecture.
4512 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004513 };
4514
4515 // Determines the location of the code pointer.
4516 enum class CodePtrLocation {
4517 // Recursive call, use local PC-relative call instruction.
4518 kCallSelf,
4519
Vladimir Marko58155012015-08-19 12:49:41 +00004520 // Use code pointer from the ArtMethod*.
4521 // Used when we don't know the target code. This is also the last-resort-kind used when
4522 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4523 kCallArtMethod,
4524 };
4525
4526 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004527 MethodLoadKind method_load_kind;
4528 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004529 // The method load data holds
4530 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4531 // Note that there are multiple string init methods, each having its own offset.
4532 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004533 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004534 };
4535
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004536 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004537 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004538 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004539 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004540 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004541 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004542 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004543 InvokeType invoke_type,
4544 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004545 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304546 : HInvoke(kInvokeStaticOrDirect,
4547 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004548 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004549 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004550 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004551 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004552 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004553 return_type,
4554 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004555 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004556 resolved_method,
4557 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004558 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004559 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004560 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4561 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004563 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004564
Vladimir Markodc151b22015-10-15 18:02:30 +01004565 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004566 bool had_current_method_input = HasCurrentMethodInput();
4567 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4568
4569 // Using the current method is the default and once we find a better
4570 // method load kind, we should not go back to using the current method.
4571 DCHECK(had_current_method_input || !needs_current_method_input);
4572
4573 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004574 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4575 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004576 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004577 dispatch_info_ = dispatch_info;
4578 }
4579
Aart Bik6daebeb2017-04-03 14:35:41 -07004580 DispatchInfo GetDispatchInfo() const {
4581 return dispatch_info_;
4582 }
4583
Vladimir Markoc53c0792015-11-19 15:48:33 +00004584 void AddSpecialInput(HInstruction* input) {
4585 // We allow only one special input.
4586 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4587 DCHECK(InputCount() == GetSpecialInputIndex() ||
4588 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4589 InsertInputAt(GetSpecialInputIndex(), input);
4590 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004591
Vladimir Marko372f10e2016-05-17 16:30:10 +01004592 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004593 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004594 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4595 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4596 DCHECK(!input_records.empty());
4597 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4598 HInstruction* last_input = input_records.back().GetInstruction();
4599 // Note: `last_input` may be null during arguments setup.
4600 if (last_input != nullptr) {
4601 // `last_input` is the last input of a static invoke marked as having
4602 // an explicit clinit check. It must either be:
4603 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4604 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4605 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4606 }
4607 }
4608 return input_records;
4609 }
4610
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004611 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004612 // We access the method via the dex cache so we can't do an implicit null check.
4613 // TODO: for intrinsics we can generate implicit null checks.
4614 return false;
4615 }
4616
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004617 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004618 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004619 }
4620
Vladimir Markoc53c0792015-11-19 15:48:33 +00004621 // Get the index of the special input, if any.
4622 //
David Brazdil6de19382016-01-08 17:37:10 +00004623 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4624 // method pointer; otherwise there may be one platform-specific special input,
4625 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004626 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004627 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004628
Vladimir Marko58155012015-08-19 12:49:41 +00004629 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4630 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4631 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004632 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004633 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004634 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004635 bool HasPcRelativeMethodLoadKind() const {
4636 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004637 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004638 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004639 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004640 bool HasCurrentMethodInput() const {
4641 // This function can be called only after the invoke has been fully initialized by the builder.
4642 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004643 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004644 return true;
4645 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004646 DCHECK(InputCount() == GetSpecialInputIndex() ||
4647 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004648 return false;
4649 }
4650 }
Vladimir Marko58155012015-08-19 12:49:41 +00004651
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004652 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004653 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004654 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004655 }
4656
4657 uint64_t GetMethodAddress() const {
4658 DCHECK(HasMethodAddress());
4659 return dispatch_info_.method_load_data;
4660 }
4661
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004662 const DexFile& GetDexFileForPcRelativeDexCache() const;
4663
Vladimir Markoa1de9182016-02-25 11:37:38 +00004664 ClinitCheckRequirement GetClinitCheckRequirement() const {
4665 return GetPackedField<ClinitCheckRequirementField>();
4666 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004667
Roland Levillain4c0eb422015-04-24 16:43:49 +01004668 // Is this instruction a call to a static method?
4669 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004670 return GetInvokeType() == kStatic;
4671 }
4672
4673 MethodReference GetTargetMethod() const {
4674 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004675 }
4676
Vladimir Markofbb184a2015-11-13 14:47:00 +00004677 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4678 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4679 // instruction; only relevant for static calls with explicit clinit check.
4680 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004681 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004682 size_t last_input_index = inputs_.size() - 1u;
4683 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004684 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004685 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004686 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004687 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004688 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004689 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004690 }
4691
4692 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004693 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004694 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004695 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004696 }
4697
4698 // Is this a call to a static method whose declaring class has an
4699 // implicit intialization check requirement?
4700 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004701 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004702 }
4703
Vladimir Markob554b5a2015-11-06 12:57:55 +00004704 // Does this method load kind need the current method as an input?
4705 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004706 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004707 }
4708
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004709 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004710
Artem Serovcced8ba2017-07-19 18:18:09 +01004711 protected:
4712 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4713
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004714 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004715 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004716 static constexpr size_t kFieldClinitCheckRequirementSize =
4717 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4718 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4719 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4720 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4721 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004722 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4723 kFieldClinitCheckRequirement,
4724 kFieldClinitCheckRequirementSize>;
4725
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004726 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004727 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004728 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004729};
Vladimir Markof64242a2015-12-01 14:58:23 +00004730std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004731std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004733class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004734 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004735 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004736 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004737 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004738 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004739 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004740 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004741 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304742 : HInvoke(kInvokeVirtual,
4743 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004744 number_of_arguments,
4745 0u,
4746 return_type,
4747 dex_pc,
4748 dex_method_index,
4749 resolved_method,
4750 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304751 vtable_index_(vtable_index) {
4752 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004754 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004756 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004757 switch (GetIntrinsic()) {
4758 case Intrinsics::kThreadCurrentThread:
4759 case Intrinsics::kStringBufferAppend:
4760 case Intrinsics::kStringBufferToString:
4761 case Intrinsics::kStringBuilderAppend:
4762 case Intrinsics::kStringBuilderToString:
4763 return false;
4764 default:
4765 return HInvoke::CanBeNull();
4766 }
4767 }
4768
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004769 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004770 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004771 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004772 }
4773
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004774 uint32_t GetVTableIndex() const { return vtable_index_; }
4775
4776 DECLARE_INSTRUCTION(InvokeVirtual);
4777
Artem Serovcced8ba2017-07-19 18:18:09 +01004778 protected:
4779 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4780
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004781 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004782 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004783 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004784};
4785
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004786class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004787 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004788 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004789 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004790 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004791 uint32_t dex_pc,
4792 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004793 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004794 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304795 : HInvoke(kInvokeInterface,
4796 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004797 number_of_arguments,
4798 0u,
4799 return_type,
4800 dex_pc,
4801 dex_method_index,
4802 resolved_method,
4803 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304804 imt_index_(imt_index) {
4805 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004807 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004808
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004809 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004810 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004811 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004812 }
4813
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004814 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004815 // The assembly stub currently needs it.
4816 return true;
4817 }
4818
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004819 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004820
4821 DECLARE_INSTRUCTION(InvokeInterface);
4822
Artem Serovcced8ba2017-07-19 18:18:09 +01004823 protected:
4824 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4825
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004826 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004827 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004828 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004829};
4830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004831class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004832 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304834 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004835 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004836 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004837
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004838 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004839
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004840 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004841 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004843 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004846 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004847 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4848 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004849 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004850 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4851 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004852
Roland Levillain88cb1752014-10-20 16:36:47 +01004853 DECLARE_INSTRUCTION(Neg);
4854
Artem Serovcced8ba2017-07-19 18:18:09 +01004855 protected:
4856 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004857};
4858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004859class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004860 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004861 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304862 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004863 SetRawInputAt(0, cls);
4864 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004865 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004866 }
4867
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004868 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004869
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004870 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004871 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004872
Mingyao Yang0c365e62015-03-31 15:09:29 -07004873 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004874 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004876 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004878 HLoadClass* GetLoadClass() const {
4879 DCHECK(InputAt(0)->IsLoadClass());
4880 return InputAt(0)->AsLoadClass();
4881 }
4882
4883 HInstruction* GetLength() const {
4884 return InputAt(1);
4885 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004886
Vladimir Markob5461632018-10-15 14:24:21 +01004887 size_t GetComponentSizeShift() {
4888 return GetPackedField<ComponentSizeShiftField>();
4889 }
4890
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004891 DECLARE_INSTRUCTION(NewArray);
4892
Artem Serovcced8ba2017-07-19 18:18:09 +01004893 protected:
4894 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004895
4896 private:
4897 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4898 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4899 static constexpr size_t kNumberOfNewArrayPackedBits =
4900 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4901 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4902 using ComponentSizeShiftField =
4903 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004904};
4905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004906class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004907 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004908 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 HInstruction* left,
4910 HInstruction* right,
4911 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304912 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4913 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004915 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004916
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004917 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 return GetBlock()->GetGraph()->GetIntConstant(
4921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004922 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004924 return GetBlock()->GetGraph()->GetLongConstant(
4925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004926 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004927 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004928 return GetBlock()->GetGraph()->GetFloatConstant(
4929 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4930 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004931 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004932 return GetBlock()->GetGraph()->GetDoubleConstant(
4933 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4934 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004935
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004936 DECLARE_INSTRUCTION(Add);
4937
Artem Serovcced8ba2017-07-19 18:18:09 +01004938 protected:
4939 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004940};
4941
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004942class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004943 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004944 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 HInstruction* left,
4946 HInstruction* right,
4947 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304948 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4949 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004950
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004951 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 return GetBlock()->GetGraph()->GetIntConstant(
4955 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004956 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004957 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004958 return GetBlock()->GetGraph()->GetLongConstant(
4959 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004960 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004961 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004962 return GetBlock()->GetGraph()->GetFloatConstant(
4963 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4964 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004965 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004966 return GetBlock()->GetGraph()->GetDoubleConstant(
4967 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4968 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004969
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004970 DECLARE_INSTRUCTION(Sub);
4971
Artem Serovcced8ba2017-07-19 18:18:09 +01004972 protected:
4973 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004974};
4975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004976class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004977 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004978 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 HInstruction* left,
4980 HInstruction* right,
4981 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304982 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4983 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004985 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004986
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004987 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 return GetBlock()->GetGraph()->GetIntConstant(
4991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetLongConstant(
4995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004997 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004998 return GetBlock()->GetGraph()->GetFloatConstant(
4999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5000 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005001 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005002 return GetBlock()->GetGraph()->GetDoubleConstant(
5003 Compute(x->GetValue(), y->GetValue()), GetDexPc());
5004 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01005005
5006 DECLARE_INSTRUCTION(Mul);
5007
Artem Serovcced8ba2017-07-19 18:18:09 +01005008 protected:
5009 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005010};
5011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005012class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005013 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005014 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 HInstruction* left,
5016 HInstruction* right,
5017 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305018 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5019 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005020
Roland Levillain9867bc72015-08-05 10:21:34 +01005021 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005022 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005023 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005024 // Our graph structure ensures we never have 0 for `y` during
5025 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005026 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005027 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005028 return (y == -1) ? -x : x / y;
5029 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005030
Roland Levillain31dd3d62016-02-16 12:21:02 +00005031 template <typename T>
5032 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005033 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005034 return x / y;
5035 }
5036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005037 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005038 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005039 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005040 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005041 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005042 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005043 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5044 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005045 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005046 return GetBlock()->GetGraph()->GetFloatConstant(
5047 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5048 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005049 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005050 return GetBlock()->GetGraph()->GetDoubleConstant(
5051 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005052 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005053
5054 DECLARE_INSTRUCTION(Div);
5055
Artem Serovcced8ba2017-07-19 18:18:09 +01005056 protected:
5057 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005058};
5059
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005060class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005061 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 HInstruction* left,
5064 HInstruction* right,
5065 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305066 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5067 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005068
Roland Levillain9867bc72015-08-05 10:21:34 +01005069 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005072 // Our graph structure ensures we never have 0 for `y` during
5073 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005074 DCHECK_NE(y, 0);
5075 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5076 return (y == -1) ? 0 : x % y;
5077 }
5078
Roland Levillain31dd3d62016-02-16 12:21:02 +00005079 template <typename T>
5080 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005081 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 return std::fmod(x, y);
5083 }
5084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005086 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005087 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005088 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005089 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005090 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005091 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005092 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005094 return GetBlock()->GetGraph()->GetFloatConstant(
5095 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5096 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005097 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005098 return GetBlock()->GetGraph()->GetDoubleConstant(
5099 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5100 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005101
5102 DECLARE_INSTRUCTION(Rem);
5103
Artem Serovcced8ba2017-07-19 18:18:09 +01005104 protected:
5105 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005106};
5107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005108class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005109 public:
5110 HMin(DataType::Type result_type,
5111 HInstruction* left,
5112 HInstruction* right,
5113 uint32_t dex_pc)
5114 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005116 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005117
5118 // Evaluation for integral values.
5119 template <typename T> static T ComputeIntegral(T x, T y) {
5120 return (x <= y) ? x : y;
5121 }
5122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005123 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005124 return GetBlock()->GetGraph()->GetIntConstant(
5125 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5126 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005127 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005128 return GetBlock()->GetGraph()->GetLongConstant(
5129 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5130 }
5131 // TODO: Evaluation for floating-point values.
5132 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005133 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005134 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005136
5137 DECLARE_INSTRUCTION(Min);
5138
5139 protected:
5140 DEFAULT_COPY_CONSTRUCTOR(Min);
5141};
5142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005143class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005144 public:
5145 HMax(DataType::Type result_type,
5146 HInstruction* left,
5147 HInstruction* right,
5148 uint32_t dex_pc)
5149 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5150
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005151 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005152
5153 // Evaluation for integral values.
5154 template <typename T> static T ComputeIntegral(T x, T y) {
5155 return (x >= y) ? x : y;
5156 }
5157
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005158 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005159 return GetBlock()->GetGraph()->GetIntConstant(
5160 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5161 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005162 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005163 return GetBlock()->GetGraph()->GetLongConstant(
5164 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5165 }
5166 // TODO: Evaluation for floating-point values.
5167 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005168 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005169 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005170 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005171
5172 DECLARE_INSTRUCTION(Max);
5173
5174 protected:
5175 DEFAULT_COPY_CONSTRUCTOR(Max);
5176};
5177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005179 public:
5180 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5181 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5182
5183 // Evaluation for integral values.
5184 template <typename T> static T ComputeIntegral(T x) {
5185 return x < 0 ? -x : x;
5186 }
5187
5188 // Evaluation for floating-point values.
5189 // Note, as a "quality of implementation", rather than pure "spec compliance",
5190 // we require that Math.abs() clears the sign bit (but changes nothing else)
5191 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5192 // http://b/30758343
5193 template <typename T, typename S> static T ComputeFP(T x) {
5194 S bits = bit_cast<S, T>(x);
5195 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5196 }
5197
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005198 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005199 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5200 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005201 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005202 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5203 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005204 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005205 return GetBlock()->GetGraph()->GetFloatConstant(
5206 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5207 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005208 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005209 return GetBlock()->GetGraph()->GetDoubleConstant(
5210 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5211 }
5212
5213 DECLARE_INSTRUCTION(Abs);
5214
5215 protected:
5216 DEFAULT_COPY_CONSTRUCTOR(Abs);
5217};
5218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005219class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005220 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005221 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005222 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5223 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005224 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005225 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005226 SetRawInputAt(0, value);
5227 }
5228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005229 bool IsClonable() const override { return true; }
5230 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005232 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005233 return true;
5234 }
5235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005236 bool NeedsEnvironment() const override { return true; }
5237 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005238
Calin Juravled0d48522014-11-04 16:40:20 +00005239 DECLARE_INSTRUCTION(DivZeroCheck);
5240
Artem Serovcced8ba2017-07-19 18:18:09 +01005241 protected:
5242 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005243};
5244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005245class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005246 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005247 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 HInstruction* value,
5249 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005250 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305251 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005252 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5253 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005254 }
5255
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005257 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 return value << (distance & max_shift_distance);
5259 }
5260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005261 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005262 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005263 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005264 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005265 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005266 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005267 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005268 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005269 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005270 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005271 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5272 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005273 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005274 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005275 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005276 LOG(FATAL) << DebugName() << " is not defined for float values";
5277 UNREACHABLE();
5278 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005279 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005280 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005281 LOG(FATAL) << DebugName() << " is not defined for double values";
5282 UNREACHABLE();
5283 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005284
5285 DECLARE_INSTRUCTION(Shl);
5286
Artem Serovcced8ba2017-07-19 18:18:09 +01005287 protected:
5288 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005289};
5290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005291class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005292 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005293 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005294 HInstruction* value,
5295 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005296 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305297 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005298 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5299 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005300 }
5301
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005303 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 return value >> (distance & max_shift_distance);
5305 }
5306
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005307 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005308 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005309 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005310 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005311 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005312 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005313 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005314 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005315 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005316 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005317 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5318 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005319 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005320 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005321 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005322 LOG(FATAL) << DebugName() << " is not defined for float values";
5323 UNREACHABLE();
5324 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005326 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005327 LOG(FATAL) << DebugName() << " is not defined for double values";
5328 UNREACHABLE();
5329 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005330
5331 DECLARE_INSTRUCTION(Shr);
5332
Artem Serovcced8ba2017-07-19 18:18:09 +01005333 protected:
5334 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005335};
5336
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005337class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005338 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005339 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005340 HInstruction* value,
5341 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005342 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305343 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005344 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5345 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005346 }
5347
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005349 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 typedef typename std::make_unsigned<T>::type V;
5351 V ux = static_cast<V>(value);
5352 return static_cast<T>(ux >> (distance & max_shift_distance));
5353 }
5354
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005356 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005357 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005358 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005359 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005360 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005361 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005362 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005363 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005364 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005365 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5366 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005367 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005368 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005369 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005370 LOG(FATAL) << DebugName() << " is not defined for float values";
5371 UNREACHABLE();
5372 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005373 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005374 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005375 LOG(FATAL) << DebugName() << " is not defined for double values";
5376 UNREACHABLE();
5377 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005378
5379 DECLARE_INSTRUCTION(UShr);
5380
Artem Serovcced8ba2017-07-19 18:18:09 +01005381 protected:
5382 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005383};
5384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005386 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005388 HInstruction* left,
5389 HInstruction* right,
5390 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305391 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5392 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005394 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005395
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005396 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005397
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005398 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005399 return GetBlock()->GetGraph()->GetIntConstant(
5400 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005401 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005403 return GetBlock()->GetGraph()->GetLongConstant(
5404 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005405 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005406 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005407 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005408 LOG(FATAL) << DebugName() << " is not defined for float values";
5409 UNREACHABLE();
5410 }
5411 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005412 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005413 LOG(FATAL) << DebugName() << " is not defined for double values";
5414 UNREACHABLE();
5415 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005416
5417 DECLARE_INSTRUCTION(And);
5418
Artem Serovcced8ba2017-07-19 18:18:09 +01005419 protected:
5420 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005421};
5422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005423class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005424 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005425 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005426 HInstruction* left,
5427 HInstruction* right,
5428 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305429 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5430 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005431
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005432 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005433
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005434 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005435
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005436 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005437 return GetBlock()->GetGraph()->GetIntConstant(
5438 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005439 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005440 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005441 return GetBlock()->GetGraph()->GetLongConstant(
5442 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005443 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005444 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005445 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005446 LOG(FATAL) << DebugName() << " is not defined for float values";
5447 UNREACHABLE();
5448 }
5449 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005450 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005451 LOG(FATAL) << DebugName() << " is not defined for double values";
5452 UNREACHABLE();
5453 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005454
5455 DECLARE_INSTRUCTION(Or);
5456
Artem Serovcced8ba2017-07-19 18:18:09 +01005457 protected:
5458 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005459};
5460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005461class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005462 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005463 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005464 HInstruction* left,
5465 HInstruction* right,
5466 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305467 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5468 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005469
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005470 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005471
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005472 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005473
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005474 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005475 return GetBlock()->GetGraph()->GetIntConstant(
5476 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005477 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005478 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005479 return GetBlock()->GetGraph()->GetLongConstant(
5480 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005481 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005482 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005483 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005484 LOG(FATAL) << DebugName() << " is not defined for float values";
5485 UNREACHABLE();
5486 }
5487 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005488 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005489 LOG(FATAL) << DebugName() << " is not defined for double values";
5490 UNREACHABLE();
5491 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005492
5493 DECLARE_INSTRUCTION(Xor);
5494
Artem Serovcced8ba2017-07-19 18:18:09 +01005495 protected:
5496 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005497};
5498
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005499class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005500 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305502 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5504 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005505 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005506
Roland Levillain5b5b9312016-03-22 14:57:31 +00005507 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005508 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005509 typedef typename std::make_unsigned<T>::type V;
5510 V ux = static_cast<V>(value);
5511 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005512 return static_cast<T>(ux);
5513 } else {
5514 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005515 return static_cast<T>(ux >> (distance & max_shift_value)) |
5516 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005517 }
5518 }
5519
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005520 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005521 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005522 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005523 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005524 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005525 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005526 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005527 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005528 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005530 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5531 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005532 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005533 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005534 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005535 LOG(FATAL) << DebugName() << " is not defined for float values";
5536 UNREACHABLE();
5537 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005538 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005539 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005540 LOG(FATAL) << DebugName() << " is not defined for double values";
5541 UNREACHABLE();
5542 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005543
5544 DECLARE_INSTRUCTION(Ror);
5545
Artem Serovcced8ba2017-07-19 18:18:09 +01005546 protected:
5547 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005548};
5549
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005550// The value of a parameter in this method. Its location depends on
5551// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005552class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005553 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005554 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005555 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005556 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005558 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305559 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005560 dex_file_(dex_file),
5561 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005562 index_(index) {
5563 SetPackedFlag<kFlagIsThis>(is_this);
5564 SetPackedFlag<kFlagCanBeNull>(!is_this);
5565 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005566
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005567 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005568 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005569 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005570 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005573 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005574
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005575 DECLARE_INSTRUCTION(ParameterValue);
5576
Artem Serovcced8ba2017-07-19 18:18:09 +01005577 protected:
5578 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5579
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005580 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005581 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005582 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5584 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5585 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5586 "Too many packed fields.");
5587
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005588 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005589 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005590 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005591 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005592 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005593};
5594
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005595class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005596 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305598 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5599 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005600
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005601 bool CanBeMoved() const override { return true; }
5602 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005603 return true;
5604 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005605
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005606 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005608 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005609 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005610 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005611 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005612 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005613 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005614 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005615 LOG(FATAL) << DebugName() << " is not defined for float values";
5616 UNREACHABLE();
5617 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005618 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005619 LOG(FATAL) << DebugName() << " is not defined for double values";
5620 UNREACHABLE();
5621 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005622
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005623 DECLARE_INSTRUCTION(Not);
5624
Artem Serovcced8ba2017-07-19 18:18:09 +01005625 protected:
5626 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005627};
5628
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005631 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305632 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5633 }
David Brazdil66d126e2015-04-03 16:02:44 +01005634
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005635 bool CanBeMoved() const override { return true; }
5636 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005637 return true;
5638 }
5639
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005640 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005641 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005642 return !x;
5643 }
5644
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005645 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005646 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005647 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005648 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005649 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005650 UNREACHABLE();
5651 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005652 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005653 LOG(FATAL) << DebugName() << " is not defined for float values";
5654 UNREACHABLE();
5655 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005656 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005657 LOG(FATAL) << DebugName() << " is not defined for double values";
5658 UNREACHABLE();
5659 }
David Brazdil66d126e2015-04-03 16:02:44 +01005660
5661 DECLARE_INSTRUCTION(BooleanNot);
5662
Artem Serovcced8ba2017-07-19 18:18:09 +01005663 protected:
5664 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005665};
5666
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005667class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005668 public:
5669 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005670 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305671 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005672 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005673 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005675 }
5676
5677 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005678 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5679 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005680
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005681 bool IsClonable() const override { return true; }
5682 bool CanBeMoved() const override { return true; }
5683 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005684 return true;
5685 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005686 // Return whether the conversion is implicit. This includes conversion to the same type.
5687 bool IsImplicitConversion() const {
5688 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5689 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005690
Mark Mendelle82549b2015-05-06 10:55:34 -04005691 // Try to statically evaluate the conversion and return a HConstant
5692 // containing the result. If the input cannot be converted, return nullptr.
5693 HConstant* TryStaticEvaluation() const;
5694
Roland Levillaindff1f282014-11-05 14:15:05 +00005695 DECLARE_INSTRUCTION(TypeConversion);
5696
Artem Serovcced8ba2017-07-19 18:18:09 +01005697 protected:
5698 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005699};
5700
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005701static constexpr uint32_t kNoRegNumber = -1;
5702
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005703class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005704 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005705 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005706 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5707 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005708 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005709 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710 SetRawInputAt(0, value);
5711 }
5712
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005713 bool IsClonable() const override { return true; }
5714 bool CanBeMoved() const override { return true; }
5715 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005716 return true;
5717 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005718
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005719 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005721 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005722
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005723 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005724
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005725 DECLARE_INSTRUCTION(NullCheck);
5726
Artem Serovcced8ba2017-07-19 18:18:09 +01005727 protected:
5728 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005729};
5730
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005731// Embeds an ArtField and all the information required by the compiler. We cache
5732// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005733class FieldInfo : public ValueObject {
5734 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005735 FieldInfo(ArtField* field,
5736 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005737 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005738 bool is_volatile,
5739 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005740 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005741 const DexFile& dex_file)
5742 : field_(field),
5743 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005744 field_type_(field_type),
5745 is_volatile_(is_volatile),
5746 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005747 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005748 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005749
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005750 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005753 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005754 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005755 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005756 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005757
5758 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005759 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005761 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005762 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005763 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005764 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005765 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005766};
5767
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005768class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005769 public:
5770 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005771 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005773 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005774 bool is_volatile,
5775 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005776 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005777 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005778 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305779 : HExpression(kInstanceFieldGet,
5780 field_type,
5781 SideEffects::FieldReadOfType(field_type, is_volatile),
5782 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005783 field_info_(field,
5784 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005785 field_type,
5786 is_volatile,
5787 field_idx,
5788 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005789 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790 SetRawInputAt(0, value);
5791 }
5792
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005793 bool IsClonable() const override { return true; }
5794 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005796 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005797 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005798 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005799 }
5800
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005801 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005802 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005803 }
5804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005805 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005806 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5807 }
5808
Calin Juravle52c48962014-12-16 17:02:57 +00005809 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005810 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005811 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005812 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005813
Vladimir Marko61b92282017-10-11 13:23:17 +01005814 void SetType(DataType::Type new_type) {
5815 DCHECK(DataType::IsIntegralType(GetType()));
5816 DCHECK(DataType::IsIntegralType(new_type));
5817 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5818 SetPackedField<TypeField>(new_type);
5819 }
5820
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005821 DECLARE_INSTRUCTION(InstanceFieldGet);
5822
Artem Serovcced8ba2017-07-19 18:18:09 +01005823 protected:
5824 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5825
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826 private:
5827 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005828};
5829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005830class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005831 public:
5832 HInstanceFieldSet(HInstruction* object,
5833 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005834 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005836 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005837 bool is_volatile,
5838 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005839 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005840 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005841 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005842 : HExpression(kInstanceFieldSet,
5843 SideEffects::FieldWriteOfType(field_type, is_volatile),
5844 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005845 field_info_(field,
5846 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005847 field_type,
5848 is_volatile,
5849 field_idx,
5850 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005851 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005852 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005853 SetRawInputAt(0, object);
5854 SetRawInputAt(1, value);
5855 }
5856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005857 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005859 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005860 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005861 }
5862
Calin Juravle52c48962014-12-16 17:02:57 +00005863 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005864 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005865 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005866 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005867 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5869 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005870
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005871 DECLARE_INSTRUCTION(InstanceFieldSet);
5872
Artem Serovcced8ba2017-07-19 18:18:09 +01005873 protected:
5874 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5875
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005876 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005877 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5878 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5879 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5880 "Too many packed fields.");
5881
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005882 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005883};
5884
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005885class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005886 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005887 HArrayGet(HInstruction* array,
5888 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005889 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005890 uint32_t dex_pc)
5891 : HArrayGet(array,
5892 index,
5893 type,
5894 SideEffects::ArrayReadOfType(type),
5895 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305896 /* is_string_char_at */ false) {
5897 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005898
5899 HArrayGet(HInstruction* array,
5900 HInstruction* index,
5901 DataType::Type type,
5902 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005903 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005904 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305905 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005906 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005907 SetRawInputAt(0, array);
5908 SetRawInputAt(1, index);
5909 }
5910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005911 bool IsClonable() const override { return true; }
5912 bool CanBeMoved() const override { return true; }
5913 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005914 return true;
5915 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005916 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005917 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005918 // Currently, unless the array is the result of NewArray, the array access is always
5919 // preceded by some form of null NullCheck necessary for the bounds check, usually
5920 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5921 // dynamic BCE. There are cases when these could be removed to produce better code.
5922 // If we ever add optimizations to do so we should allow an implicit check here
5923 // (as long as the address falls in the first page).
5924 //
5925 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5926 // a = cond ? new int[1] : null;
5927 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005928 return false;
5929 }
5930
David Brazdil4833f5a2015-12-16 10:37:39 +00005931 bool IsEquivalentOf(HArrayGet* other) const {
5932 bool result = (GetDexPc() == other->GetDexPc());
5933 if (kIsDebugBuild && result) {
5934 DCHECK_EQ(GetBlock(), other->GetBlock());
5935 DCHECK_EQ(GetArray(), other->GetArray());
5936 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 if (DataType::IsIntOrLongType(GetType())) {
5938 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005939 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5941 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005942 }
5943 }
5944 return result;
5945 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005946
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005947 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5948
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005949 HInstruction* GetArray() const { return InputAt(0); }
5950 HInstruction* GetIndex() const { return InputAt(1); }
5951
Vladimir Marko61b92282017-10-11 13:23:17 +01005952 void SetType(DataType::Type new_type) {
5953 DCHECK(DataType::IsIntegralType(GetType()));
5954 DCHECK(DataType::IsIntegralType(new_type));
5955 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5956 SetPackedField<TypeField>(new_type);
5957 }
5958
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005959 DECLARE_INSTRUCTION(ArrayGet);
5960
Artem Serovcced8ba2017-07-19 18:18:09 +01005961 protected:
5962 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5963
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005964 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005965 // We treat a String as an array, creating the HArrayGet from String.charAt()
5966 // intrinsic in the instruction simplifier. We can always determine whether
5967 // a particular HArrayGet is actually a String.charAt() by looking at the type
5968 // of the input but that requires holding the mutator lock, so we prefer to use
5969 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005970 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005971 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5972 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5973 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005974};
5975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005976class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005977 public:
5978 HArraySet(HInstruction* array,
5979 HInstruction* index,
5980 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005981 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005982 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005983 : HArraySet(array,
5984 index,
5985 value,
5986 expected_component_type,
5987 // Make a best guess for side effects now, may be refined during SSA building.
5988 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305989 dex_pc) {
5990 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005991
5992 HArraySet(HInstruction* array,
5993 HInstruction* index,
5994 HInstruction* value,
5995 DataType::Type expected_component_type,
5996 SideEffects side_effects,
5997 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005998 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005999 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006000 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 SetPackedFlag<kFlagValueCanBeNull>(true);
6002 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006003 SetRawInputAt(0, array);
6004 SetRawInputAt(1, index);
6005 SetRawInputAt(2, value);
6006 }
6007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006008 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006009
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006010 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006011 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006013 }
6014
Mingyao Yang81014cb2015-06-02 03:16:27 -07006015 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006016 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006017
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006018 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006019 // TODO: Same as for ArrayGet.
6020 return false;
6021 }
6022
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006023 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006024 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006025 }
6026
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006027 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006028 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006029 }
6030
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006031 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006032 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006033 }
6034
Vladimir Markoa1de9182016-02-25 11:37:38 +00006035 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6036 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6037 bool StaticTypeOfArrayIsObjectArray() const {
6038 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6039 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006040
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006041 HInstruction* GetArray() const { return InputAt(0); }
6042 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006043 HInstruction* GetValue() const { return InputAt(2); }
6044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006045 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006046 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6047 }
6048
6049 static DataType::Type GetComponentType(DataType::Type value_type,
6050 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006051 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006052 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006053 // be correct, we also check what is the value type. If it is a floating
6054 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006055 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006056 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006057 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006058 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006060 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006061 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006062 }
6063
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006064 static SideEffects ComputeSideEffects(DataType::Type type) {
6065 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006066 }
6067
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006068 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6069 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6070 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006071 }
6072
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006073 DECLARE_INSTRUCTION(ArraySet);
6074
Artem Serovcced8ba2017-07-19 18:18:09 +01006075 protected:
6076 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6077
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006079 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6080 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006081 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006082 static constexpr size_t kFlagNeedsTypeCheck =
6083 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6084 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006085 // Cached information for the reference_type_info_ so that codegen
6086 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006087 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6088 static constexpr size_t kNumberOfArraySetPackedBits =
6089 kFlagStaticTypeOfArrayIsObjectArray + 1;
6090 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6091 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006092 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006093};
6094
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006095class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006096 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006097 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306098 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006099 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006100 // Note that arrays do not change length, so the instruction does not
6101 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006102 SetRawInputAt(0, array);
6103 }
6104
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006105 bool IsClonable() const override { return true; }
6106 bool CanBeMoved() const override { return true; }
6107 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006108 return true;
6109 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006111 return obj == InputAt(0);
6112 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006113
Vladimir Markodce016e2016-04-28 13:10:02 +01006114 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6115
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006116 DECLARE_INSTRUCTION(ArrayLength);
6117
Artem Serovcced8ba2017-07-19 18:18:09 +01006118 protected:
6119 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6120
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006121 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006122 // We treat a String as an array, creating the HArrayLength from String.length()
6123 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6124 // determine whether a particular HArrayLength is actually a String.length() by
6125 // looking at the type of the input but that requires holding the mutator lock, so
6126 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006127 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006128 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6129 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6130 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006131};
6132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006133class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006134 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006135 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006136 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6137 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006138 HBoundsCheck(HInstruction* index,
6139 HInstruction* length,
6140 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006141 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006142 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006144 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006145 SetRawInputAt(0, index);
6146 SetRawInputAt(1, length);
6147 }
6148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006149 bool IsClonable() const override { return true; }
6150 bool CanBeMoved() const override { return true; }
6151 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006152 return true;
6153 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006155 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006156
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006157 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006158
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006159 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006160
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006161 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006162
6163 DECLARE_INSTRUCTION(BoundsCheck);
6164
Artem Serovcced8ba2017-07-19 18:18:09 +01006165 protected:
6166 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6167
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006168 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006169 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006170 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6171 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6172 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006173};
6174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006175class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006176 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006177 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006178 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306179 slow_path_(nullptr) {
6180 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006182 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006183
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006184 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006185 return true;
6186 }
6187
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006188 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6189 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006190
6191 DECLARE_INSTRUCTION(SuspendCheck);
6192
Artem Serovcced8ba2017-07-19 18:18:09 +01006193 protected:
6194 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6195
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006196 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006197 // Only used for code generation, in order to share the same slow path between back edges
6198 // of a same loop.
6199 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006200};
6201
David Srbecky0cf44932015-12-09 14:09:59 +00006202// Pseudo-instruction which provides the native debugger with mapping information.
6203// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006204class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006205 public:
6206 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006207 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306208 }
David Srbecky0cf44932015-12-09 14:09:59 +00006209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006210 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006211 return true;
6212 }
6213
6214 DECLARE_INSTRUCTION(NativeDebugInfo);
6215
Artem Serovcced8ba2017-07-19 18:18:09 +01006216 protected:
6217 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006218};
6219
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006220/**
6221 * Instruction to load a Class object.
6222 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006223class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006224 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006225 // Determines how to load the Class.
6226 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006227 // We cannot load this class. See HSharpening::SharpenLoadClass.
6228 kInvalid = -1,
6229
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006230 // Use the Class* from the method's own ArtMethod*.
6231 kReferrersClass,
6232
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006233 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006234 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006235 kBootImageLinkTimePcRelative,
6236
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006237 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006238 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006239 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006240
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006241 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006242 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006243 kBssEntry,
6244
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006245 // Use a known boot image Class* address, embedded in the code by the codegen.
6246 // Used for boot image classes referenced by apps in JIT-compiled code.
6247 kJitBootImageAddress,
6248
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006249 // Load from the root table associated with the JIT compiled method.
6250 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006251
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006252 // Load using a simple runtime call. This is the fall-back load kind when
6253 // the codegen is unable to use another appropriate kind.
6254 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006255
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006256 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006257 };
6258
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006259 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006260 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006261 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006262 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006263 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006264 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006265 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006266 : HInstruction(kLoadClass,
6267 DataType::Type::kReference,
6268 SideEffectsForArchRuntimeCalls(),
6269 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006270 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006271 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006272 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006273 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006274 // Referrers class should not need access check. We never inline unverified
6275 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 DCHECK(!is_referrers_class || !needs_access_check);
6277
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006278 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006279 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006281 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006282 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006283 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006284 }
6285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006286 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006287
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006288 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006289
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006290 LoadKind GetLoadKind() const {
6291 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006292 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006293
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006294 bool HasPcRelativeLoadKind() const {
6295 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6296 GetLoadKind() == LoadKind::kBootImageRelRo ||
6297 GetLoadKind() == LoadKind::kBssEntry;
6298 }
6299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006300 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006301
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006302 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006303
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006304 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006305
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006306 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006307
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006308 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006309 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006310 }
6311
Calin Juravle0ba218d2015-05-19 18:46:01 +01006312 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006313 // The entrypoint the code generator is going to call does not do
6314 // clinit of the class.
6315 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006316 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006317 }
6318
6319 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006320 return NeedsAccessCheck() ||
6321 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006322 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006323 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006324 }
6325
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006326 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006327 return NeedsAccessCheck() ||
6328 MustGenerateClinitCheck() ||
6329 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006330 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006331 GetLoadKind() == LoadKind::kBssEntry) &&
6332 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006333 }
6334
Calin Juravleacf735c2015-02-12 15:25:22 +00006335 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006336 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6337 // Note: The is_exact flag from the return value should not be used.
6338 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6339 } else {
6340 return ReferenceTypeInfo::CreateInvalid();
6341 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006342 }
6343
Vladimir Marko175e7862018-03-27 09:03:13 +00006344 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6345 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6346 DCHECK(klass_ != nullptr);
6347 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006348 }
6349
Andreas Gampea5b09a62016-11-17 15:21:22 -08006350 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006351 const DexFile& GetDexFile() const { return dex_file_; }
6352
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006353 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006354 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006355 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006356
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006357 static SideEffects SideEffectsForArchRuntimeCalls() {
6358 return SideEffects::CanTriggerGC();
6359 }
6360
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006361 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006363 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006365
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006366 bool MustResolveTypeOnSlowPath() const {
6367 // Check that this instruction has a slow path.
6368 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6369 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6370 return GetLoadKind() == LoadKind::kBssEntry;
6371 }
6372
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006373 void MarkInBootImage() {
6374 SetPackedFlag<kFlagIsInBootImage>(true);
6375 }
6376
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006377 void AddSpecialInput(HInstruction* special_input);
6378
6379 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006380 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006381 return ArrayRef<HUserRecord<HInstruction*>>(
6382 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6383 }
6384
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006385 Handle<mirror::Class> GetClass() const {
6386 return klass_;
6387 }
6388
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006389 DECLARE_INSTRUCTION(LoadClass);
6390
Artem Serovcced8ba2017-07-19 18:18:09 +01006391 protected:
6392 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6393
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006394 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006395 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006396 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006397 // Whether this instruction must generate the initialization check.
6398 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006399 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006400 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6401 static constexpr size_t kFieldLoadKindSize =
6402 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006403 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6404 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6407
6408 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006409 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006410 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006411 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006412 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006413 }
6414
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006415 void SetLoadKindInternal(LoadKind load_kind);
6416
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006417 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006418 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006419 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006420 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006422 // A type index and dex file where the class can be accessed. The dex file can be:
6423 // - The compiling method's dex file if the class is defined there too.
6424 // - The compiling method's dex file if the class is referenced there.
6425 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006426 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006427 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006428 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006429
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006430 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006431};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006432std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6433
6434// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006435inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6436 // The load kind should be determined before inserting the instruction to the graph.
6437 DCHECK(GetBlock() == nullptr);
6438 DCHECK(GetEnvironment() == nullptr);
6439 SetPackedField<LoadKindField>(load_kind);
6440 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6441 special_input_ = HUserRecord<HInstruction*>(nullptr);
6442 }
6443 if (!NeedsEnvironment()) {
6444 SetSideEffects(SideEffects::None());
6445 }
6446}
6447
6448// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006449inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006450 // The special input is used for PC-relative loads on some architectures,
6451 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006453 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006454 GetLoadKind() == LoadKind::kBssEntry ||
6455 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006456 DCHECK(special_input_.GetInstruction() == nullptr);
6457 special_input_ = HUserRecord<HInstruction*>(special_input);
6458 special_input->AddUseAt(this, 0);
6459}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006461class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006462 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006463 // Determines how to load the String.
6464 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006465 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006466 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006467 kBootImageLinkTimePcRelative,
6468
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006469 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006470 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006471 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006472
Vladimir Markoaad75c62016-10-03 08:46:48 +00006473 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006474 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006475 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006476
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006477 // Use a known boot image String* address, embedded in the code by the codegen.
6478 // Used for boot image strings referenced by apps in JIT-compiled code.
6479 kJitBootImageAddress,
6480
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006481 // Load from the root table associated with the JIT compiled method.
6482 kJitTableAddress,
6483
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006484 // Load using a simple runtime call. This is the fall-back load kind when
6485 // the codegen is unable to use another appropriate kind.
6486 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006487
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006488 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006489 };
6490
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006491 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006492 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006493 const DexFile& dex_file,
6494 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006495 : HInstruction(kLoadString,
6496 DataType::Type::kReference,
6497 SideEffectsForArchRuntimeCalls(),
6498 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006499 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006500 string_index_(string_index),
6501 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006502 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006503 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006505 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006506
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006507 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006508
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006509 LoadKind GetLoadKind() const {
6510 return GetPackedField<LoadKindField>();
6511 }
6512
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006513 bool HasPcRelativeLoadKind() const {
6514 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6515 GetLoadKind() == LoadKind::kBootImageRelRo ||
6516 GetLoadKind() == LoadKind::kBssEntry;
6517 }
6518
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006519 const DexFile& GetDexFile() const {
6520 return dex_file_;
6521 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006522
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006523 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524 return string_index_;
6525 }
6526
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006527 Handle<mirror::String> GetString() const {
6528 return string_;
6529 }
6530
6531 void SetString(Handle<mirror::String> str) {
6532 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006533 }
6534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006535 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006536
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006537 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006538
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006539 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006540
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006541 // Will call the runtime if we need to load the string through
6542 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006543 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006544 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006545 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006546 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006547 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006548 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006549 return false;
6550 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006551 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006552 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006554 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006555 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006556 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006557
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006558 bool CanBeNull() const override { return false; }
6559 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006560
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006561 static SideEffects SideEffectsForArchRuntimeCalls() {
6562 return SideEffects::CanTriggerGC();
6563 }
6564
Vladimir Marko372f10e2016-05-17 16:30:10 +01006565 void AddSpecialInput(HInstruction* special_input);
6566
6567 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006568 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006569 return ArrayRef<HUserRecord<HInstruction*>>(
6570 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006571 }
6572
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006573 DECLARE_INSTRUCTION(LoadString);
6574
Artem Serovcced8ba2017-07-19 18:18:09 +01006575 protected:
6576 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6577
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006578 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006579 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006580 static constexpr size_t kFieldLoadKindSize =
6581 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6582 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006583 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006584 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006585
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586 void SetLoadKindInternal(LoadKind load_kind);
6587
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006588 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006589 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006590 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006591 HUserRecord<HInstruction*> special_input_;
6592
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006593 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006594 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006595
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006596 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006597};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006598std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6599
6600// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006601inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6602 // The load kind should be determined before inserting the instruction to the graph.
6603 DCHECK(GetBlock() == nullptr);
6604 DCHECK(GetEnvironment() == nullptr);
6605 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6606 SetPackedField<LoadKindField>(load_kind);
6607 if (load_kind != LoadKind::kRuntimeCall) {
6608 special_input_ = HUserRecord<HInstruction*>(nullptr);
6609 }
6610 if (!NeedsEnvironment()) {
6611 SetSideEffects(SideEffects::None());
6612 }
6613}
6614
6615// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006616inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006617 // The special input is used for PC-relative loads on some architectures,
6618 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006619 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006620 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006621 GetLoadKind() == LoadKind::kBssEntry ||
6622 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006623 // HLoadString::GetInputRecords() returns an empty array at this point,
6624 // so use the GetInputRecords() from the base class to set the input record.
6625 DCHECK(special_input_.GetInstruction() == nullptr);
6626 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006627 special_input->AddUseAt(this, 0);
6628}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006629
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006630class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006631 public:
6632 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006633 uint16_t method_handle_idx,
6634 const DexFile& dex_file,
6635 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006636 : HInstruction(kLoadMethodHandle,
6637 DataType::Type::kReference,
6638 SideEffectsForArchRuntimeCalls(),
6639 dex_pc),
6640 special_input_(HUserRecord<HInstruction*>(current_method)),
6641 method_handle_idx_(method_handle_idx),
6642 dex_file_(dex_file) {
6643 }
6644
6645 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006646 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006647 return ArrayRef<HUserRecord<HInstruction*>>(
6648 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6649 }
6650
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006651 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006652
6653 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6654
6655 const DexFile& GetDexFile() const { return dex_file_; }
6656
6657 static SideEffects SideEffectsForArchRuntimeCalls() {
6658 return SideEffects::CanTriggerGC();
6659 }
6660
6661 DECLARE_INSTRUCTION(LoadMethodHandle);
6662
6663 protected:
6664 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6665
6666 private:
6667 // The special input is the HCurrentMethod for kRuntimeCall.
6668 HUserRecord<HInstruction*> special_input_;
6669
6670 const uint16_t method_handle_idx_;
6671 const DexFile& dex_file_;
6672};
6673
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006674class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006675 public:
6676 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006677 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006678 const DexFile& dex_file,
6679 uint32_t dex_pc)
6680 : HInstruction(kLoadMethodType,
6681 DataType::Type::kReference,
6682 SideEffectsForArchRuntimeCalls(),
6683 dex_pc),
6684 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006685 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006686 dex_file_(dex_file) {
6687 }
6688
6689 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006690 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006691 return ArrayRef<HUserRecord<HInstruction*>>(
6692 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6693 }
6694
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006695 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006696
Orion Hodson06d10a72018-05-14 08:53:38 +01006697 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006698
6699 const DexFile& GetDexFile() const { return dex_file_; }
6700
6701 static SideEffects SideEffectsForArchRuntimeCalls() {
6702 return SideEffects::CanTriggerGC();
6703 }
6704
6705 DECLARE_INSTRUCTION(LoadMethodType);
6706
6707 protected:
6708 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6709
6710 private:
6711 // The special input is the HCurrentMethod for kRuntimeCall.
6712 HUserRecord<HInstruction*> special_input_;
6713
Orion Hodson06d10a72018-05-14 08:53:38 +01006714 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006715 const DexFile& dex_file_;
6716};
6717
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006718/**
6719 * Performs an initialization check on its Class object input.
6720 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006721class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006722 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006723 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006724 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306725 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006726 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006727 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006728 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006729 SetRawInputAt(0, constant);
6730 }
Artem Serova6e26142018-06-19 14:55:17 +01006731 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006732 bool CanBeMoved() const override { return true; }
6733 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006734 return true;
6735 }
6736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006737 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006738 // May call runtime to initialize the class.
6739 return true;
6740 }
6741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006742 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006743
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006744 HLoadClass* GetLoadClass() const {
6745 DCHECK(InputAt(0)->IsLoadClass());
6746 return InputAt(0)->AsLoadClass();
6747 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006748
6749 DECLARE_INSTRUCTION(ClinitCheck);
6750
Artem Serovcced8ba2017-07-19 18:18:09 +01006751
6752 protected:
6753 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006754};
6755
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006756class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006757 public:
6758 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006759 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006760 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006761 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006762 bool is_volatile,
6763 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006764 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006765 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006766 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306767 : HExpression(kStaticFieldGet,
6768 field_type,
6769 SideEffects::FieldReadOfType(field_type, is_volatile),
6770 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006771 field_info_(field,
6772 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006773 field_type,
6774 is_volatile,
6775 field_idx,
6776 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006777 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006778 SetRawInputAt(0, cls);
6779 }
6780
Calin Juravle52c48962014-12-16 17:02:57 +00006781
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006782 bool IsClonable() const override { return true; }
6783 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006784
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006785 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006786 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006787 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006788 }
6789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006790 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006791 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6792 }
6793
Calin Juravle52c48962014-12-16 17:02:57 +00006794 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006795 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006796 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006797 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006798
Vladimir Marko61b92282017-10-11 13:23:17 +01006799 void SetType(DataType::Type new_type) {
6800 DCHECK(DataType::IsIntegralType(GetType()));
6801 DCHECK(DataType::IsIntegralType(new_type));
6802 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6803 SetPackedField<TypeField>(new_type);
6804 }
6805
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006806 DECLARE_INSTRUCTION(StaticFieldGet);
6807
Artem Serovcced8ba2017-07-19 18:18:09 +01006808 protected:
6809 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6810
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006811 private:
6812 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006813};
6814
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006815class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006816 public:
6817 HStaticFieldSet(HInstruction* cls,
6818 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006819 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006820 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006821 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006822 bool is_volatile,
6823 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006824 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006825 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006826 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006827 : HExpression(kStaticFieldSet,
6828 SideEffects::FieldWriteOfType(field_type, is_volatile),
6829 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006830 field_info_(field,
6831 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006832 field_type,
6833 is_volatile,
6834 field_idx,
6835 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006836 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006837 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006838 SetRawInputAt(0, cls);
6839 SetRawInputAt(1, value);
6840 }
6841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006842 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006843 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006844 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006845 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006846 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006847
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006848 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006849 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6850 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006851
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006852 DECLARE_INSTRUCTION(StaticFieldSet);
6853
Artem Serovcced8ba2017-07-19 18:18:09 +01006854 protected:
6855 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6856
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006857 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006858 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6859 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6860 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6861 "Too many packed fields.");
6862
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006863 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006864};
6865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006866class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 public:
6868 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006869 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006870 uint32_t field_index,
6871 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306872 : HExpression(kUnresolvedInstanceFieldGet,
6873 field_type,
6874 SideEffects::AllExceptGCDependency(),
6875 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006876 field_index_(field_index) {
6877 SetRawInputAt(0, obj);
6878 }
6879
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006880 bool IsClonable() const override { return true; }
6881 bool NeedsEnvironment() const override { return true; }
6882 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006884 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006885 uint32_t GetFieldIndex() const { return field_index_; }
6886
6887 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6888
Artem Serovcced8ba2017-07-19 18:18:09 +01006889 protected:
6890 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6891
Calin Juravlee460d1d2015-09-29 04:52:17 +01006892 private:
6893 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006894};
6895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006896class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006897 public:
6898 HUnresolvedInstanceFieldSet(HInstruction* obj,
6899 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006900 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006901 uint32_t field_index,
6902 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006903 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006904 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006905 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006906 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006907 SetRawInputAt(0, obj);
6908 SetRawInputAt(1, value);
6909 }
6910
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006911 bool IsClonable() const override { return true; }
6912 bool NeedsEnvironment() const override { return true; }
6913 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006914
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006915 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006916 uint32_t GetFieldIndex() const { return field_index_; }
6917
6918 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6919
Artem Serovcced8ba2017-07-19 18:18:09 +01006920 protected:
6921 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6922
Calin Juravlee460d1d2015-09-29 04:52:17 +01006923 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006924 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6925 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006926 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006927 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6928 kFieldFieldType + kFieldFieldTypeSize;
6929 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6930 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006931 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006932
Calin Juravlee460d1d2015-09-29 04:52:17 +01006933 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006934};
6935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006936class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006937 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006938 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006939 uint32_t field_index,
6940 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306941 : HExpression(kUnresolvedStaticFieldGet,
6942 field_type,
6943 SideEffects::AllExceptGCDependency(),
6944 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006945 field_index_(field_index) {
6946 }
6947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006948 bool IsClonable() const override { return true; }
6949 bool NeedsEnvironment() const override { return true; }
6950 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006952 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006953 uint32_t GetFieldIndex() const { return field_index_; }
6954
6955 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6956
Artem Serovcced8ba2017-07-19 18:18:09 +01006957 protected:
6958 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6959
Calin Juravlee460d1d2015-09-29 04:52:17 +01006960 private:
6961 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006962};
6963
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006964class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 public:
6966 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006967 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006968 uint32_t field_index,
6969 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006970 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006972 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006973 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006974 SetRawInputAt(0, value);
6975 }
6976
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006977 bool IsClonable() const override { return true; }
6978 bool NeedsEnvironment() const override { return true; }
6979 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006981 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006982 uint32_t GetFieldIndex() const { return field_index_; }
6983
6984 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6985
Artem Serovcced8ba2017-07-19 18:18:09 +01006986 protected:
6987 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6988
Calin Juravlee460d1d2015-09-29 04:52:17 +01006989 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006990 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6991 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006992 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006993 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6994 kFieldFieldType + kFieldFieldTypeSize;
6995 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6996 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006997 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006998
Calin Juravlee460d1d2015-09-29 04:52:17 +01006999 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01007000};
7001
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007002// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007003class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007004 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007005 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307006 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
7007 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007009 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007010
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007011 DECLARE_INSTRUCTION(LoadException);
7012
Artem Serovcced8ba2017-07-19 18:18:09 +01007013 protected:
7014 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007015};
7016
David Brazdilcb1c0552015-08-04 16:22:25 +01007017// Implicit part of move-exception which clears thread-local exception storage.
7018// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007019class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007020 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007021 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007022 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307023 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007024
7025 DECLARE_INSTRUCTION(ClearException);
7026
Artem Serovcced8ba2017-07-19 18:18:09 +01007027 protected:
7028 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007029};
7030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007031class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007032 public:
7033 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007034 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007035 SetRawInputAt(0, exception);
7036 }
7037
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007038 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007039
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007040 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007041
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007042 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007043
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007044 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007045
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007046 DECLARE_INSTRUCTION(Throw);
7047
Artem Serovcced8ba2017-07-19 18:18:09 +01007048 protected:
7049 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007050};
7051
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007052/**
7053 * Implementation strategies for the code generator of a HInstanceOf
7054 * or `HCheckCast`.
7055 */
7056enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007057 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007058 kExactCheck, // Can do a single class compare.
7059 kClassHierarchyCheck, // Can just walk the super class chain.
7060 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7061 kInterfaceCheck, // No optimization yet when checking against an interface.
7062 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007063 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007064 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007065 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007066};
7067
Roland Levillain86503782016-02-11 19:07:30 +00007068std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7069
Vladimir Marko175e7862018-03-27 09:03:13 +00007070// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7071// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7072class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007073 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007074 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007075 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007076 HInstruction* object,
7077 HInstruction* target_class_or_null,
7078 TypeCheckKind check_kind,
7079 Handle<mirror::Class> klass,
7080 uint32_t dex_pc,
7081 ArenaAllocator* allocator,
7082 HIntConstant* bitstring_path_to_root,
7083 HIntConstant* bitstring_mask,
7084 SideEffects side_effects)
7085 : HVariableInputSizeInstruction(
7086 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007087 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007088 side_effects,
7089 dex_pc,
7090 allocator,
7091 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7092 kArenaAllocTypeCheckInputs),
7093 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007094 SetPackedField<TypeCheckKindField>(check_kind);
7095 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007096 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007097 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007098 SetRawInputAt(1, target_class_or_null);
7099 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7100 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7101 if (check_kind == TypeCheckKind::kBitstringCheck) {
7102 DCHECK(target_class_or_null->IsNullConstant());
7103 SetRawInputAt(2, bitstring_path_to_root);
7104 SetRawInputAt(3, bitstring_mask);
7105 } else {
7106 DCHECK(target_class_or_null->IsLoadClass());
7107 }
Vladimir Marko87584542017-12-12 17:47:52 +00007108 }
7109
7110 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007111 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007112 HInstruction* load_class = InputAt(1);
7113 DCHECK(load_class->IsLoadClass());
7114 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007115 }
7116
Vladimir Marko175e7862018-03-27 09:03:13 +00007117 uint32_t GetBitstringPathToRoot() const {
7118 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7119 HInstruction* path_to_root = InputAt(2);
7120 DCHECK(path_to_root->IsIntConstant());
7121 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7122 }
7123
7124 uint32_t GetBitstringMask() const {
7125 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7126 HInstruction* mask = InputAt(3);
7127 DCHECK(mask->IsIntConstant());
7128 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7129 }
7130
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007131 bool IsClonable() const override { return true; }
7132 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007134 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007135 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7136 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007137 }
7138
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007139 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7140 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7141 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7142 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7143
Vladimir Marko175e7862018-03-27 09:03:13 +00007144 ReferenceTypeInfo GetTargetClassRTI() {
7145 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7146 // Note: The is_exact flag from the return value should not be used.
7147 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7148 } else {
7149 return ReferenceTypeInfo::CreateInvalid();
7150 }
7151 }
7152
7153 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7154 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7155 DCHECK(klass_ != nullptr);
7156 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7157 }
7158
7159 Handle<mirror::Class> GetClass() const {
7160 return klass_;
7161 }
7162
7163 protected:
7164 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7165
7166 private:
7167 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7168 static constexpr size_t kFieldTypeCheckKindSize =
7169 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7170 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7171 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7172 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7173 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7174 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7175
7176 Handle<mirror::Class> klass_;
7177};
7178
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007179class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007180 public:
7181 HInstanceOf(HInstruction* object,
7182 HInstruction* target_class_or_null,
7183 TypeCheckKind check_kind,
7184 Handle<mirror::Class> klass,
7185 uint32_t dex_pc,
7186 ArenaAllocator* allocator,
7187 HIntConstant* bitstring_path_to_root,
7188 HIntConstant* bitstring_mask)
7189 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007190 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007191 object,
7192 target_class_or_null,
7193 check_kind,
7194 klass,
7195 dex_pc,
7196 allocator,
7197 bitstring_path_to_root,
7198 bitstring_mask,
7199 SideEffectsForArchRuntimeCalls(check_kind)) {}
7200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007201 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007202
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007203 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007204 return CanCallRuntime(GetTypeCheckKind());
7205 }
7206
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007207 static bool CanCallRuntime(TypeCheckKind check_kind) {
7208 // Mips currently does runtime calls for any other checks.
7209 return check_kind != TypeCheckKind::kExactCheck;
7210 }
7211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007212 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007213 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007214 }
7215
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007216 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007217
Artem Serovcced8ba2017-07-19 18:18:09 +01007218 protected:
7219 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007220};
7221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007222class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007223 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007224 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307225 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007226 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7227 SetPackedFlag<kFlagUpperCanBeNull>(true);
7228 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007229 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007230 SetRawInputAt(0, input);
7231 }
7232
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007233 bool InstructionDataEquals(const HInstruction* other) const override;
7234 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007235
David Brazdilf5552582015-12-27 13:36:12 +00007236 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007237 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007238 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007239 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007240
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007241 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007242 DCHECK(GetUpperCanBeNull() || !can_be_null);
7243 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007244 }
7245
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007246 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007247
Calin Juravleb1498f62015-02-16 13:13:29 +00007248 DECLARE_INSTRUCTION(BoundType);
7249
Artem Serovcced8ba2017-07-19 18:18:09 +01007250 protected:
7251 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7252
Calin Juravleb1498f62015-02-16 13:13:29 +00007253 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007254 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7255 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007256 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007257 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7258 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7259 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7260
Calin Juravleb1498f62015-02-16 13:13:29 +00007261 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007262 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7263 // It is used to bound the type in cases like:
7264 // if (x instanceof ClassX) {
7265 // // uper_bound_ will be ClassX
7266 // }
David Brazdilf5552582015-12-27 13:36:12 +00007267 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007268};
7269
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007270class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007271 public:
7272 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007273 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007274 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007275 Handle<mirror::Class> klass,
7276 uint32_t dex_pc,
7277 ArenaAllocator* allocator,
7278 HIntConstant* bitstring_path_to_root,
7279 HIntConstant* bitstring_mask)
7280 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007281 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007282 object,
7283 target_class_or_null,
7284 check_kind,
7285 klass,
7286 dex_pc,
7287 allocator,
7288 bitstring_path_to_root,
7289 bitstring_mask,
7290 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007291
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007292 bool IsClonable() const override { return true; }
7293 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007294 // Instruction may throw a CheckCastError.
7295 return true;
7296 }
7297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007298 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007299
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007300 DECLARE_INSTRUCTION(CheckCast);
7301
Artem Serovcced8ba2017-07-19 18:18:09 +01007302 protected:
7303 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007304};
7305
Andreas Gampe26de38b2016-07-27 17:53:11 -07007306/**
7307 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7308 * @details We define the combined barrier types that are actually required
7309 * by the Java Memory Model, rather than using exactly the terminology from
7310 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7311 * primitives. Note that the JSR-133 cookbook generally does not deal with
7312 * store atomicity issues, and the recipes there are not always entirely sufficient.
7313 * The current recipe is as follows:
7314 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7315 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7316 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7317 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7318 * class has final fields.
7319 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7320 * store-to-memory instructions. Only generated together with non-temporal stores.
7321 */
7322enum MemBarrierKind {
7323 kAnyStore,
7324 kLoadAny,
7325 kStoreStore,
7326 kAnyAny,
7327 kNTStoreStore,
7328 kLastBarrierKind = kNTStoreStore
7329};
7330std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007332class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007333 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007334 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007335 : HExpression(kMemoryBarrier,
7336 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7337 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007338 SetPackedField<BarrierKindField>(barrier_kind);
7339 }
Calin Juravle27df7582015-04-17 19:12:31 +01007340
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007341 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007342
Vladimir Markoa1de9182016-02-25 11:37:38 +00007343 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007344
7345 DECLARE_INSTRUCTION(MemoryBarrier);
7346
Artem Serovcced8ba2017-07-19 18:18:09 +01007347 protected:
7348 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7349
Calin Juravle27df7582015-04-17 19:12:31 +01007350 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007351 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7352 static constexpr size_t kFieldBarrierKindSize =
7353 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7354 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7355 kFieldBarrierKind + kFieldBarrierKindSize;
7356 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7357 "Too many packed fields.");
7358 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007359};
7360
Igor Murashkind01745e2017-04-05 16:40:31 -07007361// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7362// the specified object(s), with respect to any subsequent store that might "publish"
7363// (i.e. make visible) the specified object to another thread.
7364//
7365// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7366// for all final fields (that were set) at the end of the invoked constructor.
7367//
7368// The constructor fence models the freeze actions for the final fields of an object
7369// being constructed (semantically at the end of the constructor). Constructor fences
7370// have a per-object affinity; two separate objects being constructed get two separate
7371// constructor fences.
7372//
7373// (Note: that if calling a super-constructor or forwarding to another constructor,
7374// the freezes would happen at the end of *that* constructor being invoked).
7375//
7376// The memory model guarantees that when the object being constructed is "published" after
7377// constructor completion (i.e. escapes the current thread via a store), then any final field
7378// writes must be observable on other threads (once they observe that publication).
7379//
7380// Further, anything written before the freeze, and read by dereferencing through the final field,
7381// must also be visible (so final object field could itself have an object with non-final fields;
7382// yet the freeze must also extend to them).
7383//
7384// Constructor example:
7385//
7386// class HasFinal {
7387// final int field; Optimizing IR for <init>()V:
7388// HasFinal() {
7389// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7390// // freeze(this.field); HConstructorFence(this)
7391// } HReturn
7392// }
7393//
7394// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7395// already-initialized classes; in that case the allocation must act as a "default-initializer"
7396// of the object which effectively writes the class pointer "final field".
7397//
7398// For example, we can model default-initialiation as roughly the equivalent of the following:
7399//
7400// class Object {
7401// private final Class header;
7402// }
7403//
7404// Java code: Optimizing IR:
7405//
7406// T new_instance<T>() {
7407// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7408// obj.header = T.class; // header write is done by above call.
7409// // freeze(obj.header) HConstructorFence(obj)
7410// return (T)obj;
7411// }
7412//
7413// See also:
Vladimir Markoc1c34522018-10-31 13:56:49 +00007414// * DexCompilationUnit::RequiresConstructorBarrier
Igor Murashkind01745e2017-04-05 16:40:31 -07007415// * QuasiAtomic::ThreadFenceForConstructor
7416//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007417class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007418 // A fence has variable inputs because the inputs can be removed
7419 // after prepare_for_register_allocation phase.
7420 // (TODO: In the future a fence could freeze multiple objects
7421 // after merging two fences together.)
7422 public:
7423 // `fence_object` is the reference that needs to be protected for correct publication.
7424 //
7425 // It makes sense in the following situations:
7426 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7427 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7428 //
7429 // After construction the `fence_object` becomes the 0th input.
7430 // This is not an input in a real sense, but just a convenient place to stash the information
7431 // about the associated object.
7432 HConstructorFence(HInstruction* fence_object,
7433 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007434 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007435 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7436 // constraints described in the class header. We claim that these SideEffects constraints
7437 // enforce a superset of the real constraints.
7438 //
7439 // The ordering described above is conservatively modeled with SideEffects as follows:
7440 //
7441 // * To prevent reordering of the publication stores:
7442 // ----> "Reads of objects" is the initial SideEffect.
7443 // * For every primitive final field store in the constructor:
7444 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7445 // * If there are any stores to reference final fields in the constructor:
7446 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7447 // that are reachable from `fence_object` also need to be prevented for reordering
7448 // (and we do not want to do alias analysis to figure out what those stores are).
7449 //
7450 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7451 // even more conservative approach than the one described above, and prevents all of the
7452 // above reordering without analyzing any of the instructions in the constructor.
7453 //
7454 // If in a later phase we discover that there are no writes to reference final fields,
7455 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307456 : HVariableInputSizeInstruction(kConstructorFence,
7457 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007458 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007459 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007460 /* number_of_inputs */ 1,
7461 kArenaAllocConstructorFenceInputs) {
7462 DCHECK(fence_object != nullptr);
7463 SetRawInputAt(0, fence_object);
7464 }
7465
7466 // The object associated with this constructor fence.
7467 //
7468 // (Note: This will be null after the prepare_for_register_allocation phase,
7469 // as all constructor fence inputs are removed there).
7470 HInstruction* GetFenceObject() const {
7471 return InputAt(0);
7472 }
7473
7474 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7475 // - Delete `fence_use` from `this`'s use list.
7476 // - Delete `this` from `fence_use`'s inputs list.
7477 // - If the `fence_use` is dead, remove it from the graph.
7478 //
7479 // A fence is considered dead once it no longer has any uses
7480 // and all of the inputs are dead.
7481 //
7482 // This must *not* be called during/after prepare_for_register_allocation,
7483 // because that removes all the inputs to the fences but the fence is actually
7484 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007485 //
7486 // Returns how many HConstructorFence instructions were removed from graph.
7487 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007488
Igor Murashkindd018df2017-08-09 10:38:31 -07007489 // Combine all inputs of `this` and `other` instruction and remove
7490 // `other` from the graph.
7491 //
7492 // Inputs are unique after the merge.
7493 //
7494 // Requirement: `this` must not be the same as `other.
7495 void Merge(HConstructorFence* other);
7496
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007497 // Check if this constructor fence is protecting
7498 // an HNewInstance or HNewArray that is also the immediate
7499 // predecessor of `this`.
7500 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007501 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7502 // to be one of the inputs of `this`.
7503 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007504 // Returns the associated HNewArray or HNewInstance,
7505 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007506 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007507
Igor Murashkind01745e2017-04-05 16:40:31 -07007508 DECLARE_INSTRUCTION(ConstructorFence);
7509
Artem Serovcced8ba2017-07-19 18:18:09 +01007510 protected:
7511 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007512};
7513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007514class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007515 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007516 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007517 kEnter,
7518 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007519 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007520 };
7521
7522 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007523 : HExpression(kMonitorOperation,
7524 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7525 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007526 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007527 SetRawInputAt(0, object);
7528 }
7529
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007530 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007531 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007532
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007533 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007534 // Verifier guarantees that monitor-exit cannot throw.
7535 // This is important because it allows the HGraphBuilder to remove
7536 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7537 return IsEnter();
7538 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007539
Vladimir Markoa1de9182016-02-25 11:37:38 +00007540 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7541 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007542
7543 DECLARE_INSTRUCTION(MonitorOperation);
7544
Artem Serovcced8ba2017-07-19 18:18:09 +01007545 protected:
7546 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7547
Calin Juravle52c48962014-12-16 17:02:57 +00007548 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007549 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7550 static constexpr size_t kFieldOperationKindSize =
7551 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7552 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7553 kFieldOperationKind + kFieldOperationKindSize;
7554 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7555 "Too many packed fields.");
7556 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007557};
7558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007559class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007560 public:
7561 HSelect(HInstruction* condition,
7562 HInstruction* true_value,
7563 HInstruction* false_value,
7564 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307565 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007566 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7567
7568 // First input must be `true_value` or `false_value` to allow codegens to
7569 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7570 // that architectures which implement HSelect as a conditional move also
7571 // will not need to invert the condition.
7572 SetRawInputAt(0, false_value);
7573 SetRawInputAt(1, true_value);
7574 SetRawInputAt(2, condition);
7575 }
7576
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007577 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007578 HInstruction* GetFalseValue() const { return InputAt(0); }
7579 HInstruction* GetTrueValue() const { return InputAt(1); }
7580 HInstruction* GetCondition() const { return InputAt(2); }
7581
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007582 bool CanBeMoved() const override { return true; }
7583 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007584 return true;
7585 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007586
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007587 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007588 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7589 }
7590
7591 DECLARE_INSTRUCTION(Select);
7592
Artem Serovcced8ba2017-07-19 18:18:09 +01007593 protected:
7594 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007595};
7596
Vladimir Markof9f64412015-09-02 14:05:49 +01007597class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007598 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007599 MoveOperands(Location source,
7600 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007601 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007602 HInstruction* instruction)
7603 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007604
7605 Location GetSource() const { return source_; }
7606 Location GetDestination() const { return destination_; }
7607
7608 void SetSource(Location value) { source_ = value; }
7609 void SetDestination(Location value) { destination_ = value; }
7610
7611 // The parallel move resolver marks moves as "in-progress" by clearing the
7612 // destination (but not the source).
7613 Location MarkPending() {
7614 DCHECK(!IsPending());
7615 Location dest = destination_;
7616 destination_ = Location::NoLocation();
7617 return dest;
7618 }
7619
7620 void ClearPending(Location dest) {
7621 DCHECK(IsPending());
7622 destination_ = dest;
7623 }
7624
7625 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007626 DCHECK(source_.IsValid() || destination_.IsInvalid());
7627 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007628 }
7629
7630 // True if this blocks a move from the given location.
7631 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007632 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007633 }
7634
7635 // A move is redundant if it's been eliminated, if its source and
7636 // destination are the same, or if its destination is unneeded.
7637 bool IsRedundant() const {
7638 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7639 }
7640
7641 // We clear both operands to indicate move that's been eliminated.
7642 void Eliminate() {
7643 source_ = destination_ = Location::NoLocation();
7644 }
7645
7646 bool IsEliminated() const {
7647 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7648 return source_.IsInvalid();
7649 }
7650
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007651 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007652
Nicolas Geoffray90218252015-04-15 11:56:51 +01007653 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007654 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007655 }
7656
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007657 HInstruction* GetInstruction() const { return instruction_; }
7658
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007659 private:
7660 Location source_;
7661 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007662 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007663 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007664 // The instruction this move is assocatied with. Null when this move is
7665 // for moving an input in the expected locations of user (including a phi user).
7666 // This is only used in debug mode, to ensure we do not connect interval siblings
7667 // in the same parallel move.
7668 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007669};
7670
Roland Levillainc9285912015-12-18 10:38:42 +00007671std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7672
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007673static constexpr size_t kDefaultNumberOfMoves = 4;
7674
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007675class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007676 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007677 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007678 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007679 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007680 moves_.reserve(kDefaultNumberOfMoves);
7681 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007682
Nicolas Geoffray90218252015-04-15 11:56:51 +01007683 void AddMove(Location source,
7684 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007685 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007686 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007687 DCHECK(source.IsValid());
7688 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007689 if (kIsDebugBuild) {
7690 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007691 for (const MoveOperands& move : moves_) {
7692 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007693 // Special case the situation where the move is for the spill slot
7694 // of the instruction.
7695 if ((GetPrevious() == instruction)
7696 || ((GetPrevious() == nullptr)
7697 && instruction->IsPhi()
7698 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007699 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007700 << "Doing parallel moves for the same instruction.";
7701 } else {
7702 DCHECK(false) << "Doing parallel moves for the same instruction.";
7703 }
7704 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007705 }
7706 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007707 for (const MoveOperands& move : moves_) {
7708 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007709 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007710 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007711 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007712 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007713 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007714 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007715 }
7716
Vladimir Marko225b6462015-09-28 12:17:40 +01007717 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007718 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007719 }
7720
Vladimir Marko225b6462015-09-28 12:17:40 +01007721 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007722
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007723 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007724
Artem Serovcced8ba2017-07-19 18:18:09 +01007725 protected:
7726 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7727
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007728 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007729 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007730};
7731
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007732// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7733// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7734// never used across anything that can trigger GC.
7735// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7736// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007737class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007738 public:
7739 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307740 : HExpression(kIntermediateAddress,
7741 DataType::Type::kInt32,
7742 SideEffects::DependsOnGC(),
7743 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007744 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7745 DataType::Size(DataType::Type::kReference))
7746 << "kPrimInt and kPrimNot have different sizes.";
7747 SetRawInputAt(0, base_address);
7748 SetRawInputAt(1, offset);
7749 }
7750
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007751 bool IsClonable() const override { return true; }
7752 bool CanBeMoved() const override { return true; }
7753 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007754 return true;
7755 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007756 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007757
7758 HInstruction* GetBaseAddress() const { return InputAt(0); }
7759 HInstruction* GetOffset() const { return InputAt(1); }
7760
7761 DECLARE_INSTRUCTION(IntermediateAddress);
7762
Artem Serovcced8ba2017-07-19 18:18:09 +01007763 protected:
7764 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007765};
7766
7767
Mark Mendell0616ae02015-04-17 12:49:27 -04007768} // namespace art
7769
Aart Bikf8f5a162017-02-06 15:35:29 -08007770#include "nodes_vector.h"
7771
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007772#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7773#include "nodes_shared.h"
7774#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007775#ifdef ART_ENABLE_CODEGEN_mips
7776#include "nodes_mips.h"
7777#endif
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307778#if defined(ART_ENABLE_CODEGEN_x86) || defined(ART_ENABLE_CODEGEN_x86_64)
Mark Mendell0616ae02015-04-17 12:49:27 -04007779#include "nodes_x86.h"
7780#endif
7781
7782namespace art {
7783
Igor Murashkin6ef45672017-08-08 13:59:55 -07007784class OptimizingCompilerStats;
7785
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007786class HGraphVisitor : public ValueObject {
7787 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007788 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7789 : stats_(stats),
7790 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007791 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007792
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007793 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007794 virtual void VisitBasicBlock(HBasicBlock* block);
7795
Roland Levillain633021e2014-10-01 14:12:25 +01007796 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007797 void VisitInsertionOrder();
7798
Roland Levillain633021e2014-10-01 14:12:25 +01007799 // Visit the graph following dominator tree reverse post-order.
7800 void VisitReversePostOrder();
7801
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007802 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007803
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007804 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007805#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007806 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7807
7808 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7809
7810#undef DECLARE_VISIT_INSTRUCTION
7811
Igor Murashkin6ef45672017-08-08 13:59:55 -07007812 protected:
7813 OptimizingCompilerStats* stats_;
7814
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007815 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007816 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007817
7818 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7819};
7820
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007821class HGraphDelegateVisitor : public HGraphVisitor {
7822 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007823 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7824 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007825 virtual ~HGraphDelegateVisitor() {}
7826
7827 // Visit functions that delegate to to super class.
7828#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007829 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007830
7831 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7832
7833#undef DECLARE_VISIT_INSTRUCTION
7834
7835 private:
7836 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7837};
7838
Artem Serovcced8ba2017-07-19 18:18:09 +01007839// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7840// and remove the old instruction.
7841HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7842
7843// Create a clone for each clonable instructions/phis and replace the original with the clone.
7844//
7845// Used for testing individual instruction cloner.
7846class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7847 public:
7848 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007849 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007851 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007852 if (instruction->IsClonable()) {
7853 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007854 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007855 }
7856 }
7857
Artem Serov7f4aff62017-06-21 17:02:18 +01007858 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007859
7860 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007861 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007862
7863 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7864};
7865
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007866// Iterator over the blocks that art part of the loop. Includes blocks part
7867// of an inner loop. The order in which the blocks are iterated is on their
7868// block id.
7869class HBlocksInLoopIterator : public ValueObject {
7870 public:
7871 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7872 : blocks_in_loop_(info.GetBlocks()),
7873 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7874 index_(0) {
7875 if (!blocks_in_loop_.IsBitSet(index_)) {
7876 Advance();
7877 }
7878 }
7879
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007880 bool Done() const { return index_ == blocks_.size(); }
7881 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007882 void Advance() {
7883 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007884 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007885 if (blocks_in_loop_.IsBitSet(index_)) {
7886 break;
7887 }
7888 }
7889 }
7890
7891 private:
7892 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007893 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007894 size_t index_;
7895
7896 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7897};
7898
Mingyao Yang3584bce2015-05-19 16:01:59 -07007899// Iterator over the blocks that art part of the loop. Includes blocks part
7900// of an inner loop. The order in which the blocks are iterated is reverse
7901// post order.
7902class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7903 public:
7904 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7905 : blocks_in_loop_(info.GetBlocks()),
7906 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7907 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007908 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007909 Advance();
7910 }
7911 }
7912
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007913 bool Done() const { return index_ == blocks_.size(); }
7914 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007915 void Advance() {
7916 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007917 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7918 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007919 break;
7920 }
7921 }
7922 }
7923
7924 private:
7925 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007926 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007927 size_t index_;
7928
7929 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7930};
7931
Aart Bikf3e61ee2017-04-12 17:09:20 -07007932// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007933inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007934 if (constant->IsIntConstant()) {
7935 return constant->AsIntConstant()->GetValue();
7936 } else if (constant->IsLongConstant()) {
7937 return constant->AsLongConstant()->GetValue();
7938 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007939 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007940 return 0;
7941 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007942}
7943
Aart Bikf3e61ee2017-04-12 17:09:20 -07007944// Returns true iff instruction is an integral constant (and sets value on success).
7945inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7946 if (instruction->IsIntConstant()) {
7947 *value = instruction->AsIntConstant()->GetValue();
7948 return true;
7949 } else if (instruction->IsLongConstant()) {
7950 *value = instruction->AsLongConstant()->GetValue();
7951 return true;
7952 } else if (instruction->IsNullConstant()) {
7953 *value = 0;
7954 return true;
7955 }
7956 return false;
7957}
7958
Aart Bik0148de42017-09-05 09:25:01 -07007959// Returns true iff instruction is the given integral constant.
7960inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7961 int64_t val = 0;
7962 return IsInt64AndGet(instruction, &val) && val == value;
7963}
7964
7965// Returns true iff instruction is a zero bit pattern.
7966inline bool IsZeroBitPattern(HInstruction* instruction) {
7967 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7968}
7969
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007970// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007971#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007972 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7973 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7974#undef INSTRUCTION_TYPE_CHECK
7975
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007976// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007977#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7978 std::is_base_of<BaseType, H##type>::value,
7979#define INSTRUCTION_TYPE_CHECK(type, super) \
7980 inline bool HInstruction::Is##type() const { \
7981 DCHECK_LT(GetKind(), kLastInstructionKind); \
7982 using BaseType = H##type; \
7983 static constexpr bool results[] = { \
7984 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7985 }; \
7986 return results[static_cast<size_t>(GetKind())]; \
7987 }
7988
7989 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7990#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007991#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007992
7993#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007994 inline const H##type* HInstruction::As##type() const { \
7995 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7996 } \
7997 inline H##type* HInstruction::As##type() { \
7998 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7999 }
8000
Vladimir Markoa90dd512018-05-04 15:04:45 +01008001 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
8002#undef INSTRUCTION_TYPE_CAST
8003
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008004
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00008005// Create space in `blocks` for adding `number_of_new_blocks` entries
8006// starting at location `at`. Blocks after `at` are moved accordingly.
8007inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
8008 size_t number_of_new_blocks,
8009 size_t after) {
8010 DCHECK_LT(after, blocks->size());
8011 size_t old_size = blocks->size();
8012 size_t new_size = old_size + number_of_new_blocks;
8013 blocks->resize(new_size);
8014 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8015}
8016
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008017/*
8018 * Hunt "under the hood" of array lengths (leading to array references),
8019 * null checks (also leading to array references), and new arrays
8020 * (leading to the actual length). This makes it more likely related
8021 * instructions become actually comparable.
8022 */
8023inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8024 while (instruction->IsArrayLength() ||
8025 instruction->IsNullCheck() ||
8026 instruction->IsNewArray()) {
8027 instruction = instruction->IsNewArray()
8028 ? instruction->AsNewArray()->GetLength()
8029 : instruction->InputAt(0);
8030 }
8031 return instruction;
8032}
8033
Artem Serov21c7e6f2017-07-27 16:04:42 +01008034void RemoveEnvironmentUses(HInstruction* instruction);
8035bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8036void ResetEnvironmentInputRecords(HInstruction* instruction);
8037
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008038} // namespace art
8039
8040#endif // ART_COMPILER_OPTIMIZING_NODES_H_