blob: 97b50d36da0bd8c3f0f537c6369cbc357abfe657 [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:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004852 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304853 : HExpression(kNewArray, DataType::Type::kReference, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004854 SetRawInputAt(0, cls);
4855 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004856 }
4857
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004858 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01004859
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004860 // Calls runtime so needs an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004861 bool NeedsEnvironment() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00004862
Mingyao Yang0c365e62015-03-31 15:09:29 -07004863 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004864 bool CanThrow() const override { return true; }
Mingyao Yang0c365e62015-03-31 15:09:29 -07004865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004866 bool CanBeNull() const override { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004867
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004868 HLoadClass* GetLoadClass() const {
4869 DCHECK(InputAt(0)->IsLoadClass());
4870 return InputAt(0)->AsLoadClass();
4871 }
4872
4873 HInstruction* GetLength() const {
4874 return InputAt(1);
4875 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004876
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877 DECLARE_INSTRUCTION(NewArray);
4878
Artem Serovcced8ba2017-07-19 18:18:09 +01004879 protected:
4880 DEFAULT_COPY_CONSTRUCTOR(NewArray);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004881};
4882
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004883class HAdd final : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004884 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004885 HAdd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004886 HInstruction* left,
4887 HInstruction* right,
4888 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304889 : HBinaryOperation(kAdd, result_type, left, right, SideEffects::None(), dex_pc) {
4890 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004891
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004892 bool IsCommutative() const override { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004893
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004894 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004896 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004897 return GetBlock()->GetGraph()->GetIntConstant(
4898 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004899 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004900 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004901 return GetBlock()->GetGraph()->GetLongConstant(
4902 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004903 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004904 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004905 return GetBlock()->GetGraph()->GetFloatConstant(
4906 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4907 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004908 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004909 return GetBlock()->GetGraph()->GetDoubleConstant(
4910 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4911 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004912
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004913 DECLARE_INSTRUCTION(Add);
4914
Artem Serovcced8ba2017-07-19 18:18:09 +01004915 protected:
4916 DEFAULT_COPY_CONSTRUCTOR(Add);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004917};
4918
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004919class HSub final : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004920 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004921 HSub(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004922 HInstruction* left,
4923 HInstruction* right,
4924 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304925 : HBinaryOperation(kSub, result_type, left, right, SideEffects::None(), dex_pc) {
4926 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004928 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004930 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 return GetBlock()->GetGraph()->GetIntConstant(
4932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004933 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004934 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004935 return GetBlock()->GetGraph()->GetLongConstant(
4936 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004937 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004938 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004939 return GetBlock()->GetGraph()->GetFloatConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4941 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004942 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004943 return GetBlock()->GetGraph()->GetDoubleConstant(
4944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4945 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004947 DECLARE_INSTRUCTION(Sub);
4948
Artem Serovcced8ba2017-07-19 18:18:09 +01004949 protected:
4950 DEFAULT_COPY_CONSTRUCTOR(Sub);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951};
4952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004953class HMul final : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004954 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004955 HMul(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 HInstruction* left,
4957 HInstruction* right,
4958 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304959 : HBinaryOperation(kMul, result_type, left, right, SideEffects::None(), dex_pc) {
4960 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004961
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004962 bool IsCommutative() const override { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004963
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004964 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004965
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004966 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 return GetBlock()->GetGraph()->GetIntConstant(
4968 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004969 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004970 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004971 return GetBlock()->GetGraph()->GetLongConstant(
4972 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004973 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004974 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004975 return GetBlock()->GetGraph()->GetFloatConstant(
4976 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4977 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004978 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004979 return GetBlock()->GetGraph()->GetDoubleConstant(
4980 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4981 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004982
4983 DECLARE_INSTRUCTION(Mul);
4984
Artem Serovcced8ba2017-07-19 18:18:09 +01004985 protected:
4986 DEFAULT_COPY_CONSTRUCTOR(Mul);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004987};
4988
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01004989class HDiv final : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004990 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004991 HDiv(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004992 HInstruction* left,
4993 HInstruction* right,
4994 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05304995 : HBinaryOperation(kDiv, result_type, left, right, SideEffects::None(), dex_pc) {
4996 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004997
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005000 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005001 // Our graph structure ensures we never have 0 for `y` during
5002 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005003 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00005004 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00005005 return (y == -1) ? -x : x / y;
5006 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005007
Roland Levillain31dd3d62016-02-16 12:21:02 +00005008 template <typename T>
5009 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005011 return x / y;
5012 }
5013
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005014 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005015 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005016 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005017 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005018 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005019 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005020 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5021 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005022 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005023 return GetBlock()->GetGraph()->GetFloatConstant(
5024 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5025 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005026 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005027 return GetBlock()->GetGraph()->GetDoubleConstant(
5028 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005029 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00005030
5031 DECLARE_INSTRUCTION(Div);
5032
Artem Serovcced8ba2017-07-19 18:18:09 +01005033 protected:
5034 DEFAULT_COPY_CONSTRUCTOR(Div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00005035};
5036
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005037class HRem final : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00005038 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005039 HRem(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005040 HInstruction* left,
5041 HInstruction* right,
5042 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305043 : HBinaryOperation(kRem, result_type, left, right, SideEffects::None(), dex_pc) {
5044 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005045
Roland Levillain9867bc72015-08-05 10:21:34 +01005046 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00005047 T ComputeIntegral(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005048 DCHECK(!DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01005049 // Our graph structure ensures we never have 0 for `y` during
5050 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00005051 DCHECK_NE(y, 0);
5052 // Special case -1 to avoid getting a SIGFPE on x86(_64).
5053 return (y == -1) ? 0 : x % y;
5054 }
5055
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 template <typename T>
5057 T ComputeFP(T x, T y) const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005058 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
Roland Levillain31dd3d62016-02-16 12:21:02 +00005059 return std::fmod(x, y);
5060 }
5061
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005062 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005064 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005065 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005066 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005067 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00005068 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00005069 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005070 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005071 return GetBlock()->GetGraph()->GetFloatConstant(
5072 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5073 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005074 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005075 return GetBlock()->GetGraph()->GetDoubleConstant(
5076 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
5077 }
Calin Juravlebacfec32014-11-14 15:54:36 +00005078
5079 DECLARE_INSTRUCTION(Rem);
5080
Artem Serovcced8ba2017-07-19 18:18:09 +01005081 protected:
5082 DEFAULT_COPY_CONSTRUCTOR(Rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00005083};
5084
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005085class HMin final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005086 public:
5087 HMin(DataType::Type result_type,
5088 HInstruction* left,
5089 HInstruction* right,
5090 uint32_t dex_pc)
5091 : HBinaryOperation(kMin, result_type, left, right, SideEffects::None(), dex_pc) {}
5092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005093 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005094
5095 // Evaluation for integral values.
5096 template <typename T> static T ComputeIntegral(T x, T y) {
5097 return (x <= y) ? x : y;
5098 }
5099
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005101 return GetBlock()->GetGraph()->GetIntConstant(
5102 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005105 return GetBlock()->GetGraph()->GetLongConstant(
5106 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5107 }
5108 // TODO: Evaluation for floating-point values.
5109 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005110 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005111 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005112 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005113
5114 DECLARE_INSTRUCTION(Min);
5115
5116 protected:
5117 DEFAULT_COPY_CONSTRUCTOR(Min);
5118};
5119
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005120class HMax final : public HBinaryOperation {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005121 public:
5122 HMax(DataType::Type result_type,
5123 HInstruction* left,
5124 HInstruction* right,
5125 uint32_t dex_pc)
5126 : HBinaryOperation(kMax, result_type, left, right, SideEffects::None(), dex_pc) {}
5127
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005128 bool IsCommutative() const override { return true; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005129
5130 // Evaluation for integral values.
5131 template <typename T> static T ComputeIntegral(T x, T y) {
5132 return (x >= y) ? x : y;
5133 }
5134
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005135 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005136 return GetBlock()->GetGraph()->GetIntConstant(
5137 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5138 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005139 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Aart Bik1f8d51b2018-02-15 10:42:37 -08005140 return GetBlock()->GetGraph()->GetLongConstant(
5141 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
5142 }
5143 // TODO: Evaluation for floating-point values.
5144 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005145 HFloatConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005146 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005147 HDoubleConstant* y ATTRIBUTE_UNUSED) const override { return nullptr; }
Aart Bik1f8d51b2018-02-15 10:42:37 -08005148
5149 DECLARE_INSTRUCTION(Max);
5150
5151 protected:
5152 DEFAULT_COPY_CONSTRUCTOR(Max);
5153};
5154
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005155class HAbs final : public HUnaryOperation {
Aart Bik3dad3412018-02-28 12:01:46 -08005156 public:
5157 HAbs(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5158 : HUnaryOperation(kAbs, result_type, input, dex_pc) {}
5159
5160 // Evaluation for integral values.
5161 template <typename T> static T ComputeIntegral(T x) {
5162 return x < 0 ? -x : x;
5163 }
5164
5165 // Evaluation for floating-point values.
5166 // Note, as a "quality of implementation", rather than pure "spec compliance",
5167 // we require that Math.abs() clears the sign bit (but changes nothing else)
5168 // for all floating-point numbers, including NaN (signaling NaN may become quiet though).
5169 // http://b/30758343
5170 template <typename T, typename S> static T ComputeFP(T x) {
5171 S bits = bit_cast<S, T>(x);
5172 return bit_cast<T, S>(bits & std::numeric_limits<S>::max());
5173 }
5174
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005175 HConstant* Evaluate(HIntConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005176 return GetBlock()->GetGraph()->GetIntConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5177 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005178 HConstant* Evaluate(HLongConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005179 return GetBlock()->GetGraph()->GetLongConstant(ComputeIntegral(x->GetValue()), GetDexPc());
5180 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005181 HConstant* Evaluate(HFloatConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005182 return GetBlock()->GetGraph()->GetFloatConstant(
5183 ComputeFP<float, int32_t>(x->GetValue()), GetDexPc());
5184 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005185 HConstant* Evaluate(HDoubleConstant* x) const override {
Aart Bik3dad3412018-02-28 12:01:46 -08005186 return GetBlock()->GetGraph()->GetDoubleConstant(
5187 ComputeFP<double, int64_t>(x->GetValue()), GetDexPc());
5188 }
5189
5190 DECLARE_INSTRUCTION(Abs);
5191
5192 protected:
5193 DEFAULT_COPY_CONSTRUCTOR(Abs);
5194};
5195
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005196class HDivZeroCheck final : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00005197 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00005198 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005199 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5200 // instruction following the current one; thus 'SideEffects::None()' is used.
Calin Juravled0d48522014-11-04 16:40:20 +00005201 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005202 : HExpression(kDivZeroCheck, value->GetType(), SideEffects::None(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00005203 SetRawInputAt(0, value);
5204 }
5205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005206 bool IsClonable() const override { return true; }
5207 bool CanBeMoved() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005208
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005209 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Calin Juravled0d48522014-11-04 16:40:20 +00005210 return true;
5211 }
5212
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005213 bool NeedsEnvironment() const override { return true; }
5214 bool CanThrow() const override { return true; }
Calin Juravled0d48522014-11-04 16:40:20 +00005215
Calin Juravled0d48522014-11-04 16:40:20 +00005216 DECLARE_INSTRUCTION(DivZeroCheck);
5217
Artem Serovcced8ba2017-07-19 18:18:09 +01005218 protected:
5219 DEFAULT_COPY_CONSTRUCTOR(DivZeroCheck);
Calin Juravled0d48522014-11-04 16:40:20 +00005220};
5221
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005222class HShl final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005223 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005224 HShl(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005225 HInstruction* value,
5226 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005227 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305228 : HBinaryOperation(kShl, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5230 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005231 }
5232
Roland Levillain5b5b9312016-03-22 14:57:31 +00005233 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005234 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005235 return value << (distance & max_shift_distance);
5236 }
5237
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005238 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005239 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005240 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005241 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005242 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005243 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005244 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005245 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005246 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005247 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005248 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5249 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005250 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005251 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005252 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005253 LOG(FATAL) << DebugName() << " is not defined for float values";
5254 UNREACHABLE();
5255 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005256 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005257 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005258 LOG(FATAL) << DebugName() << " is not defined for double values";
5259 UNREACHABLE();
5260 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005261
5262 DECLARE_INSTRUCTION(Shl);
5263
Artem Serovcced8ba2017-07-19 18:18:09 +01005264 protected:
5265 DEFAULT_COPY_CONSTRUCTOR(Shl);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005266};
5267
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005268class HShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005269 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005270 HShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005271 HInstruction* value,
5272 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005273 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305274 : HBinaryOperation(kShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005275 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5276 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01005277 }
5278
Roland Levillain5b5b9312016-03-22 14:57:31 +00005279 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005280 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005281 return value >> (distance & max_shift_distance);
5282 }
5283
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005284 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005285 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005286 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005287 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005288 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005289 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005290 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005291 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005292 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005293 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005294 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5295 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01005296 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005297 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005298 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005299 LOG(FATAL) << DebugName() << " is not defined for float values";
5300 UNREACHABLE();
5301 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005302 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005303 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005304 LOG(FATAL) << DebugName() << " is not defined for double values";
5305 UNREACHABLE();
5306 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005307
5308 DECLARE_INSTRUCTION(Shr);
5309
Artem Serovcced8ba2017-07-19 18:18:09 +01005310 protected:
5311 DEFAULT_COPY_CONSTRUCTOR(Shr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005312};
5313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005314class HUShr final : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00005315 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005316 HUShr(DataType::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00005317 HInstruction* value,
5318 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005319 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305320 : HBinaryOperation(kUShr, result_type, value, distance, SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005321 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5322 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005323 }
5324
Roland Levillain5b5b9312016-03-22 14:57:31 +00005325 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005326 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005327 typedef typename std::make_unsigned<T>::type V;
5328 V ux = static_cast<V>(value);
5329 return static_cast<T>(ux >> (distance & max_shift_distance));
5330 }
5331
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005332 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005333 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005334 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005335 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005336 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005337 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005338 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005339 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005340 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005341 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005342 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5343 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005344 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005345 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005346 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005347 LOG(FATAL) << DebugName() << " is not defined for float values";
5348 UNREACHABLE();
5349 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005350 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005351 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005352 LOG(FATAL) << DebugName() << " is not defined for double values";
5353 UNREACHABLE();
5354 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005355
5356 DECLARE_INSTRUCTION(UShr);
5357
Artem Serovcced8ba2017-07-19 18:18:09 +01005358 protected:
5359 DEFAULT_COPY_CONSTRUCTOR(UShr);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005360};
5361
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005362class HAnd final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005363 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005364 HAnd(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005365 HInstruction* left,
5366 HInstruction* right,
5367 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305368 : HBinaryOperation(kAnd, result_type, left, right, SideEffects::None(), dex_pc) {
5369 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005370
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005371 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005372
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005373 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005374
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005376 return GetBlock()->GetGraph()->GetIntConstant(
5377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005378 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005380 return GetBlock()->GetGraph()->GetLongConstant(
5381 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005382 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005383 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005384 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005385 LOG(FATAL) << DebugName() << " is not defined for float values";
5386 UNREACHABLE();
5387 }
5388 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005389 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005390 LOG(FATAL) << DebugName() << " is not defined for double values";
5391 UNREACHABLE();
5392 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005393
5394 DECLARE_INSTRUCTION(And);
5395
Artem Serovcced8ba2017-07-19 18:18:09 +01005396 protected:
5397 DEFAULT_COPY_CONSTRUCTOR(And);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005398};
5399
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005400class HOr final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005401 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005402 HOr(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005403 HInstruction* left,
5404 HInstruction* right,
5405 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305406 : HBinaryOperation(kOr, result_type, left, right, SideEffects::None(), dex_pc) {
5407 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005408
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005409 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005410
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005411 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005413 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005414 return GetBlock()->GetGraph()->GetIntConstant(
5415 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005416 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005418 return GetBlock()->GetGraph()->GetLongConstant(
5419 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005420 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005421 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005422 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005423 LOG(FATAL) << DebugName() << " is not defined for float values";
5424 UNREACHABLE();
5425 }
5426 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005427 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005428 LOG(FATAL) << DebugName() << " is not defined for double values";
5429 UNREACHABLE();
5430 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005431
5432 DECLARE_INSTRUCTION(Or);
5433
Artem Serovcced8ba2017-07-19 18:18:09 +01005434 protected:
5435 DEFAULT_COPY_CONSTRUCTOR(Or);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005436};
5437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005438class HXor final : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005439 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005440 HXor(DataType::Type result_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005441 HInstruction* left,
5442 HInstruction* right,
5443 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305444 : HBinaryOperation(kXor, result_type, left, right, SideEffects::None(), dex_pc) {
5445 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005446
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005447 bool IsCommutative() const override { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005448
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005449 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005452 return GetBlock()->GetGraph()->GetIntConstant(
5453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005454 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005455 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005456 return GetBlock()->GetGraph()->GetLongConstant(
5457 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005458 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005459 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005460 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005461 LOG(FATAL) << DebugName() << " is not defined for float values";
5462 UNREACHABLE();
5463 }
5464 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005465 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005466 LOG(FATAL) << DebugName() << " is not defined for double values";
5467 UNREACHABLE();
5468 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005469
5470 DECLARE_INSTRUCTION(Xor);
5471
Artem Serovcced8ba2017-07-19 18:18:09 +01005472 protected:
5473 DEFAULT_COPY_CONSTRUCTOR(Xor);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005474};
5475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005476class HRor final : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005477 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005478 HRor(DataType::Type result_type, HInstruction* value, HInstruction* distance)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305479 : HBinaryOperation(kRor, result_type, value, distance) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 DCHECK_EQ(result_type, DataType::Kind(value->GetType()));
5481 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005482 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005483
Roland Levillain5b5b9312016-03-22 14:57:31 +00005484 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005485 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005486 typedef typename std::make_unsigned<T>::type V;
5487 V ux = static_cast<V>(value);
5488 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005489 return static_cast<T>(ux);
5490 } else {
5491 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005492 return static_cast<T>(ux >> (distance & max_shift_value)) |
5493 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005494 }
5495 }
5496
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005497 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005498 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005499 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005500 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005501 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const override {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005502 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005503 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005504 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005505 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005506 HLongConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005507 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5508 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005509 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005510 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005511 HFloatConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005512 LOG(FATAL) << DebugName() << " is not defined for float values";
5513 UNREACHABLE();
5514 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005515 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005516 HDoubleConstant* distance ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005517 LOG(FATAL) << DebugName() << " is not defined for double values";
5518 UNREACHABLE();
5519 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005520
5521 DECLARE_INSTRUCTION(Ror);
5522
Artem Serovcced8ba2017-07-19 18:18:09 +01005523 protected:
5524 DEFAULT_COPY_CONSTRUCTOR(Ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005525};
5526
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005527// The value of a parameter in this method. Its location depends on
5528// the calling convention.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005529class HParameterValue final : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005530 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005531 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005532 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005533 uint8_t index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 DataType::Type parameter_type,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005535 bool is_this = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305536 : HExpression(kParameterValue, parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005537 dex_file_(dex_file),
5538 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 index_(index) {
5540 SetPackedFlag<kFlagIsThis>(is_this);
5541 SetPackedFlag<kFlagCanBeNull>(!is_this);
5542 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005543
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005544 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005545 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005546 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005547 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005548
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005549 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005550 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005551
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005552 DECLARE_INSTRUCTION(ParameterValue);
5553
Artem Serovcced8ba2017-07-19 18:18:09 +01005554 protected:
5555 DEFAULT_COPY_CONSTRUCTOR(ParameterValue);
5556
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005557 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005558 // Whether or not the parameter value corresponds to 'this' argument.
Vladimir Markobd785672018-05-03 17:09:09 +01005559 static constexpr size_t kFlagIsThis = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005560 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5561 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5562 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5563 "Too many packed fields.");
5564
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005565 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005566 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005567 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005568 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005569 const uint8_t index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005570};
5571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005572class HNot final : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005573 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 HNot(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305575 : HUnaryOperation(kNot, result_type, input, dex_pc) {
5576 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005577
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005578 bool CanBeMoved() const override { return true; }
5579 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005580 return true;
5581 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005582
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005583 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005584
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005585 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005586 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005587 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005588 HConstant* Evaluate(HLongConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005589 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005590 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005591 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005592 LOG(FATAL) << DebugName() << " is not defined for float values";
5593 UNREACHABLE();
5594 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005595 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005596 LOG(FATAL) << DebugName() << " is not defined for double values";
5597 UNREACHABLE();
5598 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005599
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005600 DECLARE_INSTRUCTION(Not);
5601
Artem Serovcced8ba2017-07-19 18:18:09 +01005602 protected:
5603 DEFAULT_COPY_CONSTRUCTOR(Not);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005604};
5605
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005606class HBooleanNot final : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005608 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305609 : HUnaryOperation(kBooleanNot, DataType::Type::kBool, input, dex_pc) {
5610 }
David Brazdil66d126e2015-04-03 16:02:44 +01005611
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005612 bool CanBeMoved() const override { return true; }
5613 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
David Brazdil66d126e2015-04-03 16:02:44 +01005614 return true;
5615 }
5616
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005617 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005618 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005619 return !x;
5620 }
5621
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005622 HConstant* Evaluate(HIntConstant* x) const override {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005623 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005624 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005625 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain9867bc72015-08-05 10:21:34 +01005626 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005627 UNREACHABLE();
5628 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005629 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005630 LOG(FATAL) << DebugName() << " is not defined for float values";
5631 UNREACHABLE();
5632 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005633 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const override {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005634 LOG(FATAL) << DebugName() << " is not defined for double values";
5635 UNREACHABLE();
5636 }
David Brazdil66d126e2015-04-03 16:02:44 +01005637
5638 DECLARE_INSTRUCTION(BooleanNot);
5639
Artem Serovcced8ba2017-07-19 18:18:09 +01005640 protected:
5641 DEFAULT_COPY_CONSTRUCTOR(BooleanNot);
David Brazdil66d126e2015-04-03 16:02:44 +01005642};
5643
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005644class HTypeConversion final : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005645 public:
5646 // Instantiate a type conversion of `input` to `result_type`.
Vladimir Markoc8fb2112017-10-03 11:37:52 +01005647 HTypeConversion(DataType::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305648 : HExpression(kTypeConversion, result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005649 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005650 // Invariant: We should never generate a conversion to a Boolean value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005651 DCHECK_NE(DataType::Type::kBool, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005652 }
5653
5654 HInstruction* GetInput() const { return InputAt(0); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005655 DataType::Type GetInputType() const { return GetInput()->GetType(); }
5656 DataType::Type GetResultType() const { return GetType(); }
Roland Levillaindff1f282014-11-05 14:15:05 +00005657
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005658 bool IsClonable() const override { return true; }
5659 bool CanBeMoved() const override { return true; }
5660 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005661 return true;
5662 }
Nicolas Geoffrayacc56ac2018-10-09 08:45:24 +01005663 // Return whether the conversion is implicit. This includes conversion to the same type.
5664 bool IsImplicitConversion() const {
5665 return DataType::IsTypeConversionImplicit(GetInputType(), GetResultType());
5666 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005667
Mark Mendelle82549b2015-05-06 10:55:34 -04005668 // Try to statically evaluate the conversion and return a HConstant
5669 // containing the result. If the input cannot be converted, return nullptr.
5670 HConstant* TryStaticEvaluation() const;
5671
Roland Levillaindff1f282014-11-05 14:15:05 +00005672 DECLARE_INSTRUCTION(TypeConversion);
5673
Artem Serovcced8ba2017-07-19 18:18:09 +01005674 protected:
5675 DEFAULT_COPY_CONSTRUCTOR(TypeConversion);
Roland Levillaindff1f282014-11-05 14:15:05 +00005676};
5677
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005678static constexpr uint32_t kNoRegNumber = -1;
5679
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005680class HNullCheck final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005681 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005682 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01005683 // constructor. However it can only do it on a fatal slow path so execution never returns to the
5684 // instruction following the current one; thus 'SideEffects::None()' is used.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005685 HNullCheck(HInstruction* value, uint32_t dex_pc)
Artem Serovd1aa7d02018-06-22 11:35:46 +01005686 : HExpression(kNullCheck, value->GetType(), SideEffects::None(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005687 SetRawInputAt(0, value);
5688 }
5689
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005690 bool IsClonable() const override { return true; }
5691 bool CanBeMoved() const override { return true; }
5692 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005693 return true;
5694 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005695
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005696 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005697
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005698 bool CanThrow() const override { return true; }
Calin Juravle10e244f2015-01-26 18:54:32 +00005699
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005700 bool CanBeNull() const override { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005701
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702 DECLARE_INSTRUCTION(NullCheck);
5703
Artem Serovcced8ba2017-07-19 18:18:09 +01005704 protected:
5705 DEFAULT_COPY_CONSTRUCTOR(NullCheck);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005706};
5707
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005708// Embeds an ArtField and all the information required by the compiler. We cache
5709// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710class FieldInfo : public ValueObject {
5711 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005712 FieldInfo(ArtField* field,
5713 MemberOffset field_offset,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005714 DataType::Type field_type,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005715 bool is_volatile,
5716 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005717 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005718 const DexFile& dex_file)
5719 : field_(field),
5720 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005721 field_type_(field_type),
5722 is_volatile_(is_volatile),
5723 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005724 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005725 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005727 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005728 MemberOffset GetFieldOffset() const { return field_offset_; }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005729 DataType::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005730 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005731 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005732 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005733 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734
5735 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005736 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005737 const MemberOffset field_offset_;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 const DataType::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005739 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005740 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005741 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005742 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005743};
5744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005745class HInstanceFieldGet final : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 public:
5747 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005748 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005750 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005751 bool is_volatile,
5752 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005753 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005754 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005755 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305756 : HExpression(kInstanceFieldGet,
5757 field_type,
5758 SideEffects::FieldReadOfType(field_type, is_volatile),
5759 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005760 field_info_(field,
5761 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005762 field_type,
5763 is_volatile,
5764 field_idx,
5765 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005766 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005767 SetRawInputAt(0, value);
5768 }
5769
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005770 bool IsClonable() const override { return true; }
5771 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005772
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005773 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01005774 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005775 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005776 }
5777
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005778 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005779 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005780 }
5781
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005782 size_t ComputeHashCode() const override {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005783 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5784 }
5785
Calin Juravle52c48962014-12-16 17:02:57 +00005786 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005787 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005788 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005789 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005790
Vladimir Marko61b92282017-10-11 13:23:17 +01005791 void SetType(DataType::Type new_type) {
5792 DCHECK(DataType::IsIntegralType(GetType()));
5793 DCHECK(DataType::IsIntegralType(new_type));
5794 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5795 SetPackedField<TypeField>(new_type);
5796 }
5797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005798 DECLARE_INSTRUCTION(InstanceFieldGet);
5799
Artem Serovcced8ba2017-07-19 18:18:09 +01005800 protected:
5801 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldGet);
5802
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005803 private:
5804 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005805};
5806
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005807class HInstanceFieldSet final : public HExpression<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005808 public:
5809 HInstanceFieldSet(HInstruction* object,
5810 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005811 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005813 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005814 bool is_volatile,
5815 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005816 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005817 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005818 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005819 : HExpression(kInstanceFieldSet,
5820 SideEffects::FieldWriteOfType(field_type, is_volatile),
5821 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005822 field_info_(field,
5823 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005824 field_type,
5825 is_volatile,
5826 field_idx,
5827 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005828 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005829 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005830 SetRawInputAt(0, object);
5831 SetRawInputAt(1, value);
5832 }
5833
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005834 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005835
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005836 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005837 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005838 }
5839
Calin Juravle52c48962014-12-16 17:02:57 +00005840 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005843 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005844 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005845 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5846 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005847
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005848 DECLARE_INSTRUCTION(InstanceFieldSet);
5849
Artem Serovcced8ba2017-07-19 18:18:09 +01005850 protected:
5851 DEFAULT_COPY_CONSTRUCTOR(InstanceFieldSet);
5852
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005853 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005854 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5855 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5856 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5857 "Too many packed fields.");
5858
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005859 const FieldInfo field_info_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005860};
5861
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005862class HArrayGet final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005863 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005864 HArrayGet(HInstruction* array,
5865 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005866 DataType::Type type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005867 uint32_t dex_pc)
5868 : HArrayGet(array,
5869 index,
5870 type,
5871 SideEffects::ArrayReadOfType(type),
5872 dex_pc,
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305873 /* is_string_char_at */ false) {
5874 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005875
5876 HArrayGet(HInstruction* array,
5877 HInstruction* index,
5878 DataType::Type type,
5879 SideEffects side_effects,
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005880 uint32_t dex_pc,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005881 bool is_string_char_at)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305882 : HExpression(kArrayGet, type, side_effects, dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005883 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884 SetRawInputAt(0, array);
5885 SetRawInputAt(1, index);
5886 }
5887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005888 bool IsClonable() const override { return true; }
5889 bool CanBeMoved() const override { return true; }
5890 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005891 return true;
5892 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005893 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005894 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005895 // Currently, unless the array is the result of NewArray, the array access is always
5896 // preceded by some form of null NullCheck necessary for the bounds check, usually
5897 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5898 // dynamic BCE. There are cases when these could be removed to produce better code.
5899 // If we ever add optimizations to do so we should allow an implicit check here
5900 // (as long as the address falls in the first page).
5901 //
5902 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5903 // a = cond ? new int[1] : null;
5904 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005905 return false;
5906 }
5907
David Brazdil4833f5a2015-12-16 10:37:39 +00005908 bool IsEquivalentOf(HArrayGet* other) const {
5909 bool result = (GetDexPc() == other->GetDexPc());
5910 if (kIsDebugBuild && result) {
5911 DCHECK_EQ(GetBlock(), other->GetBlock());
5912 DCHECK_EQ(GetArray(), other->GetArray());
5913 DCHECK_EQ(GetIndex(), other->GetIndex());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 if (DataType::IsIntOrLongType(GetType())) {
5915 DCHECK(DataType::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005916 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 DCHECK(DataType::IsFloatingPointType(GetType())) << GetType();
5918 DCHECK(DataType::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005919 }
5920 }
5921 return result;
5922 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005923
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005924 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5925
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005926 HInstruction* GetArray() const { return InputAt(0); }
5927 HInstruction* GetIndex() const { return InputAt(1); }
5928
Vladimir Marko61b92282017-10-11 13:23:17 +01005929 void SetType(DataType::Type new_type) {
5930 DCHECK(DataType::IsIntegralType(GetType()));
5931 DCHECK(DataType::IsIntegralType(new_type));
5932 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
5933 SetPackedField<TypeField>(new_type);
5934 }
5935
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 DECLARE_INSTRUCTION(ArrayGet);
5937
Artem Serovcced8ba2017-07-19 18:18:09 +01005938 protected:
5939 DEFAULT_COPY_CONSTRUCTOR(ArrayGet);
5940
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005942 // We treat a String as an array, creating the HArrayGet from String.charAt()
5943 // intrinsic in the instruction simplifier. We can always determine whether
5944 // a particular HArrayGet is actually a String.charAt() by looking at the type
5945 // of the input but that requires holding the mutator lock, so we prefer to use
5946 // a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01005947 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005948 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5949 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5950 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005951};
5952
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005953class HArraySet final : public HExpression<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005954 public:
5955 HArraySet(HInstruction* array,
5956 HInstruction* index,
5957 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005958 DataType::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005959 uint32_t dex_pc)
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005960 : HArraySet(array,
5961 index,
5962 value,
5963 expected_component_type,
5964 // Make a best guess for side effects now, may be refined during SSA building.
5965 ComputeSideEffects(GetComponentType(value->GetType(), expected_component_type)),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05305966 dex_pc) {
5967 }
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005968
5969 HArraySet(HInstruction* array,
5970 HInstruction* index,
5971 HInstruction* value,
5972 DataType::Type expected_component_type,
5973 SideEffects side_effects,
5974 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01005975 : HExpression(kArraySet, side_effects, dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005976 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == DataType::Type::kReference);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005978 SetPackedFlag<kFlagValueCanBeNull>(true);
5979 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 SetRawInputAt(0, array);
5981 SetRawInputAt(1, index);
5982 SetRawInputAt(2, value);
5983 }
5984
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005985 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01005986
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005987 bool NeedsEnvironment() const override {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005988 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005989 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005990 }
5991
Mingyao Yang81014cb2015-06-02 03:16:27 -07005992 // Can throw ArrayStoreException.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005993 bool CanThrow() const override { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005994
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01005995 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const override {
Calin Juravle77520bc2015-01-12 18:45:46 +00005996 // TODO: Same as for ArrayGet.
5997 return false;
5998 }
5999
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006000 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006001 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006002 }
6003
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006004 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006005 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006006 }
6007
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006008 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006009 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006010 }
6011
Vladimir Markoa1de9182016-02-25 11:37:38 +00006012 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6013 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
6014 bool StaticTypeOfArrayIsObjectArray() const {
6015 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
6016 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006017
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006018 HInstruction* GetArray() const { return InputAt(0); }
6019 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006020 HInstruction* GetValue() const { return InputAt(2); }
6021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 DataType::Type GetComponentType() const {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006023 return GetComponentType(GetValue()->GetType(), GetRawExpectedComponentType());
6024 }
6025
6026 static DataType::Type GetComponentType(DataType::Type value_type,
6027 DataType::Type expected_component_type) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006028 // The Dex format does not type floating point index operations. Since the
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006029 // `expected_component_type` comes from SSA building and can therefore not
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006030 // be correct, we also check what is the value type. If it is a floating
6031 // point type, we must use that type.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006032 return ((value_type == DataType::Type::kFloat32) || (value_type == DataType::Type::kFloat64))
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006033 ? value_type
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006034 : expected_component_type;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01006035 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01006036
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006037 DataType::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006038 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006039 }
6040
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006041 static SideEffects ComputeSideEffects(DataType::Type type) {
6042 return SideEffects::ArrayWriteOfType(type).Union(SideEffectsForArchRuntimeCalls(type));
Aart Bik18b36ab2016-04-13 16:41:35 -07006043 }
6044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006045 static SideEffects SideEffectsForArchRuntimeCalls(DataType::Type value_type) {
6046 return (value_type == DataType::Type::kReference) ? SideEffects::CanTriggerGC()
6047 : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006048 }
6049
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006050 DECLARE_INSTRUCTION(ArraySet);
6051
Artem Serovcced8ba2017-07-19 18:18:09 +01006052 protected:
6053 DEFAULT_COPY_CONSTRUCTOR(ArraySet);
6054
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006055 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006056 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
6057 static constexpr size_t kFieldExpectedComponentTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006058 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006059 static constexpr size_t kFlagNeedsTypeCheck =
6060 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
6061 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006062 // Cached information for the reference_type_info_ so that codegen
6063 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006064 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
6065 static constexpr size_t kNumberOfArraySetPackedBits =
6066 kFlagStaticTypeOfArrayIsObjectArray + 1;
6067 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6068 using ExpectedComponentTypeField =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006069 BitField<DataType::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006070};
6071
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006072class HArrayLength final : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006073 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006074 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306075 : HExpression(kArrayLength, DataType::Type::kInt32, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006076 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006077 // Note that arrays do not change length, so the instruction does not
6078 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006079 SetRawInputAt(0, array);
6080 }
6081
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006082 bool IsClonable() const override { return true; }
6083 bool CanBeMoved() const override { return true; }
6084 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006085 return true;
6086 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006087 bool CanDoImplicitNullCheckOn(HInstruction* obj) const override {
Calin Juravle641547a2015-04-21 22:08:51 +01006088 return obj == InputAt(0);
6089 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006090
Vladimir Markodce016e2016-04-28 13:10:02 +01006091 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
6092
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006093 DECLARE_INSTRUCTION(ArrayLength);
6094
Artem Serovcced8ba2017-07-19 18:18:09 +01006095 protected:
6096 DEFAULT_COPY_CONSTRUCTOR(ArrayLength);
6097
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01006099 // We treat a String as an array, creating the HArrayLength from String.length()
6100 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
6101 // determine whether a particular HArrayLength is actually a String.length() by
6102 // looking at the type of the input but that requires holding the mutator lock, so
6103 // we prefer to use a flag, so that code generators don't need to do the locking.
Vladimir Markobd785672018-05-03 17:09:09 +01006104 static constexpr size_t kFlagIsStringLength = kNumberOfGenericPackedBits;
Vladimir Markodce016e2016-04-28 13:10:02 +01006105 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
6106 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6107 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006108};
6109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006110class HBoundsCheck final : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006111 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00006112 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
Artem Serovd1aa7d02018-06-22 11:35:46 +01006113 // constructor. However it can only do it on a fatal slow path so execution never returns to the
6114 // instruction following the current one; thus 'SideEffects::None()' is used.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006115 HBoundsCheck(HInstruction* index,
6116 HInstruction* length,
6117 uint32_t dex_pc,
Vladimir Marko0259c242017-12-04 11:27:47 +00006118 bool is_string_char_at = false)
Artem Serovd1aa7d02018-06-22 11:35:46 +01006119 : HExpression(kBoundsCheck, index->GetType(), SideEffects::None(), dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 DCHECK_EQ(DataType::Type::kInt32, DataType::Kind(index->GetType()));
Vladimir Marko0259c242017-12-04 11:27:47 +00006121 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006122 SetRawInputAt(0, index);
6123 SetRawInputAt(1, length);
6124 }
6125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006126 bool IsClonable() const override { return true; }
6127 bool CanBeMoved() const override { return true; }
6128 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07006129 return true;
6130 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01006131
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006132 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006134 bool CanThrow() const override { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01006135
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00006136 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006137
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006138 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006139
6140 DECLARE_INSTRUCTION(BoundsCheck);
6141
Artem Serovcced8ba2017-07-19 18:18:09 +01006142 protected:
6143 DEFAULT_COPY_CONSTRUCTOR(BoundsCheck);
6144
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006145 private:
Vladimir Markobd785672018-05-03 17:09:09 +01006146 static constexpr size_t kFlagIsStringCharAt = kNumberOfGenericPackedBits;
Vladimir Markof02046e2018-10-02 15:31:32 +01006147 static constexpr size_t kNumberOfBoundsCheckPackedBits = kFlagIsStringCharAt + 1;
6148 static_assert(kNumberOfBoundsCheckPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6149 "Too many packed fields.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006150};
6151
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006152class HSuspendCheck final : public HExpression<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006153 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00006154 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006155 : HExpression(kSuspendCheck, SideEffects::CanTriggerGC(), dex_pc),
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306156 slow_path_(nullptr) {
6157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006158
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006159 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006161 bool NeedsEnvironment() const override {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006162 return true;
6163 }
6164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006165 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
6166 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006167
6168 DECLARE_INSTRUCTION(SuspendCheck);
6169
Artem Serovcced8ba2017-07-19 18:18:09 +01006170 protected:
6171 DEFAULT_COPY_CONSTRUCTOR(SuspendCheck);
6172
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006173 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006174 // Only used for code generation, in order to share the same slow path between back edges
6175 // of a same loop.
6176 SlowPathCode* slow_path_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006177};
6178
David Srbecky0cf44932015-12-09 14:09:59 +00006179// Pseudo-instruction which provides the native debugger with mapping information.
6180// It ensures that we can generate line number and local variables at this point.
Vladimir Markobd785672018-05-03 17:09:09 +01006181class HNativeDebugInfo : public HExpression<0> {
David Srbecky0cf44932015-12-09 14:09:59 +00006182 public:
6183 explicit HNativeDebugInfo(uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006184 : HExpression<0>(kNativeDebugInfo, SideEffects::None(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306185 }
David Srbecky0cf44932015-12-09 14:09:59 +00006186
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006187 bool NeedsEnvironment() const override {
David Srbecky0cf44932015-12-09 14:09:59 +00006188 return true;
6189 }
6190
6191 DECLARE_INSTRUCTION(NativeDebugInfo);
6192
Artem Serovcced8ba2017-07-19 18:18:09 +01006193 protected:
6194 DEFAULT_COPY_CONSTRUCTOR(NativeDebugInfo);
David Srbecky0cf44932015-12-09 14:09:59 +00006195};
6196
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006197/**
6198 * Instruction to load a Class object.
6199 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006200class HLoadClass final : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006201 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006202 // Determines how to load the Class.
6203 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006204 // We cannot load this class. See HSharpening::SharpenLoadClass.
6205 kInvalid = -1,
6206
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006207 // Use the Class* from the method's own ArtMethod*.
6208 kReferrersClass,
6209
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006210 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006211 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006212 kBootImageLinkTimePcRelative,
6213
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006214 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006215 // Used for boot image classes referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006216 kBootImageRelRo,
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006217
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006219 // Used for classes outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006220 kBssEntry,
6221
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006222 // Use a known boot image Class* address, embedded in the code by the codegen.
6223 // Used for boot image classes referenced by apps in JIT-compiled code.
6224 kJitBootImageAddress,
6225
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006226 // Load from the root table associated with the JIT compiled method.
6227 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006228
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006229 // Load using a simple runtime call. This is the fall-back load kind when
6230 // the codegen is unable to use another appropriate kind.
6231 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006232
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006233 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006234 };
6235
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006236 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006237 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006238 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006239 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006240 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01006241 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00006242 bool needs_access_check)
Vladimir Markobd785672018-05-03 17:09:09 +01006243 : HInstruction(kLoadClass,
6244 DataType::Type::kReference,
6245 SideEffectsForArchRuntimeCalls(),
6246 dex_pc),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006247 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006248 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01006249 dex_file_(dex_file),
Vladimir Marko175e7862018-03-27 09:03:13 +00006250 klass_(klass) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01006251 // Referrers class should not need access check. We never inline unverified
6252 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 DCHECK(!is_referrers_class || !needs_access_check);
6254
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006255 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006256 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006257 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00006258 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00006259 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Marko175e7862018-03-27 09:03:13 +00006260 SetPackedFlag<kFlagValidLoadedClassRTI>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006261 }
6262
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006263 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006264
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006265 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006266
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006267 LoadKind GetLoadKind() const {
6268 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006269 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006270
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006271 bool HasPcRelativeLoadKind() const {
6272 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6273 GetLoadKind() == LoadKind::kBootImageRelRo ||
6274 GetLoadKind() == LoadKind::kBssEntry;
6275 }
6276
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006277 bool CanBeMoved() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006278
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006279 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006280
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006281 size_t ComputeHashCode() const override { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006282
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006283 bool CanBeNull() const override { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006284
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006285 bool NeedsEnvironment() const override {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006286 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006287 }
6288
Calin Juravle0ba218d2015-05-19 18:46:01 +01006289 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00006290 // The entrypoint the code generator is going to call does not do
6291 // clinit of the class.
6292 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006294 }
6295
6296 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00006297 return NeedsAccessCheck() ||
6298 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006299 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006300 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01006301 }
6302
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006303 bool CanThrow() const override {
Vladimir Marko41559982017-01-06 14:04:23 +00006304 return NeedsAccessCheck() ||
6305 MustGenerateClinitCheck() ||
6306 // If the class is in the boot image, the lookup in the runtime call cannot throw.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006307 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006308 GetLoadKind() == LoadKind::kBssEntry) &&
6309 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006310 }
6311
Calin Juravleacf735c2015-02-12 15:25:22 +00006312 ReferenceTypeInfo GetLoadedClassRTI() {
Vladimir Marko175e7862018-03-27 09:03:13 +00006313 if (GetPackedFlag<kFlagValidLoadedClassRTI>()) {
6314 // Note: The is_exact flag from the return value should not be used.
6315 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
6316 } else {
6317 return ReferenceTypeInfo::CreateInvalid();
6318 }
Calin Juravleacf735c2015-02-12 15:25:22 +00006319 }
6320
Vladimir Marko175e7862018-03-27 09:03:13 +00006321 // Loaded class RTI is marked as valid by RTP if the klass_ is admissible.
6322 void SetValidLoadedClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
6323 DCHECK(klass_ != nullptr);
6324 SetPackedFlag<kFlagValidLoadedClassRTI>(true);
Calin Juravleacf735c2015-02-12 15:25:22 +00006325 }
6326
Andreas Gampea5b09a62016-11-17 15:21:22 -08006327 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006328 const DexFile& GetDexFile() const { return dex_file_; }
6329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006330 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006331 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006332 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00006333
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006334 static SideEffects SideEffectsForArchRuntimeCalls() {
6335 return SideEffects::CanTriggerGC();
6336 }
6337
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006338 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006339 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006340 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006342
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006343 bool MustResolveTypeOnSlowPath() const {
6344 // Check that this instruction has a slow path.
6345 DCHECK(GetLoadKind() != LoadKind::kRuntimeCall); // kRuntimeCall calls on main path.
6346 DCHECK(GetLoadKind() == LoadKind::kBssEntry || MustGenerateClinitCheck());
6347 return GetLoadKind() == LoadKind::kBssEntry;
6348 }
6349
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006350 void MarkInBootImage() {
6351 SetPackedFlag<kFlagIsInBootImage>(true);
6352 }
6353
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006354 void AddSpecialInput(HInstruction* special_input);
6355
6356 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006357 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006358 return ArrayRef<HUserRecord<HInstruction*>>(
6359 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6360 }
6361
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006362 Handle<mirror::Class> GetClass() const {
6363 return klass_;
6364 }
6365
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006366 DECLARE_INSTRUCTION(LoadClass);
6367
Artem Serovcced8ba2017-07-19 18:18:09 +01006368 protected:
6369 DEFAULT_COPY_CONSTRUCTOR(LoadClass);
6370
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006371 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00006373 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006374 // Whether this instruction must generate the initialization check.
6375 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006376 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006377 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
6378 static constexpr size_t kFieldLoadKindSize =
6379 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
Vladimir Marko175e7862018-03-27 09:03:13 +00006380 static constexpr size_t kFlagValidLoadedClassRTI = kFieldLoadKind + kFieldLoadKindSize;
6381 static constexpr size_t kNumberOfLoadClassPackedBits = kFlagValidLoadedClassRTI + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006383 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
6384
6385 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006386 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006387 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006388 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006389 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006390 }
6391
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006392 void SetLoadKindInternal(LoadKind load_kind);
6393
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006394 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006395 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006396 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006397 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006398
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006399 // A type index and dex file where the class can be accessed. The dex file can be:
6400 // - The compiling method's dex file if the class is defined there too.
6401 // - The compiling method's dex file if the class is referenced there.
6402 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006403 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08006404 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006406
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006407 Handle<mirror::Class> klass_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006408};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006409std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
6410
6411// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006412inline void HLoadClass::SetLoadKind(LoadKind load_kind) {
6413 // The load kind should be determined before inserting the instruction to the graph.
6414 DCHECK(GetBlock() == nullptr);
6415 DCHECK(GetEnvironment() == nullptr);
6416 SetPackedField<LoadKindField>(load_kind);
6417 if (load_kind != LoadKind::kRuntimeCall && load_kind != LoadKind::kReferrersClass) {
6418 special_input_ = HUserRecord<HInstruction*>(nullptr);
6419 }
6420 if (!NeedsEnvironment()) {
6421 SetSideEffects(SideEffects::None());
6422 }
6423}
6424
6425// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006426inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006427 // The special input is used for PC-relative loads on some architectures,
6428 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006429 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006430 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006431 GetLoadKind() == LoadKind::kBssEntry ||
6432 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006433 DCHECK(special_input_.GetInstruction() == nullptr);
6434 special_input_ = HUserRecord<HInstruction*>(special_input);
6435 special_input->AddUseAt(this, 0);
6436}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006437
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006438class HLoadString final : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006439 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006440 // Determines how to load the String.
6441 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006442 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01006443 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006444 kBootImageLinkTimePcRelative,
6445
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006446 // Load from an entry in the .data.bimg.rel.ro using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006447 // Used for boot image strings referenced by apps in AOT-compiled code.
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006448 kBootImageRelRo,
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006449
Vladimir Markoaad75c62016-10-03 08:46:48 +00006450 // Load from an entry in the .bss section using a PC-relative load.
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006451 // Used for strings outside boot image referenced by AOT-compiled app and boot image code.
Vladimir Markoaad75c62016-10-03 08:46:48 +00006452 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006453
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006454 // Use a known boot image String* address, embedded in the code by the codegen.
6455 // Used for boot image strings referenced by apps in JIT-compiled code.
6456 kJitBootImageAddress,
6457
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006458 // Load from the root table associated with the JIT compiled method.
6459 kJitTableAddress,
6460
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006461 // Load using a simple runtime call. This is the fall-back load kind when
6462 // the codegen is unable to use another appropriate kind.
6463 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006464
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006465 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006466 };
6467
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006468 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006469 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006470 const DexFile& dex_file,
6471 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006472 : HInstruction(kLoadString,
6473 DataType::Type::kReference,
6474 SideEffectsForArchRuntimeCalls(),
6475 dex_pc),
Vladimir Marko372f10e2016-05-17 16:30:10 +01006476 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006477 string_index_(string_index),
6478 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006479 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006480 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006481
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006482 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01006483
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006484 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006485
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006486 LoadKind GetLoadKind() const {
6487 return GetPackedField<LoadKindField>();
6488 }
6489
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006490 bool HasPcRelativeLoadKind() const {
6491 return GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
6492 GetLoadKind() == LoadKind::kBootImageRelRo ||
6493 GetLoadKind() == LoadKind::kBssEntry;
6494 }
6495
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006496 const DexFile& GetDexFile() const {
6497 return dex_file_;
6498 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006499
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006500 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006501 return string_index_;
6502 }
6503
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006504 Handle<mirror::String> GetString() const {
6505 return string_;
6506 }
6507
6508 void SetString(Handle<mirror::String> str) {
6509 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006510 }
6511
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006512 bool CanBeMoved() const override { return true; }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006513
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006514 bool InstructionDataEquals(const HInstruction* other) const override;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006515
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006516 size_t ComputeHashCode() const override { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006517
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006518 // Will call the runtime if we need to load the string through
6519 // the dex cache and the string is not guaranteed to be there yet.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006520 bool NeedsEnvironment() const override {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006521 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01006522 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006523 load_kind == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006524 load_kind == LoadKind::kJitBootImageAddress ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006525 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006526 return false;
6527 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006528 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006529 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006530
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006531 bool NeedsDexCacheOfDeclaringClass() const override {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006532 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006533 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006534
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006535 bool CanBeNull() const override { return false; }
6536 bool CanThrow() const override { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006537
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006538 static SideEffects SideEffectsForArchRuntimeCalls() {
6539 return SideEffects::CanTriggerGC();
6540 }
6541
Vladimir Marko372f10e2016-05-17 16:30:10 +01006542 void AddSpecialInput(HInstruction* special_input);
6543
6544 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006545 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006546 return ArrayRef<HUserRecord<HInstruction*>>(
6547 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006548 }
6549
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006550 DECLARE_INSTRUCTION(LoadString);
6551
Artem Serovcced8ba2017-07-19 18:18:09 +01006552 protected:
6553 DEFAULT_COPY_CONSTRUCTOR(LoadString);
6554
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006555 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00006556 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557 static constexpr size_t kFieldLoadKindSize =
6558 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
6559 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006560 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006561 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006562
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006563 void SetLoadKindInternal(LoadKind load_kind);
6564
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006565 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006566 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00006567 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01006568 HUserRecord<HInstruction*> special_input_;
6569
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006570 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006571 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006572
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006573 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006574};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006575std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6576
6577// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Marko28e012a2017-12-07 11:22:59 +00006578inline void HLoadString::SetLoadKind(LoadKind load_kind) {
6579 // The load kind should be determined before inserting the instruction to the graph.
6580 DCHECK(GetBlock() == nullptr);
6581 DCHECK(GetEnvironment() == nullptr);
6582 DCHECK_EQ(GetLoadKind(), LoadKind::kRuntimeCall);
6583 SetPackedField<LoadKindField>(load_kind);
6584 if (load_kind != LoadKind::kRuntimeCall) {
6585 special_input_ = HUserRecord<HInstruction*>(nullptr);
6586 }
6587 if (!NeedsEnvironment()) {
6588 SetSideEffects(SideEffects::None());
6589 }
6590}
6591
6592// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006593inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006594 // The special input is used for PC-relative loads on some architectures,
6595 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006596 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006597 GetLoadKind() == LoadKind::kBootImageRelRo ||
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006598 GetLoadKind() == LoadKind::kBssEntry ||
6599 GetLoadKind() == LoadKind::kJitBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006600 // HLoadString::GetInputRecords() returns an empty array at this point,
6601 // so use the GetInputRecords() from the base class to set the input record.
6602 DCHECK(special_input_.GetInstruction() == nullptr);
6603 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604 special_input->AddUseAt(this, 0);
6605}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006606
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006607class HLoadMethodHandle final : public HInstruction {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006608 public:
6609 HLoadMethodHandle(HCurrentMethod* current_method,
Orion Hodson4c8e12e2018-05-18 08:33:20 +01006610 uint16_t method_handle_idx,
6611 const DexFile& dex_file,
6612 uint32_t dex_pc)
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006613 : HInstruction(kLoadMethodHandle,
6614 DataType::Type::kReference,
6615 SideEffectsForArchRuntimeCalls(),
6616 dex_pc),
6617 special_input_(HUserRecord<HInstruction*>(current_method)),
6618 method_handle_idx_(method_handle_idx),
6619 dex_file_(dex_file) {
6620 }
6621
6622 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006623 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006624 return ArrayRef<HUserRecord<HInstruction*>>(
6625 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6626 }
6627
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006628 bool IsClonable() const override { return true; }
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006629
6630 uint16_t GetMethodHandleIndex() const { return method_handle_idx_; }
6631
6632 const DexFile& GetDexFile() const { return dex_file_; }
6633
6634 static SideEffects SideEffectsForArchRuntimeCalls() {
6635 return SideEffects::CanTriggerGC();
6636 }
6637
6638 DECLARE_INSTRUCTION(LoadMethodHandle);
6639
6640 protected:
6641 DEFAULT_COPY_CONSTRUCTOR(LoadMethodHandle);
6642
6643 private:
6644 // The special input is the HCurrentMethod for kRuntimeCall.
6645 HUserRecord<HInstruction*> special_input_;
6646
6647 const uint16_t method_handle_idx_;
6648 const DexFile& dex_file_;
6649};
6650
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006651class HLoadMethodType final : public HInstruction {
Orion Hodson18259d72018-04-12 11:18:23 +01006652 public:
6653 HLoadMethodType(HCurrentMethod* current_method,
Orion Hodson06d10a72018-05-14 08:53:38 +01006654 dex::ProtoIndex proto_index,
Orion Hodson18259d72018-04-12 11:18:23 +01006655 const DexFile& dex_file,
6656 uint32_t dex_pc)
6657 : HInstruction(kLoadMethodType,
6658 DataType::Type::kReference,
6659 SideEffectsForArchRuntimeCalls(),
6660 dex_pc),
6661 special_input_(HUserRecord<HInstruction*>(current_method)),
Orion Hodson06d10a72018-05-14 08:53:38 +01006662 proto_index_(proto_index),
Orion Hodson18259d72018-04-12 11:18:23 +01006663 dex_file_(dex_file) {
6664 }
6665
6666 using HInstruction::GetInputRecords; // Keep the const version visible.
Roland Levillain625ca472018-08-24 19:14:16 +01006667 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() final {
Orion Hodson18259d72018-04-12 11:18:23 +01006668 return ArrayRef<HUserRecord<HInstruction*>>(
6669 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
6670 }
6671
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006672 bool IsClonable() const override { return true; }
Orion Hodson18259d72018-04-12 11:18:23 +01006673
Orion Hodson06d10a72018-05-14 08:53:38 +01006674 dex::ProtoIndex GetProtoIndex() const { return proto_index_; }
Orion Hodson18259d72018-04-12 11:18:23 +01006675
6676 const DexFile& GetDexFile() const { return dex_file_; }
6677
6678 static SideEffects SideEffectsForArchRuntimeCalls() {
6679 return SideEffects::CanTriggerGC();
6680 }
6681
6682 DECLARE_INSTRUCTION(LoadMethodType);
6683
6684 protected:
6685 DEFAULT_COPY_CONSTRUCTOR(LoadMethodType);
6686
6687 private:
6688 // The special input is the HCurrentMethod for kRuntimeCall.
6689 HUserRecord<HInstruction*> special_input_;
6690
Orion Hodson06d10a72018-05-14 08:53:38 +01006691 const dex::ProtoIndex proto_index_;
Orion Hodson18259d72018-04-12 11:18:23 +01006692 const DexFile& dex_file_;
6693};
6694
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006695/**
6696 * Performs an initialization check on its Class object input.
6697 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006698class HClinitCheck final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006699 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006700 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006701 : HExpression(
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306702 kClinitCheck,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 DataType::Type::kReference,
Mingyao Yang217eb062017-12-11 15:20:07 -08006704 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006705 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006706 SetRawInputAt(0, constant);
6707 }
Artem Serova6e26142018-06-19 14:55:17 +01006708 // TODO: Make ClinitCheck clonable.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006709 bool CanBeMoved() const override { return true; }
6710 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006711 return true;
6712 }
6713
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006714 bool NeedsEnvironment() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006715 // May call runtime to initialize the class.
6716 return true;
6717 }
6718
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006719 bool CanThrow() const override { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006720
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006721 HLoadClass* GetLoadClass() const {
6722 DCHECK(InputAt(0)->IsLoadClass());
6723 return InputAt(0)->AsLoadClass();
6724 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006725
6726 DECLARE_INSTRUCTION(ClinitCheck);
6727
Artem Serovcced8ba2017-07-19 18:18:09 +01006728
6729 protected:
6730 DEFAULT_COPY_CONSTRUCTOR(ClinitCheck);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006731};
6732
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006733class HStaticFieldGet final : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006734 public:
6735 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006736 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006737 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006738 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006739 bool is_volatile,
6740 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006741 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006742 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006743 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306744 : HExpression(kStaticFieldGet,
6745 field_type,
6746 SideEffects::FieldReadOfType(field_type, is_volatile),
6747 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006748 field_info_(field,
6749 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006750 field_type,
6751 is_volatile,
6752 field_idx,
6753 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006754 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755 SetRawInputAt(0, cls);
6756 }
6757
Calin Juravle52c48962014-12-16 17:02:57 +00006758
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006759 bool IsClonable() const override { return true; }
6760 bool CanBeMoved() const override { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006761
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006762 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01006763 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006764 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006765 }
6766
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006767 size_t ComputeHashCode() const override {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006768 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6769 }
6770
Calin Juravle52c48962014-12-16 17:02:57 +00006771 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006772 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006773 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006774 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006775
Vladimir Marko61b92282017-10-11 13:23:17 +01006776 void SetType(DataType::Type new_type) {
6777 DCHECK(DataType::IsIntegralType(GetType()));
6778 DCHECK(DataType::IsIntegralType(new_type));
6779 DCHECK_EQ(DataType::Size(GetType()), DataType::Size(new_type));
6780 SetPackedField<TypeField>(new_type);
6781 }
6782
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006783 DECLARE_INSTRUCTION(StaticFieldGet);
6784
Artem Serovcced8ba2017-07-19 18:18:09 +01006785 protected:
6786 DEFAULT_COPY_CONSTRUCTOR(StaticFieldGet);
6787
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006788 private:
6789 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006790};
6791
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006792class HStaticFieldSet final : public HExpression<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006793 public:
6794 HStaticFieldSet(HInstruction* cls,
6795 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006796 ArtField* field,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 DataType::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006798 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006799 bool is_volatile,
6800 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006801 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006802 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006803 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006804 : HExpression(kStaticFieldSet,
6805 SideEffects::FieldWriteOfType(field_type, is_volatile),
6806 dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006807 field_info_(field,
6808 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006809 field_type,
6810 is_volatile,
6811 field_idx,
6812 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006813 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006814 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006815 SetRawInputAt(0, cls);
6816 SetRawInputAt(1, value);
6817 }
6818
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006819 bool IsClonable() const override { return true; }
Calin Juravle52c48962014-12-16 17:02:57 +00006820 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006821 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006822 DataType::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006823 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006825 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006826 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6827 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006829 DECLARE_INSTRUCTION(StaticFieldSet);
6830
Artem Serovcced8ba2017-07-19 18:18:09 +01006831 protected:
6832 DEFAULT_COPY_CONSTRUCTOR(StaticFieldSet);
6833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006834 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006835 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6836 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6837 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6838 "Too many packed fields.");
6839
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006840 const FieldInfo field_info_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006841};
6842
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006843class HUnresolvedInstanceFieldGet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006844 public:
6845 HUnresolvedInstanceFieldGet(HInstruction* obj,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006846 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006847 uint32_t field_index,
6848 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306849 : HExpression(kUnresolvedInstanceFieldGet,
6850 field_type,
6851 SideEffects::AllExceptGCDependency(),
6852 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006853 field_index_(field_index) {
6854 SetRawInputAt(0, obj);
6855 }
6856
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006857 bool IsClonable() const override { return true; }
6858 bool NeedsEnvironment() const override { return true; }
6859 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006862 uint32_t GetFieldIndex() const { return field_index_; }
6863
6864 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6865
Artem Serovcced8ba2017-07-19 18:18:09 +01006866 protected:
6867 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldGet);
6868
Calin Juravlee460d1d2015-09-29 04:52:17 +01006869 private:
6870 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006871};
6872
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006873class HUnresolvedInstanceFieldSet final : public HExpression<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006874 public:
6875 HUnresolvedInstanceFieldSet(HInstruction* obj,
6876 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006877 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006878 uint32_t field_index,
6879 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006880 : HExpression(kUnresolvedInstanceFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006881 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006882 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006883 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006884 SetRawInputAt(0, obj);
6885 SetRawInputAt(1, value);
6886 }
6887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006888 bool IsClonable() const override { return true; }
6889 bool NeedsEnvironment() const override { return true; }
6890 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006892 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006893 uint32_t GetFieldIndex() const { return field_index_; }
6894
6895 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6896
Artem Serovcced8ba2017-07-19 18:18:09 +01006897 protected:
6898 DEFAULT_COPY_CONSTRUCTOR(UnresolvedInstanceFieldSet);
6899
Calin Juravlee460d1d2015-09-29 04:52:17 +01006900 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006901 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6902 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006903 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006904 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6905 kFieldFieldType + kFieldFieldTypeSize;
6906 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6907 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006908 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006909
Calin Juravlee460d1d2015-09-29 04:52:17 +01006910 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006911};
6912
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006913class HUnresolvedStaticFieldGet final : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006914 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006915 HUnresolvedStaticFieldGet(DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006916 uint32_t field_index,
6917 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306918 : HExpression(kUnresolvedStaticFieldGet,
6919 field_type,
6920 SideEffects::AllExceptGCDependency(),
6921 dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006922 field_index_(field_index) {
6923 }
6924
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006925 bool IsClonable() const override { return true; }
6926 bool NeedsEnvironment() const override { return true; }
6927 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006929 DataType::Type GetFieldType() const { return GetType(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006930 uint32_t GetFieldIndex() const { return field_index_; }
6931
6932 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6933
Artem Serovcced8ba2017-07-19 18:18:09 +01006934 protected:
6935 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldGet);
6936
Calin Juravlee460d1d2015-09-29 04:52:17 +01006937 private:
6938 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006939};
6940
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006941class HUnresolvedStaticFieldSet final : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006942 public:
6943 HUnresolvedStaticFieldSet(HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006944 DataType::Type field_type,
Calin Juravlee460d1d2015-09-29 04:52:17 +01006945 uint32_t field_index,
6946 uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01006947 : HExpression(kUnresolvedStaticFieldSet, SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006948 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006949 SetPackedField<FieldTypeField>(field_type);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006950 DCHECK_EQ(DataType::Kind(field_type), DataType::Kind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006951 SetRawInputAt(0, value);
6952 }
6953
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006954 bool IsClonable() const override { return true; }
6955 bool NeedsEnvironment() const override { return true; }
6956 bool CanThrow() const override { return true; }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006957
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006958 DataType::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006959 uint32_t GetFieldIndex() const { return field_index_; }
6960
6961 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6962
Artem Serovcced8ba2017-07-19 18:18:09 +01006963 protected:
6964 DEFAULT_COPY_CONSTRUCTOR(UnresolvedStaticFieldSet);
6965
Calin Juravlee460d1d2015-09-29 04:52:17 +01006966 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006967 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6968 static constexpr size_t kFieldFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006969 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Vladimir Markoa1de9182016-02-25 11:37:38 +00006970 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6971 kFieldFieldType + kFieldFieldTypeSize;
6972 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6973 "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006974 using FieldTypeField = BitField<DataType::Type, kFieldFieldType, kFieldFieldTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00006975
Calin Juravlee460d1d2015-09-29 04:52:17 +01006976 const uint32_t field_index_;
Calin Juravlee460d1d2015-09-29 04:52:17 +01006977};
6978
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006979// Implement the move-exception DEX instruction.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006980class HLoadException final : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006982 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05306983 : HExpression(kLoadException, DataType::Type::kReference, SideEffects::None(), dex_pc) {
6984 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006985
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006986 bool CanBeNull() const override { return false; }
David Brazdilbbd733e2015-08-18 17:48:17 +01006987
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006988 DECLARE_INSTRUCTION(LoadException);
6989
Artem Serovcced8ba2017-07-19 18:18:09 +01006990 protected:
6991 DEFAULT_COPY_CONSTRUCTOR(LoadException);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006992};
6993
David Brazdilcb1c0552015-08-04 16:22:25 +01006994// Implicit part of move-exception which clears thread-local exception storage.
6995// Must not be removed because the runtime expects the TLS to get cleared.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01006996class HClearException final : public HExpression<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006998 explicit HClearException(uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01006999 : HExpression(kClearException, SideEffects::AllWrites(), dex_pc) {
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307000 }
David Brazdilcb1c0552015-08-04 16:22:25 +01007001
7002 DECLARE_INSTRUCTION(ClearException);
7003
Artem Serovcced8ba2017-07-19 18:18:09 +01007004 protected:
7005 DEFAULT_COPY_CONSTRUCTOR(ClearException);
David Brazdilcb1c0552015-08-04 16:22:25 +01007006};
7007
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007008class HThrow final : public HExpression<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007009 public:
7010 HThrow(HInstruction* exception, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007011 : HExpression(kThrow, SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007012 SetRawInputAt(0, exception);
7013 }
7014
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007015 bool IsControlFlow() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007016
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007017 bool NeedsEnvironment() const override { return true; }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007018
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007019 bool CanThrow() const override { return true; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007020
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007021 bool AlwaysThrows() const override { return true; }
Aart Bika8b8e9b2018-01-09 11:01:02 -08007022
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007023 DECLARE_INSTRUCTION(Throw);
7024
Artem Serovcced8ba2017-07-19 18:18:09 +01007025 protected:
7026 DEFAULT_COPY_CONSTRUCTOR(Throw);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007027};
7028
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007029/**
7030 * Implementation strategies for the code generator of a HInstanceOf
7031 * or `HCheckCast`.
7032 */
7033enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01007034 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 kExactCheck, // Can do a single class compare.
7036 kClassHierarchyCheck, // Can just walk the super class chain.
7037 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
7038 kInterfaceCheck, // No optimization yet when checking against an interface.
7039 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007040 kArrayCheck, // No optimization yet when checking against a generic array.
Vladimir Marko175e7862018-03-27 09:03:13 +00007041 kBitstringCheck, // Compare the type check bitstring.
Vladimir Markoa1de9182016-02-25 11:37:38 +00007042 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007043};
7044
Roland Levillain86503782016-02-11 19:07:30 +00007045std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
7046
Vladimir Marko175e7862018-03-27 09:03:13 +00007047// Note: HTypeCheckInstruction is just a helper class, not an abstract instruction with an
7048// `IsTypeCheckInstruction()`. (New virtual methods in the HInstruction class have a high cost.)
7049class HTypeCheckInstruction : public HVariableInputSizeInstruction {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007050 public:
Vladimir Marko175e7862018-03-27 09:03:13 +00007051 HTypeCheckInstruction(InstructionKind kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007052 DataType::Type type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007053 HInstruction* object,
7054 HInstruction* target_class_or_null,
7055 TypeCheckKind check_kind,
7056 Handle<mirror::Class> klass,
7057 uint32_t dex_pc,
7058 ArenaAllocator* allocator,
7059 HIntConstant* bitstring_path_to_root,
7060 HIntConstant* bitstring_mask,
7061 SideEffects side_effects)
7062 : HVariableInputSizeInstruction(
7063 kind,
Vladimir Markobd785672018-05-03 17:09:09 +01007064 type,
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 side_effects,
7066 dex_pc,
7067 allocator,
7068 /* number_of_inputs */ check_kind == TypeCheckKind::kBitstringCheck ? 4u : 2u,
7069 kArenaAllocTypeCheckInputs),
7070 klass_(klass) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007071 SetPackedField<TypeCheckKindField>(check_kind);
7072 SetPackedFlag<kFlagMustDoNullCheck>(true);
Vladimir Marko175e7862018-03-27 09:03:13 +00007073 SetPackedFlag<kFlagValidTargetClassRTI>(false);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007074 SetRawInputAt(0, object);
Vladimir Marko175e7862018-03-27 09:03:13 +00007075 SetRawInputAt(1, target_class_or_null);
7076 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_path_to_root != nullptr);
7077 DCHECK_EQ(check_kind == TypeCheckKind::kBitstringCheck, bitstring_mask != nullptr);
7078 if (check_kind == TypeCheckKind::kBitstringCheck) {
7079 DCHECK(target_class_or_null->IsNullConstant());
7080 SetRawInputAt(2, bitstring_path_to_root);
7081 SetRawInputAt(3, bitstring_mask);
7082 } else {
7083 DCHECK(target_class_or_null->IsLoadClass());
7084 }
Vladimir Marko87584542017-12-12 17:47:52 +00007085 }
7086
7087 HLoadClass* GetTargetClass() const {
Vladimir Marko175e7862018-03-27 09:03:13 +00007088 DCHECK_NE(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
Vladimir Marko87584542017-12-12 17:47:52 +00007089 HInstruction* load_class = InputAt(1);
7090 DCHECK(load_class->IsLoadClass());
7091 return load_class->AsLoadClass();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007092 }
7093
Vladimir Marko175e7862018-03-27 09:03:13 +00007094 uint32_t GetBitstringPathToRoot() const {
7095 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7096 HInstruction* path_to_root = InputAt(2);
7097 DCHECK(path_to_root->IsIntConstant());
7098 return static_cast<uint32_t>(path_to_root->AsIntConstant()->GetValue());
7099 }
7100
7101 uint32_t GetBitstringMask() const {
7102 DCHECK_EQ(GetTypeCheckKind(), TypeCheckKind::kBitstringCheck);
7103 HInstruction* mask = InputAt(3);
7104 DCHECK(mask->IsIntConstant());
7105 return static_cast<uint32_t>(mask->AsIntConstant()->GetValue());
7106 }
7107
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007108 bool IsClonable() const override { return true; }
7109 bool CanBeMoved() const override { return true; }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007111 bool InstructionDataEquals(const HInstruction* other) const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007112 DCHECK(other->IsInstanceOf() || other->IsCheckCast()) << other->DebugName();
7113 return GetPackedFields() == down_cast<const HTypeCheckInstruction*>(other)->GetPackedFields();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007114 }
7115
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007116 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
7117 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
7118 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
7119 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
7120
Vladimir Marko175e7862018-03-27 09:03:13 +00007121 ReferenceTypeInfo GetTargetClassRTI() {
7122 if (GetPackedFlag<kFlagValidTargetClassRTI>()) {
7123 // Note: The is_exact flag from the return value should not be used.
7124 return ReferenceTypeInfo::CreateUnchecked(klass_, /* is_exact */ true);
7125 } else {
7126 return ReferenceTypeInfo::CreateInvalid();
7127 }
7128 }
7129
7130 // Target class RTI is marked as valid by RTP if the klass_ is admissible.
7131 void SetValidTargetClassRTI() REQUIRES_SHARED(Locks::mutator_lock_) {
7132 DCHECK(klass_ != nullptr);
7133 SetPackedFlag<kFlagValidTargetClassRTI>(true);
7134 }
7135
7136 Handle<mirror::Class> GetClass() const {
7137 return klass_;
7138 }
7139
7140 protected:
7141 DEFAULT_COPY_CONSTRUCTOR(TypeCheckInstruction);
7142
7143 private:
7144 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
7145 static constexpr size_t kFieldTypeCheckKindSize =
7146 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
7147 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
7148 static constexpr size_t kFlagValidTargetClassRTI = kFlagMustDoNullCheck + 1;
7149 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagValidTargetClassRTI + 1;
7150 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7151 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
7152
7153 Handle<mirror::Class> klass_;
7154};
7155
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007156class HInstanceOf final : public HTypeCheckInstruction {
Vladimir Marko175e7862018-03-27 09:03:13 +00007157 public:
7158 HInstanceOf(HInstruction* object,
7159 HInstruction* target_class_or_null,
7160 TypeCheckKind check_kind,
7161 Handle<mirror::Class> klass,
7162 uint32_t dex_pc,
7163 ArenaAllocator* allocator,
7164 HIntConstant* bitstring_path_to_root,
7165 HIntConstant* bitstring_mask)
7166 : HTypeCheckInstruction(kInstanceOf,
Vladimir Markobd785672018-05-03 17:09:09 +01007167 DataType::Type::kBool,
Vladimir Marko175e7862018-03-27 09:03:13 +00007168 object,
7169 target_class_or_null,
7170 check_kind,
7171 klass,
7172 dex_pc,
7173 allocator,
7174 bitstring_path_to_root,
7175 bitstring_mask,
7176 SideEffectsForArchRuntimeCalls(check_kind)) {}
7177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007178 bool IsClonable() const override { return true; }
Artem Serova6e26142018-06-19 14:55:17 +01007179
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007180 bool NeedsEnvironment() const override {
Vladimir Marko175e7862018-03-27 09:03:13 +00007181 return CanCallRuntime(GetTypeCheckKind());
7182 }
7183
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007184 static bool CanCallRuntime(TypeCheckKind check_kind) {
7185 // Mips currently does runtime calls for any other checks.
7186 return check_kind != TypeCheckKind::kExactCheck;
7187 }
7188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007189 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007190 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01007191 }
7192
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007193 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007194
Artem Serovcced8ba2017-07-19 18:18:09 +01007195 protected:
7196 DEFAULT_COPY_CONSTRUCTOR(InstanceOf);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007197};
7198
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007199class HBoundType final : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00007200 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07007201 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307202 : HExpression(kBoundType, DataType::Type::kReference, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00007203 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
7204 SetPackedFlag<kFlagUpperCanBeNull>(true);
7205 SetPackedFlag<kFlagCanBeNull>(true);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007206 DCHECK_EQ(input->GetType(), DataType::Type::kReference);
Calin Juravleb1498f62015-02-16 13:13:29 +00007207 SetRawInputAt(0, input);
7208 }
7209
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007210 bool InstructionDataEquals(const HInstruction* other) const override;
7211 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007212
David Brazdilf5552582015-12-27 13:36:12 +00007213 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007214 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00007215 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00007216 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007217
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007218 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007219 DCHECK(GetUpperCanBeNull() || !can_be_null);
7220 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00007221 }
7222
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007223 bool CanBeNull() const override { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007224
Calin Juravleb1498f62015-02-16 13:13:29 +00007225 DECLARE_INSTRUCTION(BoundType);
7226
Artem Serovcced8ba2017-07-19 18:18:09 +01007227 protected:
7228 DEFAULT_COPY_CONSTRUCTOR(BoundType);
7229
Calin Juravleb1498f62015-02-16 13:13:29 +00007230 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007231 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
7232 // is false then CanBeNull() cannot be true).
Vladimir Markobd785672018-05-03 17:09:09 +01007233 static constexpr size_t kFlagUpperCanBeNull = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00007234 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
7235 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
7236 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
7237
Calin Juravleb1498f62015-02-16 13:13:29 +00007238 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00007239 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
7240 // It is used to bound the type in cases like:
7241 // if (x instanceof ClassX) {
7242 // // uper_bound_ will be ClassX
7243 // }
David Brazdilf5552582015-12-27 13:36:12 +00007244 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00007245};
7246
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007247class HCheckCast final : public HTypeCheckInstruction {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007248 public:
7249 HCheckCast(HInstruction* object,
Vladimir Marko175e7862018-03-27 09:03:13 +00007250 HInstruction* target_class_or_null,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007251 TypeCheckKind check_kind,
Vladimir Marko175e7862018-03-27 09:03:13 +00007252 Handle<mirror::Class> klass,
7253 uint32_t dex_pc,
7254 ArenaAllocator* allocator,
7255 HIntConstant* bitstring_path_to_root,
7256 HIntConstant* bitstring_mask)
7257 : HTypeCheckInstruction(kCheckCast,
Vladimir Markobd785672018-05-03 17:09:09 +01007258 DataType::Type::kVoid,
Vladimir Marko175e7862018-03-27 09:03:13 +00007259 object,
7260 target_class_or_null,
7261 check_kind,
7262 klass,
7263 dex_pc,
7264 allocator,
7265 bitstring_path_to_root,
7266 bitstring_mask,
7267 SideEffects::CanTriggerGC()) {}
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007268
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007269 bool IsClonable() const override { return true; }
7270 bool NeedsEnvironment() const override {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007271 // Instruction may throw a CheckCastError.
7272 return true;
7273 }
7274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007275 bool CanThrow() const override { return true; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007276
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007277 DECLARE_INSTRUCTION(CheckCast);
7278
Artem Serovcced8ba2017-07-19 18:18:09 +01007279 protected:
7280 DEFAULT_COPY_CONSTRUCTOR(CheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007281};
7282
Andreas Gampe26de38b2016-07-27 17:53:11 -07007283/**
7284 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
7285 * @details We define the combined barrier types that are actually required
7286 * by the Java Memory Model, rather than using exactly the terminology from
7287 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
7288 * primitives. Note that the JSR-133 cookbook generally does not deal with
7289 * store atomicity issues, and the recipes there are not always entirely sufficient.
7290 * The current recipe is as follows:
7291 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
7292 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
7293 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
7294 * -# Use StoreStore barrier after all stores but before return from any constructor whose
7295 * class has final fields.
7296 * -# Use NTStoreStore to order non-temporal stores with respect to all later
7297 * store-to-memory instructions. Only generated together with non-temporal stores.
7298 */
7299enum MemBarrierKind {
7300 kAnyStore,
7301 kLoadAny,
7302 kStoreStore,
7303 kAnyAny,
7304 kNTStoreStore,
7305 kLastBarrierKind = kNTStoreStore
7306};
7307std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
7308
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007309class HMemoryBarrier final : public HExpression<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01007310 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06007311 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007312 : HExpression(kMemoryBarrier,
7313 SideEffects::AllWritesAndReads(), // Assume write/read on all fields/arrays.
7314 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007315 SetPackedField<BarrierKindField>(barrier_kind);
7316 }
Calin Juravle27df7582015-04-17 19:12:31 +01007317
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007318 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007319
Vladimir Markoa1de9182016-02-25 11:37:38 +00007320 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01007321
7322 DECLARE_INSTRUCTION(MemoryBarrier);
7323
Artem Serovcced8ba2017-07-19 18:18:09 +01007324 protected:
7325 DEFAULT_COPY_CONSTRUCTOR(MemoryBarrier);
7326
Calin Juravle27df7582015-04-17 19:12:31 +01007327 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007328 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
7329 static constexpr size_t kFieldBarrierKindSize =
7330 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
7331 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
7332 kFieldBarrierKind + kFieldBarrierKindSize;
7333 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
7334 "Too many packed fields.");
7335 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01007336};
7337
Igor Murashkind01745e2017-04-05 16:40:31 -07007338// A constructor fence orders all prior stores to fields that could be accessed via a final field of
7339// the specified object(s), with respect to any subsequent store that might "publish"
7340// (i.e. make visible) the specified object to another thread.
7341//
7342// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
7343// for all final fields (that were set) at the end of the invoked constructor.
7344//
7345// The constructor fence models the freeze actions for the final fields of an object
7346// being constructed (semantically at the end of the constructor). Constructor fences
7347// have a per-object affinity; two separate objects being constructed get two separate
7348// constructor fences.
7349//
7350// (Note: that if calling a super-constructor or forwarding to another constructor,
7351// the freezes would happen at the end of *that* constructor being invoked).
7352//
7353// The memory model guarantees that when the object being constructed is "published" after
7354// constructor completion (i.e. escapes the current thread via a store), then any final field
7355// writes must be observable on other threads (once they observe that publication).
7356//
7357// Further, anything written before the freeze, and read by dereferencing through the final field,
7358// must also be visible (so final object field could itself have an object with non-final fields;
7359// yet the freeze must also extend to them).
7360//
7361// Constructor example:
7362//
7363// class HasFinal {
7364// final int field; Optimizing IR for <init>()V:
7365// HasFinal() {
7366// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
7367// // freeze(this.field); HConstructorFence(this)
7368// } HReturn
7369// }
7370//
7371// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
7372// already-initialized classes; in that case the allocation must act as a "default-initializer"
7373// of the object which effectively writes the class pointer "final field".
7374//
7375// For example, we can model default-initialiation as roughly the equivalent of the following:
7376//
7377// class Object {
7378// private final Class header;
7379// }
7380//
7381// Java code: Optimizing IR:
7382//
7383// T new_instance<T>() {
7384// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
7385// obj.header = T.class; // header write is done by above call.
7386// // freeze(obj.header) HConstructorFence(obj)
7387// return (T)obj;
7388// }
7389//
7390// See also:
7391// * CompilerDriver::RequiresConstructorBarrier
7392// * QuasiAtomic::ThreadFenceForConstructor
7393//
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007394class HConstructorFence final : public HVariableInputSizeInstruction {
Igor Murashkind01745e2017-04-05 16:40:31 -07007395 // A fence has variable inputs because the inputs can be removed
7396 // after prepare_for_register_allocation phase.
7397 // (TODO: In the future a fence could freeze multiple objects
7398 // after merging two fences together.)
7399 public:
7400 // `fence_object` is the reference that needs to be protected for correct publication.
7401 //
7402 // It makes sense in the following situations:
7403 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
7404 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
7405 //
7406 // After construction the `fence_object` becomes the 0th input.
7407 // This is not an input in a real sense, but just a convenient place to stash the information
7408 // about the associated object.
7409 HConstructorFence(HInstruction* fence_object,
7410 uint32_t dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007411 ArenaAllocator* allocator)
Igor Murashkind01745e2017-04-05 16:40:31 -07007412 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
7413 // constraints described in the class header. We claim that these SideEffects constraints
7414 // enforce a superset of the real constraints.
7415 //
7416 // The ordering described above is conservatively modeled with SideEffects as follows:
7417 //
7418 // * To prevent reordering of the publication stores:
7419 // ----> "Reads of objects" is the initial SideEffect.
7420 // * For every primitive final field store in the constructor:
7421 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
7422 // * If there are any stores to reference final fields in the constructor:
7423 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
7424 // that are reachable from `fence_object` also need to be prevented for reordering
7425 // (and we do not want to do alias analysis to figure out what those stores are).
7426 //
7427 // In the implementation, this initially starts out as an "all reads" side effect; this is an
7428 // even more conservative approach than the one described above, and prevents all of the
7429 // above reordering without analyzing any of the instructions in the constructor.
7430 //
7431 // If in a later phase we discover that there are no writes to reference final fields,
7432 // we can refine the side effect to a smaller set of type reads (see above constraints).
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307433 : HVariableInputSizeInstruction(kConstructorFence,
7434 SideEffects::AllReads(),
Igor Murashkind01745e2017-04-05 16:40:31 -07007435 dex_pc,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007436 allocator,
Igor Murashkind01745e2017-04-05 16:40:31 -07007437 /* number_of_inputs */ 1,
7438 kArenaAllocConstructorFenceInputs) {
7439 DCHECK(fence_object != nullptr);
7440 SetRawInputAt(0, fence_object);
7441 }
7442
7443 // The object associated with this constructor fence.
7444 //
7445 // (Note: This will be null after the prepare_for_register_allocation phase,
7446 // as all constructor fence inputs are removed there).
7447 HInstruction* GetFenceObject() const {
7448 return InputAt(0);
7449 }
7450
7451 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
7452 // - Delete `fence_use` from `this`'s use list.
7453 // - Delete `this` from `fence_use`'s inputs list.
7454 // - If the `fence_use` is dead, remove it from the graph.
7455 //
7456 // A fence is considered dead once it no longer has any uses
7457 // and all of the inputs are dead.
7458 //
7459 // This must *not* be called during/after prepare_for_register_allocation,
7460 // because that removes all the inputs to the fences but the fence is actually
7461 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07007462 //
7463 // Returns how many HConstructorFence instructions were removed from graph.
7464 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07007465
Igor Murashkindd018df2017-08-09 10:38:31 -07007466 // Combine all inputs of `this` and `other` instruction and remove
7467 // `other` from the graph.
7468 //
7469 // Inputs are unique after the merge.
7470 //
7471 // Requirement: `this` must not be the same as `other.
7472 void Merge(HConstructorFence* other);
7473
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007474 // Check if this constructor fence is protecting
7475 // an HNewInstance or HNewArray that is also the immediate
7476 // predecessor of `this`.
7477 //
Igor Murashkindd018df2017-08-09 10:38:31 -07007478 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
7479 // to be one of the inputs of `this`.
7480 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007481 // Returns the associated HNewArray or HNewInstance,
7482 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07007483 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07007484
Igor Murashkind01745e2017-04-05 16:40:31 -07007485 DECLARE_INSTRUCTION(ConstructorFence);
7486
Artem Serovcced8ba2017-07-19 18:18:09 +01007487 protected:
7488 DEFAULT_COPY_CONSTRUCTOR(ConstructorFence);
Igor Murashkind01745e2017-04-05 16:40:31 -07007489};
7490
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007491class HMonitorOperation final : public HExpression<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007492 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007493 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007494 kEnter,
7495 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00007496 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007497 };
7498
7499 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Vladimir Markobd785672018-05-03 17:09:09 +01007500 : HExpression(kMonitorOperation,
7501 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
7502 dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00007503 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007504 SetRawInputAt(0, object);
7505 }
7506
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00007507 // Instruction may go into runtime, so we need an environment.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007508 bool NeedsEnvironment() const override { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00007509
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007510 bool CanThrow() const override {
David Brazdilbff75032015-07-08 17:26:51 +00007511 // Verifier guarantees that monitor-exit cannot throw.
7512 // This is important because it allows the HGraphBuilder to remove
7513 // a dead throw-catch loop generated for `synchronized` blocks/methods.
7514 return IsEnter();
7515 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007516
Vladimir Markoa1de9182016-02-25 11:37:38 +00007517 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
7518 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007519
7520 DECLARE_INSTRUCTION(MonitorOperation);
7521
Artem Serovcced8ba2017-07-19 18:18:09 +01007522 protected:
7523 DEFAULT_COPY_CONSTRUCTOR(MonitorOperation);
7524
Calin Juravle52c48962014-12-16 17:02:57 +00007525 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00007526 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
7527 static constexpr size_t kFieldOperationKindSize =
7528 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
7529 static constexpr size_t kNumberOfMonitorOperationPackedBits =
7530 kFieldOperationKind + kFieldOperationKindSize;
7531 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
7532 "Too many packed fields.");
7533 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007534};
7535
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007536class HSelect final : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00007537 public:
7538 HSelect(HInstruction* condition,
7539 HInstruction* true_value,
7540 HInstruction* false_value,
7541 uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307542 : HExpression(kSelect, HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
David Brazdil74eb1b22015-12-14 11:44:01 +00007543 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
7544
7545 // First input must be `true_value` or `false_value` to allow codegens to
7546 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
7547 // that architectures which implement HSelect as a conditional move also
7548 // will not need to invert the condition.
7549 SetRawInputAt(0, false_value);
7550 SetRawInputAt(1, true_value);
7551 SetRawInputAt(2, condition);
7552 }
7553
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007554 bool IsClonable() const override { return true; }
David Brazdil74eb1b22015-12-14 11:44:01 +00007555 HInstruction* GetFalseValue() const { return InputAt(0); }
7556 HInstruction* GetTrueValue() const { return InputAt(1); }
7557 HInstruction* GetCondition() const { return InputAt(2); }
7558
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007559 bool CanBeMoved() const override { return true; }
7560 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Vladimir Marko372f10e2016-05-17 16:30:10 +01007561 return true;
7562 }
David Brazdil74eb1b22015-12-14 11:44:01 +00007563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007564 bool CanBeNull() const override {
David Brazdil74eb1b22015-12-14 11:44:01 +00007565 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
7566 }
7567
7568 DECLARE_INSTRUCTION(Select);
7569
Artem Serovcced8ba2017-07-19 18:18:09 +01007570 protected:
7571 DEFAULT_COPY_CONSTRUCTOR(Select);
David Brazdil74eb1b22015-12-14 11:44:01 +00007572};
7573
Vladimir Markof9f64412015-09-02 14:05:49 +01007574class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007575 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01007576 MoveOperands(Location source,
7577 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007578 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007579 HInstruction* instruction)
7580 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007581
7582 Location GetSource() const { return source_; }
7583 Location GetDestination() const { return destination_; }
7584
7585 void SetSource(Location value) { source_ = value; }
7586 void SetDestination(Location value) { destination_ = value; }
7587
7588 // The parallel move resolver marks moves as "in-progress" by clearing the
7589 // destination (but not the source).
7590 Location MarkPending() {
7591 DCHECK(!IsPending());
7592 Location dest = destination_;
7593 destination_ = Location::NoLocation();
7594 return dest;
7595 }
7596
7597 void ClearPending(Location dest) {
7598 DCHECK(IsPending());
7599 destination_ = dest;
7600 }
7601
7602 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00007603 DCHECK(source_.IsValid() || destination_.IsInvalid());
7604 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007605 }
7606
7607 // True if this blocks a move from the given location.
7608 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08007609 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007610 }
7611
7612 // A move is redundant if it's been eliminated, if its source and
7613 // destination are the same, or if its destination is unneeded.
7614 bool IsRedundant() const {
7615 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
7616 }
7617
7618 // We clear both operands to indicate move that's been eliminated.
7619 void Eliminate() {
7620 source_ = destination_ = Location::NoLocation();
7621 }
7622
7623 bool IsEliminated() const {
7624 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
7625 return source_.IsInvalid();
7626 }
7627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007628 DataType::Type GetType() const { return type_; }
Alexey Frunze4dda3372015-06-01 18:31:49 -07007629
Nicolas Geoffray90218252015-04-15 11:56:51 +01007630 bool Is64BitMove() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007631 return DataType::Is64BitType(type_);
Nicolas Geoffray90218252015-04-15 11:56:51 +01007632 }
7633
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007634 HInstruction* GetInstruction() const { return instruction_; }
7635
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007636 private:
7637 Location source_;
7638 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01007639 // The type this move is for.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007640 DataType::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007641 // The instruction this move is assocatied with. Null when this move is
7642 // for moving an input in the expected locations of user (including a phi user).
7643 // This is only used in debug mode, to ensure we do not connect interval siblings
7644 // in the same parallel move.
7645 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007646};
7647
Roland Levillainc9285912015-12-18 10:38:42 +00007648std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
7649
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007650static constexpr size_t kDefaultNumberOfMoves = 4;
7651
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007652class HParallelMove final : public HExpression<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007653 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007654 explicit HParallelMove(ArenaAllocator* allocator, uint32_t dex_pc = kNoDexPc)
Vladimir Markobd785672018-05-03 17:09:09 +01007655 : HExpression(kParallelMove, SideEffects::None(), dex_pc),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01007656 moves_(allocator->Adapter(kArenaAllocMoveOperands)) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007657 moves_.reserve(kDefaultNumberOfMoves);
7658 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007659
Nicolas Geoffray90218252015-04-15 11:56:51 +01007660 void AddMove(Location source,
7661 Location destination,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007662 DataType::Type type,
Nicolas Geoffray90218252015-04-15 11:56:51 +01007663 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00007664 DCHECK(source.IsValid());
7665 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007666 if (kIsDebugBuild) {
7667 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007668 for (const MoveOperands& move : moves_) {
7669 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007670 // Special case the situation where the move is for the spill slot
7671 // of the instruction.
7672 if ((GetPrevious() == instruction)
7673 || ((GetPrevious() == nullptr)
7674 && instruction->IsPhi()
7675 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007676 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007677 << "Doing parallel moves for the same instruction.";
7678 } else {
7679 DCHECK(false) << "Doing parallel moves for the same instruction.";
7680 }
7681 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00007682 }
7683 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007684 for (const MoveOperands& move : moves_) {
7685 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007686 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01007687 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01007688 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007689 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01007690 }
Vladimir Marko225b6462015-09-28 12:17:40 +01007691 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007692 }
7693
Vladimir Marko225b6462015-09-28 12:17:40 +01007694 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007695 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007696 }
7697
Vladimir Marko225b6462015-09-28 12:17:40 +01007698 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007699
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01007700 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007701
Artem Serovcced8ba2017-07-19 18:18:09 +01007702 protected:
7703 DEFAULT_COPY_CONSTRUCTOR(ParallelMove);
7704
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007705 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01007706 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007707};
7708
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007709// This instruction computes an intermediate address pointing in the 'middle' of an object. The
7710// result pointer cannot be handled by GC, so extra care is taken to make sure that this value is
7711// never used across anything that can trigger GC.
7712// The result of this instruction is not a pointer in the sense of `DataType::Type::kreference`.
7713// So we represent it by the type `DataType::Type::kInt`.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007714class HIntermediateAddress final : public HExpression<2> {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007715 public:
7716 HIntermediateAddress(HInstruction* base_address, HInstruction* offset, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +05307717 : HExpression(kIntermediateAddress,
7718 DataType::Type::kInt32,
7719 SideEffects::DependsOnGC(),
7720 dex_pc) {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007721 DCHECK_EQ(DataType::Size(DataType::Type::kInt32),
7722 DataType::Size(DataType::Type::kReference))
7723 << "kPrimInt and kPrimNot have different sizes.";
7724 SetRawInputAt(0, base_address);
7725 SetRawInputAt(1, offset);
7726 }
7727
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007728 bool IsClonable() const override { return true; }
7729 bool CanBeMoved() const override { return true; }
7730 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007731 return true;
7732 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007733 bool IsActualObject() const override { return false; }
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007734
7735 HInstruction* GetBaseAddress() const { return InputAt(0); }
7736 HInstruction* GetOffset() const { return InputAt(1); }
7737
7738 DECLARE_INSTRUCTION(IntermediateAddress);
7739
Artem Serovcced8ba2017-07-19 18:18:09 +01007740 protected:
7741 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddress);
xueliang.zhonge0eb4832017-10-30 13:43:14 +00007742};
7743
7744
Mark Mendell0616ae02015-04-17 12:49:27 -04007745} // namespace art
7746
Aart Bikf8f5a162017-02-06 15:35:29 -08007747#include "nodes_vector.h"
7748
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007749#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
7750#include "nodes_shared.h"
7751#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007752#ifdef ART_ENABLE_CODEGEN_mips
7753#include "nodes_mips.h"
7754#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04007755#ifdef ART_ENABLE_CODEGEN_x86
7756#include "nodes_x86.h"
7757#endif
7758
7759namespace art {
7760
Igor Murashkin6ef45672017-08-08 13:59:55 -07007761class OptimizingCompilerStats;
7762
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007763class HGraphVisitor : public ValueObject {
7764 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007765 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7766 : stats_(stats),
7767 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07007768 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007769
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007770 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007771 virtual void VisitBasicBlock(HBasicBlock* block);
7772
Roland Levillain633021e2014-10-01 14:12:25 +01007773 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007774 void VisitInsertionOrder();
7775
Roland Levillain633021e2014-10-01 14:12:25 +01007776 // Visit the graph following dominator tree reverse post-order.
7777 void VisitReversePostOrder();
7778
Nicolas Geoffray787c3072014-03-17 10:20:19 +00007779 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007780
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007781 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007782#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007783 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
7784
7785 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7786
7787#undef DECLARE_VISIT_INSTRUCTION
7788
Igor Murashkin6ef45672017-08-08 13:59:55 -07007789 protected:
7790 OptimizingCompilerStats* stats_;
7791
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007792 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07007793 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007794
7795 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
7796};
7797
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007798class HGraphDelegateVisitor : public HGraphVisitor {
7799 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07007800 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
7801 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007802 virtual ~HGraphDelegateVisitor() {}
7803
7804 // Visit functions that delegate to to super class.
7805#define DECLARE_VISIT_INSTRUCTION(name, super) \
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007806 void Visit##name(H##name* instr) override { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01007807
7808 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
7809
7810#undef DECLARE_VISIT_INSTRUCTION
7811
7812 private:
7813 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
7814};
7815
Artem Serovcced8ba2017-07-19 18:18:09 +01007816// Create a clone of the instruction, insert it into the graph; replace the old one with a new
7817// and remove the old instruction.
7818HInstruction* ReplaceInstrOrPhiByClone(HInstruction* instr);
7819
7820// Create a clone for each clonable instructions/phis and replace the original with the clone.
7821//
7822// Used for testing individual instruction cloner.
7823class CloneAndReplaceInstructionVisitor : public HGraphDelegateVisitor {
7824 public:
7825 explicit CloneAndReplaceInstructionVisitor(HGraph* graph)
Artem Serov7f4aff62017-06-21 17:02:18 +01007826 : HGraphDelegateVisitor(graph), instr_replaced_by_clones_count_(0) {}
Artem Serovcced8ba2017-07-19 18:18:09 +01007827
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01007828 void VisitInstruction(HInstruction* instruction) override {
Artem Serovcced8ba2017-07-19 18:18:09 +01007829 if (instruction->IsClonable()) {
7830 ReplaceInstrOrPhiByClone(instruction);
Artem Serov7f4aff62017-06-21 17:02:18 +01007831 instr_replaced_by_clones_count_++;
Artem Serovcced8ba2017-07-19 18:18:09 +01007832 }
7833 }
7834
Artem Serov7f4aff62017-06-21 17:02:18 +01007835 size_t GetInstrReplacedByClonesCount() const { return instr_replaced_by_clones_count_; }
Artem Serovcced8ba2017-07-19 18:18:09 +01007836
7837 private:
Artem Serov7f4aff62017-06-21 17:02:18 +01007838 size_t instr_replaced_by_clones_count_;
Artem Serovcced8ba2017-07-19 18:18:09 +01007839
7840 DISALLOW_COPY_AND_ASSIGN(CloneAndReplaceInstructionVisitor);
7841};
7842
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007843// Iterator over the blocks that art part of the loop. Includes blocks part
7844// of an inner loop. The order in which the blocks are iterated is on their
7845// block id.
7846class HBlocksInLoopIterator : public ValueObject {
7847 public:
7848 explicit HBlocksInLoopIterator(const HLoopInformation& info)
7849 : blocks_in_loop_(info.GetBlocks()),
7850 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
7851 index_(0) {
7852 if (!blocks_in_loop_.IsBitSet(index_)) {
7853 Advance();
7854 }
7855 }
7856
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007857 bool Done() const { return index_ == blocks_.size(); }
7858 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007859 void Advance() {
7860 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007861 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007862 if (blocks_in_loop_.IsBitSet(index_)) {
7863 break;
7864 }
7865 }
7866 }
7867
7868 private:
7869 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007870 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00007871 size_t index_;
7872
7873 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
7874};
7875
Mingyao Yang3584bce2015-05-19 16:01:59 -07007876// Iterator over the blocks that art part of the loop. Includes blocks part
7877// of an inner loop. The order in which the blocks are iterated is reverse
7878// post order.
7879class HBlocksInLoopReversePostOrderIterator : public ValueObject {
7880 public:
7881 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
7882 : blocks_in_loop_(info.GetBlocks()),
7883 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
7884 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007885 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007886 Advance();
7887 }
7888 }
7889
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007890 bool Done() const { return index_ == blocks_.size(); }
7891 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07007892 void Advance() {
7893 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007894 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7895 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007896 break;
7897 }
7898 }
7899 }
7900
7901 private:
7902 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007903 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007904 size_t index_;
7905
7906 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7907};
7908
Aart Bikf3e61ee2017-04-12 17:09:20 -07007909// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007910inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007911 if (constant->IsIntConstant()) {
7912 return constant->AsIntConstant()->GetValue();
7913 } else if (constant->IsLongConstant()) {
7914 return constant->AsLongConstant()->GetValue();
7915 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007916 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007917 return 0;
7918 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007919}
7920
Aart Bikf3e61ee2017-04-12 17:09:20 -07007921// Returns true iff instruction is an integral constant (and sets value on success).
7922inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7923 if (instruction->IsIntConstant()) {
7924 *value = instruction->AsIntConstant()->GetValue();
7925 return true;
7926 } else if (instruction->IsLongConstant()) {
7927 *value = instruction->AsLongConstant()->GetValue();
7928 return true;
7929 } else if (instruction->IsNullConstant()) {
7930 *value = 0;
7931 return true;
7932 }
7933 return false;
7934}
7935
Aart Bik0148de42017-09-05 09:25:01 -07007936// Returns true iff instruction is the given integral constant.
7937inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7938 int64_t val = 0;
7939 return IsInt64AndGet(instruction, &val) && val == value;
7940}
7941
7942// Returns true iff instruction is a zero bit pattern.
7943inline bool IsZeroBitPattern(HInstruction* instruction) {
7944 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7945}
7946
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007947// Implement HInstruction::Is##type() for concrete instructions.
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007948#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Markoa90dd512018-05-04 15:04:45 +01007949 inline bool HInstruction::Is##type() const { return GetKind() == k##type; }
7950 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7951#undef INSTRUCTION_TYPE_CHECK
7952
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007953// Implement HInstruction::Is##type() for abstract instructions.
Vladimir Markoa90dd512018-05-04 15:04:45 +01007954#define INSTRUCTION_TYPE_CHECK_RESULT(type, super) \
7955 std::is_base_of<BaseType, H##type>::value,
7956#define INSTRUCTION_TYPE_CHECK(type, super) \
7957 inline bool HInstruction::Is##type() const { \
7958 DCHECK_LT(GetKind(), kLastInstructionKind); \
7959 using BaseType = H##type; \
7960 static constexpr bool results[] = { \
7961 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK_RESULT) \
7962 }; \
7963 return results[static_cast<size_t>(GetKind())]; \
7964 }
7965
7966 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7967#undef INSTRUCTION_TYPE_CHECK
Vladimir Marko6d5b7e32018-05-09 16:52:48 +01007968#undef INSTRUCTION_TYPE_CHECK_RESULT
Vladimir Markoa90dd512018-05-04 15:04:45 +01007969
7970#define INSTRUCTION_TYPE_CAST(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007971 inline const H##type* HInstruction::As##type() const { \
7972 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7973 } \
7974 inline H##type* HInstruction::As##type() { \
7975 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7976 }
7977
Vladimir Markoa90dd512018-05-04 15:04:45 +01007978 FOR_EACH_INSTRUCTION(INSTRUCTION_TYPE_CAST)
7979#undef INSTRUCTION_TYPE_CAST
7980
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007981
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007982// Create space in `blocks` for adding `number_of_new_blocks` entries
7983// starting at location `at`. Blocks after `at` are moved accordingly.
7984inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7985 size_t number_of_new_blocks,
7986 size_t after) {
7987 DCHECK_LT(after, blocks->size());
7988 size_t old_size = blocks->size();
7989 size_t new_size = old_size + number_of_new_blocks;
7990 blocks->resize(new_size);
7991 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7992}
7993
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007994/*
7995 * Hunt "under the hood" of array lengths (leading to array references),
7996 * null checks (also leading to array references), and new arrays
7997 * (leading to the actual length). This makes it more likely related
7998 * instructions become actually comparable.
7999 */
8000inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
8001 while (instruction->IsArrayLength() ||
8002 instruction->IsNullCheck() ||
8003 instruction->IsNewArray()) {
8004 instruction = instruction->IsNewArray()
8005 ? instruction->AsNewArray()->GetLength()
8006 : instruction->InputAt(0);
8007 }
8008 return instruction;
8009}
8010
Artem Serov21c7e6f2017-07-27 16:04:42 +01008011void RemoveEnvironmentUses(HInstruction* instruction);
8012bool HasEnvironmentUsedByOthers(HInstruction* instruction);
8013void ResetEnvironmentInputRecords(HInstruction* instruction);
8014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00008015} // namespace art
8016
8017#endif // ART_COMPILER_OPTIMIZING_NODES_H_