blob: 6ebe89eae1b8d315f0e8620a2d65c0ea1d64c9c9 [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
1500#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1501
1502#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1503 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001504 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001505 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1506 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001507 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001508 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001509 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1510 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1511
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001512#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1513 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001514 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001515 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001516 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001517 M(Invoke, Instruction) \
1518 M(VecOperation, Instruction) \
1519 M(VecUnaryOperation, VecOperation) \
1520 M(VecBinaryOperation, VecOperation) \
1521 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001522
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001523#define FOR_EACH_INSTRUCTION(M) \
1524 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1525 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1526
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001527#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001528FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1529#undef FORWARD_DECLARATION
1530
Vladimir Marko372f10e2016-05-17 16:30:10 +01001531#define DECLARE_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001532 private: \
1533 H##type& operator=(const H##type&) = delete; \
1534 public: \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001535 const char* DebugName() const override { return #type; } \
1536 HInstruction* Clone(ArenaAllocator* arena) const override { \
Artem Serovcced8ba2017-07-19 18:18:09 +01001537 DCHECK(IsClonable()); \
1538 return new (arena) H##type(*this->As##type()); \
1539 } \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01001540 void Accept(HGraphVisitor* visitor) override
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001541
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001542#define DECLARE_ABSTRACT_INSTRUCTION(type) \
Artem Serovcced8ba2017-07-19 18:18:09 +01001543 private: \
1544 H##type& operator=(const H##type&) = delete; \
Vladimir Markoa90dd512018-05-04 15:04:45 +01001545 public:
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001546
Artem Serovcced8ba2017-07-19 18:18:09 +01001547#define DEFAULT_COPY_CONSTRUCTOR(type) \
1548 explicit H##type(const H##type& other) = default;
1549
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001550template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001551class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1552 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001553 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001554 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001555 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001556 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001557 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001558 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001559 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001560
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001561 private:
David Brazdiled596192015-01-23 10:39:45 +00001562 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001563 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001564
1565 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001566 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001567
Vladimir Marko46817b82016-03-29 12:21:58 +01001568 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569
1570 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1571};
1572
David Brazdiled596192015-01-23 10:39:45 +00001573template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001574using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001575
David Brazdil1abb4192015-02-17 18:33:36 +00001576// This class is used by HEnvironment and HInstruction classes to record the
1577// instructions they use and pointers to the corresponding HUseListNodes kept
1578// by the used instructions.
1579template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001580class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001581 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001582 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1583 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001584
Vladimir Marko46817b82016-03-29 12:21:58 +01001585 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1586 : HUserRecord(old_record.instruction_, before_use_node) {}
1587 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1588 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001589 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001590 }
1591
1592 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001593 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1594 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001595
1596 private:
1597 // Instruction used by the user.
1598 HInstruction* instruction_;
1599
Vladimir Marko46817b82016-03-29 12:21:58 +01001600 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1601 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001602};
1603
Vladimir Markoe9004912016-06-16 16:50:52 +01001604// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1605// This is used for HInstruction::GetInputs() to return a container wrapper providing
1606// HInstruction* values even though the underlying container has HUserRecord<>s.
1607struct HInputExtractor {
1608 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1609 return record.GetInstruction();
1610 }
1611 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1612 return record.GetInstruction();
1613 }
1614};
1615
1616using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1617using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001621 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001622 * For write/read dependences on fields/arrays, the dependence analysis uses
1623 * type disambiguation (e.g. a float field write cannot modify the value of an
1624 * integer field read) and the access type (e.g. a reference array write cannot
1625 * modify the value of a reference field read [although it may modify the
1626 * reference fetch prior to reading the field, which is represented by its own
1627 * write/read dependence]). The analysis makes conservative points-to
1628 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1629 * the same, and any reference read depends on any reference read without
1630 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001631 *
Artem Serovd1aa7d02018-06-22 11:35:46 +01001632 * kDependsOnGCBit is defined in the following way: instructions with kDependsOnGCBit must not be
1633 * alive across the point where garbage collection might happen.
1634 *
1635 * Note: Instructions with kCanTriggerGCBit do not depend on each other.
1636 *
1637 * kCanTriggerGCBit must be used for instructions for which GC might happen on the path across
1638 * those instructions from the compiler perspective (between this instruction and the next one
1639 * in the IR).
1640 *
1641 * Note: Instructions which can cause GC only on a fatal slow path do not need
1642 * kCanTriggerGCBit as the execution never returns to the instruction next to the exceptional
1643 * one. However the execution may return to compiled code if there is a catch block in the
1644 * current method; for this purpose the TryBoundary exit instruction has kCanTriggerGCBit
1645 * set.
1646 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 * The internal representation uses 38-bit and is described in the table below.
1648 * The first line indicates the side effect, and for field/array accesses the
1649 * second line indicates the type of the access (in the order of the
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 * DataType::Type enum).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 * The two numbered lines below indicate the bit position in the bitfield (read
1652 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001653 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1655 * +-------------+---------+---------+--------------+---------+---------+
1656 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1657 * | 3 |333333322|222222221| 1 |111111110|000000000|
1658 * | 7 |654321098|765432109| 8 |765432109|876543210|
1659 *
1660 * Note that, to ease the implementation, 'changes' bits are least significant
1661 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001662 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663class SideEffects : public ValueObject {
1664 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001665 SideEffects() : flags_(0) {}
1666
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001667 static SideEffects None() {
1668 return SideEffects(0);
1669 }
1670
1671 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001672 return SideEffects(kAllChangeBits | kAllDependOnBits);
1673 }
1674
1675 static SideEffects AllChanges() {
1676 return SideEffects(kAllChangeBits);
1677 }
1678
1679 static SideEffects AllDependencies() {
1680 return SideEffects(kAllDependOnBits);
1681 }
1682
1683 static SideEffects AllExceptGCDependency() {
1684 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1685 }
1686
1687 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001688 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001689 }
1690
Aart Bik34c3ba92015-07-20 14:08:59 -07001691 static SideEffects AllWrites() {
1692 return SideEffects(kAllWrites);
1693 }
1694
1695 static SideEffects AllReads() {
1696 return SideEffects(kAllReads);
1697 }
1698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001699 static SideEffects FieldWriteOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001700 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001701 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001702 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001703 }
1704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001705 static SideEffects ArrayWriteOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001706 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001707 }
1708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001709 static SideEffects FieldReadOfType(DataType::Type type, bool is_volatile) {
Aart Bik34c3ba92015-07-20 14:08:59 -07001710 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001712 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001713 }
1714
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001715 static SideEffects ArrayReadOfType(DataType::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001716 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001717 }
1718
Artem Serovd1aa7d02018-06-22 11:35:46 +01001719 // Returns whether GC might happen across this instruction from the compiler perspective so
1720 // the next instruction in the IR would see that.
1721 //
1722 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 static SideEffects CanTriggerGC() {
1724 return SideEffects(1ULL << kCanTriggerGCBit);
1725 }
1726
Artem Serovd1aa7d02018-06-22 11:35:46 +01001727 // Returns whether the instruction must not be alive across a GC point.
1728 //
1729 // See the SideEffect class comments.
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001730 static SideEffects DependsOnGC() {
1731 return SideEffects(1ULL << kDependsOnGCBit);
1732 }
1733
Aart Bik854a02b2015-07-14 16:07:00 -07001734 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001735 SideEffects Union(SideEffects other) const {
1736 return SideEffects(flags_ | other.flags_);
1737 }
1738
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001739 SideEffects Exclusion(SideEffects other) const {
1740 return SideEffects(flags_ & ~other.flags_);
1741 }
1742
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001743 void Add(SideEffects other) {
1744 flags_ |= other.flags_;
1745 }
1746
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001747 bool Includes(SideEffects other) const {
1748 return (other.flags_ & flags_) == other.flags_;
1749 }
1750
1751 bool HasSideEffects() const {
1752 return (flags_ & kAllChangeBits);
1753 }
1754
1755 bool HasDependencies() const {
1756 return (flags_ & kAllDependOnBits);
1757 }
1758
1759 // Returns true if there are no side effects or dependencies.
1760 bool DoesNothing() const {
1761 return flags_ == 0;
1762 }
1763
Aart Bik854a02b2015-07-14 16:07:00 -07001764 // Returns true if something is written.
1765 bool DoesAnyWrite() const {
1766 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001767 }
1768
Aart Bik854a02b2015-07-14 16:07:00 -07001769 // Returns true if something is read.
1770 bool DoesAnyRead() const {
1771 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001772 }
1773
Aart Bik854a02b2015-07-14 16:07:00 -07001774 // Returns true if potentially everything is written and read
1775 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001776 bool DoesAllReadWrite() const {
1777 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1778 }
1779
Aart Bik854a02b2015-07-14 16:07:00 -07001780 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001781 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001782 }
1783
Roland Levillain0d5a2812015-11-13 10:07:31 +00001784 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001785 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001786 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1787 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001788 }
1789
1790 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001791 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001792 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001793 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001794 for (int s = kLastBit; s >= 0; s--) {
1795 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1796 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1797 // This is a bit for the GC side effect.
1798 if (current_bit_is_set) {
1799 flags += "GC";
1800 }
Aart Bik854a02b2015-07-14 16:07:00 -07001801 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001802 } else {
1803 // This is a bit for the array/field analysis.
1804 // The underscore character stands for the 'can trigger GC' bit.
1805 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1806 if (current_bit_is_set) {
1807 flags += kDebug[s];
1808 }
1809 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1810 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1811 flags += "|";
1812 }
1813 }
Aart Bik854a02b2015-07-14 16:07:00 -07001814 }
1815 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001816 }
1817
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001818 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001819
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001820 private:
1821 static constexpr int kFieldArrayAnalysisBits = 9;
1822
1823 static constexpr int kFieldWriteOffset = 0;
1824 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1825 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1826 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1827
1828 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1829
1830 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1831 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1832 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1833 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1834
1835 static constexpr int kLastBit = kDependsOnGCBit;
1836 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1837
1838 // Aliases.
1839
1840 static_assert(kChangeBits == kDependOnBits,
1841 "the 'change' bits should match the 'depend on' bits.");
1842
1843 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1844 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1845 static constexpr uint64_t kAllWrites =
1846 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1847 static constexpr uint64_t kAllReads =
1848 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001849
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001850 // Translates type to bit flag. The type must correspond to a Java type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001851 static uint64_t TypeFlag(DataType::Type type, int offset) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001852 int shift;
1853 switch (type) {
1854 case DataType::Type::kReference: shift = 0; break;
1855 case DataType::Type::kBool: shift = 1; break;
1856 case DataType::Type::kInt8: shift = 2; break;
1857 case DataType::Type::kUint16: shift = 3; break;
1858 case DataType::Type::kInt16: shift = 4; break;
1859 case DataType::Type::kInt32: shift = 5; break;
1860 case DataType::Type::kInt64: shift = 6; break;
1861 case DataType::Type::kFloat32: shift = 7; break;
1862 case DataType::Type::kFloat64: shift = 8; break;
1863 default:
1864 LOG(FATAL) << "Unexpected data type " << type;
1865 UNREACHABLE();
1866 }
Aart Bik854a02b2015-07-14 16:07:00 -07001867 DCHECK_LE(kFieldWriteOffset, shift);
1868 DCHECK_LT(shift, kArrayWriteOffset);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001869 return UINT64_C(1) << (shift + offset);
Aart Bik854a02b2015-07-14 16:07:00 -07001870 }
1871
1872 // Private constructor on direct flags value.
1873 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1874
1875 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876};
1877
David Brazdiled596192015-01-23 10:39:45 +00001878// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001879class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001880 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001881 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
Mingyao Yang01b47b02017-02-03 12:09:57 -08001882 size_t number_of_vregs,
1883 ArtMethod* method,
1884 uint32_t dex_pc,
1885 HInstruction* holder)
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001886 : vregs_(number_of_vregs, allocator->Adapter(kArenaAllocEnvironmentVRegs)),
1887 locations_(allocator->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001888 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001889 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001890 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001891 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001892 }
1893
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001894 ALWAYS_INLINE HEnvironment(ArenaAllocator* allocator,
1895 const HEnvironment& to_copy,
1896 HInstruction* holder)
1897 : HEnvironment(allocator,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001898 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001899 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001900 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001901 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001902
Vladimir Markoec32f642017-06-02 10:51:55 +01001903 void AllocateLocations() {
1904 DCHECK(locations_.empty());
1905 locations_.resize(vregs_.size());
1906 }
1907
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001908 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001909 if (parent_ != nullptr) {
1910 parent_->SetAndCopyParentChain(allocator, parent);
1911 } else {
1912 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1913 parent_->CopyFrom(parent);
1914 if (parent->GetParent() != nullptr) {
1915 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1916 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001917 }
David Brazdiled596192015-01-23 10:39:45 +00001918 }
1919
Vladimir Marko69d310e2017-10-09 14:12:23 +01001920 void CopyFrom(ArrayRef<HInstruction* const> locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001921 void CopyFrom(HEnvironment* environment);
1922
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001923 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1924 // input to the loop phi instead. This is for inserting instructions that
1925 // require an environment (like HDeoptimization) in the loop pre-header.
1926 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001927
1928 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001929 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001930 }
1931
1932 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001933 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001934 }
1935
David Brazdil1abb4192015-02-17 18:33:36 +00001936 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001937
Artem Serovca210e32017-12-15 13:43:20 +00001938 // Replaces the input at the position 'index' with the replacement; the replacement and old
1939 // input instructions' env_uses_ lists are adjusted. The function works similar to
1940 // HInstruction::ReplaceInput.
1941 void ReplaceInput(HInstruction* replacement, size_t index);
1942
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001943 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001944
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001945 HEnvironment* GetParent() const { return parent_; }
1946
1947 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001948 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001949 }
1950
1951 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001952 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001953 }
1954
1955 uint32_t GetDexPc() const {
1956 return dex_pc_;
1957 }
1958
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001959 ArtMethod* GetMethod() const {
1960 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001961 }
1962
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001963 HInstruction* GetHolder() const {
1964 return holder_;
1965 }
1966
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001967
1968 bool IsFromInlinedInvoke() const {
1969 return GetParent() != nullptr;
1970 }
1971
David Brazdiled596192015-01-23 10:39:45 +00001972 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001973 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1974 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001975 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001976 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001977 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001978
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001979 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001980 HInstruction* const holder_;
1981
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001982 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001983
1984 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1985};
1986
Vladimir Markof9f64412015-09-02 14:05:49 +01001987class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001988 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301989#define DECLARE_KIND(type, super) k##type,
1990 enum InstructionKind {
Vladimir Markoe3946222018-05-04 14:18:47 +01001991 FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_KIND)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05301992 kLastInstructionKind
1993 };
1994#undef DECLARE_KIND
1995
1996 HInstruction(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01001997 : HInstruction(kind, DataType::Type::kVoid, side_effects, dex_pc) {}
1998
1999 HInstruction(InstructionKind kind, DataType::Type type, SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002000 : previous_(nullptr),
2001 next_(nullptr),
2002 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002003 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002004 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002005 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002006 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002008 locations_(nullptr),
2009 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002010 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00002011 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002012 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302013 SetPackedField<InstructionKindField>(kind);
Vladimir Markobd785672018-05-03 17:09:09 +01002014 SetPackedField<TypeField>(type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002015 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
2016 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002017
Dave Allison20dfc792014-06-16 20:44:29 -07002018 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002019
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002020
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002021 HInstruction* GetNext() const { return next_; }
2022 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002023
Calin Juravle77520bc2015-01-12 18:45:46 +00002024 HInstruction* GetNextDisregardingMoves() const;
2025 HInstruction* GetPreviousDisregardingMoves() const;
2026
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002027 HBasicBlock* GetBlock() const { return block_; }
Vladimir Markoca6fff82017-10-03 14:49:14 +01002028 ArenaAllocator* GetAllocator() const { return block_->GetGraph()->GetAllocator(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002029 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01002030 bool IsInBlock() const { return block_ != nullptr; }
2031 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00002032 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
2033 bool IsIrreducibleLoopHeaderPhi() const {
2034 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
2035 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002036
Vladimir Marko372f10e2016-05-17 16:30:10 +01002037 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
2038
2039 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
2040 // One virtual method is enough, just const_cast<> and then re-add the const.
2041 return ArrayRef<const HUserRecord<HInstruction*>>(
2042 const_cast<HInstruction*>(this)->GetInputRecords());
2043 }
2044
Vladimir Markoe9004912016-06-16 16:50:52 +01002045 HInputsRef GetInputs() {
2046 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002047 }
2048
Vladimir Markoe9004912016-06-16 16:50:52 +01002049 HConstInputsRef GetInputs() const {
2050 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01002051 }
2052
2053 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00002054 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002055
Aart Bik2767f4b2016-10-28 15:03:53 -07002056 bool HasInput(HInstruction* input) const {
2057 for (const HInstruction* i : GetInputs()) {
2058 if (i == input) {
2059 return true;
2060 }
2061 }
2062 return false;
2063 }
2064
Vladimir Marko372f10e2016-05-17 16:30:10 +01002065 void SetRawInputAt(size_t index, HInstruction* input) {
2066 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
2067 }
2068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002069 virtual void Accept(HGraphVisitor* visitor) = 0;
2070 virtual const char* DebugName() const = 0;
2071
Vladimir Markobd785672018-05-03 17:09:09 +01002072 DataType::Type GetType() const {
2073 return TypeField::Decode(GetPackedFields());
2074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002075
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002077
2078 uint32_t GetDexPc() const { return dex_pc_; }
2079
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002080 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01002081
Nicolas Geoffray331605a2017-03-01 11:01:41 +00002082 // Can the instruction throw?
2083 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
2084 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01002085 virtual bool CanThrow() const { return false; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08002086
2087 // Does the instruction always throw an exception unconditionally?
2088 virtual bool AlwaysThrows() const { return false; }
2089
David Brazdilec16f792015-08-19 15:04:01 +01002090 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002091
Alexandre Rames78e3ef62015-08-12 13:43:29 +01002092 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07002093 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094
Calin Juravle10e244f2015-01-26 18:54:32 +00002095 // Does not apply for all instructions, but having this at top level greatly
2096 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00002097 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002098 virtual bool CanBeNull() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 DCHECK_EQ(GetType(), DataType::Type::kReference) << "CanBeNull only applies to reference types";
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002100 return true;
2101 }
Calin Juravle10e244f2015-01-26 18:54:32 +00002102
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002103 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01002104 return false;
2105 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002106
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01002107 // If this instruction will do an implicit null check, return the `HNullCheck` associated
2108 // with it. Otherwise return null.
2109 HNullCheck* GetImplicitNullCheck() const {
2110 // Find the first previous instruction which is not a move.
2111 HInstruction* first_prev_not_move = GetPreviousDisregardingMoves();
2112 if (first_prev_not_move != nullptr &&
2113 first_prev_not_move->IsNullCheck() &&
2114 first_prev_not_move->IsEmittedAtUseSite()) {
2115 return first_prev_not_move->AsNullCheck();
2116 }
2117 return nullptr;
2118 }
2119
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002120 virtual bool IsActualObject() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 return GetType() == DataType::Type::kReference;
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00002122 }
2123
Calin Juravle2e768302015-07-28 14:41:11 +00002124 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00002125
Calin Juravle61d544b2015-02-23 16:46:57 +00002126 ReferenceTypeInfo GetReferenceTypeInfo() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 DCHECK_EQ(GetType(), DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002128 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002129 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002130 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002131
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002132 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002133 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002134 // Note: fixup_end remains valid across push_front().
2135 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2136 HUseListNode<HInstruction*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002137 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HInstruction*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002138 uses_.push_front(*new_node);
2139 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002140 }
2141
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002142 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002143 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002144 // Note: env_fixup_end remains valid across push_front().
2145 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2146 HUseListNode<HEnvironment*>* new_node =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002147 new (GetBlock()->GetGraph()->GetAllocator()) HUseListNode<HEnvironment*>(user, index);
Vladimir Marko46817b82016-03-29 12:21:58 +01002148 env_uses_.push_front(*new_node);
2149 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002150 }
2151
David Brazdil1abb4192015-02-17 18:33:36 +00002152 void RemoveAsUserOfInput(size_t input) {
2153 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002154 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2155 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2156 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002157 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002158
Vladimir Marko372f10e2016-05-17 16:30:10 +01002159 void RemoveAsUserOfAllInputs() {
2160 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2161 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2162 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2163 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2164 }
2165 }
2166
David Brazdil1abb4192015-02-17 18:33:36 +00002167 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2168 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002169
Vladimir Marko46817b82016-03-29 12:21:58 +01002170 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2171 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2172 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002173 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002174 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002175 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002176
Aart Bikcc42be02016-10-20 16:14:16 -07002177 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002178 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002179 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002180 !CanThrow() &&
2181 !IsSuspendCheck() &&
2182 !IsControlFlow() &&
2183 !IsNativeDebugInfo() &&
2184 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002185 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002186 !IsMemoryBarrier() &&
2187 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002188 }
2189
Aart Bikcc42be02016-10-20 16:14:16 -07002190 bool IsDeadAndRemovable() const {
2191 return IsRemovable() && !HasUses();
2192 }
2193
Roland Levillain6c82d402014-10-13 16:10:27 +01002194 // Does this instruction strictly dominate `other_instruction`?
2195 // Returns false if this instruction and `other_instruction` are the same.
2196 // Aborts if this instruction and `other_instruction` are both phis.
2197 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002198
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002199 int GetId() const { return id_; }
2200 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002202 int GetSsaIndex() const { return ssa_index_; }
2203 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2204 bool HasSsaIndex() const { return ssa_index_ != -1; }
2205
2206 bool HasEnvironment() const { return environment_ != nullptr; }
2207 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002208 // Set the `environment_` field. Raw because this method does not
2209 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002210 void SetRawEnvironment(HEnvironment* environment) {
2211 DCHECK(environment_ == nullptr);
2212 DCHECK_EQ(environment->GetHolder(), this);
2213 environment_ = environment;
2214 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002215
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002216 void InsertRawEnvironment(HEnvironment* environment) {
2217 DCHECK(environment_ != nullptr);
2218 DCHECK_EQ(environment->GetHolder(), this);
2219 DCHECK(environment->GetParent() == nullptr);
2220 environment->parent_ = environment_;
2221 environment_ = environment;
2222 }
2223
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002224 void RemoveEnvironment();
2225
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002226 // Set the environment of this instruction, copying it from `environment`. While
2227 // copying, the uses lists are being updated.
2228 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002229 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002230 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002231 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002232 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002233 if (environment->GetParent() != nullptr) {
2234 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2235 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002236 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002237
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002238 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2239 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002240 DCHECK(environment_ == nullptr);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002241 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetAllocator();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002242 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002243 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002244 if (environment->GetParent() != nullptr) {
2245 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2246 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002247 }
2248
Nicolas Geoffray39468442014-09-02 15:17:15 +01002249 // Returns the number of entries in the environment. Typically, that is the
2250 // number of dex registers in a method. It could be more in case of inlining.
2251 size_t EnvironmentSize() const;
2252
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002253 LocationSummary* GetLocations() const { return locations_; }
2254 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002255
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002256 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002257 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +01002258 void ReplaceEnvUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002259 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002260
Alexandre Rames188d4312015-04-09 18:30:21 +01002261 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2262 // uses of this instruction by `other` are *not* updated.
2263 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2264 ReplaceWith(other);
2265 other->ReplaceInput(this, use_index);
2266 }
2267
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002268 // Move `this` instruction before `cursor`
2269 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002270
Vladimir Markofb337ea2015-11-25 15:25:10 +00002271 // Move `this` before its first user and out of any loops. If there is no
2272 // out-of-loop user that dominates all other users, move the instruction
2273 // to the end of the out-of-loop common dominator of the user's blocks.
2274 //
2275 // This can be used only on non-throwing instructions with no side effects that
2276 // have at least one use but no environment uses.
2277 void MoveBeforeFirstUserAndOutOfLoops();
2278
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002279#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01002280 bool Is##type() const;
2281
2282 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2283#undef INSTRUCTION_TYPE_CHECK
2284
2285#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002286 const H##type* As##type() const; \
2287 H##type* As##type();
2288
Vladimir Markoa90dd512018-05-04 15:04:45 +01002289 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
2290#undef INSTRUCTION_TYPE_CAST
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002291
Artem Serovcced8ba2017-07-19 18:18:09 +01002292 // Return a clone of the instruction if it is clonable (shallow copy by default, custom copy
2293 // if a custom copy-constructor is provided for a particular type). If IsClonable() is false for
2294 // the instruction then the behaviour of this function is undefined.
2295 //
2296 // Note: It is semantically valid to create a clone of the instruction only until
2297 // prepare_for_register_allocator phase as lifetime, intervals and codegen info are not
2298 // copied.
2299 //
2300 // Note: HEnvironment and some other fields are not copied and are set to default values, see
2301 // 'explicit HInstruction(const HInstruction& other)' for details.
2302 virtual HInstruction* Clone(ArenaAllocator* arena ATTRIBUTE_UNUSED) const {
2303 LOG(FATAL) << "Cloning is not implemented for the instruction " <<
2304 DebugName() << " " << GetId();
2305 UNREACHABLE();
2306 }
2307
2308 // Return whether instruction can be cloned (copied).
2309 virtual bool IsClonable() const { return false; }
2310
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002311 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002312 // TODO: this method is used by LICM and GVN with possibly different
2313 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314 virtual bool CanBeMoved() const { return false; }
2315
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002316 // Returns whether any data encoded in the two instructions is equal.
2317 // This method does not look at the inputs. Both instructions must be
2318 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002319 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002320 return false;
2321 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002322
2323 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002324 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002325 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002326 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002327
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002328 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2329 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2330 // the virtual function because the __attribute__((__pure__)) doesn't really
2331 // apply the strong requirement for virtual functions, preventing optimizations.
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302332 InstructionKind GetKind() const { return GetPackedField<InstructionKindField>(); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002333
2334 virtual size_t ComputeHashCode() const {
2335 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002336 for (const HInstruction* input : GetInputs()) {
2337 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002338 }
2339 return result;
2340 }
2341
2342 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002343 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002344 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002345
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002346 size_t GetLifetimePosition() const { return lifetime_position_; }
2347 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2348 LiveInterval* GetLiveInterval() const { return live_interval_; }
2349 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2350 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2351
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002352 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2353
2354 // Returns whether the code generation of the instruction will require to have access
2355 // to the current method. Such instructions are:
2356 // (1): Instructions that require an environment, as calling the runtime requires
2357 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002358 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2359 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002360 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002361 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002362 }
2363
Vladimir Markodc151b22015-10-15 18:02:30 +01002364 // Returns whether the code generation of the instruction will require to have access
2365 // to the dex cache of the current method's declaring class via the current method.
2366 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002367
Mark Mendellc4701932015-04-10 13:18:51 -04002368 // Does this instruction have any use in an environment before
2369 // control flow hits 'other'?
2370 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2371
2372 // Remove all references to environment uses of this instruction.
2373 // The caller must ensure that this is safe to do.
2374 void RemoveEnvironmentUsers();
2375
Vladimir Markoa1de9182016-02-25 11:37:38 +00002376 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2377 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002378
David Brazdil1abb4192015-02-17 18:33:36 +00002379 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002380 // If set, the machine code for this instruction is assumed to be generated by
2381 // its users. Used by liveness analysis to compute use positions accordingly.
2382 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2383 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302384 static constexpr size_t kFieldInstructionKind = kFlagReferenceTypeIsExact + 1;
2385 static constexpr size_t kFieldInstructionKindSize =
2386 MinimumBitsToStore(static_cast<size_t>(InstructionKind::kLastInstructionKind - 1));
Vladimir Markobd785672018-05-03 17:09:09 +01002387 static constexpr size_t kFieldType =
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302388 kFieldInstructionKind + kFieldInstructionKindSize;
Vladimir Markobd785672018-05-03 17:09:09 +01002389 static constexpr size_t kFieldTypeSize =
2390 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
2391 static constexpr size_t kNumberOfGenericPackedBits = kFieldType + kFieldTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00002392 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2393
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302394 static_assert(kNumberOfGenericPackedBits <= kMaxNumberOfPackedBits,
2395 "Too many generic packed fields");
2396
Vladimir Markobd785672018-05-03 17:09:09 +01002397 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
2398
Vladimir Marko372f10e2016-05-17 16:30:10 +01002399 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2400 return GetInputRecords()[i];
2401 }
2402
2403 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2404 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2405 input_records[index] = input;
2406 }
David Brazdil1abb4192015-02-17 18:33:36 +00002407
Vladimir Markoa1de9182016-02-25 11:37:38 +00002408 uint32_t GetPackedFields() const {
2409 return packed_fields_;
2410 }
2411
2412 template <size_t flag>
2413 bool GetPackedFlag() const {
2414 return (packed_fields_ & (1u << flag)) != 0u;
2415 }
2416
2417 template <size_t flag>
2418 void SetPackedFlag(bool value = true) {
2419 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2420 }
2421
2422 template <typename BitFieldType>
2423 typename BitFieldType::value_type GetPackedField() const {
2424 return BitFieldType::Decode(packed_fields_);
2425 }
2426
2427 template <typename BitFieldType>
2428 void SetPackedField(typename BitFieldType::value_type value) {
2429 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2430 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2431 }
2432
Artem Serovcced8ba2017-07-19 18:18:09 +01002433 // Copy construction for the instruction (used for Clone function).
2434 //
2435 // Fields (e.g. lifetime, intervals and codegen info) associated with phases starting from
2436 // prepare_for_register_allocator are not copied (set to default values).
2437 //
2438 // Copy constructors must be provided for every HInstruction type; default copy constructor is
2439 // fine for most of them. However for some of the instructions a custom copy constructor must be
2440 // specified (when instruction has non-trivially copyable fields and must have a special behaviour
2441 // for copying them).
2442 explicit HInstruction(const HInstruction& other)
2443 : previous_(nullptr),
2444 next_(nullptr),
2445 block_(nullptr),
2446 dex_pc_(other.dex_pc_),
2447 id_(-1),
2448 ssa_index_(-1),
2449 packed_fields_(other.packed_fields_),
2450 environment_(nullptr),
2451 locations_(nullptr),
2452 live_interval_(nullptr),
2453 lifetime_position_(kNoLifetime),
2454 side_effects_(other.side_effects_),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302455 reference_type_handle_(other.reference_type_handle_) {
2456 }
Artem Serovcced8ba2017-07-19 18:18:09 +01002457
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458 private:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302459 using InstructionKindField =
2460 BitField<InstructionKind, kFieldInstructionKind, kFieldInstructionKindSize>;
2461
Vladimir Marko46817b82016-03-29 12:21:58 +01002462 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2463 auto before_use_node = uses_.before_begin();
2464 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2465 HInstruction* user = use_node->GetUser();
2466 size_t input_index = use_node->GetIndex();
2467 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2468 before_use_node = use_node;
2469 }
2470 }
2471
2472 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2473 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2474 if (next != uses_.end()) {
2475 HInstruction* next_user = next->GetUser();
2476 size_t next_index = next->GetIndex();
2477 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2478 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2479 }
2480 }
2481
2482 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2483 auto before_env_use_node = env_uses_.before_begin();
2484 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2485 HEnvironment* user = env_use_node->GetUser();
2486 size_t input_index = env_use_node->GetIndex();
2487 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2488 before_env_use_node = env_use_node;
2489 }
2490 }
2491
2492 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2493 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2494 if (next != env_uses_.end()) {
2495 HEnvironment* next_user = next->GetUser();
2496 size_t next_index = next->GetIndex();
2497 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2498 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2499 }
2500 }
David Brazdil1abb4192015-02-17 18:33:36 +00002501
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002502 HInstruction* previous_;
2503 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002504 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002505 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002506
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002507 // An instruction gets an id when it is added to the graph.
2508 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002509 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002510 int id_;
2511
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002512 // When doing liveness analysis, instructions that have uses get an SSA index.
2513 int ssa_index_;
2514
Vladimir Markoa1de9182016-02-25 11:37:38 +00002515 // Packed fields.
2516 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002517
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002518 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002519 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002520
2521 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002522 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002523
Nicolas Geoffray39468442014-09-02 15:17:15 +01002524 // The environment associated with this instruction. Not null if the instruction
2525 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002526 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002527
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002528 // Set by the code generator.
2529 LocationSummary* locations_;
2530
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002531 // Set by the liveness analysis.
2532 LiveInterval* live_interval_;
2533
2534 // Set by the liveness analysis, this is the position in a linear
2535 // order of blocks where this instruction's live interval start.
2536 size_t lifetime_position_;
2537
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002538 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002539
Vladimir Markoa1de9182016-02-25 11:37:38 +00002540 // The reference handle part of the reference type info.
2541 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002542 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002543 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002544
David Brazdil1abb4192015-02-17 18:33:36 +00002545 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002546 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002547 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002548 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002549 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002550};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002551std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002552
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002553// Iterates over the instructions, while preserving the next instruction
2554// in case the current instruction gets removed from the list by the user
2555// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002556class HInstructionIterator : public ValueObject {
2557 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002558 explicit HInstructionIterator(const HInstructionList& instructions)
2559 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002560 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002561 }
2562
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002563 bool Done() const { return instruction_ == nullptr; }
2564 HInstruction* Current() const { return instruction_; }
2565 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002566 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002567 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002568 }
2569
2570 private:
2571 HInstruction* instruction_;
2572 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002573
2574 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002575};
2576
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002577// Iterates over the instructions without saving the next instruction,
2578// therefore handling changes in the graph potentially made by the user
2579// of this iterator.
2580class HInstructionIteratorHandleChanges : public ValueObject {
2581 public:
2582 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2583 : instruction_(instructions.first_instruction_) {
2584 }
2585
2586 bool Done() const { return instruction_ == nullptr; }
2587 HInstruction* Current() const { return instruction_; }
2588 void Advance() {
2589 instruction_ = instruction_->GetNext();
2590 }
2591
2592 private:
2593 HInstruction* instruction_;
2594
2595 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2596};
2597
2598
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002599class HBackwardInstructionIterator : public ValueObject {
2600 public:
2601 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2602 : instruction_(instructions.last_instruction_) {
2603 next_ = Done() ? nullptr : instruction_->GetPrevious();
2604 }
2605
2606 bool Done() const { return instruction_ == nullptr; }
2607 HInstruction* Current() const { return instruction_; }
2608 void Advance() {
2609 instruction_ = next_;
2610 next_ = Done() ? nullptr : instruction_->GetPrevious();
2611 }
2612
2613 private:
2614 HInstruction* instruction_;
2615 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002616
2617 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002618};
2619
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002620class HVariableInputSizeInstruction : public HInstruction {
2621 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002622 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002623 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002624 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2625 }
2626
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002627 void AddInput(HInstruction* input);
2628 void InsertInputAt(size_t index, HInstruction* input);
2629 void RemoveInputAt(size_t index);
2630
Igor Murashkind01745e2017-04-05 16:40:31 -07002631 // Removes all the inputs.
2632 // Also removes this instructions from each input's use list
2633 // (for non-environment uses only).
2634 void RemoveAllInputs();
2635
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002636 protected:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302637 HVariableInputSizeInstruction(InstructionKind inst_kind,
2638 SideEffects side_effects,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002639 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002640 ArenaAllocator* allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002641 size_t number_of_inputs,
2642 ArenaAllocKind kind)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302643 : HInstruction(inst_kind, side_effects, dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002644 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Vladimir Markobd785672018-05-03 17:09:09 +01002645 HVariableInputSizeInstruction(InstructionKind inst_kind,
2646 DataType::Type type,
2647 SideEffects side_effects,
2648 uint32_t dex_pc,
2649 ArenaAllocator* allocator,
2650 size_t number_of_inputs,
2651 ArenaAllocKind kind)
2652 : HInstruction(inst_kind, type, side_effects, dex_pc),
2653 inputs_(number_of_inputs, allocator->Adapter(kind)) {}
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002654
Artem Serovcced8ba2017-07-19 18:18:09 +01002655 DEFAULT_COPY_CONSTRUCTOR(VariableInputSizeInstruction);
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002656
Artem Serovcced8ba2017-07-19 18:18:09 +01002657 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002658};
2659
Vladimir Markof9f64412015-09-02 14:05:49 +01002660template<size_t N>
Vladimir Markobd785672018-05-03 17:09:09 +01002661class HExpression : public HInstruction {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002663 HExpression<N>(InstructionKind kind, SideEffects side_effects, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302664 : HInstruction(kind, side_effects, dex_pc), inputs_() {}
Vladimir Markobd785672018-05-03 17:09:09 +01002665 HExpression<N>(InstructionKind kind,
2666 DataType::Type type,
2667 SideEffects side_effects,
2668 uint32_t dex_pc)
2669 : HInstruction(kind, type, side_effects, dex_pc), inputs_() {}
2670 virtual ~HExpression() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671
Vladimir Marko372f10e2016-05-17 16:30:10 +01002672 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002673 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002674 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002675 }
2676
Artem Serovcced8ba2017-07-19 18:18:09 +01002677 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002678 DEFAULT_COPY_CONSTRUCTOR(Expression<N>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002679
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002681 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002682
2683 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002684};
2685
Vladimir Markobd785672018-05-03 17:09:09 +01002686// HExpression specialization for N=0.
Vladimir Markof9f64412015-09-02 14:05:49 +01002687template<>
Vladimir Markobd785672018-05-03 17:09:09 +01002688class HExpression<0> : public HInstruction {
Vladimir Markof9f64412015-09-02 14:05:49 +01002689 public:
Vladimir Markobd785672018-05-03 17:09:09 +01002690 using HInstruction::HInstruction;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002691
Vladimir Markobd785672018-05-03 17:09:09 +01002692 virtual ~HExpression() {}
Vladimir Markof9f64412015-09-02 14:05:49 +01002693
Vladimir Marko372f10e2016-05-17 16:30:10 +01002694 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01002695 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01002696 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002697 }
2698
Artem Serovcced8ba2017-07-19 18:18:09 +01002699 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01002700 DEFAULT_COPY_CONSTRUCTOR(Expression<0>);
Artem Serovcced8ba2017-07-19 18:18:09 +01002701
Vladimir Markof9f64412015-09-02 14:05:49 +01002702 private:
2703 friend class SsaBuilder;
2704};
2705
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002706// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2707// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002708class HReturnVoid final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002709 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002711 : HExpression(kReturnVoid, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302712 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002714 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002715
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002716 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002717
Artem Serovcced8ba2017-07-19 18:18:09 +01002718 protected:
2719 DEFAULT_COPY_CONSTRUCTOR(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002720};
2721
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002722// Represents dex's RETURN opcodes. A HReturn is a control flow
2723// instruction that branches to the exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002724class HReturn final : public HExpression<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002725 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002726 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002727 : HExpression(kReturn, SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002728 SetRawInputAt(0, value);
2729 }
2730
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002731 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002732
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002733 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002734
Artem Serovcced8ba2017-07-19 18:18:09 +01002735 protected:
2736 DEFAULT_COPY_CONSTRUCTOR(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002737};
2738
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002739class HPhi final : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002740 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002741 HPhi(ArenaAllocator* allocator,
David Brazdildee58d62016-04-07 09:54:26 +00002742 uint32_t reg_number,
2743 size_t number_of_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 DataType::Type type,
David Brazdildee58d62016-04-07 09:54:26 +00002745 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002746 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302747 kPhi,
Vladimir Markobd785672018-05-03 17:09:09 +01002748 ToPhiType(type),
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002749 SideEffects::None(),
2750 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01002751 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002752 number_of_inputs,
2753 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002754 reg_number_(reg_number) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 DCHECK_NE(GetType(), DataType::Type::kVoid);
David Brazdildee58d62016-04-07 09:54:26 +00002756 // Phis are constructed live and marked dead if conflicting or unused.
2757 // Individual steps of SsaBuilder should assume that if a phi has been
2758 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2759 SetPackedFlag<kFlagIsLive>(true);
2760 SetPackedFlag<kFlagCanBeNull>(true);
2761 }
2762
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002763 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01002764
David Brazdildee58d62016-04-07 09:54:26 +00002765 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 static DataType::Type ToPhiType(DataType::Type type) {
2767 return DataType::Kind(type);
David Brazdildee58d62016-04-07 09:54:26 +00002768 }
2769
2770 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002772 void SetType(DataType::Type new_type) {
David Brazdildee58d62016-04-07 09:54:26 +00002773 // Make sure that only valid type changes occur. The following are allowed:
2774 // (1) int -> float/ref (primitive type propagation),
2775 // (2) long -> double (primitive type propagation).
2776 DCHECK(GetType() == new_type ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kFloat32) ||
2778 (GetType() == DataType::Type::kInt32 && new_type == DataType::Type::kReference) ||
2779 (GetType() == DataType::Type::kInt64 && new_type == DataType::Type::kFloat64));
David Brazdildee58d62016-04-07 09:54:26 +00002780 SetPackedField<TypeField>(new_type);
2781 }
2782
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002783 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
David Brazdildee58d62016-04-07 09:54:26 +00002784 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2785
2786 uint32_t GetRegNumber() const { return reg_number_; }
2787
2788 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2789 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2790 bool IsDead() const { return !IsLive(); }
2791 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2792
Vladimir Markoe9004912016-06-16 16:50:52 +01002793 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002794 return other != nullptr
2795 && other->IsPhi()
2796 && other->AsPhi()->GetBlock() == GetBlock()
2797 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2798 }
2799
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002800 bool HasEquivalentPhi() const {
2801 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2802 return true;
2803 }
2804 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2805 return true;
2806 }
2807 return false;
2808 }
2809
David Brazdildee58d62016-04-07 09:54:26 +00002810 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2811 // An equivalent phi is a phi having the same dex register and type.
2812 // It assumes that phis with the same dex register are adjacent.
2813 HPhi* GetNextEquivalentPhiWithSameType() {
2814 HInstruction* next = GetNext();
2815 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2816 if (next->GetType() == GetType()) {
2817 return next->AsPhi();
2818 }
2819 next = next->GetNext();
2820 }
2821 return nullptr;
2822 }
2823
2824 DECLARE_INSTRUCTION(Phi);
2825
Artem Serovcced8ba2017-07-19 18:18:09 +01002826 protected:
2827 DEFAULT_COPY_CONSTRUCTOR(Phi);
2828
David Brazdildee58d62016-04-07 09:54:26 +00002829 private:
Vladimir Markobd785672018-05-03 17:09:09 +01002830 static constexpr size_t kFlagIsLive = HInstruction::kNumberOfGenericPackedBits;
David Brazdildee58d62016-04-07 09:54:26 +00002831 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2832 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2833 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
David Brazdildee58d62016-04-07 09:54:26 +00002834
David Brazdildee58d62016-04-07 09:54:26 +00002835 const uint32_t reg_number_;
David Brazdildee58d62016-04-07 09:54:26 +00002836};
2837
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002838// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002839// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002840// exit block.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002841class HExit final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002842 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302843 explicit HExit(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002844 : HExpression(kExit, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302845 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002846
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002847 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002848
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002849 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002850
Artem Serovcced8ba2017-07-19 18:18:09 +01002851 protected:
2852 DEFAULT_COPY_CONSTRUCTOR(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002853};
2854
2855// Jumps from one block to another.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002856class HGoto final : public HExpression<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002857 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302858 explicit HGoto(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01002859 : HExpression(kGoto, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302860 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002862 bool IsClonable() const override { return true; }
2863 bool IsControlFlow() const override { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002864
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002865 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002866 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002867 }
2868
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002869 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002870
Artem Serovcced8ba2017-07-19 18:18:09 +01002871 protected:
2872 DEFAULT_COPY_CONSTRUCTOR(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002873};
2874
Roland Levillain9867bc72015-08-05 10:21:34 +01002875class HConstant : public HExpression<0> {
2876 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302877 explicit HConstant(InstructionKind kind, DataType::Type type, uint32_t dex_pc = kNoDexPc)
2878 : HExpression(kind, type, SideEffects::None(), dex_pc) {
2879 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002880
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002881 bool CanBeMoved() const override { return true; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002882
Roland Levillain1a653882016-03-18 18:05:57 +00002883 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002884 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002885 // Is this constant 0 in the arithmetic sense?
2886 virtual bool IsArithmeticZero() const { return false; }
2887 // Is this constant a 0-bit pattern?
2888 virtual bool IsZeroBitPattern() const { return false; }
2889 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002890 virtual bool IsOne() const { return false; }
2891
David Brazdil77a48ae2015-09-15 12:34:04 +00002892 virtual uint64_t GetValueAsUint64() const = 0;
2893
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002894 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002895
Artem Serovcced8ba2017-07-19 18:18:09 +01002896 protected:
2897 DEFAULT_COPY_CONSTRUCTOR(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002898};
2899
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002900class HNullConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002901 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002902 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01002903 return true;
2904 }
2905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002906 uint64_t GetValueAsUint64() const override { return 0; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002908 size_t ComputeHashCode() const override { return 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002909
Roland Levillain1a653882016-03-18 18:05:57 +00002910 // The null constant representation is a 0-bit pattern.
2911 virtual bool IsZeroBitPattern() const { return true; }
2912
Roland Levillain9867bc72015-08-05 10:21:34 +01002913 DECLARE_INSTRUCTION(NullConstant);
2914
Artem Serovcced8ba2017-07-19 18:18:09 +01002915 protected:
2916 DEFAULT_COPY_CONSTRUCTOR(NullConstant);
2917
Roland Levillain9867bc72015-08-05 10:21:34 +01002918 private:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 explicit HNullConstant(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302920 : HConstant(kNullConstant, DataType::Type::kReference, dex_pc) {
2921 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002922
2923 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01002924};
2925
2926// Constants of the type int. Those can be from Dex instructions, or
2927// synthesized (for example with the if-eqz instruction).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002928class HIntConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002929 public:
2930 int32_t GetValue() const { return value_; }
2931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002932 uint64_t GetValueAsUint64() const override {
David Brazdil9f389d42015-10-01 14:32:56 +01002933 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2934 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002936 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002937 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002938 return other->AsIntConstant()->value_ == value_;
2939 }
2940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002941 size_t ComputeHashCode() const override { return GetValue(); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002942
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002943 bool IsMinusOne() const override { return GetValue() == -1; }
2944 bool IsArithmeticZero() const override { return GetValue() == 0; }
2945 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2946 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002947
Roland Levillain1a653882016-03-18 18:05:57 +00002948 // Integer constants are used to encode Boolean values as well,
2949 // where 1 means true and 0 means false.
2950 bool IsTrue() const { return GetValue() == 1; }
2951 bool IsFalse() const { return GetValue() == 0; }
2952
Roland Levillain9867bc72015-08-05 10:21:34 +01002953 DECLARE_INSTRUCTION(IntConstant);
2954
Artem Serovcced8ba2017-07-19 18:18:09 +01002955 protected:
2956 DEFAULT_COPY_CONSTRUCTOR(IntConstant);
2957
Roland Levillain9867bc72015-08-05 10:21:34 +01002958 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002959 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302960 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc), value_(value) {
2961 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002962 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302963 : HConstant(kIntConstant, DataType::Type::kInt32, dex_pc),
2964 value_(value ? 1 : 0) {
2965 }
Roland Levillain9867bc72015-08-05 10:21:34 +01002966
2967 const int32_t value_;
2968
2969 friend class HGraph;
2970 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2971 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
Roland Levillain9867bc72015-08-05 10:21:34 +01002972};
2973
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002974class HLongConstant final : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002975 public:
2976 int64_t GetValue() const { return value_; }
2977
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002978 uint64_t GetValueAsUint64() const override { return value_; }
David Brazdil77a48ae2015-09-15 12:34:04 +00002979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002980 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002981 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002982 return other->AsLongConstant()->value_ == value_;
2983 }
2984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002985 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain9867bc72015-08-05 10:21:34 +01002986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01002987 bool IsMinusOne() const override { return GetValue() == -1; }
2988 bool IsArithmeticZero() const override { return GetValue() == 0; }
2989 bool IsZeroBitPattern() const override { return GetValue() == 0; }
2990 bool IsOne() const override { return GetValue() == 1; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002991
2992 DECLARE_INSTRUCTION(LongConstant);
2993
Artem Serovcced8ba2017-07-19 18:18:09 +01002994 protected:
2995 DEFAULT_COPY_CONSTRUCTOR(LongConstant);
2996
Roland Levillain9867bc72015-08-05 10:21:34 +01002997 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002998 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05302999 : HConstant(kLongConstant, DataType::Type::kInt64, dex_pc),
3000 value_(value) {
3001 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003002
3003 const int64_t value_;
3004
3005 friend class HGraph;
Roland Levillain9867bc72015-08-05 10:21:34 +01003006};
Dave Allison20dfc792014-06-16 20:44:29 -07003007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003008class HFloatConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003009 public:
3010 float GetValue() const { return value_; }
3011
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003012 uint64_t GetValueAsUint64() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003013 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
3014 }
3015
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003016 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003017 DCHECK(other->IsFloatConstant()) << other->DebugName();
3018 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
3019 }
3020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003021 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003022
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003023 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003024 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
3025 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003026 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003027 return std::fpclassify(value_) == FP_ZERO;
3028 }
3029 bool IsArithmeticPositiveZero() const {
3030 return IsArithmeticZero() && !std::signbit(value_);
3031 }
3032 bool IsArithmeticNegativeZero() const {
3033 return IsArithmeticZero() && std::signbit(value_);
3034 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003035 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003036 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00003037 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003038 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003039 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
3040 }
3041 bool IsNaN() const {
3042 return std::isnan(value_);
3043 }
3044
3045 DECLARE_INSTRUCTION(FloatConstant);
3046
Artem Serovcced8ba2017-07-19 18:18:09 +01003047 protected:
3048 DEFAULT_COPY_CONSTRUCTOR(FloatConstant);
3049
Roland Levillain31dd3d62016-02-16 12:21:02 +00003050 private:
3051 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303052 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3053 value_(value) {
3054 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003055 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303056 : HConstant(kFloatConstant, DataType::Type::kFloat32, dex_pc),
3057 value_(bit_cast<float, int32_t>(value)) {
3058 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003059
3060 const float value_;
3061
3062 // Only the SsaBuilder and HGraph can create floating-point constants.
3063 friend class SsaBuilder;
3064 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003065};
3066
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003067class HDoubleConstant final : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003068 public:
3069 double GetValue() const { return value_; }
3070
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003071 uint64_t GetValueAsUint64() const override { return bit_cast<uint64_t, double>(value_); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003072
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003073 bool InstructionDataEquals(const HInstruction* other) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003074 DCHECK(other->IsDoubleConstant()) << other->DebugName();
3075 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
3076 }
3077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003078 size_t ComputeHashCode() const override { return static_cast<size_t>(GetValue()); }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003080 bool IsMinusOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003081 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
3082 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003083 bool IsArithmeticZero() const override {
Roland Levillain1a653882016-03-18 18:05:57 +00003084 return std::fpclassify(value_) == FP_ZERO;
3085 }
3086 bool IsArithmeticPositiveZero() const {
3087 return IsArithmeticZero() && !std::signbit(value_);
3088 }
3089 bool IsArithmeticNegativeZero() const {
3090 return IsArithmeticZero() && std::signbit(value_);
3091 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003092 bool IsZeroBitPattern() const override {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00003093 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00003094 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003095 bool IsOne() const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
3097 }
3098 bool IsNaN() const {
3099 return std::isnan(value_);
3100 }
3101
3102 DECLARE_INSTRUCTION(DoubleConstant);
3103
Artem Serovcced8ba2017-07-19 18:18:09 +01003104 protected:
3105 DEFAULT_COPY_CONSTRUCTOR(DoubleConstant);
3106
Roland Levillain31dd3d62016-02-16 12:21:02 +00003107 private:
3108 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303109 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3110 value_(value) {
3111 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003112 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303113 : HConstant(kDoubleConstant, DataType::Type::kFloat64, dex_pc),
3114 value_(bit_cast<double, int64_t>(value)) {
3115 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003116
3117 const double value_;
3118
3119 // Only the SsaBuilder and HGraph can create floating-point constants.
3120 friend class SsaBuilder;
3121 friend class HGraph;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122};
3123
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003124// Conditional branch. A block ending with an HIf instruction must have
3125// two successors.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003126class HIf final : public HExpression<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003127 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003128 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003129 : HExpression(kIf, SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003130 SetRawInputAt(0, input);
3131 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003133 bool IsClonable() const override { return true; }
3134 bool IsControlFlow() const override { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003135
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003136 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003137 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003138 }
3139
3140 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01003141 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003142 }
3143
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003144 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003145
Artem Serovcced8ba2017-07-19 18:18:09 +01003146 protected:
3147 DEFAULT_COPY_CONSTRUCTOR(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00003148};
3149
David Brazdilfc6a86a2015-06-26 10:33:45 +00003150
3151// Abstract instruction which marks the beginning and/or end of a try block and
3152// links it to the respective exception handlers. Behaves the same as a Goto in
3153// non-exceptional control flow.
3154// Normal-flow successor is stored at index zero, exception handlers under
3155// higher indices in no particular order.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003156class HTryBoundary final : public HExpression<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003157 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003158 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01003159 kEntry,
3160 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003161 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01003162 };
3163
Artem Serovd1aa7d02018-06-22 11:35:46 +01003164 // SideEffects::CanTriggerGC prevents instructions with SideEffects::DependOnGC to be alive
3165 // across the catch block entering edges as GC might happen during throwing an exception.
3166 // TryBoundary with BoundaryKind::kExit is conservatively used for that as there is no
3167 // HInstruction which a catch block must start from.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003168 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01003169 : HExpression(kTryBoundary,
3170 (kind == BoundaryKind::kExit) ? SideEffects::CanTriggerGC()
3171 : SideEffects::None(),
3172 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003173 SetPackedField<BoundaryKindField>(kind);
3174 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003175
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003176 bool IsControlFlow() const override { return true; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003177
3178 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01003179 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003180
David Brazdild26a4112015-11-10 11:07:31 +00003181 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
3182 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
3183 }
3184
David Brazdilfc6a86a2015-06-26 10:33:45 +00003185 // Returns whether `handler` is among its exception handlers (non-zero index
3186 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01003187 bool HasExceptionHandler(const HBasicBlock& handler) const {
3188 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00003189 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00003190 }
3191
3192 // If not present already, adds `handler` to its block's list of exception
3193 // handlers.
3194 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01003195 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00003196 GetBlock()->AddSuccessor(handler);
3197 }
3198 }
3199
Vladimir Markoa1de9182016-02-25 11:37:38 +00003200 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
3201 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00003202
David Brazdilffee3d32015-07-06 11:48:53 +01003203 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
3204
David Brazdilfc6a86a2015-06-26 10:33:45 +00003205 DECLARE_INSTRUCTION(TryBoundary);
3206
Artem Serovcced8ba2017-07-19 18:18:09 +01003207 protected:
3208 DEFAULT_COPY_CONSTRUCTOR(TryBoundary);
3209
David Brazdilfc6a86a2015-06-26 10:33:45 +00003210 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003211 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
3212 static constexpr size_t kFieldBoundaryKindSize =
3213 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
3214 static constexpr size_t kNumberOfTryBoundaryPackedBits =
3215 kFieldBoundaryKind + kFieldBoundaryKindSize;
3216 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
3217 "Too many packed fields.");
3218 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00003219};
3220
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003221// Deoptimize to interpreter, upon checking a condition.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003222class HDeoptimize final : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003223 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003224 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3225 // across.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003226 HDeoptimize(ArenaAllocator* allocator,
3227 HInstruction* cond,
3228 DeoptimizationKind kind,
3229 uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003230 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303231 kDeoptimize,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003232 SideEffects::All(),
3233 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003234 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003235 /* number_of_inputs */ 1,
3236 kArenaAllocMisc) {
3237 SetPackedFlag<kFieldCanBeMoved>(false);
3238 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003239 SetRawInputAt(0, cond);
3240 }
3241
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003242 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003243
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003244 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3245 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3246 // instead of `guard`.
3247 // We set CanTriggerGC to prevent any intermediate address to be live
3248 // at the point of the `HDeoptimize`.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003249 HDeoptimize(ArenaAllocator* allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003250 HInstruction* cond,
3251 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003252 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003253 uint32_t dex_pc)
3254 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303255 kDeoptimize,
Vladimir Markobd785672018-05-03 17:09:09 +01003256 guard->GetType(),
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003257 SideEffects::CanTriggerGC(),
3258 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003259 allocator,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003260 /* number_of_inputs */ 2,
3261 kArenaAllocMisc) {
3262 SetPackedFlag<kFieldCanBeMoved>(true);
3263 SetPackedField<DeoptimizeKindField>(kind);
3264 SetRawInputAt(0, cond);
3265 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003266 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003268 bool CanBeMoved() const override { return GetPackedFlag<kFieldCanBeMoved>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003269
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003270 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003271 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3272 }
3273
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003274 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003276 bool CanThrow() const override { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003277
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003278 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003279
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003280 bool GuardsAnInput() const {
3281 return InputCount() == 2;
3282 }
3283
3284 HInstruction* GuardedInput() const {
3285 DCHECK(GuardsAnInput());
3286 return InputAt(1);
3287 }
3288
3289 void RemoveGuard() {
3290 RemoveInputAt(1);
3291 }
3292
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003293 DECLARE_INSTRUCTION(Deoptimize);
3294
Artem Serovcced8ba2017-07-19 18:18:09 +01003295 protected:
3296 DEFAULT_COPY_CONSTRUCTOR(Deoptimize);
3297
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003298 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003299 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3300 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3301 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003302 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003303 static constexpr size_t kNumberOfDeoptimizePackedBits =
3304 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3305 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3306 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003307 using DeoptimizeKindField =
3308 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003309};
3310
Mingyao Yang063fc772016-08-02 11:02:54 -07003311// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3312// The compiled code checks this flag value in a guard before devirtualized call and
3313// if it's true, starts to do deoptimization.
3314// It has a 4-byte slot on stack.
3315// TODO: allocate a register for this flag.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003316class HShouldDeoptimizeFlag final : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003317 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003318 // CHA guards are only optimized in a separate pass and it has no side effects
3319 // with regard to other passes.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01003320 HShouldDeoptimizeFlag(ArenaAllocator* allocator, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303321 : HVariableInputSizeInstruction(kShouldDeoptimizeFlag,
Vladimir Markobd785672018-05-03 17:09:09 +01003322 DataType::Type::kInt32,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303323 SideEffects::None(),
3324 dex_pc,
3325 allocator,
3326 0,
3327 kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003328 }
3329
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003330 // We do all CHA guard elimination/motion in a single pass, after which there is no
3331 // further guard elimination/motion since a guard might have been used for justification
3332 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3333 // to avoid other optimizations trying to move it.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003334 bool CanBeMoved() const override { return false; }
Mingyao Yang063fc772016-08-02 11:02:54 -07003335
3336 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3337
Artem Serovcced8ba2017-07-19 18:18:09 +01003338 protected:
3339 DEFAULT_COPY_CONSTRUCTOR(ShouldDeoptimizeFlag);
Mingyao Yang063fc772016-08-02 11:02:54 -07003340};
3341
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003342// Represents the ArtMethod that was passed as a first argument to
3343// the method. It is used by instructions that depend on it, like
3344// instructions that work with the dex cache.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003345class HCurrentMethod final : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003346 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003347 explicit HCurrentMethod(DataType::Type type, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303348 : HExpression(kCurrentMethod, type, SideEffects::None(), dex_pc) {
3349 }
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003350
3351 DECLARE_INSTRUCTION(CurrentMethod);
3352
Artem Serovcced8ba2017-07-19 18:18:09 +01003353 protected:
3354 DEFAULT_COPY_CONSTRUCTOR(CurrentMethod);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003355};
3356
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003357// Fetches an ArtMethod from the virtual table or the interface method table
3358// of a class.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003359class HClassTableGet final : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003360 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003362 kVTable,
3363 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003364 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003365 };
3366 HClassTableGet(HInstruction* cls,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003367 DataType::Type type,
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003368 TableKind kind,
3369 size_t index,
3370 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303371 : HExpression(kClassTableGet, type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003372 index_(index) {
3373 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003374 SetRawInputAt(0, cls);
3375 }
3376
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003377 bool IsClonable() const override { return true; }
3378 bool CanBeMoved() const override { return true; }
3379 bool InstructionDataEquals(const HInstruction* other) const override {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003380 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003381 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003382 }
3383
Vladimir Markoa1de9182016-02-25 11:37:38 +00003384 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003385 size_t GetIndex() const { return index_; }
3386
3387 DECLARE_INSTRUCTION(ClassTableGet);
3388
Artem Serovcced8ba2017-07-19 18:18:09 +01003389 protected:
3390 DEFAULT_COPY_CONSTRUCTOR(ClassTableGet);
3391
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003392 private:
Vladimir Markobd785672018-05-03 17:09:09 +01003393 static constexpr size_t kFieldTableKind = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003394 static constexpr size_t kFieldTableKindSize =
3395 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3396 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3397 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3398 "Too many packed fields.");
3399 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3400
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003401 // The index of the ArtMethod in the table.
3402 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003403};
3404
Mark Mendellfe57faa2015-09-18 09:26:15 -04003405// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3406// have one successor for each entry in the switch table, and the final successor
3407// will be the block containing the next Dex opcode.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003408class HPackedSwitch final : public HExpression<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003409 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003410 HPackedSwitch(int32_t start_value,
3411 uint32_t num_entries,
3412 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003413 uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01003414 : HExpression(kPackedSwitch, SideEffects::None(), dex_pc),
Mark Mendellfe57faa2015-09-18 09:26:15 -04003415 start_value_(start_value),
3416 num_entries_(num_entries) {
3417 SetRawInputAt(0, input);
3418 }
3419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003420 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003421
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003422 bool IsControlFlow() const override { return true; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003423
3424 int32_t GetStartValue() const { return start_value_; }
3425
Vladimir Marko211c2112015-09-24 16:52:33 +01003426 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003427
3428 HBasicBlock* GetDefaultBlock() const {
3429 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003430 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003431 }
3432 DECLARE_INSTRUCTION(PackedSwitch);
3433
Artem Serovcced8ba2017-07-19 18:18:09 +01003434 protected:
3435 DEFAULT_COPY_CONSTRUCTOR(PackedSwitch);
3436
Mark Mendellfe57faa2015-09-18 09:26:15 -04003437 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003438 const int32_t start_value_;
3439 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003440};
3441
Roland Levillain88cb1752014-10-20 16:36:47 +01003442class HUnaryOperation : public HExpression<1> {
3443 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303444 HUnaryOperation(InstructionKind kind,
3445 DataType::Type result_type,
3446 HInstruction* input,
3447 uint32_t dex_pc = kNoDexPc)
3448 : HExpression(kind, result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003449 SetRawInputAt(0, input);
3450 }
3451
Artem Serovcced8ba2017-07-19 18:18:09 +01003452 // All of the UnaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003453 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003454
Roland Levillain88cb1752014-10-20 16:36:47 +01003455 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003456 DataType::Type GetResultType() const { return GetType(); }
Roland Levillain88cb1752014-10-20 16:36:47 +01003457
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003458 bool CanBeMoved() const override { return true; }
3459 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003460 return true;
3461 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003462
Roland Levillain31dd3d62016-02-16 12:21:02 +00003463 // Try to statically evaluate `this` and return a HConstant
3464 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003465 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003466 HConstant* TryStaticEvaluation() const;
3467
3468 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003469 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3470 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3472 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003473
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003474 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003475
Artem Serovcced8ba2017-07-19 18:18:09 +01003476 protected:
3477 DEFAULT_COPY_CONSTRUCTOR(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003478};
3479
Dave Allison20dfc792014-06-16 20:44:29 -07003480class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003481 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303482 HBinaryOperation(InstructionKind kind,
3483 DataType::Type result_type,
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003484 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003485 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003486 SideEffects side_effects = SideEffects::None(),
3487 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303488 : HExpression(kind, result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003489 SetRawInputAt(0, left);
3490 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003491 }
3492
Artem Serovcced8ba2017-07-19 18:18:09 +01003493 // All of the BinaryOperation instructions are clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003494 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01003495
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003496 HInstruction* GetLeft() const { return InputAt(0); }
3497 HInstruction* GetRight() const { return InputAt(1); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 DataType::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003499
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003500 virtual bool IsCommutative() const { return false; }
3501
3502 // Put constant on the right.
3503 // Returns whether order is changed.
3504 bool OrderInputsWithConstantOnTheRight() {
3505 HInstruction* left = InputAt(0);
3506 HInstruction* right = InputAt(1);
3507 if (left->IsConstant() && !right->IsConstant()) {
3508 ReplaceInput(right, 0);
3509 ReplaceInput(left, 1);
3510 return true;
3511 }
3512 return false;
3513 }
3514
3515 // Order inputs by instruction id, but favor constant on the right side.
3516 // This helps GVN for commutative ops.
3517 void OrderInputs() {
3518 DCHECK(IsCommutative());
3519 HInstruction* left = InputAt(0);
3520 HInstruction* right = InputAt(1);
3521 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3522 return;
3523 }
3524 if (OrderInputsWithConstantOnTheRight()) {
3525 return;
3526 }
3527 // Order according to instruction id.
3528 if (left->GetId() > right->GetId()) {
3529 ReplaceInput(right, 0);
3530 ReplaceInput(left, 1);
3531 }
3532 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003533
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003534 bool CanBeMoved() const override { return true; }
3535 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003536 return true;
3537 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003538
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 // Try to statically evaluate `this` and return a HConstant
3540 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003541 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003542 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003543
3544 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3546 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003547 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3548 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003550 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3551 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003552 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3553 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003554 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3555 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003556 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3558 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003559
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003560 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003561 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003562 HConstant* GetConstantRight() const;
3563
3564 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003565 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003566 HInstruction* GetLeastConstantLeft() const;
3567
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003568 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003569
Artem Serovcced8ba2017-07-19 18:18:09 +01003570 protected:
3571 DEFAULT_COPY_CONSTRUCTOR(BinaryOperation);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003572};
3573
Mark Mendellc4701932015-04-10 13:18:51 -04003574// The comparison bias applies for floating point operations and indicates how NaN
3575// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003576enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003577 kNoBias, // bias is not applicable (i.e. for long operation)
3578 kGtBias, // return 1 for NaN comparisons
3579 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003580 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003581};
3582
Roland Levillain31dd3d62016-02-16 12:21:02 +00003583std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3584
Dave Allison20dfc792014-06-16 20:44:29 -07003585class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003586 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303587 HCondition(InstructionKind kind,
3588 HInstruction* first,
3589 HInstruction* second,
3590 uint32_t dex_pc = kNoDexPc)
3591 : HBinaryOperation(kind,
3592 DataType::Type::kBool,
3593 first,
3594 second,
3595 SideEffects::None(),
3596 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003597 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3598 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003599
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003600 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003601 // `instruction`, and disregard moves in between.
3602 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003603
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003604 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003605
3606 virtual IfCondition GetCondition() const = 0;
3607
Mark Mendellc4701932015-04-10 13:18:51 -04003608 virtual IfCondition GetOppositeCondition() const = 0;
3609
Vladimir Markoa1de9182016-02-25 11:37:38 +00003610 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003611 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3612
Vladimir Markoa1de9182016-02-25 11:37:38 +00003613 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3614 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003615
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003616 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003617 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003618 }
3619
Roland Levillain4fa13f62015-07-06 18:11:54 +01003620 bool IsFPConditionTrueIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003622 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003623 if (if_cond == kCondNE) {
3624 return true;
3625 } else if (if_cond == kCondEQ) {
3626 return false;
3627 }
3628 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003629 }
3630
3631 bool IsFPConditionFalseIfNaN() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003633 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003634 if (if_cond == kCondEQ) {
3635 return true;
3636 } else if (if_cond == kCondNE) {
3637 return false;
3638 }
3639 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003640 }
3641
Roland Levillain31dd3d62016-02-16 12:21:02 +00003642 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003643 // Needed if we merge a HCompare into a HCondition.
Vladimir Markobd785672018-05-03 17:09:09 +01003644 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003645 static constexpr size_t kFieldComparisonBiasSize =
3646 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3647 static constexpr size_t kNumberOfConditionPackedBits =
3648 kFieldComparisonBias + kFieldComparisonBiasSize;
3649 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3650 using ComparisonBiasField =
3651 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3652
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 template <typename T>
3654 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3655
3656 template <typename T>
3657 int32_t CompareFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3660 // Handle the bias.
3661 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3662 }
3663
3664 // Return an integer constant containing the result of a condition evaluated at compile time.
3665 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3666 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3667 }
3668
Artem Serovcced8ba2017-07-19 18:18:09 +01003669 DEFAULT_COPY_CONSTRUCTOR(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003670};
3671
3672// Instruction to check if two inputs are equal to each other.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003673class HEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003674 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003675 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303676 : HCondition(kEqual, first, second, dex_pc) {
3677 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003678
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003679 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003680
Vladimir Marko9e23df52015-11-10 17:14:35 +00003681 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003682 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeConstantCondition(true, GetDexPc());
3684 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003685 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003686 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3687 }
3688 // In the following Evaluate methods, a HCompare instruction has
3689 // been merged into this HEqual instruction; evaluate it as
3690 // `Compare(x, y) == 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003691 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3693 GetDexPc());
3694 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003695 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3697 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003698 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003699 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003700 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003701
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003702 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003704 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003705 return kCondEQ;
3706 }
3707
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003708 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003709 return kCondNE;
3710 }
3711
Artem Serovcced8ba2017-07-19 18:18:09 +01003712 protected:
3713 DEFAULT_COPY_CONSTRUCTOR(Equal);
3714
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003715 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003716 template <typename T> static bool Compute(T x, T y) { return x == y; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003717};
3718
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003719class HNotEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003720 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303721 HNotEqual(HInstruction* first, HInstruction* second,
3722 uint32_t dex_pc = kNoDexPc)
3723 : HCondition(kNotEqual, first, second, dex_pc) {
3724 }
Dave Allison20dfc792014-06-16 20:44:29 -07003725
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003726 bool IsCommutative() const override { return true; }
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003727
Vladimir Marko9e23df52015-11-10 17:14:35 +00003728 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003729 HNullConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(false, GetDexPc());
3731 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3734 }
3735 // In the following Evaluate methods, a HCompare instruction has
3736 // been merged into this HNotEqual instruction; evaluate it as
3737 // `Compare(x, y) != 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003738 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3740 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003741 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3743 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003744 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003746 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003747
Dave Allison20dfc792014-06-16 20:44:29 -07003748 DECLARE_INSTRUCTION(NotEqual);
3749
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003750 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003751 return kCondNE;
3752 }
3753
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003754 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003755 return kCondEQ;
3756 }
3757
Artem Serovcced8ba2017-07-19 18:18:09 +01003758 protected:
3759 DEFAULT_COPY_CONSTRUCTOR(NotEqual);
3760
Dave Allison20dfc792014-06-16 20:44:29 -07003761 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003762 template <typename T> static bool Compute(T x, T y) { return x != y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003763};
3764
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003765class HLessThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003766 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303767 HLessThan(HInstruction* first, HInstruction* second,
3768 uint32_t dex_pc = kNoDexPc)
3769 : HCondition(kLessThan, first, second, dex_pc) {
3770 }
Dave Allison20dfc792014-06-16 20:44:29 -07003771
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003773 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003774 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003775 // In the following Evaluate methods, a HCompare instruction has
3776 // been merged into this HLessThan instruction; evaluate it as
3777 // `Compare(x, y) < 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003778 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3780 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003781 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3783 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003784 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003786 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003787
Dave Allison20dfc792014-06-16 20:44:29 -07003788 DECLARE_INSTRUCTION(LessThan);
3789
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003790 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003791 return kCondLT;
3792 }
3793
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003794 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003795 return kCondGE;
3796 }
3797
Artem Serovcced8ba2017-07-19 18:18:09 +01003798 protected:
3799 DEFAULT_COPY_CONSTRUCTOR(LessThan);
3800
Dave Allison20dfc792014-06-16 20:44:29 -07003801 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003802 template <typename T> static bool Compute(T x, T y) { return x < y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003803};
3804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003805class HLessThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003806 public:
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303807 HLessThanOrEqual(HInstruction* first, HInstruction* second,
3808 uint32_t dex_pc = kNoDexPc)
3809 : HCondition(kLessThanOrEqual, first, second, dex_pc) {
3810 }
Dave Allison20dfc792014-06-16 20:44:29 -07003811
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003812 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003813 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003814 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003815 // In the following Evaluate methods, a HCompare instruction has
3816 // been merged into this HLessThanOrEqual instruction; evaluate it as
3817 // `Compare(x, y) <= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003818 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3820 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003821 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3823 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003824 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003825 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003826 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003827
Dave Allison20dfc792014-06-16 20:44:29 -07003828 DECLARE_INSTRUCTION(LessThanOrEqual);
3829
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003830 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003831 return kCondLE;
3832 }
3833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003834 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003835 return kCondGT;
3836 }
3837
Artem Serovcced8ba2017-07-19 18:18:09 +01003838 protected:
3839 DEFAULT_COPY_CONSTRUCTOR(LessThanOrEqual);
3840
Dave Allison20dfc792014-06-16 20:44:29 -07003841 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003842 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003843};
3844
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003845class HGreaterThan final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003846 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003847 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303848 : HCondition(kGreaterThan, first, second, dex_pc) {
3849 }
Dave Allison20dfc792014-06-16 20:44:29 -07003850
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003851 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003853 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 // In the following Evaluate methods, a HCompare instruction has
3855 // been merged into this HGreaterThan instruction; evaluate it as
3856 // `Compare(x, y) > 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003857 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3859 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003860 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3862 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003863 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003865 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003866
Dave Allison20dfc792014-06-16 20:44:29 -07003867 DECLARE_INSTRUCTION(GreaterThan);
3868
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003869 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003870 return kCondGT;
3871 }
3872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003873 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003874 return kCondLE;
3875 }
3876
Artem Serovcced8ba2017-07-19 18:18:09 +01003877 protected:
3878 DEFAULT_COPY_CONSTRUCTOR(GreaterThan);
3879
Dave Allison20dfc792014-06-16 20:44:29 -07003880 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003881 template <typename T> static bool Compute(T x, T y) { return x > y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003882};
3883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003884class HGreaterThanOrEqual final : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003885 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003886 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303887 : HCondition(kGreaterThanOrEqual, first, second, dex_pc) {
3888 }
Dave Allison20dfc792014-06-16 20:44:29 -07003889
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003890 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003891 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003892 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003893 // In the following Evaluate methods, a HCompare instruction has
3894 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3895 // `Compare(x, y) >= 0`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003896 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3898 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003899 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003900 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3901 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003902 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003903 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003904 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003905
Dave Allison20dfc792014-06-16 20:44:29 -07003906 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3907
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003908 IfCondition GetCondition() const override {
Dave Allison20dfc792014-06-16 20:44:29 -07003909 return kCondGE;
3910 }
3911
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003912 IfCondition GetOppositeCondition() const override {
Mark Mendellc4701932015-04-10 13:18:51 -04003913 return kCondLT;
3914 }
3915
Artem Serovcced8ba2017-07-19 18:18:09 +01003916 protected:
3917 DEFAULT_COPY_CONSTRUCTOR(GreaterThanOrEqual);
3918
Dave Allison20dfc792014-06-16 20:44:29 -07003919 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003920 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Dave Allison20dfc792014-06-16 20:44:29 -07003921};
3922
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003923class HBelow final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003924 public:
3925 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303926 : HCondition(kBelow, first, second, dex_pc) {
3927 }
Aart Bike9f37602015-10-09 11:15:55 -07003928
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003929 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003930 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003931 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003932 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003933 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3934 }
3935 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003936 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003937 LOG(FATAL) << DebugName() << " is not defined for float values";
3938 UNREACHABLE();
3939 }
3940 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003941 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003942 LOG(FATAL) << DebugName() << " is not defined for double values";
3943 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003944 }
3945
3946 DECLARE_INSTRUCTION(Below);
3947
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003948 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003949 return kCondB;
3950 }
3951
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003952 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003953 return kCondAE;
3954 }
3955
Artem Serovcced8ba2017-07-19 18:18:09 +01003956 protected:
3957 DEFAULT_COPY_CONSTRUCTOR(Below);
3958
Aart Bike9f37602015-10-09 11:15:55 -07003959 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003960 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003961 return MakeUnsigned(x) < MakeUnsigned(y);
3962 }
Aart Bike9f37602015-10-09 11:15:55 -07003963};
3964
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003965class HBelowOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003966 public:
3967 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05303968 : HCondition(kBelowOrEqual, first, second, dex_pc) {
3969 }
Aart Bike9f37602015-10-09 11:15:55 -07003970
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003971 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003972 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003974 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003975 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3976 }
3977 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003978 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003979 LOG(FATAL) << DebugName() << " is not defined for float values";
3980 UNREACHABLE();
3981 }
3982 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003983 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003984 LOG(FATAL) << DebugName() << " is not defined for double values";
3985 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003986 }
3987
3988 DECLARE_INSTRUCTION(BelowOrEqual);
3989
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003990 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003991 return kCondBE;
3992 }
3993
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01003994 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07003995 return kCondA;
3996 }
3997
Artem Serovcced8ba2017-07-19 18:18:09 +01003998 protected:
3999 DEFAULT_COPY_CONSTRUCTOR(BelowOrEqual);
4000
Aart Bike9f37602015-10-09 11:15:55 -07004001 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004002 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004003 return MakeUnsigned(x) <= MakeUnsigned(y);
4004 }
Aart Bike9f37602015-10-09 11:15:55 -07004005};
4006
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004007class HAbove final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004008 public:
4009 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304010 : HCondition(kAbove, first, second, dex_pc) {
4011 }
Aart Bike9f37602015-10-09 11:15:55 -07004012
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004013 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004014 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004015 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004016 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004017 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4018 }
4019 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004020 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004021 LOG(FATAL) << DebugName() << " is not defined for float values";
4022 UNREACHABLE();
4023 }
4024 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004025 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004026 LOG(FATAL) << DebugName() << " is not defined for double values";
4027 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004028 }
4029
4030 DECLARE_INSTRUCTION(Above);
4031
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004032 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004033 return kCondA;
4034 }
4035
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004036 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004037 return kCondBE;
4038 }
4039
Artem Serovcced8ba2017-07-19 18:18:09 +01004040 protected:
4041 DEFAULT_COPY_CONSTRUCTOR(Above);
4042
Aart Bike9f37602015-10-09 11:15:55 -07004043 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004044 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004045 return MakeUnsigned(x) > MakeUnsigned(y);
4046 }
Aart Bike9f37602015-10-09 11:15:55 -07004047};
4048
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004049class HAboveOrEqual final : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07004050 public:
4051 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304052 : HCondition(kAboveOrEqual, first, second, dex_pc) {
4053 }
Aart Bike9f37602015-10-09 11:15:55 -07004054
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004055 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004056 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07004057 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004058 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004059 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4060 }
4061 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004062 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004063 LOG(FATAL) << DebugName() << " is not defined for float values";
4064 UNREACHABLE();
4065 }
4066 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004067 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004068 LOG(FATAL) << DebugName() << " is not defined for double values";
4069 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07004070 }
4071
4072 DECLARE_INSTRUCTION(AboveOrEqual);
4073
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004074 IfCondition GetCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004075 return kCondAE;
4076 }
4077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004078 IfCondition GetOppositeCondition() const override {
Aart Bike9f37602015-10-09 11:15:55 -07004079 return kCondB;
4080 }
4081
Artem Serovcced8ba2017-07-19 18:18:09 +01004082 protected:
4083 DEFAULT_COPY_CONSTRUCTOR(AboveOrEqual);
4084
Aart Bike9f37602015-10-09 11:15:55 -07004085 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004086 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004087 return MakeUnsigned(x) >= MakeUnsigned(y);
4088 }
Aart Bike9f37602015-10-09 11:15:55 -07004089};
Dave Allison20dfc792014-06-16 20:44:29 -07004090
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004091// Instruction to check how two inputs compare to each other.
4092// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004093class HCompare final : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00004095 // Note that `comparison_type` is the type of comparison performed
4096 // between the comparison's inputs, not the type of the instantiated
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004097 // HCompare instruction (which is always DataType::Type::kInt).
4098 HCompare(DataType::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004099 HInstruction* first,
4100 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04004101 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07004102 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304103 : HBinaryOperation(kCompare,
4104 DataType::Type::kInt32,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004105 first,
4106 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00004107 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 dex_pc) {
4109 SetPackedField<ComparisonBiasField>(bias);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004110 DCHECK_EQ(comparison_type, DataType::Kind(first->GetType()));
4111 DCHECK_EQ(comparison_type, DataType::Kind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004112 }
4113
Roland Levillain9867bc72015-08-05 10:21:34 +01004114 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004115 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
4116
4117 template <typename T>
4118 int32_t ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00004120 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
4121 // Handle the bias.
4122 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
4123 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004125 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004126 // Note that there is no "cmp-int" Dex instruction so we shouldn't
4127 // reach this code path when processing a freshly built HIR
4128 // graph. However HCompare integer instructions can be synthesized
4129 // by the instruction simplifier to implement IntegerCompare and
4130 // IntegerSignum intrinsics, so we have to handle this case.
4131 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004132 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004133 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004134 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
4135 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004136 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004137 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004139 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004140 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01004141 }
4142
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004143 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004144 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00004145 }
4146
Vladimir Markoa1de9182016-02-25 11:37:38 +00004147 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04004148
Roland Levillain31dd3d62016-02-16 12:21:02 +00004149 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00004151 bool IsGtBias() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 DCHECK(DataType::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004153 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00004154 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004155
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004156 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type type ATTRIBUTE_UNUSED) {
Roland Levillain1693a1f2016-03-15 14:57:31 +00004157 // Comparisons do not require a runtime call in any back end.
4158 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01004159 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07004160
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 DECLARE_INSTRUCTION(Compare);
4162
Roland Levillain31dd3d62016-02-16 12:21:02 +00004163 protected:
Vladimir Markobd785672018-05-03 17:09:09 +01004164 static constexpr size_t kFieldComparisonBias = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 static constexpr size_t kFieldComparisonBiasSize =
4166 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
4167 static constexpr size_t kNumberOfComparePackedBits =
4168 kFieldComparisonBias + kFieldComparisonBiasSize;
4169 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4170 using ComparisonBiasField =
4171 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
4172
Roland Levillain31dd3d62016-02-16 12:21:02 +00004173 // Return an integer constant containing the result of a comparison evaluated at compile time.
4174 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
4175 DCHECK(value == -1 || value == 0 || value == 1) << value;
4176 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
4177 }
4178
Artem Serovcced8ba2017-07-19 18:18:09 +01004179 DEFAULT_COPY_CONSTRUCTOR(Compare);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004180};
4181
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004182class HNewInstance final : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00004183 public:
4184 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00004185 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004186 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00004187 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00004188 bool finalizable,
4189 QuickEntrypointEnum entrypoint)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304190 : HExpression(kNewInstance,
4191 DataType::Type::kReference,
4192 SideEffects::CanTriggerGC(),
4193 dex_pc),
David Brazdil6de19382016-01-08 17:37:10 +00004194 type_index_(type_index),
4195 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00004196 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004197 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00004198 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00004199 }
4200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004201 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004202
Andreas Gampea5b09a62016-11-17 15:21:22 -08004203 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00004204 const DexFile& GetDexFile() const { return dex_file_; }
4205
4206 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004207 bool NeedsEnvironment() const override { return true; }
David Brazdil6de19382016-01-08 17:37:10 +00004208
Mingyao Yang062157f2016-03-02 10:15:36 -08004209 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004210 bool CanThrow() const override { return true; }
Mingyao Yang062157f2016-03-02 10:15:36 -08004211
Nicolas Geoffray5247c082017-01-13 14:17:29 +00004212 bool NeedsChecks() const {
4213 return entrypoint_ == kQuickAllocObjectWithChecks;
4214 }
David Brazdil6de19382016-01-08 17:37:10 +00004215
Vladimir Markoa1de9182016-02-25 11:37:38 +00004216 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00004217
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004218 bool CanBeNull() const override { return false; }
David Brazdil6de19382016-01-08 17:37:10 +00004219
4220 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4221
4222 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
4223 entrypoint_ = entrypoint;
4224 }
4225
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004226 HLoadClass* GetLoadClass() const {
4227 HInstruction* input = InputAt(0);
4228 if (input->IsClinitCheck()) {
4229 input = input->InputAt(0);
4230 }
4231 DCHECK(input->IsLoadClass());
4232 return input->AsLoadClass();
4233 }
4234
David Brazdil6de19382016-01-08 17:37:10 +00004235 bool IsStringAlloc() const;
4236
4237 DECLARE_INSTRUCTION(NewInstance);
4238
Artem Serovcced8ba2017-07-19 18:18:09 +01004239 protected:
4240 DEFAULT_COPY_CONSTRUCTOR(NewInstance);
4241
David Brazdil6de19382016-01-08 17:37:10 +00004242 private:
Vladimir Markobd785672018-05-03 17:09:09 +01004243 static constexpr size_t kFlagFinalizable = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004244 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
4245 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
4246 "Too many packed fields.");
4247
Andreas Gampea5b09a62016-11-17 15:21:22 -08004248 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00004249 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00004250 QuickEntrypointEnum entrypoint_;
David Brazdil6de19382016-01-08 17:37:10 +00004251};
4252
Agi Csaki05f20562015-08-19 14:58:14 -07004253enum IntrinsicNeedsEnvironmentOrCache {
4254 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
4255 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07004256};
4257
Aart Bik5d75afe2015-12-14 11:57:01 -08004258enum IntrinsicSideEffects {
4259 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
4260 kReadSideEffects, // Intrinsic may read heap memory.
4261 kWriteSideEffects, // Intrinsic may write heap memory.
4262 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
4263};
4264
4265enum IntrinsicExceptions {
4266 kNoThrow, // Intrinsic does not throw any exceptions.
4267 kCanThrow // Intrinsic may throw exceptions.
4268};
4269
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004270class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004271 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004272 bool NeedsEnvironment() const override;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004273
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01004274 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004275 SetRawInputAt(index, argument);
4276 }
4277
Roland Levillain3e3d7332015-04-28 11:00:54 +01004278 // Return the number of arguments. This number can be lower than
4279 // the number of inputs returned by InputCount(), as some invoke
4280 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4281 // inputs at the end of their list of inputs.
4282 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4283
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004284 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4285
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004286 InvokeType GetInvokeType() const {
4287 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004288 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004289
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004290 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004291 return intrinsic_;
4292 }
4293
Aart Bik5d75afe2015-12-14 11:57:01 -08004294 void SetIntrinsic(Intrinsics intrinsic,
4295 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4296 IntrinsicSideEffects side_effects,
4297 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004298
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004299 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004300 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004301 }
4302
Aart Bikff7d89c2016-11-07 08:49:28 -08004303 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4304
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004305 bool CanThrow() const override { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004306
Aart Bika8b8e9b2018-01-09 11:01:02 -08004307 void SetAlwaysThrows(bool always_throws) { SetPackedFlag<kFlagAlwaysThrows>(always_throws); }
4308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004309 bool AlwaysThrows() const override { return GetPackedFlag<kFlagAlwaysThrows>(); }
Aart Bika8b8e9b2018-01-09 11:01:02 -08004310
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004311 bool CanBeMoved() const override { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004313 bool InstructionDataEquals(const HInstruction* other) const override {
Aart Bik5d75afe2015-12-14 11:57:01 -08004314 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4315 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004316
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004317 uint32_t* GetIntrinsicOptimizations() {
4318 return &intrinsic_optimizations_;
4319 }
4320
4321 const uint32_t* GetIntrinsicOptimizations() const {
4322 return &intrinsic_optimizations_;
4323 }
4324
4325 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4326
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004327 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004328 void SetResolvedMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004329
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004330 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004331
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004332 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004333 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4334 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
Vladimir Markobd785672018-05-03 17:09:09 +01004336 static constexpr size_t kFlagCanThrow = kFieldInvokeType + kFieldInvokeTypeSize;
Aart Bika8b8e9b2018-01-09 11:01:02 -08004337 static constexpr size_t kFlagAlwaysThrows = kFlagCanThrow + 1;
4338 static constexpr size_t kNumberOfInvokePackedBits = kFlagAlwaysThrows + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004339 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004340 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004341
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304342 HInvoke(InstructionKind kind,
4343 ArenaAllocator* allocator,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004344 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004345 uint32_t number_of_other_inputs,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004346 DataType::Type return_type,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004347 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004348 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004349 ArtMethod* resolved_method,
4350 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004351 : HVariableInputSizeInstruction(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304352 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01004353 return_type,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004354 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4355 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004356 allocator,
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004357 number_of_arguments + number_of_other_inputs,
4358 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004359 number_of_arguments_(number_of_arguments),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004360 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004361 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004362 intrinsic_optimizations_(0) {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004364 SetPackedFlag<kFlagCanThrow>(true);
Nicolas Geoffray76d4bb0f32018-09-21 12:58:45 +01004365 // Check mutator lock, constructors lack annotalysis support.
4366 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
4367 SetResolvedMethod(resolved_method);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004368 }
4369
Artem Serovcced8ba2017-07-19 18:18:09 +01004370 DEFAULT_COPY_CONSTRUCTOR(Invoke);
4371
Roland Levillain3e3d7332015-04-28 11:00:54 +01004372 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004373 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004374 const uint32_t dex_method_index_;
4375 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004376
4377 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4378 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379};
4380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004381class HInvokeUnresolved final : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004382 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004383 HInvokeUnresolved(ArenaAllocator* allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004384 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004385 DataType::Type return_type,
Calin Juravle175dc732015-08-25 15:42:32 +01004386 uint32_t dex_pc,
4387 uint32_t dex_method_index,
4388 InvokeType invoke_type)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304389 : HInvoke(kInvokeUnresolved,
4390 allocator,
Calin Juravle175dc732015-08-25 15:42:32 +01004391 number_of_arguments,
4392 0u /* number_of_other_inputs */,
4393 return_type,
4394 dex_pc,
4395 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004396 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004397 invoke_type) {
4398 }
4399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004400 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004401
Calin Juravle175dc732015-08-25 15:42:32 +01004402 DECLARE_INSTRUCTION(InvokeUnresolved);
4403
Artem Serovcced8ba2017-07-19 18:18:09 +01004404 protected:
4405 DEFAULT_COPY_CONSTRUCTOR(InvokeUnresolved);
Calin Juravle175dc732015-08-25 15:42:32 +01004406};
4407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004408class HInvokePolymorphic final : public HInvoke {
Orion Hodsonac141392017-01-13 11:53:47 +00004409 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004410 HInvokePolymorphic(ArenaAllocator* allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004411 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004412 DataType::Type return_type,
Orion Hodsonac141392017-01-13 11:53:47 +00004413 uint32_t dex_pc,
4414 uint32_t dex_method_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304415 : HInvoke(kInvokePolymorphic,
4416 allocator,
Orion Hodsonac141392017-01-13 11:53:47 +00004417 number_of_arguments,
4418 0u /* number_of_other_inputs */,
4419 return_type,
4420 dex_pc,
4421 dex_method_index,
4422 nullptr,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304423 kVirtual) {
4424 }
Orion Hodsonac141392017-01-13 11:53:47 +00004425
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004426 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004427
Orion Hodsonac141392017-01-13 11:53:47 +00004428 DECLARE_INSTRUCTION(InvokePolymorphic);
4429
Artem Serovcced8ba2017-07-19 18:18:09 +01004430 protected:
4431 DEFAULT_COPY_CONSTRUCTOR(InvokePolymorphic);
Orion Hodsonac141392017-01-13 11:53:47 +00004432};
4433
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004434class HInvokeCustom final : public HInvoke {
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004435 public:
4436 HInvokeCustom(ArenaAllocator* allocator,
4437 uint32_t number_of_arguments,
4438 uint32_t call_site_index,
4439 DataType::Type return_type,
4440 uint32_t dex_pc)
4441 : HInvoke(kInvokeCustom,
4442 allocator,
4443 number_of_arguments,
4444 /* number_of_other_inputs */ 0u,
4445 return_type,
4446 dex_pc,
4447 /* dex_method_index */ dex::kDexNoIndex,
4448 /* resolved_method */ nullptr,
4449 kStatic),
4450 call_site_index_(call_site_index) {
4451 }
4452
4453 uint32_t GetCallSiteIndex() const { return call_site_index_; }
4454
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004455 bool IsClonable() const override { return true; }
Orion Hodson4c8e12e2018-05-18 08:33:20 +01004456
4457 DECLARE_INSTRUCTION(InvokeCustom);
4458
4459 protected:
4460 DEFAULT_COPY_CONSTRUCTOR(InvokeCustom);
4461
4462 private:
4463 uint32_t call_site_index_;
4464};
4465
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004466class HInvokeStaticOrDirect final : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004467 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004468 // Requirements of this method call regarding the class
4469 // initialization (clinit) check of its declaring class.
4470 enum class ClinitCheckRequirement {
4471 kNone, // Class already initialized.
4472 kExplicit, // Static call having explicit clinit check as last input.
4473 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004474 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004475 };
4476
Vladimir Marko58155012015-08-19 12:49:41 +00004477 // Determines how to load the target ArtMethod*.
4478 enum class MethodLoadKind {
4479 // Use a String init ArtMethod* loaded from Thread entrypoints.
4480 kStringInit,
4481
4482 // Use the method's own ArtMethod* loaded by the register allocator.
4483 kRecursive,
4484
Vladimir Marko65979462017-05-19 17:25:12 +01004485 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4486 // Used for boot image methods referenced by boot image code.
4487 kBootImageLinkTimePcRelative,
4488
Vladimir Markob066d432018-01-03 13:14:37 +00004489 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
4490 // Used for app->boot calls with relocatable image.
4491 kBootImageRelRo,
4492
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004493 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004494 // Used for methods outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004495 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004496
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004497 // Use ArtMethod* at a known address, embed the direct address in the code.
4498 // Used for for JIT-compiled calls.
4499 kJitDirectAddress,
4500
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004501 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4502 // used when other kinds are unimplemented on a particular architecture.
4503 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004504 };
4505
4506 // Determines the location of the code pointer.
4507 enum class CodePtrLocation {
4508 // Recursive call, use local PC-relative call instruction.
4509 kCallSelf,
4510
Vladimir Marko58155012015-08-19 12:49:41 +00004511 // Use code pointer from the ArtMethod*.
4512 // Used when we don't know the target code. This is also the last-resort-kind used when
4513 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4514 kCallArtMethod,
4515 };
4516
4517 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004518 MethodLoadKind method_load_kind;
4519 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004520 // The method load data holds
4521 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4522 // Note that there are multiple string init methods, each having its own offset.
4523 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004524 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004525 };
4526
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004527 HInvokeStaticOrDirect(ArenaAllocator* allocator,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004528 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004529 DataType::Type return_type,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004530 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004531 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004532 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004533 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004534 InvokeType invoke_type,
4535 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004536 ClinitCheckRequirement clinit_check_requirement)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304537 : HInvoke(kInvokeStaticOrDirect,
4538 allocator,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004539 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004540 // There is potentially one extra argument for the HCurrentMethod node, and
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +01004541 // potentially one other if the clinit check is explicit.
Vladimir Markob554b5a2015-11-06 12:57:55 +00004542 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004543 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004544 return_type,
4545 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004546 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004547 resolved_method,
4548 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004549 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004550 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004551 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4552 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004554 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004555
Vladimir Markodc151b22015-10-15 18:02:30 +01004556 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004557 bool had_current_method_input = HasCurrentMethodInput();
4558 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4559
4560 // Using the current method is the default and once we find a better
4561 // method load kind, we should not go back to using the current method.
4562 DCHECK(had_current_method_input || !needs_current_method_input);
4563
4564 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004565 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4566 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004567 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004568 dispatch_info_ = dispatch_info;
4569 }
4570
Aart Bik6daebeb2017-04-03 14:35:41 -07004571 DispatchInfo GetDispatchInfo() const {
4572 return dispatch_info_;
4573 }
4574
Vladimir Markoc53c0792015-11-19 15:48:33 +00004575 void AddSpecialInput(HInstruction* input) {
4576 // We allow only one special input.
4577 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4578 DCHECK(InputCount() == GetSpecialInputIndex() ||
4579 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4580 InsertInputAt(GetSpecialInputIndex(), input);
4581 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004582
Vladimir Marko372f10e2016-05-17 16:30:10 +01004583 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004584 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01004585 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4586 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4587 DCHECK(!input_records.empty());
4588 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4589 HInstruction* last_input = input_records.back().GetInstruction();
4590 // Note: `last_input` may be null during arguments setup.
4591 if (last_input != nullptr) {
4592 // `last_input` is the last input of a static invoke marked as having
4593 // an explicit clinit check. It must either be:
4594 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4595 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4596 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4597 }
4598 }
4599 return input_records;
4600 }
4601
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004602 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004603 // We access the method via the dex cache so we can't do an implicit null check.
4604 // TODO: for intrinsics we can generate implicit null checks.
4605 return false;
4606 }
4607
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004608 bool CanBeNull() const override {
Vladimir Markobd785672018-05-03 17:09:09 +01004609 return GetType() == DataType::Type::kReference && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004610 }
4611
Vladimir Markoc53c0792015-11-19 15:48:33 +00004612 // Get the index of the special input, if any.
4613 //
David Brazdil6de19382016-01-08 17:37:10 +00004614 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4615 // method pointer; otherwise there may be one platform-specific special input,
4616 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004617 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004618 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004619
Vladimir Marko58155012015-08-19 12:49:41 +00004620 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4621 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4622 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004623 bool NeedsDexCacheOfDeclaringClass() const override;
Vladimir Marko58155012015-08-19 12:49:41 +00004624 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01004625 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kJitDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004626 bool HasPcRelativeMethodLoadKind() const {
4627 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markob066d432018-01-03 13:14:37 +00004628 GetMethodLoadKind() == MethodLoadKind::kBootImageRelRo ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004629 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004630 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004631 bool HasCurrentMethodInput() const {
4632 // This function can be called only after the invoke has been fully initialized by the builder.
4633 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004634 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004635 return true;
4636 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004637 DCHECK(InputCount() == GetSpecialInputIndex() ||
4638 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004639 return false;
4640 }
4641 }
Vladimir Marko58155012015-08-19 12:49:41 +00004642
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004643 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004644 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004645 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004646 }
4647
4648 uint64_t GetMethodAddress() const {
4649 DCHECK(HasMethodAddress());
4650 return dispatch_info_.method_load_data;
4651 }
4652
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004653 const DexFile& GetDexFileForPcRelativeDexCache() const;
4654
Vladimir Markoa1de9182016-02-25 11:37:38 +00004655 ClinitCheckRequirement GetClinitCheckRequirement() const {
4656 return GetPackedField<ClinitCheckRequirementField>();
4657 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004658
Roland Levillain4c0eb422015-04-24 16:43:49 +01004659 // Is this instruction a call to a static method?
4660 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004661 return GetInvokeType() == kStatic;
4662 }
4663
4664 MethodReference GetTargetMethod() const {
4665 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004666 }
4667
Vladimir Markofbb184a2015-11-13 14:47:00 +00004668 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4669 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4670 // instruction; only relevant for static calls with explicit clinit check.
4671 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004672 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004673 size_t last_input_index = inputs_.size() - 1u;
4674 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004675 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004676 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004677 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004678 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004679 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004680 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004681 }
4682
4683 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004684 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004685 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004686 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004687 }
4688
4689 // Is this a call to a static method whose declaring class has an
4690 // implicit intialization check requirement?
4691 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004692 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004693 }
4694
Vladimir Markob554b5a2015-11-06 12:57:55 +00004695 // Does this method load kind need the current method as an input?
4696 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004697 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004698 }
4699
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004700 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004701
Artem Serovcced8ba2017-07-19 18:18:09 +01004702 protected:
4703 DEFAULT_COPY_CONSTRUCTOR(InvokeStaticOrDirect);
4704
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004705 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004706 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004707 static constexpr size_t kFieldClinitCheckRequirementSize =
4708 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4709 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4710 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4711 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4712 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004713 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4714 kFieldClinitCheckRequirement,
4715 kFieldClinitCheckRequirementSize>;
4716
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004717 // Cached values of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffrayea179f42018-02-08 22:30:18 +00004718 const MethodReference target_method_;
Vladimir Marko58155012015-08-19 12:49:41 +00004719 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004720};
Vladimir Markof64242a2015-12-01 14:58:23 +00004721std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004722std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004723
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004724class HInvokeVirtual final : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004725 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004726 HInvokeVirtual(ArenaAllocator* allocator,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004727 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004728 DataType::Type return_type,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004729 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004730 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004731 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004732 uint32_t vtable_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304733 : HInvoke(kInvokeVirtual,
4734 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004735 number_of_arguments,
4736 0u,
4737 return_type,
4738 dex_pc,
4739 dex_method_index,
4740 resolved_method,
4741 kVirtual),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304742 vtable_index_(vtable_index) {
4743 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004745 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004747 bool CanBeNull() const override {
Aart Bik71bf7b42016-11-16 10:17:46 -08004748 switch (GetIntrinsic()) {
4749 case Intrinsics::kThreadCurrentThread:
4750 case Intrinsics::kStringBufferAppend:
4751 case Intrinsics::kStringBufferToString:
4752 case Intrinsics::kStringBuilderAppend:
4753 case Intrinsics::kStringBuilderToString:
4754 return false;
4755 default:
4756 return HInvoke::CanBeNull();
4757 }
4758 }
4759
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004760 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004761 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004762 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004763 }
4764
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004765 uint32_t GetVTableIndex() const { return vtable_index_; }
4766
4767 DECLARE_INSTRUCTION(InvokeVirtual);
4768
Artem Serovcced8ba2017-07-19 18:18:09 +01004769 protected:
4770 DEFAULT_COPY_CONSTRUCTOR(InvokeVirtual);
4771
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004772 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004773 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004774 const uint32_t vtable_index_;
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004775};
4776
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004777class HInvokeInterface final : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004778 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01004779 HInvokeInterface(ArenaAllocator* allocator,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004780 uint32_t number_of_arguments,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004781 DataType::Type return_type,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004782 uint32_t dex_pc,
4783 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004784 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004785 uint32_t imt_index)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304786 : HInvoke(kInvokeInterface,
4787 allocator,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004788 number_of_arguments,
4789 0u,
4790 return_type,
4791 dex_pc,
4792 dex_method_index,
4793 resolved_method,
4794 kInterface),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304795 imt_index_(imt_index) {
4796 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004797
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004798 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004799
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004800 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00004801 // TODO: Add implicit null checks in intrinsics.
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01004802 return (obj == InputAt(0)) && !IsIntrinsic();
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 }
4804
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004805 bool NeedsDexCacheOfDeclaringClass() const override {
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004806 // The assembly stub currently needs it.
4807 return true;
4808 }
4809
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004810 uint32_t GetImtIndex() const { return imt_index_; }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004811
4812 DECLARE_INSTRUCTION(InvokeInterface);
4813
Artem Serovcced8ba2017-07-19 18:18:09 +01004814 protected:
4815 DEFAULT_COPY_CONSTRUCTOR(InvokeInterface);
4816
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004817 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004818 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004819 const uint32_t imt_index_;
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004820};
4821
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004822class HNeg final : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004823 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 HNeg(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304825 : HUnaryOperation(kNeg, result_type, input, dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 DCHECK_EQ(result_type, DataType::Kind(input->GetType()));
Roland Levillain937e6cd2016-03-22 11:54:37 +00004827 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004828
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004829 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004830
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004831 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004832 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004834 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004837 HConstant* Evaluate(HFloatConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004838 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4839 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004840 HConstant* Evaluate(HDoubleConstant* x) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4842 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004843
Roland Levillain88cb1752014-10-20 16:36:47 +01004844 DECLARE_INSTRUCTION(Neg);
4845
Artem Serovcced8ba2017-07-19 18:18:09 +01004846 protected:
4847 DEFAULT_COPY_CONSTRUCTOR(Neg);
Roland Levillain88cb1752014-10-20 16:36:47 +01004848};
4849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004850class HNewArray final : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004851 public:
Vladimir Markob5461632018-10-15 14:24:21 +01004852 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc, size_t component_size_shift)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304853 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004854 SetRawInputAt(0, cls);
4855 SetRawInputAt(1, length);
Vladimir Markob5461632018-10-15 14:24:21 +01004856 SetPackedField<ComponentSizeShiftField>(component_size_shift);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004857 }
4858
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004859 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004860
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004861 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004862 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004863
Mingyao Yang0c365e62015-03-31 15:09:29 -07004864 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004865 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004866
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004867 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004868
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004869 HLoadClass* GetLoadClass() const {
4870 DCHECK(InputAt(0)->IsLoadClass());
4871 return InputAt(0)->AsLoadClass();
4872 }
4873
4874 HInstruction* GetLength() const {
4875 return InputAt(1);
4876 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004877
Vladimir Markob5461632018-10-15 14:24:21 +01004878 size_t GetComponentSizeShift() {
4879 return GetPackedField<ComponentSizeShiftField>();
4880 }
4881
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004882 DECLARE_INSTRUCTION(NewArray);
4883
Artem Serovcced8ba2017-07-19 18:18:09 +01004884 protected:
4885 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Vladimir Markob5461632018-10-15 14:24:21 +01004886
4887 private:
4888 static constexpr size_t kFieldComponentSizeShift = kNumberOfGenericPackedBits;
4889 static constexpr size_t kFieldComponentSizeShiftSize = MinimumBitsToStore(3u);
4890 static constexpr size_t kNumberOfNewArrayPackedBits =
4891 kFieldComponentSizeShift + kFieldComponentSizeShiftSize;
4892 static_assert(kNumberOfNewArrayPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
4893 using ComponentSizeShiftField =
4894 BitField<size_t, kFieldComponentSizeShift, kFieldComponentSizeShift>;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004895};
4896
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004897class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004898 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004899 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004900 HInstruction* left,
4901 HInstruction* right,
4902 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304903 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4904 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004906 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004907
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004908 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004910 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004911 return GetBlock()->GetGraph()->GetIntConstant(
4912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004913 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004914 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004915 return GetBlock()->GetGraph()->GetLongConstant(
4916 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004917 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004918 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004919 return GetBlock()->GetGraph()->GetFloatConstant(
4920 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4921 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004922 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004923 return GetBlock()->GetGraph()->GetDoubleConstant(
4924 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4925 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004926
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004927 DECLARE_INSTRUCTION(Add);
4928
Artem Serovcced8ba2017-07-19 18:18:09 +01004929 protected:
4930 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004931};
4932
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004933class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004934 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 HInstruction* left,
4937 HInstruction* right,
4938 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304939 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4940 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004941
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004942 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004944 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 return GetBlock()->GetGraph()->GetIntConstant(
4946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004947 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004948 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004949 return GetBlock()->GetGraph()->GetLongConstant(
4950 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004951 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004952 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004953 return GetBlock()->GetGraph()->GetFloatConstant(
4954 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4955 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004956 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004957 return GetBlock()->GetGraph()->GetDoubleConstant(
4958 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4959 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004961 DECLARE_INSTRUCTION(Sub);
4962
Artem Serovcced8ba2017-07-19 18:18:09 +01004963 protected:
4964 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004965};
4966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004967class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004968 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 HInstruction* left,
4971 HInstruction* right,
4972 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304973 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4974 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004975
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004976 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004977
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004978 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004979
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetIntConstant(
4982 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 return GetBlock()->GetGraph()->GetLongConstant(
4986 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004987 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004988 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004989 return GetBlock()->GetGraph()->GetFloatConstant(
4990 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4991 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004992 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004993 return GetBlock()->GetGraph()->GetDoubleConstant(
4994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4995 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004996
4997 DECLARE_INSTRUCTION(Mul);
4998
Artem Serovcced8ba2017-07-19 18:18:09 +01004999 protected:
5000 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01005001};
5002
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005003class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00005004 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005005 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005006 HInstruction* left,
5007 HInstruction* right,
5008 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305009 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
5010 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005011
Roland Levillain9867bc72015-08-05 10:21:34 +01005012 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005014 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005015 // Our graph structure ensures we never have 0 for `y` during
5016 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005017 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005018 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005019 return (y == -1) ? -x : x / y;
5020 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005021
Roland Levillain31dd3d62016-02-16 12:21:02 +00005022 template <typename T>
5023 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005025 return x / y;
5026 }
5027
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005028 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005029 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005030 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005031 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005032 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005033 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005034 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5035 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005036 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 return GetBlock()->GetGraph()->GetFloatConstant(
5038 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5039 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005040 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 return GetBlock()->GetGraph()->GetDoubleConstant(
5042 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005043 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005044
5045 DECLARE_INSTRUCTION(Div);
5046
Artem Serovcced8ba2017-07-19 18:18:09 +01005047 protected:
5048 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005049};
5050
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005051class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005052 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005054 HInstruction* left,
5055 HInstruction* right,
5056 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305057 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5058 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005059
Roland Levillain9867bc72015-08-05 10:21:34 +01005060 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005062 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005063 // Our graph structure ensures we never have 0 for `y` during
5064 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005065 DCHECK_NE(y, 0);
5066 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5067 return (y == -1) ? 0 : x % y;
5068 }
5069
Roland Levillain31dd3d62016-02-16 12:21:02 +00005070 template <typename T>
5071 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005073 return std::fmod(x, y);
5074 }
5075
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005076 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005078 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005079 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005080 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005081 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005082 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005083 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005084 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005085 return GetBlock()->GetGraph()->GetFloatConstant(
5086 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5087 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005088 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005089 return GetBlock()->GetGraph()->GetDoubleConstant(
5090 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5091 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005092
5093 DECLARE_INSTRUCTION(Rem);
5094
Artem Serovcced8ba2017-07-19 18:18:09 +01005095 protected:
5096 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005097};
5098
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005099class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005100 public:
5101 HMin(DataType::Type result_type,
5102 HInstruction* left,
5103 HInstruction* right,
5104 uint32_t dex_pc)
5105 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005107 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005108
5109 // Evaluation for integral values.
5110 template <typename T> static T ComputeIntegral(T x, T y) {
5111 return (x <= y) ? x : y;
5112 }
5113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005114 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005115 return GetBlock()->GetGraph()->GetIntConstant(
5116 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5117 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005118 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005119 return GetBlock()->GetGraph()->GetLongConstant(
5120 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5121 }
5122 // TODO: Evaluation for floating-point values.
5123 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005124 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005125 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005126 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005127
5128 DECLARE_INSTRUCTION(Min);
5129
5130 protected:
5131 DEFAULT_COPY_CONSTRUCTOR(Min);
5132};
5133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005134class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005135 public:
5136 HMax(DataType::Type result_type,
5137 HInstruction* left,
5138 HInstruction* right,
5139 uint32_t dex_pc)
5140 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5141
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005142 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005143
5144 // Evaluation for integral values.
5145 template <typename T> static T ComputeIntegral(T x, T y) {
5146 return (x >= y) ? x : y;
5147 }
5148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005149 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005150 return GetBlock()->GetGraph()->GetIntConstant(
5151 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5152 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005153 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005154 return GetBlock()->GetGraph()->GetLongConstant(
5155 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5156 }
5157 // TODO: Evaluation for floating-point values.
5158 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005159 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005160 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005161 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005162
5163 DECLARE_INSTRUCTION(Max);
5164
5165 protected:
5166 DEFAULT_COPY_CONSTRUCTOR(Max);
5167};
5168
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005169class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005170 public:
5171 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5172 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5173
5174 // Evaluation for integral values.
5175 template <typename T> static T ComputeIntegral(T x) {
5176 return x < 0 ? -x : x;
5177 }
5178
5179 // Evaluation for floating-point values.
5180 // Note, as a "quality of implementation", rather than pure "spec compliance",
5181 // we require that Math.abs() clears the sign bit (but changes nothing else)
5182 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5183 // http://b/30758343
5184 template <typename T, typename S> static T ComputeFP(T x) {
5185 S bits = bit_cast<S, T>(x);
5186 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5187 }
5188
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005189 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005190 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5191 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005192 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005193 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5194 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005195 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005196 return GetBlock()->GetGraph()->GetFloatConstant(
5197 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5198 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005199 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005200 return GetBlock()->GetGraph()->GetDoubleConstant(
5201 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5202 }
5203
5204 DECLARE_INSTRUCTION(Abs);
5205
5206 protected:
5207 DEFAULT_COPY_CONSTRUCTOR(Abs);
5208};
5209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005210class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005211 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005212 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005213 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5214 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005215 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005216 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005217 SetRawInputAt(0, value);
5218 }
5219
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005220 bool IsClonable() const override { return true; }
5221 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005223 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005224 return true;
5225 }
5226
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005227 bool NeedsEnvironment() const override { return true; }
5228 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005229
Calin Juravled0d48522014-11-04 16:40:20 +00005230 DECLARE_INSTRUCTION(DivZeroCheck);
5231
Artem Serovcced8ba2017-07-19 18:18:09 +01005232 protected:
5233 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005234};
5235
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005236class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005237 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005238 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005239 HInstruction* value,
5240 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005241 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305242 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005243 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5244 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005245 }
5246
Roland Levillain5b5b9312016-03-22 14:57:31 +00005247 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005248 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005249 return value << (distance & max_shift_distance);
5250 }
5251
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005253 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005254 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005255 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005256 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005257 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005258 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005259 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005260 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005261 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005262 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5263 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005264 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005265 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005266 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005267 LOG(FATAL) << DebugName() << " is not defined for float values";
5268 UNREACHABLE();
5269 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005270 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005271 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005272 LOG(FATAL) << DebugName() << " is not defined for double values";
5273 UNREACHABLE();
5274 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005275
5276 DECLARE_INSTRUCTION(Shl);
5277
Artem Serovcced8ba2017-07-19 18:18:09 +01005278 protected:
5279 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005280};
5281
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005282class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005283 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005284 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005285 HInstruction* value,
5286 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005287 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305288 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005289 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5290 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 }
5292
Roland Levillain5b5b9312016-03-22 14:57:31 +00005293 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005294 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005295 return value >> (distance & max_shift_distance);
5296 }
5297
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005298 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005299 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005300 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005301 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005302 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005303 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005304 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005305 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005306 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005307 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005308 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5309 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005310 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005311 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005312 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005313 LOG(FATAL) << DebugName() << " is not defined for float values";
5314 UNREACHABLE();
5315 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005316 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005317 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005318 LOG(FATAL) << DebugName() << " is not defined for double values";
5319 UNREACHABLE();
5320 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005321
5322 DECLARE_INSTRUCTION(Shr);
5323
Artem Serovcced8ba2017-07-19 18:18:09 +01005324 protected:
5325 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005326};
5327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005328class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005329 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005330 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005331 HInstruction* value,
5332 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005333 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305334 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005335 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5336 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005337 }
5338
Roland Levillain5b5b9312016-03-22 14:57:31 +00005339 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005340 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005341 typedef typename std::make_unsigned<T>::type V;
5342 V ux = static_cast<V>(value);
5343 return static_cast<T>(ux >> (distance & max_shift_distance));
5344 }
5345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005347 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005348 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005349 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005350 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005351 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005352 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005353 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005354 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005355 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005356 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5357 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005358 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005359 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005360 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005361 LOG(FATAL) << DebugName() << " is not defined for float values";
5362 UNREACHABLE();
5363 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005364 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005365 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005366 LOG(FATAL) << DebugName() << " is not defined for double values";
5367 UNREACHABLE();
5368 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005369
5370 DECLARE_INSTRUCTION(UShr);
5371
Artem Serovcced8ba2017-07-19 18:18:09 +01005372 protected:
5373 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005374};
5375
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005376class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005377 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005378 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005379 HInstruction* left,
5380 HInstruction* right,
5381 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305382 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5383 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005384
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005385 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005386
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005387 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005388
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005390 return GetBlock()->GetGraph()->GetIntConstant(
5391 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005392 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005393 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005394 return GetBlock()->GetGraph()->GetLongConstant(
5395 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005396 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005397 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005398 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005399 LOG(FATAL) << DebugName() << " is not defined for float values";
5400 UNREACHABLE();
5401 }
5402 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005403 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005404 LOG(FATAL) << DebugName() << " is not defined for double values";
5405 UNREACHABLE();
5406 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005407
5408 DECLARE_INSTRUCTION(And);
5409
Artem Serovcced8ba2017-07-19 18:18:09 +01005410 protected:
5411 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005412};
5413
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005414class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005415 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005416 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005417 HInstruction* left,
5418 HInstruction* right,
5419 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305420 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5421 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005422
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005423 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005424
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005425 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005427 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005428 return GetBlock()->GetGraph()->GetIntConstant(
5429 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005430 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005431 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005432 return GetBlock()->GetGraph()->GetLongConstant(
5433 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005434 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005435 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005436 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005437 LOG(FATAL) << DebugName() << " is not defined for float values";
5438 UNREACHABLE();
5439 }
5440 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005441 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005442 LOG(FATAL) << DebugName() << " is not defined for double values";
5443 UNREACHABLE();
5444 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005445
5446 DECLARE_INSTRUCTION(Or);
5447
Artem Serovcced8ba2017-07-19 18:18:09 +01005448 protected:
5449 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005450};
5451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005452class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005453 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005455 HInstruction* left,
5456 HInstruction* right,
5457 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305458 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5459 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005460
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005461 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005462
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005463 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005464
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005465 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005466 return GetBlock()->GetGraph()->GetIntConstant(
5467 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005468 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005469 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005470 return GetBlock()->GetGraph()->GetLongConstant(
5471 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005472 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005473 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005474 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005475 LOG(FATAL) << DebugName() << " is not defined for float values";
5476 UNREACHABLE();
5477 }
5478 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005479 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005480 LOG(FATAL) << DebugName() << " is not defined for double values";
5481 UNREACHABLE();
5482 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005483
5484 DECLARE_INSTRUCTION(Xor);
5485
Artem Serovcced8ba2017-07-19 18:18:09 +01005486 protected:
5487 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005488};
5489
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005490class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005491 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005492 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305493 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005494 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5495 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005496 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005497
Roland Levillain5b5b9312016-03-22 14:57:31 +00005498 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005499 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005500 typedef typename std::make_unsigned<T>::type V;
5501 V ux = static_cast<V>(value);
5502 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005503 return static_cast<T>(ux);
5504 } else {
5505 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005506 return static_cast<T>(ux >> (distance & max_shift_value)) |
5507 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005508 }
5509 }
5510
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005511 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005512 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005513 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005514 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005515 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005516 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005517 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005518 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005519 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005520 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005521 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5522 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005523 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005524 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005525 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005526 LOG(FATAL) << DebugName() << " is not defined for float values";
5527 UNREACHABLE();
5528 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005529 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005530 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005531 LOG(FATAL) << DebugName() << " is not defined for double values";
5532 UNREACHABLE();
5533 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005534
5535 DECLARE_INSTRUCTION(Ror);
5536
Artem Serovcced8ba2017-07-19 18:18:09 +01005537 protected:
5538 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005539};
5540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005541// The value of a parameter in this method. Its location depends on
5542// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005543class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005544 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005545 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005546 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005547 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005549 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305550 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005551 dex_file_(dex_file),
5552 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005553 index_(index) {
5554 SetPackedFlag<kFlagIsThis>(is_this);
5555 SetPackedFlag<kFlagCanBeNull>(!is_this);
5556 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005557
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005558 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005559 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005560 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005561 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005563 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005564 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005565
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005566 DECLARE_INSTRUCTION(ParameterValue);
5567
Artem Serovcced8ba2017-07-19 18:18:09 +01005568 protected:
5569 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5570
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005571 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005572 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005573 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005574 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5575 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5576 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5577 "Too many packed fields.");
5578
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005579 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005580 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005581 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005582 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005583 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005584};
5585
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005586class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005587 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305589 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5590 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005591
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005592 bool CanBeMoved() const override { return true; }
5593 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005594 return true;
5595 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005596
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005597 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005598
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005599 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005600 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005601 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005602 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005603 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005604 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005605 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005606 LOG(FATAL) << DebugName() << " is not defined for float values";
5607 UNREACHABLE();
5608 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005609 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005610 LOG(FATAL) << DebugName() << " is not defined for double values";
5611 UNREACHABLE();
5612 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005613
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005614 DECLARE_INSTRUCTION(Not);
5615
Artem Serovcced8ba2017-07-19 18:18:09 +01005616 protected:
5617 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005618};
5619
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005620class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005622 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305623 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5624 }
David Brazdil66d126e2015-04-03 16:02:44 +01005625
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005626 bool CanBeMoved() const override { return true; }
5627 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005628 return true;
5629 }
5630
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005631 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005632 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005633 return !x;
5634 }
5635
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005636 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005637 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005638 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005639 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005640 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005641 UNREACHABLE();
5642 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005643 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005644 LOG(FATAL) << DebugName() << " is not defined for float values";
5645 UNREACHABLE();
5646 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005647 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005648 LOG(FATAL) << DebugName() << " is not defined for double values";
5649 UNREACHABLE();
5650 }
David Brazdil66d126e2015-04-03 16:02:44 +01005651
5652 DECLARE_INSTRUCTION(BooleanNot);
5653
Artem Serovcced8ba2017-07-19 18:18:09 +01005654 protected:
5655 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005656};
5657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005659 public:
5660 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005661 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305662 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005663 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005664 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005665 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005666 }
5667
5668 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5670 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005672 bool IsClonable() const override { return true; }
5673 bool CanBeMoved() const override { return true; }
5674 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005675 return true;
5676 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005677 // Return whether the conversion is implicit. This includes conversion to the same type.
5678 bool IsImplicitConversion() const {
5679 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5680 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005681
Mark Mendelle82549b2015-05-06 10:55:34 -04005682 // Try to statically evaluate the conversion and return a HConstant
5683 // containing the result. If the input cannot be converted, return nullptr.
5684 HConstant* TryStaticEvaluation() const;
5685
Roland Levillaindff1f282014-11-05 14:15:05 +00005686 DECLARE_INSTRUCTION(TypeConversion);
5687
Artem Serovcced8ba2017-07-19 18:18:09 +01005688 protected:
5689 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005690};
5691
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005692static constexpr uint32_t kNoRegNumber = -1;
5693
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005694class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005696 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005697 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5698 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005699 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005700 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005701 SetRawInputAt(0, value);
5702 }
5703
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005704 bool IsClonable() const override { return true; }
5705 bool CanBeMoved() const override { return true; }
5706 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005707 return true;
5708 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005709
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005710 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005711
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005712 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005714 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005715
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005716 DECLARE_INSTRUCTION(NullCheck);
5717
Artem Serovcced8ba2017-07-19 18:18:09 +01005718 protected:
5719 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720};
5721
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005722// Embeds an ArtField and all the information required by the compiler. We cache
5723// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005724class FieldInfo : public ValueObject {
5725 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005726 FieldInfo(ArtField* field,
5727 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005728 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005729 bool is_volatile,
5730 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005731 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005732 const DexFile& dex_file)
5733 : field_(field),
5734 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005735 field_type_(field_type),
5736 is_volatile_(is_volatile),
5737 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005738 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005739 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005740
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005741 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005743 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005744 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005745 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005746 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005747 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005748
5749 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005750 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005751 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005753 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005754 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005755 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005756 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005757};
5758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005759class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 public:
5761 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005762 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005763 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005764 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005765 bool is_volatile,
5766 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005767 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005768 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005769 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305770 : HExpression(kInstanceFieldGet,
5771 field_type,
5772 SideEffects::FieldReadOfType(field_type, is_volatile),
5773 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005774 field_info_(field,
5775 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005776 field_type,
5777 is_volatile,
5778 field_idx,
5779 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005780 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005781 SetRawInputAt(0, value);
5782 }
5783
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005784 bool IsClonable() const override { return true; }
5785 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005786
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005787 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005788 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005789 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005790 }
5791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005792 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005793 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005794 }
5795
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005796 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005797 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5798 }
5799
Calin Juravle52c48962014-12-16 17:02:57 +00005800 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005801 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005802 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005803 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005804
Vladimir Marko61b92282017-10-11 13:23:17 +01005805 void SetType(DataType::Type new_type) {
5806 DCHECK(DataType::IsIntegralType(GetType()));
5807 DCHECK(DataType::IsIntegralType(new_type));
5808 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5809 SetPackedField<TypeField>(new_type);
5810 }
5811
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812 DECLARE_INSTRUCTION(InstanceFieldGet);
5813
Artem Serovcced8ba2017-07-19 18:18:09 +01005814 protected:
5815 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5816
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005817 private:
5818 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005819};
5820
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005821class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822 public:
5823 HInstanceFieldSet(HInstruction* object,
5824 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005825 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005827 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005828 bool is_volatile,
5829 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005830 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005831 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005832 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005833 : HExpression(kInstanceFieldSet,
5834 SideEffects::FieldWriteOfType(field_type, is_volatile),
5835 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005836 field_info_(field,
5837 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005838 field_type,
5839 is_volatile,
5840 field_idx,
5841 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005842 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005843 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005844 SetRawInputAt(0, object);
5845 SetRawInputAt(1, value);
5846 }
5847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005848 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005849
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005850 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005851 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005852 }
5853
Calin Juravle52c48962014-12-16 17:02:57 +00005854 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005855 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005856 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005857 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005858 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5860 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005861
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005862 DECLARE_INSTRUCTION(InstanceFieldSet);
5863
Artem Serovcced8ba2017-07-19 18:18:09 +01005864 protected:
5865 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5866
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005867 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005868 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5869 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5870 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5871 "Too many packed fields.");
5872
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005873 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005874};
5875
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005876class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005877 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005878 HArrayGet(HInstruction* array,
5879 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005881 uint32_t dex_pc)
5882 : HArrayGet(array,
5883 index,
5884 type,
5885 SideEffects::ArrayReadOfType(type),
5886 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305887 /* is_string_char_at */ false) {
5888 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005889
5890 HArrayGet(HInstruction* array,
5891 HInstruction* index,
5892 DataType::Type type,
5893 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005894 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005895 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305896 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005897 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005898 SetRawInputAt(0, array);
5899 SetRawInputAt(1, index);
5900 }
5901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005902 bool IsClonable() const override { return true; }
5903 bool CanBeMoved() const override { return true; }
5904 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005905 return true;
5906 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005907 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005908 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005909 // Currently, unless the array is the result of NewArray, the array access is always
5910 // preceded by some form of null NullCheck necessary for the bounds check, usually
5911 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5912 // dynamic BCE. There are cases when these could be removed to produce better code.
5913 // If we ever add optimizations to do so we should allow an implicit check here
5914 // (as long as the address falls in the first page).
5915 //
5916 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5917 // a = cond ? new int[1] : null;
5918 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005919 return false;
5920 }
5921
David Brazdil4833f5a2015-12-16 10:37:39 +00005922 bool IsEquivalentOf(HArrayGet* other) const {
5923 bool result = (GetDexPc() == other->GetDexPc());
5924 if (kIsDebugBuild && result) {
5925 DCHECK_EQ(GetBlock(), other->GetBlock());
5926 DCHECK_EQ(GetArray(), other->GetArray());
5927 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 if (DataType::IsIntOrLongType(GetType())) {
5929 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005930 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5932 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005933 }
5934 }
5935 return result;
5936 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005937
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005938 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5939
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005940 HInstruction* GetArray() const { return InputAt(0); }
5941 HInstruction* GetIndex() const { return InputAt(1); }
5942
Vladimir Marko61b92282017-10-11 13:23:17 +01005943 void SetType(DataType::Type new_type) {
5944 DCHECK(DataType::IsIntegralType(GetType()));
5945 DCHECK(DataType::IsIntegralType(new_type));
5946 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5947 SetPackedField<TypeField>(new_type);
5948 }
5949
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005950 DECLARE_INSTRUCTION(ArrayGet);
5951
Artem Serovcced8ba2017-07-19 18:18:09 +01005952 protected:
5953 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5954
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005955 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005956 // We treat a String as an array, creating the HArrayGet from String.charAt()
5957 // intrinsic in the instruction simplifier. We can always determine whether
5958 // a particular HArrayGet is actually a String.charAt() by looking at the type
5959 // of the input but that requires holding the mutator lock, so we prefer to use
5960 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005961 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005962 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5963 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5964 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005965};
5966
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005967class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005968 public:
5969 HArraySet(HInstruction* array,
5970 HInstruction* index,
5971 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005973 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005974 : HArraySet(array,
5975 index,
5976 value,
5977 expected_component_type,
5978 // Make a best guess for side effects now, may be refined during SSA building.
5979 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305980 dex_pc) {
5981 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005982
5983 HArraySet(HInstruction* array,
5984 HInstruction* index,
5985 HInstruction* value,
5986 DataType::Type expected_component_type,
5987 SideEffects side_effects,
5988 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005989 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005990 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005991 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005992 SetPackedFlag<kFlagValueCanBeNull>(true);
5993 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 SetRawInputAt(0, array);
5995 SetRawInputAt(1, index);
5996 SetRawInputAt(2, value);
5997 }
5998
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005999 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006000
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006001 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006002 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006003 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 }
6005
Mingyao Yang81014cb2015-06-02 03:16:27 -07006006 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006007 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07006008
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006009 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00006010 // TODO: Same as for ArrayGet.
6011 return false;
6012 }
6013
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006014 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006015 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006016 }
6017
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006018 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006019 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006020 }
6021
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006022 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006023 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006024 }
6025
Vladimir Markoa1de9182016-02-25 11:37:38 +00006026 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6027 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6028 bool StaticTypeOfArrayIsObjectArray() const {
6029 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6030 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006031
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006032 HInstruction* GetArray() const { return InputAt(0); }
6033 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006034 HInstruction* GetValue() const { return InputAt(2); }
6035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006037 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6038 }
6039
6040 static DataType::Type GetComponentType(DataType::Type value_type,
6041 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006042 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006043 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006044 // be correct, we also check what is the value type. If it is a floating
6045 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006046 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006047 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006048 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006049 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006051 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006052 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006053 }
6054
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006055 static SideEffects ComputeSideEffects(DataType::Type type) {
6056 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006057 }
6058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6060 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6061 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006062 }
6063
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006064 DECLARE_INSTRUCTION(ArraySet);
6065
Artem Serovcced8ba2017-07-19 18:18:09 +01006066 protected:
6067 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6068
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006070 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6071 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006072 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 static constexpr size_t kFlagNeedsTypeCheck =
6074 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6075 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006076 // Cached information for the reference_type_info_ so that codegen
6077 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6079 static constexpr size_t kNumberOfArraySetPackedBits =
6080 kFlagStaticTypeOfArrayIsObjectArray + 1;
6081 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6082 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006084};
6085
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006086class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006087 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006088 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306089 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006090 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006091 // Note that arrays do not change length, so the instruction does not
6092 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006093 SetRawInputAt(0, array);
6094 }
6095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006096 bool IsClonable() const override { return true; }
6097 bool CanBeMoved() const override { return true; }
6098 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006099 return true;
6100 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006101 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006102 return obj == InputAt(0);
6103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006104
Vladimir Markodce016e2016-04-28 13:10:02 +01006105 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6106
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107 DECLARE_INSTRUCTION(ArrayLength);
6108
Artem Serovcced8ba2017-07-19 18:18:09 +01006109 protected:
6110 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6111
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006112 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006113 // We treat a String as an array, creating the HArrayLength from String.length()
6114 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6115 // determine whether a particular HArrayLength is actually a String.length() by
6116 // looking at the type of the input but that requires holding the mutator lock, so
6117 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006118 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006119 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6120 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6121 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006122};
6123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006124class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006125 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006126 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006127 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6128 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006129 HBoundsCheck(HInstruction* index,
6130 HInstruction* length,
6131 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006132 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006133 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006134 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006135 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006136 SetRawInputAt(0, index);
6137 SetRawInputAt(1, length);
6138 }
6139
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006140 bool IsClonable() const override { return true; }
6141 bool CanBeMoved() const override { return true; }
6142 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006143 return true;
6144 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006146 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006148 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006149
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006150 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006151
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006152 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006153
6154 DECLARE_INSTRUCTION(BoundsCheck);
6155
Artem Serovcced8ba2017-07-19 18:18:09 +01006156 protected:
6157 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6158
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006159 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006160 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006161 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6162 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6163 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006164};
6165
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006166class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006167 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006168 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006169 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306170 slow_path_(nullptr) {
6171 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006172
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006173 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006175 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006176 return true;
6177 }
6178
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006179 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6180 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006181
6182 DECLARE_INSTRUCTION(SuspendCheck);
6183
Artem Serovcced8ba2017-07-19 18:18:09 +01006184 protected:
6185 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6186
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006187 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006188 // Only used for code generation, in order to share the same slow path between back edges
6189 // of a same loop.
6190 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006191};
6192
David Srbecky0cf44932015-12-09 14:09:59 +00006193// Pseudo-instruction which provides the native debugger with mapping information.
6194// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006195class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006196 public:
6197 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006198 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306199 }
David Srbecky0cf44932015-12-09 14:09:59 +00006200
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006201 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006202 return true;
6203 }
6204
6205 DECLARE_INSTRUCTION(NativeDebugInfo);
6206
Artem Serovcced8ba2017-07-19 18:18:09 +01006207 protected:
6208 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006209};
6210
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006211/**
6212 * Instruction to load a Class object.
6213 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006214class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006215 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006216 // Determines how to load the Class.
6217 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006218 // We cannot load this class. See HSharpening::SharpenLoadClass.
6219 kInvalid = -1,
6220
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006221 // Use the Class* from the method's own ArtMethod*.
6222 kReferrersClass,
6223
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006224 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006225 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006226 kBootImageLinkTimePcRelative,
6227
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006228 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006229 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006230 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006231
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006232 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006233 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006234 kBssEntry,
6235
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006236 // Use a known boot image Class* address, embedded in the code by the codegen.
6237 // Used for boot image classes referenced by apps in JIT-compiled code.
6238 kJitBootImageAddress,
6239
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006240 // Load from the root table associated with the JIT compiled method.
6241 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006242
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006243 // Load using a simple runtime call. This is the fall-back load kind when
6244 // the codegen is unable to use another appropriate kind.
6245 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006246
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006247 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006248 };
6249
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006250 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006251 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006252 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006253 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006254 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006255 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006256 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006257 : HInstruction(kLoadClass,
6258 DataType::Type::kReference,
6259 SideEffectsForArchRuntimeCalls(),
6260 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006261 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006262 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006263 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006264 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006265 // Referrers class should not need access check. We never inline unverified
6266 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006267 DCHECK(!is_referrers_class || !needs_access_check);
6268
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006269 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006270 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006271 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006272 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006273 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006274 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006275 }
6276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006277 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006278
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006279 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006280
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006281 LoadKind GetLoadKind() const {
6282 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006283 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006284
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006285 bool HasPcRelativeLoadKind() const {
6286 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6287 GetLoadKind() == LoadKind::kBootImageRelRo ||
6288 GetLoadKind() == LoadKind::kBssEntry;
6289 }
6290
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006291 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006292
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006293 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006294
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006295 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006296
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006297 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006298
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006299 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006300 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006301 }
6302
Calin Juravle0ba218d2015-05-19 18:46:01 +01006303 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006304 // The entrypoint the code generator is going to call does not do
6305 // clinit of the class.
6306 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006307 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006308 }
6309
6310 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006311 return NeedsAccessCheck() ||
6312 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006313 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006314 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006315 }
6316
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006317 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006318 return NeedsAccessCheck() ||
6319 MustGenerateClinitCheck() ||
6320 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006321 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006322 GetLoadKind() == LoadKind::kBssEntry) &&
6323 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006324 }
6325
Calin Juravleacf735c2015-02-12 15:25:22 +00006326 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006327 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6328 // Note: The is_exact flag from the return value should not be used.
6329 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6330 } else {
6331 return ReferenceTypeInfo::CreateInvalid();
6332 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006333 }
6334
Vladimir Marko175e7862018-03-27 09:03:13 +00006335 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6336 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6337 DCHECK(klass_ != nullptr);
6338 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006339 }
6340
Andreas Gampea5b09a62016-11-17 15:21:22 -08006341 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006342 const DexFile& GetDexFile() const { return dex_file_; }
6343
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006344 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006345 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006346 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006347
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006348 static SideEffects SideEffectsForArchRuntimeCalls() {
6349 return SideEffects::CanTriggerGC();
6350 }
6351
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006352 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006354 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006355 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006356
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006357 bool MustResolveTypeOnSlowPath() const {
6358 // Check that this instruction has a slow path.
6359 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6360 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6361 return GetLoadKind() == LoadKind::kBssEntry;
6362 }
6363
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006364 void MarkInBootImage() {
6365 SetPackedFlag<kFlagIsInBootImage>(true);
6366 }
6367
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006368 void AddSpecialInput(HInstruction* special_input);
6369
6370 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006371 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 return ArrayRef<HUserRecord<HInstruction*>>(
6373 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6374 }
6375
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006376 Handle<mirror::Class> GetClass() const {
6377 return klass_;
6378 }
6379
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006380 DECLARE_INSTRUCTION(LoadClass);
6381
Artem Serovcced8ba2017-07-19 18:18:09 +01006382 protected:
6383 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006385 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006386 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006387 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006388 // Whether this instruction must generate the initialization check.
6389 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006390 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6392 static constexpr size_t kFieldLoadKindSize =
6393 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006394 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6395 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006396 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006397 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6398
6399 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006400 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006401 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006402 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006403 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006404 }
6405
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006406 void SetLoadKindInternal(LoadKind load_kind);
6407
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006408 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006409 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006410 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006411 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006413 // A type index and dex file where the class can be accessed. The dex file can be:
6414 // - The compiling method's dex file if the class is defined there too.
6415 // - The compiling method's dex file if the class is referenced there.
6416 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006417 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006418 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006419 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006420
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006421 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006422};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006423std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6424
6425// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006426inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6427 // The load kind should be determined before inserting the instruction to the graph.
6428 DCHECK(GetBlock() == nullptr);
6429 DCHECK(GetEnvironment() == nullptr);
6430 SetPackedField<LoadKindField>(load_kind);
6431 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6432 special_input_ = HUserRecord<HInstruction*>(nullptr);
6433 }
6434 if (!NeedsEnvironment()) {
6435 SetSideEffects(SideEffects::None());
6436 }
6437}
6438
6439// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006440inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006441 // The special input is used for PC-relative loads on some architectures,
6442 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006443 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006444 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006445 GetLoadKind() == LoadKind::kBssEntry ||
6446 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006447 DCHECK(special_input_.GetInstruction() == nullptr);
6448 special_input_ = HUserRecord<HInstruction*>(special_input);
6449 special_input->AddUseAt(this, 0);
6450}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006451
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006452class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006453 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006454 // Determines how to load the String.
6455 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006456 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006457 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006458 kBootImageLinkTimePcRelative,
6459
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006460 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006461 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006462 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006463
Vladimir Markoaad75c62016-10-03 08:46:48 +00006464 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006465 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006466 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006467
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006468 // Use a known boot image String* address, embedded in the code by the codegen.
6469 // Used for boot image strings referenced by apps in JIT-compiled code.
6470 kJitBootImageAddress,
6471
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006472 // Load from the root table associated with the JIT compiled method.
6473 kJitTableAddress,
6474
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006475 // Load using a simple runtime call. This is the fall-back load kind when
6476 // the codegen is unable to use another appropriate kind.
6477 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006478
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006479 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006480 };
6481
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006482 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006483 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006484 const DexFile& dex_file,
6485 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006486 : HInstruction(kLoadString,
6487 DataType::Type::kReference,
6488 SideEffectsForArchRuntimeCalls(),
6489 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006490 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006491 string_index_(string_index),
6492 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006493 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006494 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006495
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006496 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006497
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006498 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006499
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006500 LoadKind GetLoadKind() const {
6501 return GetPackedField<LoadKindField>();
6502 }
6503
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006504 bool HasPcRelativeLoadKind() const {
6505 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6506 GetLoadKind() == LoadKind::kBootImageRelRo ||
6507 GetLoadKind() == LoadKind::kBssEntry;
6508 }
6509
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006510 const DexFile& GetDexFile() const {
6511 return dex_file_;
6512 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006513
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006514 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006515 return string_index_;
6516 }
6517
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006518 Handle<mirror::String> GetString() const {
6519 return string_;
6520 }
6521
6522 void SetString(Handle<mirror::String> str) {
6523 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006524 }
6525
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006526 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006528 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006529
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006530 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006531
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006532 // Will call the runtime if we need to load the string through
6533 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006534 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006535 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006536 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006537 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006538 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006539 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006540 return false;
6541 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006542 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006543 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006544
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006545 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006546 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006547 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006549 bool CanBeNull() const override { return false; }
6550 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006551
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006552 static SideEffects SideEffectsForArchRuntimeCalls() {
6553 return SideEffects::CanTriggerGC();
6554 }
6555
Vladimir Marko372f10e2016-05-17 16:30:10 +01006556 void AddSpecialInput(HInstruction* special_input);
6557
6558 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006559 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006560 return ArrayRef<HUserRecord<HInstruction*>>(
6561 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006562 }
6563
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006564 DECLARE_INSTRUCTION(LoadString);
6565
Artem Serovcced8ba2017-07-19 18:18:09 +01006566 protected:
6567 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6568
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006569 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006570 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006571 static constexpr size_t kFieldLoadKindSize =
6572 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6573 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006574 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006575 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006576
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006577 void SetLoadKindInternal(LoadKind load_kind);
6578
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006579 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006580 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006581 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006582 HUserRecord<HInstruction*> special_input_;
6583
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006584 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006585 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006586
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006587 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006588};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006589std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6590
6591// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006592inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6593 // The load kind should be determined before inserting the instruction to the graph.
6594 DCHECK(GetBlock() == nullptr);
6595 DCHECK(GetEnvironment() == nullptr);
6596 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6597 SetPackedField<LoadKindField>(load_kind);
6598 if (load_kind != LoadKind::kRuntimeCall) {
6599 special_input_ = HUserRecord<HInstruction*>(nullptr);
6600 }
6601 if (!NeedsEnvironment()) {
6602 SetSideEffects(SideEffects::None());
6603 }
6604}
6605
6606// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006607inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006608 // The special input is used for PC-relative loads on some architectures,
6609 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006610 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006611 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006612 GetLoadKind() == LoadKind::kBssEntry ||
6613 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006614 // HLoadString::GetInputRecords() returns an empty array at this point,
6615 // so use the GetInputRecords() from the base class to set the input record.
6616 DCHECK(special_input_.GetInstruction() == nullptr);
6617 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006618 special_input->AddUseAt(this, 0);
6619}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006620
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006621class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006622 public:
6623 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006624 uint16_t method_handle_idx,
6625 const DexFile& dex_file,
6626 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006627 : HInstruction(kLoadMethodHandle,
6628 DataType::Type::kReference,
6629 SideEffectsForArchRuntimeCalls(),
6630 dex_pc),
6631 special_input_(HUserRecord<HInstruction*>(current_method)),
6632 method_handle_idx_(method_handle_idx),
6633 dex_file_(dex_file) {
6634 }
6635
6636 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006637 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006638 return ArrayRef<HUserRecord<HInstruction*>>(
6639 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6640 }
6641
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006642 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006643
6644 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6645
6646 const DexFile& GetDexFile() const { return dex_file_; }
6647
6648 static SideEffects SideEffectsForArchRuntimeCalls() {
6649 return SideEffects::CanTriggerGC();
6650 }
6651
6652 DECLARE_INSTRUCTION(LoadMethodHandle);
6653
6654 protected:
6655 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6656
6657 private:
6658 // The special input is the HCurrentMethod for kRuntimeCall.
6659 HUserRecord<HInstruction*> special_input_;
6660
6661 const uint16_t method_handle_idx_;
6662 const DexFile& dex_file_;
6663};
6664
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006665class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006666 public:
6667 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006668 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006669 const DexFile& dex_file,
6670 uint32_t dex_pc)
6671 : HInstruction(kLoadMethodType,
6672 DataType::Type::kReference,
6673 SideEffectsForArchRuntimeCalls(),
6674 dex_pc),
6675 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006676 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006677 dex_file_(dex_file) {
6678 }
6679
6680 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006681 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006682 return ArrayRef<HUserRecord<HInstruction*>>(
6683 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6684 }
6685
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006686 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006687
Orion Hodson06d10a72018-05-14 08:53:38 +01006688 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006689
6690 const DexFile& GetDexFile() const { return dex_file_; }
6691
6692 static SideEffects SideEffectsForArchRuntimeCalls() {
6693 return SideEffects::CanTriggerGC();
6694 }
6695
6696 DECLARE_INSTRUCTION(LoadMethodType);
6697
6698 protected:
6699 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6700
6701 private:
6702 // The special input is the HCurrentMethod for kRuntimeCall.
6703 HUserRecord<HInstruction*> special_input_;
6704
Orion Hodson06d10a72018-05-14 08:53:38 +01006705 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006706 const DexFile& dex_file_;
6707};
6708
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006709/**
6710 * Performs an initialization check on its Class object input.
6711 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006712class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006713 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006714 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006715 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306716 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006717 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006718 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006719 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006720 SetRawInputAt(0, constant);
6721 }
Artem Serova6e26142018-06-19 14:55:17 +01006722 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006723 bool CanBeMoved() const override { return true; }
6724 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006725 return true;
6726 }
6727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006728 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006729 // May call runtime to initialize the class.
6730 return true;
6731 }
6732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006733 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006734
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006735 HLoadClass* GetLoadClass() const {
6736 DCHECK(InputAt(0)->IsLoadClass());
6737 return InputAt(0)->AsLoadClass();
6738 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006739
6740 DECLARE_INSTRUCTION(ClinitCheck);
6741
Artem Serovcced8ba2017-07-19 18:18:09 +01006742
6743 protected:
6744 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006745};
6746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006747class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006748 public:
6749 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006750 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006751 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006752 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006753 bool is_volatile,
6754 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006755 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006756 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006757 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306758 : HExpression(kStaticFieldGet,
6759 field_type,
6760 SideEffects::FieldReadOfType(field_type, is_volatile),
6761 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006762 field_info_(field,
6763 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006764 field_type,
6765 is_volatile,
6766 field_idx,
6767 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006768 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006769 SetRawInputAt(0, cls);
6770 }
6771
Calin Juravle52c48962014-12-16 17:02:57 +00006772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006773 bool IsClonable() const override { return true; }
6774 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006775
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006776 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006777 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006778 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006779 }
6780
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006781 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006782 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6783 }
6784
Calin Juravle52c48962014-12-16 17:02:57 +00006785 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006786 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006787 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006788 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006789
Vladimir Marko61b92282017-10-11 13:23:17 +01006790 void SetType(DataType::Type new_type) {
6791 DCHECK(DataType::IsIntegralType(GetType()));
6792 DCHECK(DataType::IsIntegralType(new_type));
6793 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6794 SetPackedField<TypeField>(new_type);
6795 }
6796
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006797 DECLARE_INSTRUCTION(StaticFieldGet);
6798
Artem Serovcced8ba2017-07-19 18:18:09 +01006799 protected:
6800 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6801
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006802 private:
6803 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006804};
6805
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006806class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006807 public:
6808 HStaticFieldSet(HInstruction* cls,
6809 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006810 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006811 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006812 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006813 bool is_volatile,
6814 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006815 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006816 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006817 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006818 : HExpression(kStaticFieldSet,
6819 SideEffects::FieldWriteOfType(field_type, is_volatile),
6820 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006821 field_info_(field,
6822 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006823 field_type,
6824 is_volatile,
6825 field_idx,
6826 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006827 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006828 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006829 SetRawInputAt(0, cls);
6830 SetRawInputAt(1, value);
6831 }
6832
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006833 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006834 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006835 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006837 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006838
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006839 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006840 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6841 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006842
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006843 DECLARE_INSTRUCTION(StaticFieldSet);
6844
Artem Serovcced8ba2017-07-19 18:18:09 +01006845 protected:
6846 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006848 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006849 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6850 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6851 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6852 "Too many packed fields.");
6853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006854 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006855};
6856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006857class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006858 public:
6859 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006861 uint32_t field_index,
6862 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306863 : HExpression(kUnresolvedInstanceFieldGet,
6864 field_type,
6865 SideEffects::AllExceptGCDependency(),
6866 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006867 field_index_(field_index) {
6868 SetRawInputAt(0, obj);
6869 }
6870
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006871 bool IsClonable() const override { return true; }
6872 bool NeedsEnvironment() const override { return true; }
6873 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006875 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006876 uint32_t GetFieldIndex() const { return field_index_; }
6877
6878 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6879
Artem Serovcced8ba2017-07-19 18:18:09 +01006880 protected:
6881 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6882
Calin Juravlee460d1d2015-09-29 04:52:17 +01006883 private:
6884 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006885};
6886
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006887class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006888 public:
6889 HUnresolvedInstanceFieldSet(HInstruction* obj,
6890 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006891 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006892 uint32_t field_index,
6893 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006894 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006895 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006896 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006897 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006898 SetRawInputAt(0, obj);
6899 SetRawInputAt(1, value);
6900 }
6901
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006902 bool IsClonable() const override { return true; }
6903 bool NeedsEnvironment() const override { return true; }
6904 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006906 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006907 uint32_t GetFieldIndex() const { return field_index_; }
6908
6909 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6910
Artem Serovcced8ba2017-07-19 18:18:09 +01006911 protected:
6912 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6913
Calin Juravlee460d1d2015-09-29 04:52:17 +01006914 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006915 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6916 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006917 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006918 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6919 kFieldFieldType + kFieldFieldTypeSize;
6920 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6921 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006922 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006923
Calin Juravlee460d1d2015-09-29 04:52:17 +01006924 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006925};
6926
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006927class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006928 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006929 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006930 uint32_t field_index,
6931 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306932 : HExpression(kUnresolvedStaticFieldGet,
6933 field_type,
6934 SideEffects::AllExceptGCDependency(),
6935 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006936 field_index_(field_index) {
6937 }
6938
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006939 bool IsClonable() const override { return true; }
6940 bool NeedsEnvironment() const override { return true; }
6941 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006943 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006944 uint32_t GetFieldIndex() const { return field_index_; }
6945
6946 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6947
Artem Serovcced8ba2017-07-19 18:18:09 +01006948 protected:
6949 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6950
Calin Juravlee460d1d2015-09-29 04:52:17 +01006951 private:
6952 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006953};
6954
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006955class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006956 public:
6957 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006958 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006959 uint32_t field_index,
6960 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006961 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006962 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006963 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006964 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006965 SetRawInputAt(0, value);
6966 }
6967
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006968 bool IsClonable() const override { return true; }
6969 bool NeedsEnvironment() const override { return true; }
6970 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006972 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006973 uint32_t GetFieldIndex() const { return field_index_; }
6974
6975 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6976
Artem Serovcced8ba2017-07-19 18:18:09 +01006977 protected:
6978 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6979
Calin Juravlee460d1d2015-09-29 04:52:17 +01006980 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006981 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6982 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006983 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006984 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6985 kFieldFieldType + kFieldFieldTypeSize;
6986 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6987 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006988 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006989
Calin Juravlee460d1d2015-09-29 04:52:17 +01006990 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006991};
6992
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006993// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006994class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006995 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006996 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306997 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6998 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006999
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007000 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01007001
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007002 DECLARE_INSTRUCTION(LoadException);
7003
Artem Serovcced8ba2017-07-19 18:18:09 +01007004 protected:
7005 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007006};
7007
David Brazdilcb1c0552015-08-04 16:22:25 +01007008// Implicit part of move-exception which clears thread-local exception storage.
7009// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007010class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01007011 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007012 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007013 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307014 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007015
7016 DECLARE_INSTRUCTION(ClearException);
7017
Artem Serovcced8ba2017-07-19 18:18:09 +01007018 protected:
7019 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007020};
7021
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007022class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007023 public:
7024 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007025 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007026 SetRawInputAt(0, exception);
7027 }
7028
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007029 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007030
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007031 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007032
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007033 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007034
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007035 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007036
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007037 DECLARE_INSTRUCTION(Throw);
7038
Artem Serovcced8ba2017-07-19 18:18:09 +01007039 protected:
7040 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007041};
7042
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007043/**
7044 * Implementation strategies for the code generator of a HInstanceOf
7045 * or `HCheckCast`.
7046 */
7047enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007048 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007049 kExactCheck, // Can do a single class compare.
7050 kClassHierarchyCheck, // Can just walk the super class chain.
7051 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7052 kInterfaceCheck, // No optimization yet when checking against an interface.
7053 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007054 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007055 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007056 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057};
7058
Roland Levillain86503782016-02-11 19:07:30 +00007059std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7060
Vladimir Marko175e7862018-03-27 09:03:13 +00007061// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7062// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7063class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007064 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007066 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007067 HInstruction* object,
7068 HInstruction* target_class_or_null,
7069 TypeCheckKind check_kind,
7070 Handle<mirror::Class> klass,
7071 uint32_t dex_pc,
7072 ArenaAllocator* allocator,
7073 HIntConstant* bitstring_path_to_root,
7074 HIntConstant* bitstring_mask,
7075 SideEffects side_effects)
7076 : HVariableInputSizeInstruction(
7077 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007078 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007079 side_effects,
7080 dex_pc,
7081 allocator,
7082 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7083 kArenaAllocTypeCheckInputs),
7084 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007085 SetPackedField<TypeCheckKindField>(check_kind);
7086 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007087 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007088 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007089 SetRawInputAt(1, target_class_or_null);
7090 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7091 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7092 if (check_kind == TypeCheckKind::kBitstringCheck) {
7093 DCHECK(target_class_or_null->IsNullConstant());
7094 SetRawInputAt(2, bitstring_path_to_root);
7095 SetRawInputAt(3, bitstring_mask);
7096 } else {
7097 DCHECK(target_class_or_null->IsLoadClass());
7098 }
Vladimir Marko87584542017-12-12 17:47:52 +00007099 }
7100
7101 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007102 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007103 HInstruction* load_class = InputAt(1);
7104 DCHECK(load_class->IsLoadClass());
7105 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007106 }
7107
Vladimir Marko175e7862018-03-27 09:03:13 +00007108 uint32_t GetBitstringPathToRoot() const {
7109 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7110 HInstruction* path_to_root = InputAt(2);
7111 DCHECK(path_to_root->IsIntConstant());
7112 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7113 }
7114
7115 uint32_t GetBitstringMask() const {
7116 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7117 HInstruction* mask = InputAt(3);
7118 DCHECK(mask->IsIntConstant());
7119 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7120 }
7121
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007122 bool IsClonable() const override { return true; }
7123 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007124
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007125 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007126 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7127 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007128 }
7129
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007130 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7131 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7132 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7133 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7134
Vladimir Marko175e7862018-03-27 09:03:13 +00007135 ReferenceTypeInfo GetTargetClassRTI() {
7136 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7137 // Note: The is_exact flag from the return value should not be used.
7138 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7139 } else {
7140 return ReferenceTypeInfo::CreateInvalid();
7141 }
7142 }
7143
7144 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7145 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7146 DCHECK(klass_ != nullptr);
7147 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7148 }
7149
7150 Handle<mirror::Class> GetClass() const {
7151 return klass_;
7152 }
7153
7154 protected:
7155 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7156
7157 private:
7158 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7159 static constexpr size_t kFieldTypeCheckKindSize =
7160 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7161 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7162 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7163 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7164 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7165 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7166
7167 Handle<mirror::Class> klass_;
7168};
7169
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007170class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007171 public:
7172 HInstanceOf(HInstruction* object,
7173 HInstruction* target_class_or_null,
7174 TypeCheckKind check_kind,
7175 Handle<mirror::Class> klass,
7176 uint32_t dex_pc,
7177 ArenaAllocator* allocator,
7178 HIntConstant* bitstring_path_to_root,
7179 HIntConstant* bitstring_mask)
7180 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007181 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007182 object,
7183 target_class_or_null,
7184 check_kind,
7185 klass,
7186 dex_pc,
7187 allocator,
7188 bitstring_path_to_root,
7189 bitstring_mask,
7190 SideEffectsForArchRuntimeCalls(check_kind)) {}
7191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007192 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007194 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007195 return CanCallRuntime(GetTypeCheckKind());
7196 }
7197
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007198 static bool CanCallRuntime(TypeCheckKind check_kind) {
7199 // Mips currently does runtime calls for any other checks.
7200 return check_kind != TypeCheckKind::kExactCheck;
7201 }
7202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007203 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007204 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007205 }
7206
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007207 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007208
Artem Serovcced8ba2017-07-19 18:18:09 +01007209 protected:
7210 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007211};
7212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007213class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007214 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007215 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307216 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007217 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7218 SetPackedFlag<kFlagUpperCanBeNull>(true);
7219 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007220 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007221 SetRawInputAt(0, input);
7222 }
7223
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007224 bool InstructionDataEquals(const HInstruction* other) const override;
7225 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007226
David Brazdilf5552582015-12-27 13:36:12 +00007227 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007228 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007229 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007230 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007231
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007232 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007233 DCHECK(GetUpperCanBeNull() || !can_be_null);
7234 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007235 }
7236
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007237 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007238
Calin Juravleb1498f62015-02-16 13:13:29 +00007239 DECLARE_INSTRUCTION(BoundType);
7240
Artem Serovcced8ba2017-07-19 18:18:09 +01007241 protected:
7242 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7243
Calin Juravleb1498f62015-02-16 13:13:29 +00007244 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007245 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7246 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007247 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007248 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7249 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7250 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7251
Calin Juravleb1498f62015-02-16 13:13:29 +00007252 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007253 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7254 // It is used to bound the type in cases like:
7255 // if (x instanceof ClassX) {
7256 // // uper_bound_ will be ClassX
7257 // }
David Brazdilf5552582015-12-27 13:36:12 +00007258 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007259};
7260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007261class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007262 public:
7263 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007264 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007265 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007266 Handle<mirror::Class> klass,
7267 uint32_t dex_pc,
7268 ArenaAllocator* allocator,
7269 HIntConstant* bitstring_path_to_root,
7270 HIntConstant* bitstring_mask)
7271 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007272 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007273 object,
7274 target_class_or_null,
7275 check_kind,
7276 klass,
7277 dex_pc,
7278 allocator,
7279 bitstring_path_to_root,
7280 bitstring_mask,
7281 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007283 bool IsClonable() const override { return true; }
7284 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007285 // Instruction may throw a CheckCastError.
7286 return true;
7287 }
7288
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007289 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007290
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007291 DECLARE_INSTRUCTION(CheckCast);
7292
Artem Serovcced8ba2017-07-19 18:18:09 +01007293 protected:
7294 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007295};
7296
Andreas Gampe26de38b2016-07-27 17:53:11 -07007297/**
7298 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7299 * @details We define the combined barrier types that are actually required
7300 * by the Java Memory Model, rather than using exactly the terminology from
7301 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7302 * primitives. Note that the JSR-133 cookbook generally does not deal with
7303 * store atomicity issues, and the recipes there are not always entirely sufficient.
7304 * The current recipe is as follows:
7305 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7306 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7307 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7308 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7309 * class has final fields.
7310 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7311 * store-to-memory instructions. Only generated together with non-temporal stores.
7312 */
7313enum MemBarrierKind {
7314 kAnyStore,
7315 kLoadAny,
7316 kStoreStore,
7317 kAnyAny,
7318 kNTStoreStore,
7319 kLastBarrierKind = kNTStoreStore
7320};
7321std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7322
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007323class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007325 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007326 : HExpression(kMemoryBarrier,
7327 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7328 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007329 SetPackedField<BarrierKindField>(barrier_kind);
7330 }
Calin Juravle27df7582015-04-17 19:12:31 +01007331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007332 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007333
Vladimir Markoa1de9182016-02-25 11:37:38 +00007334 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007335
7336 DECLARE_INSTRUCTION(MemoryBarrier);
7337
Artem Serovcced8ba2017-07-19 18:18:09 +01007338 protected:
7339 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7340
Calin Juravle27df7582015-04-17 19:12:31 +01007341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007342 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7343 static constexpr size_t kFieldBarrierKindSize =
7344 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7345 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7346 kFieldBarrierKind + kFieldBarrierKindSize;
7347 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7348 "Too many packed fields.");
7349 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007350};
7351
Igor Murashkind01745e2017-04-05 16:40:31 -07007352// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7353// the specified object(s), with respect to any subsequent store that might "publish"
7354// (i.e. make visible) the specified object to another thread.
7355//
7356// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7357// for all final fields (that were set) at the end of the invoked constructor.
7358//
7359// The constructor fence models the freeze actions for the final fields of an object
7360// being constructed (semantically at the end of the constructor). Constructor fences
7361// have a per-object affinity; two separate objects being constructed get two separate
7362// constructor fences.
7363//
7364// (Note: that if calling a super-constructor or forwarding to another constructor,
7365// the freezes would happen at the end of *that* constructor being invoked).
7366//
7367// The memory model guarantees that when the object being constructed is "published" after
7368// constructor completion (i.e. escapes the current thread via a store), then any final field
7369// writes must be observable on other threads (once they observe that publication).
7370//
7371// Further, anything written before the freeze, and read by dereferencing through the final field,
7372// must also be visible (so final object field could itself have an object with non-final fields;
7373// yet the freeze must also extend to them).
7374//
7375// Constructor example:
7376//
7377// class HasFinal {
7378// final int field; Optimizing IR for <init>()V:
7379// HasFinal() {
7380// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7381// // freeze(this.field); HConstructorFence(this)
7382// } HReturn
7383// }
7384//
7385// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7386// already-initialized classes; in that case the allocation must act as a "default-initializer"
7387// of the object which effectively writes the class pointer "final field".
7388//
7389// For example, we can model default-initialiation as roughly the equivalent of the following:
7390//
7391// class Object {
7392// private final Class header;
7393// }
7394//
7395// Java code: Optimizing IR:
7396//
7397// T new_instance<T>() {
7398// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7399// obj.header = T.class; // header write is done by above call.
7400// // freeze(obj.header) HConstructorFence(obj)
7401// return (T)obj;
7402// }
7403//
7404// See also:
7405// * CompilerDriver::RequiresConstructorBarrier
7406// * QuasiAtomic::ThreadFenceForConstructor
7407//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007408class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007409 // A fence has variable inputs because the inputs can be removed
7410 // after prepare_for_register_allocation phase.
7411 // (TODO: In the future a fence could freeze multiple objects
7412 // after merging two fences together.)
7413 public:
7414 // `fence_object` is the reference that needs to be protected for correct publication.
7415 //
7416 // It makes sense in the following situations:
7417 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7418 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7419 //
7420 // After construction the `fence_object` becomes the 0th input.
7421 // This is not an input in a real sense, but just a convenient place to stash the information
7422 // about the associated object.
7423 HConstructorFence(HInstruction* fence_object,
7424 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007425 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007426 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7427 // constraints described in the class header. We claim that these SideEffects constraints
7428 // enforce a superset of the real constraints.
7429 //
7430 // The ordering described above is conservatively modeled with SideEffects as follows:
7431 //
7432 // * To prevent reordering of the publication stores:
7433 // ----> "Reads of objects" is the initial SideEffect.
7434 // * For every primitive final field store in the constructor:
7435 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7436 // * If there are any stores to reference final fields in the constructor:
7437 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7438 // that are reachable from `fence_object` also need to be prevented for reordering
7439 // (and we do not want to do alias analysis to figure out what those stores are).
7440 //
7441 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7442 // even more conservative approach than the one described above, and prevents all of the
7443 // above reordering without analyzing any of the instructions in the constructor.
7444 //
7445 // If in a later phase we discover that there are no writes to reference final fields,
7446 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307447 : HVariableInputSizeInstruction(kConstructorFence,
7448 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007449 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007450 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007451 /* number_of_inputs */ 1,
7452 kArenaAllocConstructorFenceInputs) {
7453 DCHECK(fence_object != nullptr);
7454 SetRawInputAt(0, fence_object);
7455 }
7456
7457 // The object associated with this constructor fence.
7458 //
7459 // (Note: This will be null after the prepare_for_register_allocation phase,
7460 // as all constructor fence inputs are removed there).
7461 HInstruction* GetFenceObject() const {
7462 return InputAt(0);
7463 }
7464
7465 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7466 // - Delete `fence_use` from `this`'s use list.
7467 // - Delete `this` from `fence_use`'s inputs list.
7468 // - If the `fence_use` is dead, remove it from the graph.
7469 //
7470 // A fence is considered dead once it no longer has any uses
7471 // and all of the inputs are dead.
7472 //
7473 // This must *not* be called during/after prepare_for_register_allocation,
7474 // because that removes all the inputs to the fences but the fence is actually
7475 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007476 //
7477 // Returns how many HConstructorFence instructions were removed from graph.
7478 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007479
Igor Murashkindd018df2017-08-09 10:38:31 -07007480 // Combine all inputs of `this` and `other` instruction and remove
7481 // `other` from the graph.
7482 //
7483 // Inputs are unique after the merge.
7484 //
7485 // Requirement: `this` must not be the same as `other.
7486 void Merge(HConstructorFence* other);
7487
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007488 // Check if this constructor fence is protecting
7489 // an HNewInstance or HNewArray that is also the immediate
7490 // predecessor of `this`.
7491 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007492 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7493 // to be one of the inputs of `this`.
7494 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007495 // Returns the associated HNewArray or HNewInstance,
7496 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007497 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007498
Igor Murashkind01745e2017-04-05 16:40:31 -07007499 DECLARE_INSTRUCTION(ConstructorFence);
7500
Artem Serovcced8ba2017-07-19 18:18:09 +01007501 protected:
7502 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007503};
7504
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007505class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007506 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007507 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007508 kEnter,
7509 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007510 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007511 };
7512
7513 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007514 : HExpression(kMonitorOperation,
7515 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7516 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007517 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007518 SetRawInputAt(0, object);
7519 }
7520
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007521 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007522 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007523
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007524 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007525 // Verifier guarantees that monitor-exit cannot throw.
7526 // This is important because it allows the HGraphBuilder to remove
7527 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7528 return IsEnter();
7529 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007530
Vladimir Markoa1de9182016-02-25 11:37:38 +00007531 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7532 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007533
7534 DECLARE_INSTRUCTION(MonitorOperation);
7535
Artem Serovcced8ba2017-07-19 18:18:09 +01007536 protected:
7537 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7538
Calin Juravle52c48962014-12-16 17:02:57 +00007539 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007540 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7541 static constexpr size_t kFieldOperationKindSize =
7542 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7543 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7544 kFieldOperationKind + kFieldOperationKindSize;
7545 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7546 "Too many packed fields.");
7547 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007548};
7549
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007550class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007551 public:
7552 HSelect(HInstruction* condition,
7553 HInstruction* true_value,
7554 HInstruction* false_value,
7555 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307556 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007557 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7558
7559 // First input must be `true_value` or `false_value` to allow codegens to
7560 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7561 // that architectures which implement HSelect as a conditional move also
7562 // will not need to invert the condition.
7563 SetRawInputAt(0, false_value);
7564 SetRawInputAt(1, true_value);
7565 SetRawInputAt(2, condition);
7566 }
7567
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007568 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007569 HInstruction* GetFalseValue() const { return InputAt(0); }
7570 HInstruction* GetTrueValue() const { return InputAt(1); }
7571 HInstruction* GetCondition() const { return InputAt(2); }
7572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007573 bool CanBeMoved() const override { return true; }
7574 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007575 return true;
7576 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007578 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007579 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7580 }
7581
7582 DECLARE_INSTRUCTION(Select);
7583
Artem Serovcced8ba2017-07-19 18:18:09 +01007584 protected:
7585 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007586};
7587
Vladimir Markof9f64412015-09-02 14:05:49 +01007588class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007589 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007590 MoveOperands(Location source,
7591 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007592 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007593 HInstruction* instruction)
7594 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007595
7596 Location GetSource() const { return source_; }
7597 Location GetDestination() const { return destination_; }
7598
7599 void SetSource(Location value) { source_ = value; }
7600 void SetDestination(Location value) { destination_ = value; }
7601
7602 // The parallel move resolver marks moves as "in-progress" by clearing the
7603 // destination (but not the source).
7604 Location MarkPending() {
7605 DCHECK(!IsPending());
7606 Location dest = destination_;
7607 destination_ = Location::NoLocation();
7608 return dest;
7609 }
7610
7611 void ClearPending(Location dest) {
7612 DCHECK(IsPending());
7613 destination_ = dest;
7614 }
7615
7616 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007617 DCHECK(source_.IsValid() || destination_.IsInvalid());
7618 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007619 }
7620
7621 // True if this blocks a move from the given location.
7622 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007623 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007624 }
7625
7626 // A move is redundant if it's been eliminated, if its source and
7627 // destination are the same, or if its destination is unneeded.
7628 bool IsRedundant() const {
7629 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7630 }
7631
7632 // We clear both operands to indicate move that's been eliminated.
7633 void Eliminate() {
7634 source_ = destination_ = Location::NoLocation();
7635 }
7636
7637 bool IsEliminated() const {
7638 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7639 return source_.IsInvalid();
7640 }
7641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007642 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007643
Nicolas Geoffray90218252015-04-15 11:56:51 +01007644 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007645 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007646 }
7647
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007648 HInstruction* GetInstruction() const { return instruction_; }
7649
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007650 private:
7651 Location source_;
7652 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007653 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007654 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007655 // The instruction this move is assocatied with. Null when this move is
7656 // for moving an input in the expected locations of user (including a phi user).
7657 // This is only used in debug mode, to ensure we do not connect interval siblings
7658 // in the same parallel move.
7659 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007660};
7661
Roland Levillainc9285912015-12-18 10:38:42 +00007662std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7663
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007664static constexpr size_t kDefaultNumberOfMoves = 4;
7665
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007666class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007667 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007668 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007669 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007670 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007671 moves_.reserve(kDefaultNumberOfMoves);
7672 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007673
Nicolas Geoffray90218252015-04-15 11:56:51 +01007674 void AddMove(Location source,
7675 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007676 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007677 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007678 DCHECK(source.IsValid());
7679 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007680 if (kIsDebugBuild) {
7681 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007682 for (const MoveOperands& move : moves_) {
7683 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007684 // Special case the situation where the move is for the spill slot
7685 // of the instruction.
7686 if ((GetPrevious() == instruction)
7687 || ((GetPrevious() == nullptr)
7688 && instruction->IsPhi()
7689 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007690 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007691 << "Doing parallel moves for the same instruction.";
7692 } else {
7693 DCHECK(false) << "Doing parallel moves for the same instruction.";
7694 }
7695 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007696 }
7697 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007698 for (const MoveOperands& move : moves_) {
7699 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007700 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007701 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007702 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007703 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007704 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007705 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007706 }
7707
Vladimir Marko225b6462015-09-28 12:17:40 +01007708 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007709 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007710 }
7711
Vladimir Marko225b6462015-09-28 12:17:40 +01007712 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007713
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007714 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007715
Artem Serovcced8ba2017-07-19 18:18:09 +01007716 protected:
7717 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7718
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007719 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007720 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007721};
7722
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007723// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7724// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7725// never used across anything that can trigger GC.
7726// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7727// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007728class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007729 public:
7730 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307731 : HExpression(kIntermediateAddress,
7732 DataType::Type::kInt32,
7733 SideEffects::DependsOnGC(),
7734 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007735 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7736 DataType::Size(DataType::Type::kReference))
7737 << "kPrimInt and kPrimNot have different sizes.";
7738 SetRawInputAt(0, base_address);
7739 SetRawInputAt(1, offset);
7740 }
7741
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007742 bool IsClonable() const override { return true; }
7743 bool CanBeMoved() const override { return true; }
7744 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007745 return true;
7746 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007747 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007748
7749 HInstruction* GetBaseAddress() const { return InputAt(0); }
7750 HInstruction* GetOffset() const { return InputAt(1); }
7751
7752 DECLARE_INSTRUCTION(IntermediateAddress);
7753
Artem Serovcced8ba2017-07-19 18:18:09 +01007754 protected:
7755 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007756};
7757
7758
Mark Mendell0616ae02015-04-17 12:49:27 -04007759} // namespace art
7760
Aart Bikf8f5a162017-02-06 15:35:29 -08007761#include "nodes_vector.h"
7762
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007763#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7764#include "nodes_shared.h"
7765#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007766#ifdef ART_ENABLE_CODEGEN_mips
7767#include "nodes_mips.h"
7768#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007769#ifdef ART_ENABLE_CODEGEN_x86
7770#include "nodes_x86.h"
7771#endif
7772
7773namespace art {
7774
Igor Murashkin6ef45672017-08-08 13:59:55 -07007775class OptimizingCompilerStats;
7776
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007777class HGraphVisitor : public ValueObject {
7778 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007779 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7780 : stats_(stats),
7781 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007782 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007784 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007785 virtual void VisitBasicBlock(HBasicBlock* block);
7786
Roland Levillain633021e2014-10-01 14:12:25 +01007787 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007788 void VisitInsertionOrder();
7789
Roland Levillain633021e2014-10-01 14:12:25 +01007790 // Visit the graph following dominator tree reverse post-order.
7791 void VisitReversePostOrder();
7792
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007793 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007794
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007795 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007796#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007797 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7798
7799 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7800
7801#undef DECLARE_VISIT_INSTRUCTION
7802
Igor Murashkin6ef45672017-08-08 13:59:55 -07007803 protected:
7804 OptimizingCompilerStats* stats_;
7805
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007806 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007807 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007808
7809 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7810};
7811
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007812class HGraphDelegateVisitor : public HGraphVisitor {
7813 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007814 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7815 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007816 virtual ~HGraphDelegateVisitor() {}
7817
7818 // Visit functions that delegate to to super class.
7819#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007820 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007821
7822 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7823
7824#undef DECLARE_VISIT_INSTRUCTION
7825
7826 private:
7827 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7828};
7829
Artem Serovcced8ba2017-07-19 18:18:09 +01007830// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7831// and remove the old instruction.
7832HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7833
7834// Create a clone for each clonable instructions/phis and replace the original with the clone.
7835//
7836// Used for testing individual instruction cloner.
7837class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7838 public:
7839 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007840 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007841
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007842 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007843 if (instruction->IsClonable()) {
7844 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007845 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007846 }
7847 }
7848
Artem Serov7f4aff62017-06-21 17:02:18 +01007849 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007850
7851 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007852 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007853
7854 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7855};
7856
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007857// Iterator over the blocks that art part of the loop. Includes blocks part
7858// of an inner loop. The order in which the blocks are iterated is on their
7859// block id.
7860class HBlocksInLoopIterator : public ValueObject {
7861 public:
7862 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7863 : blocks_in_loop_(info.GetBlocks()),
7864 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7865 index_(0) {
7866 if (!blocks_in_loop_.IsBitSet(index_)) {
7867 Advance();
7868 }
7869 }
7870
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007871 bool Done() const { return index_ == blocks_.size(); }
7872 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007873 void Advance() {
7874 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007875 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007876 if (blocks_in_loop_.IsBitSet(index_)) {
7877 break;
7878 }
7879 }
7880 }
7881
7882 private:
7883 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007884 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007885 size_t index_;
7886
7887 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7888};
7889
Mingyao Yang3584bce2015-05-19 16:01:59 -07007890// Iterator over the blocks that art part of the loop. Includes blocks part
7891// of an inner loop. The order in which the blocks are iterated is reverse
7892// post order.
7893class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7894 public:
7895 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7896 : blocks_in_loop_(info.GetBlocks()),
7897 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7898 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007899 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007900 Advance();
7901 }
7902 }
7903
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007904 bool Done() const { return index_ == blocks_.size(); }
7905 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007906 void Advance() {
7907 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007908 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7909 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007910 break;
7911 }
7912 }
7913 }
7914
7915 private:
7916 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007917 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007918 size_t index_;
7919
7920 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7921};
7922
Aart Bikf3e61ee2017-04-12 17:09:20 -07007923// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007924inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007925 if (constant->IsIntConstant()) {
7926 return constant->AsIntConstant()->GetValue();
7927 } else if (constant->IsLongConstant()) {
7928 return constant->AsLongConstant()->GetValue();
7929 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007930 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007931 return 0;
7932 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007933}
7934
Aart Bikf3e61ee2017-04-12 17:09:20 -07007935// Returns true iff instruction is an integral constant (and sets value on success).
7936inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7937 if (instruction->IsIntConstant()) {
7938 *value = instruction->AsIntConstant()->GetValue();
7939 return true;
7940 } else if (instruction->IsLongConstant()) {
7941 *value = instruction->AsLongConstant()->GetValue();
7942 return true;
7943 } else if (instruction->IsNullConstant()) {
7944 *value = 0;
7945 return true;
7946 }
7947 return false;
7948}
7949
Aart Bik0148de42017-09-05 09:25:01 -07007950// Returns true iff instruction is the given integral constant.
7951inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7952 int64_t val = 0;
7953 return IsInt64AndGet(instruction, &val) && val == value;
7954}
7955
7956// Returns true iff instruction is a zero bit pattern.
7957inline bool IsZeroBitPattern(HInstruction* instruction) {
7958 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7959}
7960
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007961// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007962#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007963 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7964 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7965#undef INSTRUCTION_TYPE_CHECK
7966
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007967// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007968#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7969 std::is_base_of<BaseType, H##type>::value,
7970#define INSTRUCTION_TYPE_CHECK(type, super) \
7971 inline bool HInstruction::Is##type() const { \
7972 DCHECK_LT(GetKind(), kLastInstructionKind); \
7973 using BaseType = H##type; \
7974 static constexpr bool results[] = { \
7975 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7976 }; \
7977 return results[static_cast<size_t>(GetKind())]; \
7978 }
7979
7980 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7981#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007982#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007983
7984#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007985 inline const H##type* HInstruction::As##type() const { \
7986 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7987 } \
7988 inline H##type* HInstruction::As##type() { \
7989 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7990 }
7991
Vladimir Markoa90dd512018-05-04 15:04:45 +01007992 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7993#undef INSTRUCTION_TYPE_CAST
7994
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007995
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007996// Create space in `blocks` for adding `number_of_new_blocks` entries
7997// starting at location `at`. Blocks after `at` are moved accordingly.
7998inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7999 size_t number_of_new_blocks,
8000 size_t after) {
8001 DCHECK_LT(after, blocks->size());
8002 size_t old_size = blocks->size();
8003 size_t new_size = old_size + number_of_new_blocks;
8004 blocks->resize(new_size);
8005 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
8006}
8007
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008008/*
8009 * Hunt "under the hood" of array lengths (leading to array references),
8010 * null checks (also leading to array references), and new arrays
8011 * (leading to the actual length). This makes it more likely related
8012 * instructions become actually comparable.
8013 */
8014inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8015 while (instruction->IsArrayLength() ||
8016 instruction->IsNullCheck() ||
8017 instruction->IsNewArray()) {
8018 instruction = instruction->IsNewArray()
8019 ? instruction->AsNewArray()->GetLength()
8020 : instruction->InputAt(0);
8021 }
8022 return instruction;
8023}
8024
Artem Serov21c7e6f2017-07-27 16:04:42 +01008025void RemoveEnvironmentUses(HInstruction* instruction);
8026bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8027void ResetEnvironmentInputRecords(HInstruction* instruction);
8028
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008029} // namespace art
8030
8031#endif // ART_COMPILER_OPTIMIZING_NODES_H_