blob: 5e8c77102a760bdec8b67db5f92ad952d9681894 [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"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010032#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080033#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000034#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "handle.h"
36#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010039#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000040#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010042#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070043#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
David Brazdil1abb4192015-02-17 18:33:36 +000048class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070050class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000055class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000061class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070062class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010064class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010065class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010068class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Mathieu Chartier736b5602015-09-02 14:54:11 -070071namespace mirror {
72class DexCache;
73} // namespace mirror
74
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075static const int kDefaultNumberOfBlocks = 8;
76static const int kDefaultNumberOfSuccessors = 2;
77static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010078static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010079static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Roland Levillain5b5b9312016-03-22 14:57:31 +000082// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
83static constexpr int32_t kMaxIntShiftDistance = 0x1f;
84// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
85static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000086
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010087static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070088static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010090static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
91
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060092static constexpr uint32_t kNoDexPc = -1;
93
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010094inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
95 // For the purposes of the compiler, the dex files must actually be the same object
96 // if we want to safely treat them as the same. This is especially important for JIT
97 // as custom class loaders can open the same underlying file (or memory) multiple
98 // times and provide different class resolution but no two class loaders should ever
99 // use the same DexFile object - doing so is an unsupported hack that can lead to
100 // all sorts of weird failures.
101 return &lhs == &rhs;
102}
103
Dave Allison20dfc792014-06-16 20:44:29 -0700104enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700105 // All types.
106 kCondEQ, // ==
107 kCondNE, // !=
108 // Signed integers and floating-point numbers.
109 kCondLT, // <
110 kCondLE, // <=
111 kCondGT, // >
112 kCondGE, // >=
113 // Unsigned integers.
114 kCondB, // <
115 kCondBE, // <=
116 kCondA, // >
117 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100118 // First and last aliases.
119 kCondFirst = kCondEQ,
120 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700121};
122
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000125 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 kAnalysisFailThrowCatchLoop,
127 kAnalysisFailAmbiguousArrayOp,
128 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000129};
130
Andreas Gampe9186ced2016-12-12 14:28:21 -0800131template <typename T>
132static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
133 return static_cast<typename std::make_unsigned<T>::type>(x);
134}
135
Vladimir Markof9f64412015-09-02 14:05:49 +0100136class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 public:
138 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
139
140 void AddInstruction(HInstruction* instruction);
141 void RemoveInstruction(HInstruction* instruction);
142
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 // Insert `instruction` before/after an existing instruction `cursor`.
144 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
145 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
146
Roland Levillain6b469232014-09-25 10:10:38 +0100147 // Return true if this list contains `instruction`.
148 bool Contains(HInstruction* instruction) const;
149
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 // Return true if `instruction1` is found before `instruction2` in
151 // this instruction list and false otherwise. Abort if none
152 // of these instructions is found.
153 bool FoundBefore(const HInstruction* instruction1,
154 const HInstruction* instruction2) const;
155
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 bool IsEmpty() const { return first_instruction_ == nullptr; }
157 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
158
159 // Update the block of all instructions to be `block`.
160 void SetBlockOfInstructions(HBasicBlock* block) const;
161
162 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000163 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 void Add(const HInstructionList& instruction_list);
165
David Brazdil2d7352b2015-04-20 14:52:42 +0100166 // Return the number of instructions in the list. This is an expensive operation.
167 size_t CountSize() const;
168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 private:
170 HInstruction* first_instruction_;
171 HInstruction* last_instruction_;
172
173 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 friend class HGraph;
175 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000177 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
180 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
181};
182
David Brazdil4833f5a2015-12-16 10:37:39 +0000183class ReferenceTypeInfo : ValueObject {
184 public:
185 typedef Handle<mirror::Class> TypeHandle;
186
Vladimir Markoa1de9182016-02-25 11:37:38 +0000187 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100190 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
191 }
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return ReferenceTypeInfo(type_handle, is_exact);
195 }
196
197 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
198
Vladimir Markof39745e2016-01-26 12:16:55 +0000199 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return handle.GetReference() != nullptr;
201 }
202
Vladimir Marko456307a2016-04-19 14:12:13 +0000203 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return IsValidHandle(type_handle_);
205 }
206
207 bool IsExact() const { return is_exact_; }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsStringClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsInterface();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsPrimitiveArray();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 if (!IsExact()) return false;
247 if (!IsArrayClass()) return false;
248 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 if (!rti.IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
257 rti.GetTypeHandle()->GetComponentType());
258 }
259
260 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
261
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000263 DCHECK(IsValid());
264 DCHECK(rti.IsValid());
265 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
272 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100308 HGraph(ArenaAllocator* arena,
309 const DexFile& dex_file,
310 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_(arena->Adapter(kArenaAllocBlockList)),
318 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
319 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 entry_block_(nullptr),
321 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100322 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 number_of_vregs_(0),
324 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000325 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400326 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000327 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700328 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800329 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000331 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100333 dex_file_(dex_file),
334 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800337 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100340 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
343 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100345 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000346 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700347 osr_(osr),
348 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100349 blocks_.reserve(kDefaultNumberOfBlocks);
350 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351
David Brazdilbadd8262016-02-02 16:28:56 +0000352 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700353 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
357
David Brazdil69ba7b72015-06-23 18:27:30 +0100358 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000359 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 HBasicBlock* GetEntryBlock() const { return entry_block_; }
362 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100363 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
366 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100370 void ComputeDominanceInformation();
371 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000372 void ClearLoopInformation();
373 void FindBackEdges(ArenaBitVector* visited);
374 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100376 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // Analyze all natural loops in this graph. Returns a code specifying that it
379 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000380 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Iterate over blocks to compute try block membership. Needs reverse post
384 // order and loop information.
385 void ComputeTryBlockInformation();
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000388 // Returns the instruction to replace the invoke expression or null if the
389 // invoke is for a void method. Note that the caller is responsible for replacing
390 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000391 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000393 // Update the loop and try membership of `block`, which was spawned from `reference`.
394 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
395 // should be the new back edge.
396 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
397 HBasicBlock* reference,
398 bool replace_if_back_edge);
399
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 // Need to add a couple of blocks to test if the loop body is entered and
401 // put deoptimization instructions, etc.
402 void TransformLoopHeaderForBCE(HBasicBlock* header);
403
Aart Bikf8f5a162017-02-06 15:35:29 -0800404 // Adds a new loop directly after the loop with the given header and exit.
405 // Returns the new preheader.
406 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
407 HBasicBlock* body,
408 HBasicBlock* exit);
409
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000410 // Removes `block` from the graph. Assumes `block` has been disconnected from
411 // other blocks and has no instructions or phis.
412 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000413
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 // Splits the edge between `block` and `successor` while preserving the
415 // indices in the predecessor/successor lists. If there are multiple edges
416 // between the blocks, the lowest indices are used.
417 // Returns the new block which is empty and has the same dex pc as `successor`.
418 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100421 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 void SimplifyLoop(HBasicBlock* header);
423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100425 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000426 return current_instruction_id_++;
427 }
428
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000429 int32_t GetCurrentInstructionId() const {
430 return current_instruction_id_;
431 }
432
433 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100434 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000435 current_instruction_id_ = id;
436 }
437
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100438 uint16_t GetMaximumNumberOfOutVRegs() const {
439 return maximum_number_of_out_vregs_;
440 }
441
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
443 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100444 }
445
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100446 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
447 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
448 }
449
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000450 void UpdateTemporariesVRegSlots(size_t slots) {
451 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000456 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100457 }
458
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459 void SetNumberOfVRegs(uint16_t number_of_vregs) {
460 number_of_vregs_ = number_of_vregs;
461 }
462
463 uint16_t GetNumberOfVRegs() const {
464 return number_of_vregs_;
465 }
466
467 void SetNumberOfInVRegs(uint16_t value) {
468 number_of_in_vregs_ = value;
469 }
470
David Brazdildee58d62016-04-07 09:54:26 +0000471 uint16_t GetNumberOfInVRegs() const {
472 return number_of_in_vregs_;
473 }
474
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100475 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100476 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100477 return number_of_vregs_ - number_of_in_vregs_;
478 }
479
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100483
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100484 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
485 DCHECK(GetReversePostOrder()[0] == entry_block_);
486 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
487 }
488
489 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
490 return ReverseRange(GetReversePostOrder());
491 }
492
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100494 return linear_order_;
495 }
496
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100497 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
498 return ReverseRange(GetLinearOrder());
499 }
500
Mark Mendell1152c922015-04-24 17:06:35 -0400501 bool HasBoundsChecks() const {
502 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 void SetHasBoundsChecks(bool value) {
506 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000509 bool IsDebuggable() const { return debuggable_; }
510
David Brazdil8d5b8b22015-03-24 10:51:52 +0000511 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 // already, it is created and inserted into the graph. This method is only for
513 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000515
516 // TODO: This is problematic for the consistency of reference type propagation
517 // because it can be created anytime after the pass and thus it will be left
518 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000520
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
522 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000523 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600524 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
525 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
528 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600530 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
531 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000532 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000533
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100534 HCurrentMethod* GetCurrentMethod();
535
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100536 const DexFile& GetDexFile() const {
537 return dex_file_;
538 }
539
540 uint32_t GetMethodIdx() const {
541 return method_idx_;
542 }
543
Igor Murashkind01745e2017-04-05 16:40:31 -0700544 // Get the method name (without the signature), e.g. "<init>"
545 const char* GetMethodName() const;
546
547 // Get the pretty method name (class + name + optionally signature).
548 std::string PrettyMethod(bool with_signature = true) const;
549
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100550 InvokeType GetInvokeType() const {
551 return invoke_type_;
552 }
553
Mark Mendellc4701932015-04-10 13:18:51 -0400554 InstructionSet GetInstructionSet() const {
555 return instruction_set_;
556 }
557
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000558 bool IsCompilingOsr() const { return osr_; }
559
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
561 return cha_single_implementation_list_;
562 }
563
564 void AddCHASingleImplementationDependency(ArtMethod* method) {
565 cha_single_implementation_list_.insert(method);
566 }
567
568 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800569 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700570 }
571
David Brazdil77a48ae2015-09-15 12:34:04 +0000572 bool HasTryCatch() const { return has_try_catch_; }
573 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100574
Aart Bikb13c65b2017-03-21 20:14:07 -0700575 bool HasSIMD() const { return has_simd_; }
576 void SetHasSIMD(bool value) { has_simd_ = value; }
577
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800578 bool HasLoops() const { return has_loops_; }
579 void SetHasLoops(bool value) { has_loops_ = value; }
580
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000581 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
582 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
583
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100584 ArtMethod* GetArtMethod() const { return art_method_; }
585 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
586
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100587 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500588 // The instruction has been inserted into the graph, either as a constant, or
589 // before cursor.
590 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
591
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000592 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
593
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800594 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
595 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
596 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
597
David Brazdil2d7352b2015-04-20 14:52:42 +0100598 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000599 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100600 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000601
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 template <class InstructionType, typename ValueType>
603 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600604 ArenaSafeMap<ValueType, InstructionType*>* cache,
605 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Try to find an existing constant of the given value.
607 InstructionType* constant = nullptr;
608 auto cached_constant = cache->find(value);
609 if (cached_constant != cache->end()) {
610 constant = cached_constant->second;
611 }
612
613 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100614 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600616 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 cache->Overwrite(value, constant);
618 InsertConstant(constant);
619 }
620 return constant;
621 }
622
David Brazdil8d5b8b22015-03-24 10:51:52 +0000623 void InsertConstant(HConstant* instruction);
624
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000625 // Cache a float constant into the graph. This method should only be
626 // called by the SsaBuilder when creating "equivalent" instructions.
627 void CacheFloatConstant(HFloatConstant* constant);
628
629 // See CacheFloatConstant comment.
630 void CacheDoubleConstant(HDoubleConstant* constant);
631
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633
634 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639
Aart Bik281c6812016-08-26 11:31:48 -0700640 // List of blocks to perform a linear order tree traversal. Unlike the reverse
641 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100642 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100643
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000644 HBasicBlock* entry_block_;
645 HBasicBlock* exit_block_;
646
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100647 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100648 uint16_t maximum_number_of_out_vregs_;
649
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100650 // The number of virtual registers in this method. Contains the parameters.
651 uint16_t number_of_vregs_;
652
653 // The number of virtual registers used by parameters of this method.
654 uint16_t number_of_in_vregs_;
655
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000656 // Number of vreg size slots that the temporaries use (used in baseline compiler).
657 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800659 // Flag whether there are bounds checks in the graph. We can skip
660 // BCE if it's false. It's only best effort to keep it up to date in
661 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400662 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are try/catch blocks in the graph. We will skip
665 // try/catch-related passes if it's false. It's only best effort to keep
666 // it up to date in the presence of code elimination so there might be
667 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000668 bool has_try_catch_;
669
Aart Bikb13c65b2017-03-21 20:14:07 -0700670 // Flag whether SIMD instructions appear in the graph. If true, the
671 // code generators may have to be more careful spilling the wider
672 // contents of SIMD registers.
673 bool has_simd_;
674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are any loops in the graph. We can skip loop
676 // optimization if it's false. It's only best effort to keep it up
677 // to date in the presence of code elimination so there might be false
678 // positives.
679 bool has_loops_;
680
681 // Flag whether there are any irreducible loops in the graph. It's only
682 // best effort to keep it up to date in the presence of code elimination
683 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000684 bool has_irreducible_loops_;
685
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000686 // Indicates whether the graph should be compiled in a way that
687 // ensures full debuggability. If false, we can apply more
688 // aggressive optimizations that may limit the level of debugging.
689 const bool debuggable_;
690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000692 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100694 // The dex file from which the method is from.
695 const DexFile& dex_file_;
696
697 // The method index in the dex file.
698 const uint32_t method_idx_;
699
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100700 // If inlined, this encodes how the callee is being invoked.
701 const InvokeType invoke_type_;
702
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100703 // Whether the graph has been transformed to SSA form. Only used
704 // in debug mode to ensure we are not using properties only valid
705 // for non-SSA form (like the number of temporaries).
706 bool in_ssa_form_;
707
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800708 // Number of CHA guards in the graph. Used to short-circuit the
709 // CHA guard optimization pass when there is no CHA guard left.
710 uint32_t number_of_cha_guards_;
711
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 const InstructionSet instruction_set_;
713
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000714 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HNullConstant* cached_null_constant_;
716 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000718 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000720
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100721 HCurrentMethod* cached_current_method_;
722
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100723 // The ArtMethod this graph is for. Note that for AOT, it may be null,
724 // for example for methods whose declaring class could not be resolved
725 // (such as when the superclass could not be found).
726 ArtMethod* art_method_;
727
David Brazdil4833f5a2015-12-16 10:37:39 +0000728 // Keep the RTI of inexact Object to avoid having to pass stack handle
729 // collection pointer to passes which may create NullConstant.
730 ReferenceTypeInfo inexact_object_rti_;
731
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000732 // Whether we are compiling this graph for on stack replacement: this will
733 // make all loops seen as irreducible and emit special stack maps to mark
734 // compiled code entries which the interpreter can directly jump to.
735 const bool osr_;
736
Mingyao Yang063fc772016-08-02 11:02:54 -0700737 // List of methods that are assumed to have single implementation.
738 ArenaSet<ArtMethod*> cha_single_implementation_list_;
739
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000740 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100741 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000742 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000743 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000744 DISALLOW_COPY_AND_ASSIGN(HGraph);
745};
746
Vladimir Markof9f64412015-09-02 14:05:49 +0100747class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748 public:
749 HLoopInformation(HBasicBlock* header, HGraph* graph)
750 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100751 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100753 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100754 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100755 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000756 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100757 back_edges_.reserve(kDefaultNumberOfBackEdges);
758 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000762
763 void Dump(std::ostream& os);
764
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 HBasicBlock* GetHeader() const {
766 return header_;
767 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 void SetHeader(HBasicBlock* block) {
770 header_ = block;
771 }
772
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
774 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
775 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
776
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000779 }
780
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100782 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 }
784
David Brazdil46e2a392015-03-16 17:31:52 +0000785 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 }
788
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000789 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000791 }
792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100793 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100796 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 }
798
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100799 // Returns the lifetime position of the back edge that has the
800 // greatest lifetime position.
801 size_t GetLifetimeEnd() const;
802
803 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100805 }
806
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000807 // Finds blocks that are part of this loop.
808 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100809
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810 // Returns whether this loop information contains `block`.
811 // Note that this loop information *must* be populated before entering this function.
812 bool Contains(const HBasicBlock& block) const;
813
814 // Returns whether this loop information is an inner loop of `other`.
815 // Note that `other` *must* be populated before entering this function.
816 bool IsIn(const HLoopInformation& other) const;
817
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800818 // Returns true if instruction is not defined within this loop.
819 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 const ArenaBitVector& GetBlocks() const { return blocks_; }
822
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000824 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000825
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000826 void ClearAllBlocks() {
827 blocks_.ClearAllBits();
828 }
829
David Brazdil3f4a5222016-05-06 12:46:21 +0100830 bool HasBackEdgeNotDominatedByHeader() const;
831
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100832 bool IsPopulated() const {
833 return blocks_.GetHighestBitSet() != -1;
834 }
835
Anton Shaminf89381f2016-05-16 16:44:13 +0600836 bool DominatesAllBackEdges(HBasicBlock* block);
837
David Sehrc757dec2016-11-04 15:48:34 -0700838 bool HasExitEdge() const;
839
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 // Internal recursive implementation of `Populate`.
842 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100843 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000845 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000847 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100848 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
853};
854
David Brazdilec16f792015-08-19 15:04:01 +0100855// Stores try/catch information for basic blocks.
856// Note that HGraph is constructed so that catch blocks cannot simultaneously
857// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100858class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100859 public:
860 // Try block information constructor.
861 explicit TryCatchInformation(const HTryBoundary& try_entry)
862 : try_entry_(&try_entry),
863 catch_dex_file_(nullptr),
864 catch_type_index_(DexFile::kDexNoIndex16) {
865 DCHECK(try_entry_ != nullptr);
866 }
867
868 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800869 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100870 : try_entry_(nullptr),
871 catch_dex_file_(&dex_file),
872 catch_type_index_(catch_type_index) {}
873
874 bool IsTryBlock() const { return try_entry_ != nullptr; }
875
876 const HTryBoundary& GetTryEntry() const {
877 DCHECK(IsTryBlock());
878 return *try_entry_;
879 }
880
881 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
882
883 bool IsCatchAllTypeIndex() const {
884 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800885 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100886 }
887
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100889 DCHECK(IsCatchBlock());
890 return catch_type_index_;
891 }
892
893 const DexFile& GetCatchDexFile() const {
894 DCHECK(IsCatchBlock());
895 return *catch_dex_file_;
896 }
897
898 private:
899 // One of possibly several TryBoundary instructions entering the block's try.
900 // Only set for try blocks.
901 const HTryBoundary* try_entry_;
902
903 // Exception type information. Only set for catch blocks.
904 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800905 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100906};
907
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100909static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911// A block in a method. Contains the list of instructions represented
912// as a double linked list. Each block knows its predecessors and
913// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100914
Vladimir Markof9f64412015-09-02 14:05:49 +0100915class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700917 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000919 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
920 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 loop_information_(nullptr),
922 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000923 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100924 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100925 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000927 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000928 try_catch_information_(nullptr) {
929 predecessors_.reserve(kDefaultNumberOfPredecessors);
930 successors_.reserve(kDefaultNumberOfSuccessors);
931 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
932 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933
Vladimir Marko60584552015-09-03 13:35:12 +0000934 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936 }
937
Vladimir Marko60584552015-09-03 13:35:12 +0000938 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940 }
941
David Brazdild26a4112015-11-10 11:07:31 +0000942 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
943 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
944
Vladimir Marko60584552015-09-03 13:35:12 +0000945 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
946 return ContainsElement(successors_, block, start_from);
947 }
948
949 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100950 return dominated_blocks_;
951 }
952
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100953 bool IsEntryBlock() const {
954 return graph_->GetEntryBlock() == this;
955 }
956
957 bool IsExitBlock() const {
958 return graph_->GetExitBlock() == this;
959 }
960
David Brazdil46e2a392015-03-16 17:31:52 +0000961 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200962 bool IsSingleReturn() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 bool IsSingleTryBoundary() const;
964
965 // Returns true if this block emits nothing but a jump.
966 bool IsSingleJump() const {
967 HLoopInformation* loop_info = GetLoopInformation();
968 return (IsSingleGoto() || IsSingleTryBoundary())
969 // Back edges generate a suspend check.
970 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
971 }
David Brazdil46e2a392015-03-16 17:31:52 +0000972
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 void AddBackEdge(HBasicBlock* back_edge) {
974 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000978 loop_information_->AddBackEdge(back_edge);
979 }
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000982 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100984 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600986 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000988 HBasicBlock* GetDominator() const { return dominator_; }
989 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000990 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
991
992 void RemoveDominatedBlock(HBasicBlock* block) {
993 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100994 }
Vladimir Marko60584552015-09-03 13:35:12 +0000995
996 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
997 ReplaceElement(dominated_blocks_, existing, new_block);
998 }
999
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001000 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001001
1002 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001003 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 }
1005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1007 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001008 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001009 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001010 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1011 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001013 HInstruction* GetFirstInstructionDisregardMoves() const;
1014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.push_back(block);
1017 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018 }
1019
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1021 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001022 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001023 new_block->predecessors_.push_back(this);
1024 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001025 }
1026
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1028 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001030 new_block->successors_.push_back(this);
1031 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032 }
1033
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001034 // Insert `this` between `predecessor` and `successor. This method
1035 // preserves the indicies, and will update the first edge found between
1036 // `predecessor` and `successor`.
1037 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1038 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001039 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001040 successor->predecessors_[predecessor_index] = this;
1041 predecessor->successors_[successor_index] = this;
1042 successors_.push_back(successor);
1043 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001044 }
1045
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001046 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001048 }
1049
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001051 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 }
1053
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001056 }
1057
1058 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 predecessors_.push_back(block);
1060 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001061 }
1062
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001063 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 DCHECK_EQ(predecessors_.size(), 2u);
1065 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001066 }
1067
David Brazdil769c9e52015-04-27 13:54:09 +01001068 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001069 DCHECK_EQ(successors_.size(), 2u);
1070 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001071 }
1072
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001074 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001075 }
1076
David Brazdilfc6a86a2015-06-26 10:33:45 +00001077 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001079 }
1080
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001082 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001083 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001084 }
1085
1086 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001087 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001088 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089 }
1090
1091 // Returns whether the first occurrence of `predecessor` in the list of
1092 // predecessors is at index `idx`.
1093 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001094 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001095 return GetPredecessorIndexOf(predecessor) == idx;
1096 }
1097
David Brazdild7558da2015-09-22 13:04:14 +01001098 // Create a new block between this block and its predecessors. The new block
1099 // is added to the graph, all predecessor edges are relinked to it and an edge
1100 // is created to `this`. Returns the new empty block. Reverse post order or
1101 // loop and try/catch information are not updated.
1102 HBasicBlock* CreateImmediateDominator();
1103
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001105 // created, latter block. Note that this method will add the block to the
1106 // graph, create a Goto at the end of the former block and will create an edge
1107 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001108 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109 HBasicBlock* SplitBefore(HInstruction* cursor);
1110
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001111 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001112 // created block. Note that this method just updates raw block information,
1113 // like predecessors, successors, dominators, and instruction list. It does not
1114 // update the graph, reverse post order, loop information, nor make sure the
1115 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001116 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1117
1118 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1119 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001120
1121 // Merge `other` at the end of `this`. Successors and dominated blocks of
1122 // `other` are changed to be successors and dominated blocks of `this`. Note
1123 // that this method does not update the graph, reverse post order, loop
1124 // information, nor make sure the blocks are consistent (for example ending
1125 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001126 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001127
1128 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1129 // of `this` are moved to `other`.
1130 // Note that this method does not update the graph, reverse post order, loop
1131 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001132 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 void ReplaceWith(HBasicBlock* other);
1134
Aart Bik6b69e0a2017-01-11 10:20:43 -08001135 // Merges the instructions of `other` at the end of `this`.
1136 void MergeInstructionsWith(HBasicBlock* other);
1137
David Brazdil2d7352b2015-04-20 14:52:42 +01001138 // Merge `other` at the end of `this`. This method updates loops, reverse post
1139 // order, links to predecessors, successors, dominators and deletes the block
1140 // from the graph. The two blocks must be successive, i.e. `this` the only
1141 // predecessor of `other` and vice versa.
1142 void MergeWith(HBasicBlock* other);
1143
1144 // Disconnects `this` from all its predecessors, successors and dominator,
1145 // removes it from all loops it is included in and eventually from the graph.
1146 // The block must not dominate any other block. Predecessors and successors
1147 // are safely updated.
1148 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001149
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001150 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001152 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001153 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001154 // Replace instruction `initial` with `replacement` within this block.
1155 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1156 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001159 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1160 // instruction list. With 'ensure_safety' set to true, it verifies that the
1161 // instruction is not in use and removes it from the use lists of its inputs.
1162 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1163 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001164 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165
1166 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001167 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168 }
1169
Roland Levillain6b879dd2014-09-22 17:13:44 +01001170 bool IsLoopPreHeaderFirstPredecessor() const {
1171 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001172 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001173 }
1174
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001175 bool IsFirstPredecessorBackEdge() const {
1176 DCHECK(IsLoopHeader());
1177 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1178 }
1179
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 HLoopInformation* GetLoopInformation() const {
1181 return loop_information_;
1182 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001183
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001187 void SetInLoop(HLoopInformation* info) {
1188 if (IsLoopHeader()) {
1189 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001190 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001191 loop_information_ = info;
1192 } else if (loop_information_->Contains(*info->GetHeader())) {
1193 // Block is currently part of an outer loop. Make it part of this inner loop.
1194 // Note that a non loop header having a loop information means this loop information
1195 // has already been populated
1196 loop_information_ = info;
1197 } else {
1198 // Block is part of an inner loop. Do not update the loop information.
1199 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1200 // at this point, because this method is being called while populating `info`.
1201 }
1202 }
1203
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204 // Raw update of the loop information.
1205 void SetLoopInformation(HLoopInformation* info) {
1206 loop_information_ = info;
1207 }
1208
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001209 bool IsInLoop() const { return loop_information_ != nullptr; }
1210
David Brazdilec16f792015-08-19 15:04:01 +01001211 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1212
1213 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1214 try_catch_information_ = try_catch_information;
1215 }
1216
1217 bool IsTryBlock() const {
1218 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1219 }
1220
1221 bool IsCatchBlock() const {
1222 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1223 }
David Brazdilffee3d32015-07-06 11:48:53 +01001224
1225 // Returns the try entry that this block's successors should have. They will
1226 // be in the same try, unless the block ends in a try boundary. In that case,
1227 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001228 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001229
David Brazdild7558da2015-09-22 13:04:14 +01001230 bool HasThrowingInstructions() const;
1231
David Brazdila4b8c212015-05-07 09:59:30 +01001232 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 bool Dominates(HBasicBlock* block) const;
1234
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001235 size_t GetLifetimeStart() const { return lifetime_start_; }
1236 size_t GetLifetimeEnd() const { return lifetime_end_; }
1237
1238 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1239 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1240
David Brazdil8d5b8b22015-03-24 10:51:52 +00001241 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001243 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001244 bool HasSinglePhi() const;
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001247 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001248 ArenaVector<HBasicBlock*> predecessors_;
1249 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 HInstructionList instructions_;
1251 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001252 HLoopInformation* loop_information_;
1253 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001254 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001255 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001256 // The dex program counter of the first instruction of this block.
1257 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001258 size_t lifetime_start_;
1259 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001260 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262 friend class HGraph;
1263 friend class HInstruction;
1264
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001265 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1266};
1267
David Brazdilb2bd1c52015-03-25 11:17:37 +00001268// Iterates over the LoopInformation of all loops which contain 'block'
1269// from the innermost to the outermost.
1270class HLoopInformationOutwardIterator : public ValueObject {
1271 public:
1272 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1273 : current_(block.GetLoopInformation()) {}
1274
1275 bool Done() const { return current_ == nullptr; }
1276
1277 void Advance() {
1278 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001279 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001280 }
1281
1282 HLoopInformation* Current() const {
1283 DCHECK(!Done());
1284 return current_;
1285 }
1286
1287 private:
1288 HLoopInformation* current_;
1289
1290 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1291};
1292
Alexandre Ramesef20f712015-06-09 10:29:30 +01001293#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001294 M(Above, Condition) \
1295 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001297 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001298 M(ArrayGet, Instruction) \
1299 M(ArrayLength, Instruction) \
1300 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001301 M(Below, Condition) \
1302 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001303 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001304 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001305 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001306 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001307 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001308 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001309 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001310 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001311 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001312 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001313 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001314 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001316 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001319 M(Exit, Instruction) \
1320 M(FloatConstant, Constant) \
1321 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(GreaterThan, Condition) \
1323 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001325 M(InstanceFieldGet, Instruction) \
1326 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001327 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001329 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001331 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001333 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(LessThan, Condition) \
1335 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001337 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001338 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001339 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001340 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001341 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001343 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(Neg, UnaryOperation) \
1345 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001347 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001349 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001351 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001352 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001356 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Return, Instruction) \
1358 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001359 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001360 M(Shl, BinaryOperation) \
1361 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001362 M(StaticFieldGet, Instruction) \
1363 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001364 M(UnresolvedInstanceFieldGet, Instruction) \
1365 M(UnresolvedInstanceFieldSet, Instruction) \
1366 M(UnresolvedStaticFieldGet, Instruction) \
1367 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001368 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001371 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001373 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001374 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001375 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001376 M(VecReplicateScalar, VecUnaryOperation) \
Nicolas Geoffray982334c2017-09-02 12:54:16 +00001377 M(VecSumReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001378 M(VecCnv, VecUnaryOperation) \
1379 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001380 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001381 M(VecNot, VecUnaryOperation) \
1382 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001383 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001384 M(VecSub, VecBinaryOperation) \
1385 M(VecMul, VecBinaryOperation) \
1386 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001387 M(VecMin, VecBinaryOperation) \
1388 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001389 M(VecAnd, VecBinaryOperation) \
1390 M(VecAndNot, VecBinaryOperation) \
1391 M(VecOr, VecBinaryOperation) \
1392 M(VecXor, VecBinaryOperation) \
1393 M(VecShl, VecBinaryOperation) \
1394 M(VecShr, VecBinaryOperation) \
1395 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001396 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001397 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001398 M(VecLoad, VecMemoryOperation) \
1399 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001400
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001401/*
1402 * Instructions, shared across several (not all) architectures.
1403 */
1404#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1405#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1406#else
1407#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001408 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001409 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001410 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001411 M(IntermediateAddress, Instruction) \
1412 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001413#endif
1414
Alexandre Ramesef20f712015-06-09 10:29:30 +01001415#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1416
1417#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1418
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001419#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001420#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001421#else
1422#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1423 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001424 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001425#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001426
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001427#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1428
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001429#ifndef ART_ENABLE_CODEGEN_x86
1430#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1431#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001432#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1433 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001434 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001435 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001436 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001437#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001438
1439#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1440
1441#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1442 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001443 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001444 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1445 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001446 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001447 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001448 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1449 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1450
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001451#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1452 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001453 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001454 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001455 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001456 M(Invoke, Instruction) \
1457 M(VecOperation, Instruction) \
1458 M(VecUnaryOperation, VecOperation) \
1459 M(VecBinaryOperation, VecOperation) \
1460 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001461
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001462#define FOR_EACH_INSTRUCTION(M) \
1463 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1464 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1465
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001466#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001467FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1468#undef FORWARD_DECLARATION
1469
Vladimir Marko372f10e2016-05-17 16:30:10 +01001470#define DECLARE_INSTRUCTION(type) \
1471 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1472 const char* DebugName() const OVERRIDE { return #type; } \
1473 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1474 return other->Is##type(); \
1475 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001476 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001477
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001478#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1479 bool Is##type() const { return As##type() != nullptr; } \
1480 const H##type* As##type() const { return this; } \
1481 H##type* As##type() { return this; }
1482
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001484class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1485 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001486 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001487 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001488 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001489 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001490 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001491 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001492 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001494 private:
David Brazdiled596192015-01-23 10:39:45 +00001495 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001496 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001497
1498 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001499 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001500
Vladimir Marko46817b82016-03-29 12:21:58 +01001501 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001502
1503 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1504};
1505
David Brazdiled596192015-01-23 10:39:45 +00001506template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001507using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001508
David Brazdil1abb4192015-02-17 18:33:36 +00001509// This class is used by HEnvironment and HInstruction classes to record the
1510// instructions they use and pointers to the corresponding HUseListNodes kept
1511// by the used instructions.
1512template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001513class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001514 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001515 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1516 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001517
Vladimir Marko46817b82016-03-29 12:21:58 +01001518 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1519 : HUserRecord(old_record.instruction_, before_use_node) {}
1520 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1521 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001522 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001523 }
1524
1525 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001526 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1527 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001528
1529 private:
1530 // Instruction used by the user.
1531 HInstruction* instruction_;
1532
Vladimir Marko46817b82016-03-29 12:21:58 +01001533 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1534 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001535};
1536
Vladimir Markoe9004912016-06-16 16:50:52 +01001537// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1538// This is used for HInstruction::GetInputs() to return a container wrapper providing
1539// HInstruction* values even though the underlying container has HUserRecord<>s.
1540struct HInputExtractor {
1541 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1542 return record.GetInstruction();
1543 }
1544 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1545 return record.GetInstruction();
1546 }
1547};
1548
1549using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1550using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1551
Aart Bik854a02b2015-07-14 16:07:00 -07001552/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001554 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001555 * For write/read dependences on fields/arrays, the dependence analysis uses
1556 * type disambiguation (e.g. a float field write cannot modify the value of an
1557 * integer field read) and the access type (e.g. a reference array write cannot
1558 * modify the value of a reference field read [although it may modify the
1559 * reference fetch prior to reading the field, which is represented by its own
1560 * write/read dependence]). The analysis makes conservative points-to
1561 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1562 * the same, and any reference read depends on any reference read without
1563 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001564 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 * The internal representation uses 38-bit and is described in the table below.
1566 * The first line indicates the side effect, and for field/array accesses the
1567 * second line indicates the type of the access (in the order of the
1568 * Primitive::Type enum).
1569 * The two numbered lines below indicate the bit position in the bitfield (read
1570 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001571 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001572 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1573 * +-------------+---------+---------+--------------+---------+---------+
1574 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1575 * | 3 |333333322|222222221| 1 |111111110|000000000|
1576 * | 7 |654321098|765432109| 8 |765432109|876543210|
1577 *
1578 * Note that, to ease the implementation, 'changes' bits are least significant
1579 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001580 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581class SideEffects : public ValueObject {
1582 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001583 SideEffects() : flags_(0) {}
1584
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001585 static SideEffects None() {
1586 return SideEffects(0);
1587 }
1588
1589 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001590 return SideEffects(kAllChangeBits | kAllDependOnBits);
1591 }
1592
1593 static SideEffects AllChanges() {
1594 return SideEffects(kAllChangeBits);
1595 }
1596
1597 static SideEffects AllDependencies() {
1598 return SideEffects(kAllDependOnBits);
1599 }
1600
1601 static SideEffects AllExceptGCDependency() {
1602 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1603 }
1604
1605 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001606 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001607 }
1608
Aart Bik34c3ba92015-07-20 14:08:59 -07001609 static SideEffects AllWrites() {
1610 return SideEffects(kAllWrites);
1611 }
1612
1613 static SideEffects AllReads() {
1614 return SideEffects(kAllReads);
1615 }
1616
1617 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1618 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001619 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001620 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001621 }
1622
Aart Bik854a02b2015-07-14 16:07:00 -07001623 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001624 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001625 }
1626
Aart Bik34c3ba92015-07-20 14:08:59 -07001627 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1628 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001630 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001631 }
1632
1633 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001634 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001635 }
1636
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 static SideEffects CanTriggerGC() {
1638 return SideEffects(1ULL << kCanTriggerGCBit);
1639 }
1640
1641 static SideEffects DependsOnGC() {
1642 return SideEffects(1ULL << kDependsOnGCBit);
1643 }
1644
Aart Bik854a02b2015-07-14 16:07:00 -07001645 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001646 SideEffects Union(SideEffects other) const {
1647 return SideEffects(flags_ | other.flags_);
1648 }
1649
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 SideEffects Exclusion(SideEffects other) const {
1651 return SideEffects(flags_ & ~other.flags_);
1652 }
1653
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001654 void Add(SideEffects other) {
1655 flags_ |= other.flags_;
1656 }
1657
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 bool Includes(SideEffects other) const {
1659 return (other.flags_ & flags_) == other.flags_;
1660 }
1661
1662 bool HasSideEffects() const {
1663 return (flags_ & kAllChangeBits);
1664 }
1665
1666 bool HasDependencies() const {
1667 return (flags_ & kAllDependOnBits);
1668 }
1669
1670 // Returns true if there are no side effects or dependencies.
1671 bool DoesNothing() const {
1672 return flags_ == 0;
1673 }
1674
Aart Bik854a02b2015-07-14 16:07:00 -07001675 // Returns true if something is written.
1676 bool DoesAnyWrite() const {
1677 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001678 }
1679
Aart Bik854a02b2015-07-14 16:07:00 -07001680 // Returns true if something is read.
1681 bool DoesAnyRead() const {
1682 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001683 }
1684
Aart Bik854a02b2015-07-14 16:07:00 -07001685 // Returns true if potentially everything is written and read
1686 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001687 bool DoesAllReadWrite() const {
1688 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1689 }
1690
Aart Bik854a02b2015-07-14 16:07:00 -07001691 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001693 }
1694
Roland Levillain0d5a2812015-11-13 10:07:31 +00001695 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001696 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1698 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001699 }
1700
1701 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001703 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001704 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 for (int s = kLastBit; s >= 0; s--) {
1706 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1707 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1708 // This is a bit for the GC side effect.
1709 if (current_bit_is_set) {
1710 flags += "GC";
1711 }
Aart Bik854a02b2015-07-14 16:07:00 -07001712 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001713 } else {
1714 // This is a bit for the array/field analysis.
1715 // The underscore character stands for the 'can trigger GC' bit.
1716 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1717 if (current_bit_is_set) {
1718 flags += kDebug[s];
1719 }
1720 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1721 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1722 flags += "|";
1723 }
1724 }
Aart Bik854a02b2015-07-14 16:07:00 -07001725 }
1726 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001727 }
1728
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001730
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001731 private:
1732 static constexpr int kFieldArrayAnalysisBits = 9;
1733
1734 static constexpr int kFieldWriteOffset = 0;
1735 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1736 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1737 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1738
1739 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1740
1741 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1742 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1743 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1744 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1745
1746 static constexpr int kLastBit = kDependsOnGCBit;
1747 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1748
1749 // Aliases.
1750
1751 static_assert(kChangeBits == kDependOnBits,
1752 "the 'change' bits should match the 'depend on' bits.");
1753
1754 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1755 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1756 static constexpr uint64_t kAllWrites =
1757 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1758 static constexpr uint64_t kAllReads =
1759 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001760
Aart Bik854a02b2015-07-14 16:07:00 -07001761 // Translates type to bit flag.
1762 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1763 CHECK_NE(type, Primitive::kPrimVoid);
1764 const uint64_t one = 1;
1765 const int shift = type; // 0-based consecutive enum
1766 DCHECK_LE(kFieldWriteOffset, shift);
1767 DCHECK_LT(shift, kArrayWriteOffset);
1768 return one << (type + offset);
1769 }
1770
1771 // Private constructor on direct flags value.
1772 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1773
1774 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001775};
1776
David Brazdiled596192015-01-23 10:39:45 +00001777// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001778class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001779 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001780 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1781 size_t number_of_vregs,
1782 ArtMethod* method,
1783 uint32_t dex_pc,
1784 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001785 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001786 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001787 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001788 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001790 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001791 }
1792
Mingyao Yang01b47b02017-02-03 12:09:57 -08001793 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001794 : HEnvironment(arena,
1795 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001796 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001797 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001798 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799
Vladimir Markoec32f642017-06-02 10:51:55 +01001800 void AllocateLocations() {
1801 DCHECK(locations_.empty());
1802 locations_.resize(vregs_.size());
1803 }
1804
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001806 if (parent_ != nullptr) {
1807 parent_->SetAndCopyParentChain(allocator, parent);
1808 } else {
1809 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1810 parent_->CopyFrom(parent);
1811 if (parent->GetParent() != nullptr) {
1812 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1813 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001814 }
David Brazdiled596192015-01-23 10:39:45 +00001815 }
1816
Vladimir Marko71bf8092015-09-15 15:33:14 +01001817 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001818 void CopyFrom(HEnvironment* environment);
1819
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001820 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1821 // input to the loop phi instead. This is for inserting instructions that
1822 // require an environment (like HDeoptimization) in the loop pre-header.
1823 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001824
1825 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001826 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001827 }
1828
1829 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001830 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001831 }
1832
David Brazdil1abb4192015-02-17 18:33:36 +00001833 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001834
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001835 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001836
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 HEnvironment* GetParent() const { return parent_; }
1838
1839 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001840 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 }
1842
1843 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001844 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001845 }
1846
1847 uint32_t GetDexPc() const {
1848 return dex_pc_;
1849 }
1850
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001851 ArtMethod* GetMethod() const {
1852 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001853 }
1854
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001855 HInstruction* GetHolder() const {
1856 return holder_;
1857 }
1858
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001859
1860 bool IsFromInlinedInvoke() const {
1861 return GetParent() != nullptr;
1862 }
1863
David Brazdiled596192015-01-23 10:39:45 +00001864 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001865 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1866 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001868 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001869 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001870
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001871 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 HInstruction* const holder_;
1873
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001874 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001875
1876 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1877};
1878
Vladimir Markof9f64412015-09-02 14:05:49 +01001879class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001881 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001882 : previous_(nullptr),
1883 next_(nullptr),
1884 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001885 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001886 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001887 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001888 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001890 locations_(nullptr),
1891 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001892 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001893 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001894 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1895 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1896 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897
Dave Allison20dfc792014-06-16 20:44:29 -07001898 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001900#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001901 enum InstructionKind {
1902 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1903 };
1904#undef DECLARE_KIND
1905
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 HInstruction* GetNext() const { return next_; }
1907 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001908
Calin Juravle77520bc2015-01-12 18:45:46 +00001909 HInstruction* GetNextDisregardingMoves() const;
1910 HInstruction* GetPreviousDisregardingMoves() const;
1911
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001913 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001915 bool IsInBlock() const { return block_ != nullptr; }
1916 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001917 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1918 bool IsIrreducibleLoopHeaderPhi() const {
1919 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1920 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001921
Vladimir Marko372f10e2016-05-17 16:30:10 +01001922 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1923
1924 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1925 // One virtual method is enough, just const_cast<> and then re-add the const.
1926 return ArrayRef<const HUserRecord<HInstruction*>>(
1927 const_cast<HInstruction*>(this)->GetInputRecords());
1928 }
1929
Vladimir Markoe9004912016-06-16 16:50:52 +01001930 HInputsRef GetInputs() {
1931 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001932 }
1933
Vladimir Markoe9004912016-06-16 16:50:52 +01001934 HConstInputsRef GetInputs() const {
1935 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001936 }
1937
1938 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001939 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940
Aart Bik2767f4b2016-10-28 15:03:53 -07001941 bool HasInput(HInstruction* input) const {
1942 for (const HInstruction* i : GetInputs()) {
1943 if (i == input) {
1944 return true;
1945 }
1946 }
1947 return false;
1948 }
1949
Vladimir Marko372f10e2016-05-17 16:30:10 +01001950 void SetRawInputAt(size_t index, HInstruction* input) {
1951 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1952 }
1953
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001954 virtual void Accept(HGraphVisitor* visitor) = 0;
1955 virtual const char* DebugName() const = 0;
1956
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001960
1961 uint32_t GetDexPc() const { return dex_pc_; }
1962
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001963 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001964
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001965 // Can the instruction throw?
1966 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1967 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001968 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001969 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001970
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001971 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001972 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973
Calin Juravle10e244f2015-01-26 18:54:32 +00001974 // Does not apply for all instructions, but having this at top level greatly
1975 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001976 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001977 virtual bool CanBeNull() const {
1978 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1979 return true;
1980 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001981
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001982 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001983 return false;
1984 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001985
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001986 virtual bool IsActualObject() const {
1987 return GetType() == Primitive::kPrimNot;
1988 }
1989
Calin Juravle2e768302015-07-28 14:41:11 +00001990 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001991
Calin Juravle61d544b2015-02-23 16:46:57 +00001992 ReferenceTypeInfo GetReferenceTypeInfo() const {
1993 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001994 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001995 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001996 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001997
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001998 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001999 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002000 // Note: fixup_end remains valid across push_front().
2001 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2002 HUseListNode<HInstruction*>* new_node =
2003 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2004 uses_.push_front(*new_node);
2005 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006 }
2007
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002009 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002010 // Note: env_fixup_end remains valid across push_front().
2011 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2012 HUseListNode<HEnvironment*>* new_node =
2013 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2014 env_uses_.push_front(*new_node);
2015 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016 }
2017
David Brazdil1abb4192015-02-17 18:33:36 +00002018 void RemoveAsUserOfInput(size_t input) {
2019 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002020 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2021 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2022 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002023 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024
Vladimir Marko372f10e2016-05-17 16:30:10 +01002025 void RemoveAsUserOfAllInputs() {
2026 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2027 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2028 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2029 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2030 }
2031 }
2032
David Brazdil1abb4192015-02-17 18:33:36 +00002033 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2034 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002035
Vladimir Marko46817b82016-03-29 12:21:58 +01002036 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2037 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2038 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002039 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002040 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002041 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042
Aart Bikcc42be02016-10-20 16:14:16 -07002043 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002044 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002045 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002046 !CanThrow() &&
2047 !IsSuspendCheck() &&
2048 !IsControlFlow() &&
2049 !IsNativeDebugInfo() &&
2050 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002051 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002052 !IsMemoryBarrier() &&
2053 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002054 }
2055
Aart Bikcc42be02016-10-20 16:14:16 -07002056 bool IsDeadAndRemovable() const {
2057 return IsRemovable() && !HasUses();
2058 }
2059
Roland Levillain6c82d402014-10-13 16:10:27 +01002060 // Does this instruction strictly dominate `other_instruction`?
2061 // Returns false if this instruction and `other_instruction` are the same.
2062 // Aborts if this instruction and `other_instruction` are both phis.
2063 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002065 int GetId() const { return id_; }
2066 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002068 int GetSsaIndex() const { return ssa_index_; }
2069 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2070 bool HasSsaIndex() const { return ssa_index_ != -1; }
2071
2072 bool HasEnvironment() const { return environment_ != nullptr; }
2073 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002074 // Set the `environment_` field. Raw because this method does not
2075 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002076 void SetRawEnvironment(HEnvironment* environment) {
2077 DCHECK(environment_ == nullptr);
2078 DCHECK_EQ(environment->GetHolder(), this);
2079 environment_ = environment;
2080 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002081
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002082 void InsertRawEnvironment(HEnvironment* environment) {
2083 DCHECK(environment_ != nullptr);
2084 DCHECK_EQ(environment->GetHolder(), this);
2085 DCHECK(environment->GetParent() == nullptr);
2086 environment->parent_ = environment_;
2087 environment_ = environment;
2088 }
2089
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002090 void RemoveEnvironment();
2091
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002092 // Set the environment of this instruction, copying it from `environment`. While
2093 // copying, the uses lists are being updated.
2094 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002095 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002096 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002097 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002098 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002099 if (environment->GetParent() != nullptr) {
2100 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2101 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002102 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002104 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2105 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002106 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002107 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002108 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002109 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002110 if (environment->GetParent() != nullptr) {
2111 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2112 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002113 }
2114
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 // Returns the number of entries in the environment. Typically, that is the
2116 // number of dex registers in a method. It could be more in case of inlining.
2117 size_t EnvironmentSize() const;
2118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002119 LocationSummary* GetLocations() const { return locations_; }
2120 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002122 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002123 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002124 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125
Alexandre Rames188d4312015-04-09 18:30:21 +01002126 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2127 // uses of this instruction by `other` are *not* updated.
2128 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2129 ReplaceWith(other);
2130 other->ReplaceInput(this, use_index);
2131 }
2132
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002133 // Move `this` instruction before `cursor`
2134 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002135
Vladimir Markofb337ea2015-11-25 15:25:10 +00002136 // Move `this` before its first user and out of any loops. If there is no
2137 // out-of-loop user that dominates all other users, move the instruction
2138 // to the end of the out-of-loop common dominator of the user's blocks.
2139 //
2140 // This can be used only on non-throwing instructions with no side effects that
2141 // have at least one use but no environment uses.
2142 void MoveBeforeFirstUserAndOutOfLoops();
2143
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002144#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002145 bool Is##type() const; \
2146 const H##type* As##type() const; \
2147 H##type* As##type();
2148
2149 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2150#undef INSTRUCTION_TYPE_CHECK
2151
2152#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002153 bool Is##type() const { return (As##type() != nullptr); } \
2154 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002156 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002157#undef INSTRUCTION_TYPE_CHECK
2158
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002160 // TODO: this method is used by LICM and GVN with possibly different
2161 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162 virtual bool CanBeMoved() const { return false; }
2163
2164 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002165 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002166 return false;
2167 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168
2169 // Returns whether any data encoded in the two instructions is equal.
2170 // This method does not look at the inputs. Both instructions must be
2171 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002172 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002173 return false;
2174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002175
2176 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002177 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002178 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002179 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002181 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2182 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2183 // the virtual function because the __attribute__((__pure__)) doesn't really
2184 // apply the strong requirement for virtual functions, preventing optimizations.
2185 InstructionKind GetKind() const PURE;
2186 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002187
2188 virtual size_t ComputeHashCode() const {
2189 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002190 for (const HInstruction* input : GetInputs()) {
2191 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002192 }
2193 return result;
2194 }
2195
2196 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002197 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002198 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002200 size_t GetLifetimePosition() const { return lifetime_position_; }
2201 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2202 LiveInterval* GetLiveInterval() const { return live_interval_; }
2203 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2204 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2205
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002206 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2207
2208 // Returns whether the code generation of the instruction will require to have access
2209 // to the current method. Such instructions are:
2210 // (1): Instructions that require an environment, as calling the runtime requires
2211 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002212 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2213 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002214 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002215 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002216 }
2217
Vladimir Markodc151b22015-10-15 18:02:30 +01002218 // Returns whether the code generation of the instruction will require to have access
2219 // to the dex cache of the current method's declaring class via the current method.
2220 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002221
Mark Mendellc4701932015-04-10 13:18:51 -04002222 // Does this instruction have any use in an environment before
2223 // control flow hits 'other'?
2224 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2225
2226 // Remove all references to environment uses of this instruction.
2227 // The caller must ensure that this is safe to do.
2228 void RemoveEnvironmentUsers();
2229
Vladimir Markoa1de9182016-02-25 11:37:38 +00002230 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2231 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002232
David Brazdil1abb4192015-02-17 18:33:36 +00002233 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002234 // If set, the machine code for this instruction is assumed to be generated by
2235 // its users. Used by liveness analysis to compute use positions accordingly.
2236 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2237 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2238 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2239 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2240
Vladimir Marko372f10e2016-05-17 16:30:10 +01002241 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2242 return GetInputRecords()[i];
2243 }
2244
2245 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2246 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2247 input_records[index] = input;
2248 }
David Brazdil1abb4192015-02-17 18:33:36 +00002249
Vladimir Markoa1de9182016-02-25 11:37:38 +00002250 uint32_t GetPackedFields() const {
2251 return packed_fields_;
2252 }
2253
2254 template <size_t flag>
2255 bool GetPackedFlag() const {
2256 return (packed_fields_ & (1u << flag)) != 0u;
2257 }
2258
2259 template <size_t flag>
2260 void SetPackedFlag(bool value = true) {
2261 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2262 }
2263
2264 template <typename BitFieldType>
2265 typename BitFieldType::value_type GetPackedField() const {
2266 return BitFieldType::Decode(packed_fields_);
2267 }
2268
2269 template <typename BitFieldType>
2270 void SetPackedField(typename BitFieldType::value_type value) {
2271 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2272 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2273 }
2274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002276 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2277 auto before_use_node = uses_.before_begin();
2278 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2279 HInstruction* user = use_node->GetUser();
2280 size_t input_index = use_node->GetIndex();
2281 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2282 before_use_node = use_node;
2283 }
2284 }
2285
2286 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2287 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2288 if (next != uses_.end()) {
2289 HInstruction* next_user = next->GetUser();
2290 size_t next_index = next->GetIndex();
2291 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2292 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2293 }
2294 }
2295
2296 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2297 auto before_env_use_node = env_uses_.before_begin();
2298 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2299 HEnvironment* user = env_use_node->GetUser();
2300 size_t input_index = env_use_node->GetIndex();
2301 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2302 before_env_use_node = env_use_node;
2303 }
2304 }
2305
2306 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2307 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2308 if (next != env_uses_.end()) {
2309 HEnvironment* next_user = next->GetUser();
2310 size_t next_index = next->GetIndex();
2311 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2312 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2313 }
2314 }
David Brazdil1abb4192015-02-17 18:33:36 +00002315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316 HInstruction* previous_;
2317 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002318 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002321 // An instruction gets an id when it is added to the graph.
2322 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002323 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002324 int id_;
2325
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002326 // When doing liveness analysis, instructions that have uses get an SSA index.
2327 int ssa_index_;
2328
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 // Packed fields.
2330 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002331
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002332 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002333 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002334
2335 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002336 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002337
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338 // The environment associated with this instruction. Not null if the instruction
2339 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002340 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002341
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002342 // Set by the code generator.
2343 LocationSummary* locations_;
2344
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002345 // Set by the liveness analysis.
2346 LiveInterval* live_interval_;
2347
2348 // Set by the liveness analysis, this is the position in a linear
2349 // order of blocks where this instruction's live interval start.
2350 size_t lifetime_position_;
2351
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002352 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002353
Vladimir Markoa1de9182016-02-25 11:37:38 +00002354 // The reference handle part of the reference type info.
2355 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002356 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002357 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002358
David Brazdil1abb4192015-02-17 18:33:36 +00002359 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002360 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002361 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002362 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002363 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364
2365 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2366};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002367std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002368
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002369// Iterates over the instructions, while preserving the next instruction
2370// in case the current instruction gets removed from the list by the user
2371// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002372class HInstructionIterator : public ValueObject {
2373 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002374 explicit HInstructionIterator(const HInstructionList& instructions)
2375 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002376 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002377 }
2378
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002379 bool Done() const { return instruction_ == nullptr; }
2380 HInstruction* Current() const { return instruction_; }
2381 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002383 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384 }
2385
2386 private:
2387 HInstruction* instruction_;
2388 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002389
2390 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391};
2392
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002393// Iterates over the instructions without saving the next instruction,
2394// therefore handling changes in the graph potentially made by the user
2395// of this iterator.
2396class HInstructionIteratorHandleChanges : public ValueObject {
2397 public:
2398 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2399 : instruction_(instructions.first_instruction_) {
2400 }
2401
2402 bool Done() const { return instruction_ == nullptr; }
2403 HInstruction* Current() const { return instruction_; }
2404 void Advance() {
2405 instruction_ = instruction_->GetNext();
2406 }
2407
2408 private:
2409 HInstruction* instruction_;
2410
2411 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2412};
2413
2414
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002415class HBackwardInstructionIterator : public ValueObject {
2416 public:
2417 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2418 : instruction_(instructions.last_instruction_) {
2419 next_ = Done() ? nullptr : instruction_->GetPrevious();
2420 }
2421
2422 bool Done() const { return instruction_ == nullptr; }
2423 HInstruction* Current() const { return instruction_; }
2424 void Advance() {
2425 instruction_ = next_;
2426 next_ = Done() ? nullptr : instruction_->GetPrevious();
2427 }
2428
2429 private:
2430 HInstruction* instruction_;
2431 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002432
2433 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002434};
2435
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002436class HVariableInputSizeInstruction : public HInstruction {
2437 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002438 using HInstruction::GetInputRecords; // Keep the const version visible.
2439 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2440 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2441 }
2442
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002443 void AddInput(HInstruction* input);
2444 void InsertInputAt(size_t index, HInstruction* input);
2445 void RemoveInputAt(size_t index);
2446
Igor Murashkind01745e2017-04-05 16:40:31 -07002447 // Removes all the inputs.
2448 // Also removes this instructions from each input's use list
2449 // (for non-environment uses only).
2450 void RemoveAllInputs();
2451
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002452 protected:
2453 HVariableInputSizeInstruction(SideEffects side_effects,
2454 uint32_t dex_pc,
2455 ArenaAllocator* arena,
2456 size_t number_of_inputs,
2457 ArenaAllocKind kind)
2458 : HInstruction(side_effects, dex_pc),
2459 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2460
2461 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2462
2463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2465};
2466
Vladimir Markof9f64412015-09-02 14:05:49 +01002467template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468class HTemplateInstruction: public HInstruction {
2469 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002470 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002472 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
Vladimir Marko372f10e2016-05-17 16:30:10 +01002474 using HInstruction::GetInputRecords; // Keep the const version visible.
2475 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2476 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002477 }
2478
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002480 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002481
2482 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483};
2484
Vladimir Markof9f64412015-09-02 14:05:49 +01002485// HTemplateInstruction specialization for N=0.
2486template<>
2487class HTemplateInstruction<0>: public HInstruction {
2488 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002489 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 : HInstruction(side_effects, dex_pc) {}
2491
Vladimir Markof9f64412015-09-02 14:05:49 +01002492 virtual ~HTemplateInstruction() {}
2493
Vladimir Marko372f10e2016-05-17 16:30:10 +01002494 using HInstruction::GetInputRecords; // Keep the const version visible.
2495 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2496 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002497 }
2498
2499 private:
2500 friend class SsaBuilder;
2501};
2502
Dave Allison20dfc792014-06-16 20:44:29 -07002503template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002504class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002505 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002506 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002507 : HTemplateInstruction<N>(side_effects, dex_pc) {
2508 this->template SetPackedField<TypeField>(type);
2509 }
Dave Allison20dfc792014-06-16 20:44:29 -07002510 virtual ~HExpression() {}
2511
Vladimir Markoa1de9182016-02-25 11:37:38 +00002512 Primitive::Type GetType() const OVERRIDE {
2513 return TypeField::Decode(this->GetPackedFields());
2514 }
Dave Allison20dfc792014-06-16 20:44:29 -07002515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002516 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002517 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2518 static constexpr size_t kFieldTypeSize =
2519 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2520 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2521 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2522 "Too many packed fields.");
2523 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002524};
2525
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002526// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2527// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002528class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2531 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002533 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002535 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2539};
2540
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002541// Represents dex's RETURN opcodes. A HReturn is a control flow
2542// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002543class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2546 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002547 SetRawInputAt(0, value);
2548 }
2549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002551
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002552 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002553
2554 private:
2555 DISALLOW_COPY_AND_ASSIGN(HReturn);
2556};
2557
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002558class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002559 public:
2560 HPhi(ArenaAllocator* arena,
2561 uint32_t reg_number,
2562 size_t number_of_inputs,
2563 Primitive::Type type,
2564 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002565 : HVariableInputSizeInstruction(
2566 SideEffects::None(),
2567 dex_pc,
2568 arena,
2569 number_of_inputs,
2570 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002571 reg_number_(reg_number) {
2572 SetPackedField<TypeField>(ToPhiType(type));
2573 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2574 // Phis are constructed live and marked dead if conflicting or unused.
2575 // Individual steps of SsaBuilder should assume that if a phi has been
2576 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2577 SetPackedFlag<kFlagIsLive>(true);
2578 SetPackedFlag<kFlagCanBeNull>(true);
2579 }
2580
2581 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2582 static Primitive::Type ToPhiType(Primitive::Type type) {
2583 return Primitive::PrimitiveKind(type);
2584 }
2585
2586 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2587
David Brazdildee58d62016-04-07 09:54:26 +00002588 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2589 void SetType(Primitive::Type new_type) {
2590 // Make sure that only valid type changes occur. The following are allowed:
2591 // (1) int -> float/ref (primitive type propagation),
2592 // (2) long -> double (primitive type propagation).
2593 DCHECK(GetType() == new_type ||
2594 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2595 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2596 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2597 SetPackedField<TypeField>(new_type);
2598 }
2599
2600 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2601 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2602
2603 uint32_t GetRegNumber() const { return reg_number_; }
2604
2605 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2606 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2607 bool IsDead() const { return !IsLive(); }
2608 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2609
Vladimir Markoe9004912016-06-16 16:50:52 +01002610 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002611 return other != nullptr
2612 && other->IsPhi()
2613 && other->AsPhi()->GetBlock() == GetBlock()
2614 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2615 }
2616
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002617 bool HasEquivalentPhi() const {
2618 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2619 return true;
2620 }
2621 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2622 return true;
2623 }
2624 return false;
2625 }
2626
David Brazdildee58d62016-04-07 09:54:26 +00002627 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2628 // An equivalent phi is a phi having the same dex register and type.
2629 // It assumes that phis with the same dex register are adjacent.
2630 HPhi* GetNextEquivalentPhiWithSameType() {
2631 HInstruction* next = GetNext();
2632 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2633 if (next->GetType() == GetType()) {
2634 return next->AsPhi();
2635 }
2636 next = next->GetNext();
2637 }
2638 return nullptr;
2639 }
2640
2641 DECLARE_INSTRUCTION(Phi);
2642
David Brazdildee58d62016-04-07 09:54:26 +00002643 private:
2644 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2645 static constexpr size_t kFieldTypeSize =
2646 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2647 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2648 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2649 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2650 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2651 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2652
David Brazdildee58d62016-04-07 09:54:26 +00002653 const uint32_t reg_number_;
2654
2655 DISALLOW_COPY_AND_ASSIGN(HPhi);
2656};
2657
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002659// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002661class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002664
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002665 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002667 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002668
2669 private:
2670 DISALLOW_COPY_AND_ASSIGN(HExit);
2671};
2672
2673// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002674class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002676 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002677
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002678 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002679
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002680 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002681 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002682 }
2683
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002684 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002685
2686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HGoto);
2688};
2689
Roland Levillain9867bc72015-08-05 10:21:34 +01002690class HConstant : public HExpression<0> {
2691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002692 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2693 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002694
2695 bool CanBeMoved() const OVERRIDE { return true; }
2696
Roland Levillain1a653882016-03-18 18:05:57 +00002697 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002699 // Is this constant 0 in the arithmetic sense?
2700 virtual bool IsArithmeticZero() const { return false; }
2701 // Is this constant a 0-bit pattern?
2702 virtual bool IsZeroBitPattern() const { return false; }
2703 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002704 virtual bool IsOne() const { return false; }
2705
David Brazdil77a48ae2015-09-15 12:34:04 +00002706 virtual uint64_t GetValueAsUint64() const = 0;
2707
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002708 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002709
2710 private:
2711 DISALLOW_COPY_AND_ASSIGN(HConstant);
2712};
2713
Vladimir Markofcb503c2016-05-18 12:48:17 +01002714class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002716 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 return true;
2718 }
2719
David Brazdil77a48ae2015-09-15 12:34:04 +00002720 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2721
Roland Levillain9867bc72015-08-05 10:21:34 +01002722 size_t ComputeHashCode() const OVERRIDE { return 0; }
2723
Roland Levillain1a653882016-03-18 18:05:57 +00002724 // The null constant representation is a 0-bit pattern.
2725 virtual bool IsZeroBitPattern() const { return true; }
2726
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 DECLARE_INSTRUCTION(NullConstant);
2728
2729 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002730 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002731
2732 friend class HGraph;
2733 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2734};
2735
2736// Constants of the type int. Those can be from Dex instructions, or
2737// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002738class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 public:
2740 int32_t GetValue() const { return value_; }
2741
David Brazdil9f389d42015-10-01 14:32:56 +01002742 uint64_t GetValueAsUint64() const OVERRIDE {
2743 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2744 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002745
Vladimir Marko372f10e2016-05-17 16:30:10 +01002746 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002747 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 return other->AsIntConstant()->value_ == value_;
2749 }
2750
2751 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2752
2753 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002754 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2755 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2757
Roland Levillain1a653882016-03-18 18:05:57 +00002758 // Integer constants are used to encode Boolean values as well,
2759 // where 1 means true and 0 means false.
2760 bool IsTrue() const { return GetValue() == 1; }
2761 bool IsFalse() const { return GetValue() == 0; }
2762
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 DECLARE_INSTRUCTION(IntConstant);
2764
2765 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002766 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2767 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2768 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2769 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002770
2771 const int32_t value_;
2772
2773 friend class HGraph;
2774 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2775 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2776 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2777};
2778
Vladimir Markofcb503c2016-05-18 12:48:17 +01002779class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002780 public:
2781 int64_t GetValue() const { return value_; }
2782
David Brazdil77a48ae2015-09-15 12:34:04 +00002783 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2784
Vladimir Marko372f10e2016-05-17 16:30:10 +01002785 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002786 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002787 return other->AsLongConstant()->value_ == value_;
2788 }
2789
2790 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2791
2792 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002793 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2794 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002795 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2796
2797 DECLARE_INSTRUCTION(LongConstant);
2798
2799 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002800 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2801 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002802
2803 const int64_t value_;
2804
2805 friend class HGraph;
2806 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2807};
Dave Allison20dfc792014-06-16 20:44:29 -07002808
Vladimir Markofcb503c2016-05-18 12:48:17 +01002809class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002810 public:
2811 float GetValue() const { return value_; }
2812
2813 uint64_t GetValueAsUint64() const OVERRIDE {
2814 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2815 }
2816
Vladimir Marko372f10e2016-05-17 16:30:10 +01002817 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002818 DCHECK(other->IsFloatConstant()) << other->DebugName();
2819 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2820 }
2821
2822 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2823
2824 bool IsMinusOne() const OVERRIDE {
2825 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2826 }
Roland Levillain1a653882016-03-18 18:05:57 +00002827 bool IsArithmeticZero() const OVERRIDE {
2828 return std::fpclassify(value_) == FP_ZERO;
2829 }
2830 bool IsArithmeticPositiveZero() const {
2831 return IsArithmeticZero() && !std::signbit(value_);
2832 }
2833 bool IsArithmeticNegativeZero() const {
2834 return IsArithmeticZero() && std::signbit(value_);
2835 }
2836 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002837 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002838 }
2839 bool IsOne() const OVERRIDE {
2840 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2841 }
2842 bool IsNaN() const {
2843 return std::isnan(value_);
2844 }
2845
2846 DECLARE_INSTRUCTION(FloatConstant);
2847
2848 private:
2849 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2850 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2851 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2852 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2853
2854 const float value_;
2855
2856 // Only the SsaBuilder and HGraph can create floating-point constants.
2857 friend class SsaBuilder;
2858 friend class HGraph;
2859 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2860};
2861
Vladimir Markofcb503c2016-05-18 12:48:17 +01002862class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002863 public:
2864 double GetValue() const { return value_; }
2865
2866 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2867
Vladimir Marko372f10e2016-05-17 16:30:10 +01002868 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002869 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2870 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2871 }
2872
2873 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2874
2875 bool IsMinusOne() const OVERRIDE {
2876 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2877 }
Roland Levillain1a653882016-03-18 18:05:57 +00002878 bool IsArithmeticZero() const OVERRIDE {
2879 return std::fpclassify(value_) == FP_ZERO;
2880 }
2881 bool IsArithmeticPositiveZero() const {
2882 return IsArithmeticZero() && !std::signbit(value_);
2883 }
2884 bool IsArithmeticNegativeZero() const {
2885 return IsArithmeticZero() && std::signbit(value_);
2886 }
2887 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002888 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002889 }
2890 bool IsOne() const OVERRIDE {
2891 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2892 }
2893 bool IsNaN() const {
2894 return std::isnan(value_);
2895 }
2896
2897 DECLARE_INSTRUCTION(DoubleConstant);
2898
2899 private:
2900 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2901 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2902 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2903 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2904
2905 const double value_;
2906
2907 // Only the SsaBuilder and HGraph can create floating-point constants.
2908 friend class SsaBuilder;
2909 friend class HGraph;
2910 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2911};
2912
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002913// Conditional branch. A block ending with an HIf instruction must have
2914// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002915class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002916 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002917 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2918 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002919 SetRawInputAt(0, input);
2920 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002921
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002922 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002923
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002924 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002925 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002926 }
2927
2928 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002929 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002930 }
2931
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002932 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002933
2934 private:
2935 DISALLOW_COPY_AND_ASSIGN(HIf);
2936};
2937
David Brazdilfc6a86a2015-06-26 10:33:45 +00002938
2939// Abstract instruction which marks the beginning and/or end of a try block and
2940// links it to the respective exception handlers. Behaves the same as a Goto in
2941// non-exceptional control flow.
2942// Normal-flow successor is stored at index zero, exception handlers under
2943// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002944class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002945 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002946 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002947 kEntry,
2948 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002949 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002950 };
2951
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002952 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002953 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2954 SetPackedField<BoundaryKindField>(kind);
2955 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002956
2957 bool IsControlFlow() const OVERRIDE { return true; }
2958
2959 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002960 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002961
David Brazdild26a4112015-11-10 11:07:31 +00002962 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2963 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2964 }
2965
David Brazdilfc6a86a2015-06-26 10:33:45 +00002966 // Returns whether `handler` is among its exception handlers (non-zero index
2967 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002968 bool HasExceptionHandler(const HBasicBlock& handler) const {
2969 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002970 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002971 }
2972
2973 // If not present already, adds `handler` to its block's list of exception
2974 // handlers.
2975 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002976 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002977 GetBlock()->AddSuccessor(handler);
2978 }
2979 }
2980
Vladimir Markoa1de9182016-02-25 11:37:38 +00002981 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2982 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002983
David Brazdilffee3d32015-07-06 11:48:53 +01002984 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2985
David Brazdilfc6a86a2015-06-26 10:33:45 +00002986 DECLARE_INSTRUCTION(TryBoundary);
2987
2988 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002989 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2990 static constexpr size_t kFieldBoundaryKindSize =
2991 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2992 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2993 kFieldBoundaryKind + kFieldBoundaryKindSize;
2994 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2995 "Too many packed fields.");
2996 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002997
2998 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2999};
3000
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003001// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003002class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003003 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003004 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3005 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003006 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003007 : HVariableInputSizeInstruction(
3008 SideEffects::All(),
3009 dex_pc,
3010 arena,
3011 /* number_of_inputs */ 1,
3012 kArenaAllocMisc) {
3013 SetPackedFlag<kFieldCanBeMoved>(false);
3014 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003015 SetRawInputAt(0, cond);
3016 }
3017
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003018 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3019 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3020 // instead of `guard`.
3021 // We set CanTriggerGC to prevent any intermediate address to be live
3022 // at the point of the `HDeoptimize`.
3023 HDeoptimize(ArenaAllocator* arena,
3024 HInstruction* cond,
3025 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003026 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003027 uint32_t dex_pc)
3028 : HVariableInputSizeInstruction(
3029 SideEffects::CanTriggerGC(),
3030 dex_pc,
3031 arena,
3032 /* number_of_inputs */ 2,
3033 kArenaAllocMisc) {
3034 SetPackedFlag<kFieldCanBeMoved>(true);
3035 SetPackedField<DeoptimizeKindField>(kind);
3036 SetRawInputAt(0, cond);
3037 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003038 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003039
3040 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3041
3042 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3043 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3044 }
3045
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003046 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003047
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003048 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003049
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003050 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003051
3052 Primitive::Type GetType() const OVERRIDE {
3053 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3054 }
3055
3056 bool GuardsAnInput() const {
3057 return InputCount() == 2;
3058 }
3059
3060 HInstruction* GuardedInput() const {
3061 DCHECK(GuardsAnInput());
3062 return InputAt(1);
3063 }
3064
3065 void RemoveGuard() {
3066 RemoveInputAt(1);
3067 }
3068
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003069 DECLARE_INSTRUCTION(Deoptimize);
3070
3071 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003072 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3073 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3074 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003075 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003076 static constexpr size_t kNumberOfDeoptimizePackedBits =
3077 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3078 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3079 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003080 using DeoptimizeKindField =
3081 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003082
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003083 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3084};
3085
Mingyao Yang063fc772016-08-02 11:02:54 -07003086// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3087// The compiled code checks this flag value in a guard before devirtualized call and
3088// if it's true, starts to do deoptimization.
3089// It has a 4-byte slot on stack.
3090// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003091class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003092 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003093 // CHA guards are only optimized in a separate pass and it has no side effects
3094 // with regard to other passes.
3095 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3096 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003097 }
3098
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003099 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3100
3101 // We do all CHA guard elimination/motion in a single pass, after which there is no
3102 // further guard elimination/motion since a guard might have been used for justification
3103 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3104 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003105 bool CanBeMoved() const OVERRIDE { return false; }
3106
3107 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3108
3109 private:
3110 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3111};
3112
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003113// Represents the ArtMethod that was passed as a first argument to
3114// the method. It is used by instructions that depend on it, like
3115// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003116class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003117 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003118 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3119 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003120
3121 DECLARE_INSTRUCTION(CurrentMethod);
3122
3123 private:
3124 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3125};
3126
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127// Fetches an ArtMethod from the virtual table or the interface method table
3128// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003129class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003130 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003131 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 kVTable,
3133 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003134 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003135 };
3136 HClassTableGet(HInstruction* cls,
3137 Primitive::Type type,
3138 TableKind kind,
3139 size_t index,
3140 uint32_t dex_pc)
3141 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003142 index_(index) {
3143 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003144 SetRawInputAt(0, cls);
3145 }
3146
3147 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003148 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003149 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151 }
3152
Vladimir Markoa1de9182016-02-25 11:37:38 +00003153 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003154 size_t GetIndex() const { return index_; }
3155
3156 DECLARE_INSTRUCTION(ClassTableGet);
3157
3158 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003159 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3160 static constexpr size_t kFieldTableKindSize =
3161 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3162 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3163 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3164 "Too many packed fields.");
3165 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3166
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003167 // The index of the ArtMethod in the table.
3168 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003169
3170 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3171};
3172
Mark Mendellfe57faa2015-09-18 09:26:15 -04003173// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3174// have one successor for each entry in the switch table, and the final successor
3175// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003176class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003177 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003178 HPackedSwitch(int32_t start_value,
3179 uint32_t num_entries,
3180 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003181 uint32_t dex_pc = kNoDexPc)
3182 : HTemplateInstruction(SideEffects::None(), dex_pc),
3183 start_value_(start_value),
3184 num_entries_(num_entries) {
3185 SetRawInputAt(0, input);
3186 }
3187
3188 bool IsControlFlow() const OVERRIDE { return true; }
3189
3190 int32_t GetStartValue() const { return start_value_; }
3191
Vladimir Marko211c2112015-09-24 16:52:33 +01003192 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003193
3194 HBasicBlock* GetDefaultBlock() const {
3195 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003196 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003197 }
3198 DECLARE_INSTRUCTION(PackedSwitch);
3199
3200 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003201 const int32_t start_value_;
3202 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003203
3204 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3205};
3206
Roland Levillain88cb1752014-10-20 16:36:47 +01003207class HUnaryOperation : public HExpression<1> {
3208 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003209 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3210 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003211 SetRawInputAt(0, input);
3212 }
3213
3214 HInstruction* GetInput() const { return InputAt(0); }
3215 Primitive::Type GetResultType() const { return GetType(); }
3216
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003217 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003218 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003219 return true;
3220 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003221
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222 // Try to statically evaluate `this` and return a HConstant
3223 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003224 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003225 HConstant* TryStaticEvaluation() const;
3226
3227 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003228 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3229 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003230 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3231 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003232
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003233 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003234
3235 private:
3236 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3237};
3238
Dave Allison20dfc792014-06-16 20:44:29 -07003239class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003240 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003241 HBinaryOperation(Primitive::Type result_type,
3242 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003243 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003244 SideEffects side_effects = SideEffects::None(),
3245 uint32_t dex_pc = kNoDexPc)
3246 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003247 SetRawInputAt(0, left);
3248 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003249 }
3250
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003251 HInstruction* GetLeft() const { return InputAt(0); }
3252 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003253 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003254
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003255 virtual bool IsCommutative() const { return false; }
3256
3257 // Put constant on the right.
3258 // Returns whether order is changed.
3259 bool OrderInputsWithConstantOnTheRight() {
3260 HInstruction* left = InputAt(0);
3261 HInstruction* right = InputAt(1);
3262 if (left->IsConstant() && !right->IsConstant()) {
3263 ReplaceInput(right, 0);
3264 ReplaceInput(left, 1);
3265 return true;
3266 }
3267 return false;
3268 }
3269
3270 // Order inputs by instruction id, but favor constant on the right side.
3271 // This helps GVN for commutative ops.
3272 void OrderInputs() {
3273 DCHECK(IsCommutative());
3274 HInstruction* left = InputAt(0);
3275 HInstruction* right = InputAt(1);
3276 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3277 return;
3278 }
3279 if (OrderInputsWithConstantOnTheRight()) {
3280 return;
3281 }
3282 // Order according to instruction id.
3283 if (left->GetId() > right->GetId()) {
3284 ReplaceInput(right, 0);
3285 ReplaceInput(left, 1);
3286 }
3287 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003288
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003289 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003290 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003291 return true;
3292 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003293
Roland Levillain31dd3d62016-02-16 12:21:02 +00003294 // Try to statically evaluate `this` and return a HConstant
3295 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003296 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003297 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003298
3299 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3301 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003302 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3303 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003305 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3306 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003307 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3308 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003309 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3310 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003311 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3313 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003314
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003315 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003316 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003317 HConstant* GetConstantRight() const;
3318
3319 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003320 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003321 HInstruction* GetLeastConstantLeft() const;
3322
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003323 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003324
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003325 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003326 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3327};
3328
Mark Mendellc4701932015-04-10 13:18:51 -04003329// The comparison bias applies for floating point operations and indicates how NaN
3330// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003331enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003332 kNoBias, // bias is not applicable (i.e. for long operation)
3333 kGtBias, // return 1 for NaN comparisons
3334 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003335 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003336};
3337
Roland Levillain31dd3d62016-02-16 12:21:02 +00003338std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3339
Dave Allison20dfc792014-06-16 20:44:29 -07003340class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003341 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003342 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3344 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3345 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003346
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003347 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003348 // `instruction`, and disregard moves in between.
3349 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003350
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003351 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003352
3353 virtual IfCondition GetCondition() const = 0;
3354
Mark Mendellc4701932015-04-10 13:18:51 -04003355 virtual IfCondition GetOppositeCondition() const = 0;
3356
Vladimir Markoa1de9182016-02-25 11:37:38 +00003357 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003358 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3359
Vladimir Markoa1de9182016-02-25 11:37:38 +00003360 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3361 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003362
Vladimir Marko372f10e2016-05-17 16:30:10 +01003363 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003364 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003365 }
3366
Roland Levillain4fa13f62015-07-06 18:11:54 +01003367 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003369 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003370 if (if_cond == kCondNE) {
3371 return true;
3372 } else if (if_cond == kCondEQ) {
3373 return false;
3374 }
3375 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003376 }
3377
3378 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003379 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003380 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003381 if (if_cond == kCondEQ) {
3382 return true;
3383 } else if (if_cond == kCondNE) {
3384 return false;
3385 }
3386 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003387 }
3388
Roland Levillain31dd3d62016-02-16 12:21:02 +00003389 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003390 // Needed if we merge a HCompare into a HCondition.
3391 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3392 static constexpr size_t kFieldComparisonBiasSize =
3393 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3394 static constexpr size_t kNumberOfConditionPackedBits =
3395 kFieldComparisonBias + kFieldComparisonBiasSize;
3396 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3397 using ComparisonBiasField =
3398 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3399
Roland Levillain31dd3d62016-02-16 12:21:02 +00003400 template <typename T>
3401 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3402
3403 template <typename T>
3404 int32_t CompareFP(T x, T y) const {
3405 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3406 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3407 // Handle the bias.
3408 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3409 }
3410
3411 // Return an integer constant containing the result of a condition evaluated at compile time.
3412 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3413 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3414 }
3415
Dave Allison20dfc792014-06-16 20:44:29 -07003416 private:
3417 DISALLOW_COPY_AND_ASSIGN(HCondition);
3418};
3419
3420// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003421class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003422 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003423 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3424 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003425
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003426 bool IsCommutative() const OVERRIDE { return true; }
3427
Vladimir Marko9e23df52015-11-10 17:14:35 +00003428 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3429 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003430 return MakeConstantCondition(true, GetDexPc());
3431 }
3432 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3433 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3434 }
3435 // In the following Evaluate methods, a HCompare instruction has
3436 // been merged into this HEqual instruction; evaluate it as
3437 // `Compare(x, y) == 0`.
3438 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3439 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3440 GetDexPc());
3441 }
3442 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3443 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3444 }
3445 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3446 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003447 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003449 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003451 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003452 return kCondEQ;
3453 }
3454
Mark Mendellc4701932015-04-10 13:18:51 -04003455 IfCondition GetOppositeCondition() const OVERRIDE {
3456 return kCondNE;
3457 }
3458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003460 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003461
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003462 DISALLOW_COPY_AND_ASSIGN(HEqual);
3463};
3464
Vladimir Markofcb503c2016-05-18 12:48:17 +01003465class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003467 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3468 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003469
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003470 bool IsCommutative() const OVERRIDE { return true; }
3471
Vladimir Marko9e23df52015-11-10 17:14:35 +00003472 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3473 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003474 return MakeConstantCondition(false, GetDexPc());
3475 }
3476 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3477 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3478 }
3479 // In the following Evaluate methods, a HCompare instruction has
3480 // been merged into this HNotEqual instruction; evaluate it as
3481 // `Compare(x, y) != 0`.
3482 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3483 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3484 }
3485 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3486 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3487 }
3488 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3489 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003490 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003491
Dave Allison20dfc792014-06-16 20:44:29 -07003492 DECLARE_INSTRUCTION(NotEqual);
3493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003494 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003495 return kCondNE;
3496 }
3497
Mark Mendellc4701932015-04-10 13:18:51 -04003498 IfCondition GetOppositeCondition() const OVERRIDE {
3499 return kCondEQ;
3500 }
3501
Dave Allison20dfc792014-06-16 20:44:29 -07003502 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003503 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003504
Dave Allison20dfc792014-06-16 20:44:29 -07003505 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3506};
3507
Vladimir Markofcb503c2016-05-18 12:48:17 +01003508class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003509 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003510 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3511 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003512
Roland Levillain9867bc72015-08-05 10:21:34 +01003513 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003515 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003516 // In the following Evaluate methods, a HCompare instruction has
3517 // been merged into this HLessThan instruction; evaluate it as
3518 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003519 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003520 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3521 }
3522 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3523 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3524 }
3525 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3526 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003527 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003528
Dave Allison20dfc792014-06-16 20:44:29 -07003529 DECLARE_INSTRUCTION(LessThan);
3530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003531 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003532 return kCondLT;
3533 }
3534
Mark Mendellc4701932015-04-10 13:18:51 -04003535 IfCondition GetOppositeCondition() const OVERRIDE {
3536 return kCondGE;
3537 }
3538
Dave Allison20dfc792014-06-16 20:44:29 -07003539 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003540 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003541
Dave Allison20dfc792014-06-16 20:44:29 -07003542 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3543};
3544
Vladimir Markofcb503c2016-05-18 12:48:17 +01003545class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003547 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3548 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003549
Roland Levillain9867bc72015-08-05 10:21:34 +01003550 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003551 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003552 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003553 // In the following Evaluate methods, a HCompare instruction has
3554 // been merged into this HLessThanOrEqual instruction; evaluate it as
3555 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003556 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003557 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3558 }
3559 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3560 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3561 }
3562 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3563 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003564 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003565
Dave Allison20dfc792014-06-16 20:44:29 -07003566 DECLARE_INSTRUCTION(LessThanOrEqual);
3567
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003568 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003569 return kCondLE;
3570 }
3571
Mark Mendellc4701932015-04-10 13:18:51 -04003572 IfCondition GetOppositeCondition() const OVERRIDE {
3573 return kCondGT;
3574 }
3575
Dave Allison20dfc792014-06-16 20:44:29 -07003576 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003577 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003578
Dave Allison20dfc792014-06-16 20:44:29 -07003579 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3580};
3581
Vladimir Markofcb503c2016-05-18 12:48:17 +01003582class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003583 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003584 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3585 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003586
Roland Levillain9867bc72015-08-05 10:21:34 +01003587 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003589 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003590 // In the following Evaluate methods, a HCompare instruction has
3591 // been merged into this HGreaterThan instruction; evaluate it as
3592 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003593 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003594 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3595 }
3596 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3597 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3598 }
3599 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3600 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003601 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003602
Dave Allison20dfc792014-06-16 20:44:29 -07003603 DECLARE_INSTRUCTION(GreaterThan);
3604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003605 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003606 return kCondGT;
3607 }
3608
Mark Mendellc4701932015-04-10 13:18:51 -04003609 IfCondition GetOppositeCondition() const OVERRIDE {
3610 return kCondLE;
3611 }
3612
Dave Allison20dfc792014-06-16 20:44:29 -07003613 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003614 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003615
Dave Allison20dfc792014-06-16 20:44:29 -07003616 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3617};
3618
Vladimir Markofcb503c2016-05-18 12:48:17 +01003619class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003621 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3622 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003623
Roland Levillain9867bc72015-08-05 10:21:34 +01003624 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003626 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003627 // In the following Evaluate methods, a HCompare instruction has
3628 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3629 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003630 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003631 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3632 }
3633 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3634 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3635 }
3636 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3637 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003638 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003639
Dave Allison20dfc792014-06-16 20:44:29 -07003640 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3641
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003642 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003643 return kCondGE;
3644 }
3645
Mark Mendellc4701932015-04-10 13:18:51 -04003646 IfCondition GetOppositeCondition() const OVERRIDE {
3647 return kCondLT;
3648 }
3649
Dave Allison20dfc792014-06-16 20:44:29 -07003650 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003651 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003652
Dave Allison20dfc792014-06-16 20:44:29 -07003653 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3654};
3655
Vladimir Markofcb503c2016-05-18 12:48:17 +01003656class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003657 public:
3658 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3659 : HCondition(first, second, dex_pc) {}
3660
3661 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003662 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003663 }
3664 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003665 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3666 }
3667 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3668 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3669 LOG(FATAL) << DebugName() << " is not defined for float values";
3670 UNREACHABLE();
3671 }
3672 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3673 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3674 LOG(FATAL) << DebugName() << " is not defined for double values";
3675 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003676 }
3677
3678 DECLARE_INSTRUCTION(Below);
3679
3680 IfCondition GetCondition() const OVERRIDE {
3681 return kCondB;
3682 }
3683
3684 IfCondition GetOppositeCondition() const OVERRIDE {
3685 return kCondAE;
3686 }
3687
3688 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003689 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003690 return MakeUnsigned(x) < MakeUnsigned(y);
3691 }
Aart Bike9f37602015-10-09 11:15:55 -07003692
3693 DISALLOW_COPY_AND_ASSIGN(HBelow);
3694};
3695
Vladimir Markofcb503c2016-05-18 12:48:17 +01003696class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003697 public:
3698 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3699 : HCondition(first, second, dex_pc) {}
3700
3701 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003703 }
3704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003705 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3706 }
3707 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3708 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3709 LOG(FATAL) << DebugName() << " is not defined for float values";
3710 UNREACHABLE();
3711 }
3712 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3713 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3714 LOG(FATAL) << DebugName() << " is not defined for double values";
3715 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003716 }
3717
3718 DECLARE_INSTRUCTION(BelowOrEqual);
3719
3720 IfCondition GetCondition() const OVERRIDE {
3721 return kCondBE;
3722 }
3723
3724 IfCondition GetOppositeCondition() const OVERRIDE {
3725 return kCondA;
3726 }
3727
3728 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003729 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeUnsigned(x) <= MakeUnsigned(y);
3731 }
Aart Bike9f37602015-10-09 11:15:55 -07003732
3733 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3734};
3735
Vladimir Markofcb503c2016-05-18 12:48:17 +01003736class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003737 public:
3738 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3739 : HCondition(first, second, dex_pc) {}
3740
3741 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003743 }
3744 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3746 }
3747 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3748 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3749 LOG(FATAL) << DebugName() << " is not defined for float values";
3750 UNREACHABLE();
3751 }
3752 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3753 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3754 LOG(FATAL) << DebugName() << " is not defined for double values";
3755 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003756 }
3757
3758 DECLARE_INSTRUCTION(Above);
3759
3760 IfCondition GetCondition() const OVERRIDE {
3761 return kCondA;
3762 }
3763
3764 IfCondition GetOppositeCondition() const OVERRIDE {
3765 return kCondBE;
3766 }
3767
3768 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003769 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 return MakeUnsigned(x) > MakeUnsigned(y);
3771 }
Aart Bike9f37602015-10-09 11:15:55 -07003772
3773 DISALLOW_COPY_AND_ASSIGN(HAbove);
3774};
3775
Vladimir Markofcb503c2016-05-18 12:48:17 +01003776class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003777 public:
3778 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3779 : HCondition(first, second, dex_pc) {}
3780
3781 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003783 }
3784 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3786 }
3787 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3788 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3789 LOG(FATAL) << DebugName() << " is not defined for float values";
3790 UNREACHABLE();
3791 }
3792 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3793 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3794 LOG(FATAL) << DebugName() << " is not defined for double values";
3795 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003796 }
3797
3798 DECLARE_INSTRUCTION(AboveOrEqual);
3799
3800 IfCondition GetCondition() const OVERRIDE {
3801 return kCondAE;
3802 }
3803
3804 IfCondition GetOppositeCondition() const OVERRIDE {
3805 return kCondB;
3806 }
3807
3808 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003809 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003810 return MakeUnsigned(x) >= MakeUnsigned(y);
3811 }
Aart Bike9f37602015-10-09 11:15:55 -07003812
3813 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3814};
Dave Allison20dfc792014-06-16 20:44:29 -07003815
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003816// Instruction to check how two inputs compare to each other.
3817// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003818class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003819 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003820 // Note that `comparison_type` is the type of comparison performed
3821 // between the comparison's inputs, not the type of the instantiated
3822 // HCompare instruction (which is always Primitive::kPrimInt).
3823 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003824 HInstruction* first,
3825 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003826 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003827 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003828 : HBinaryOperation(Primitive::kPrimInt,
3829 first,
3830 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003831 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003832 dex_pc) {
3833 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003834 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3835 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003836 }
3837
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003839 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3840
3841 template <typename T>
3842 int32_t ComputeFP(T x, T y) const {
3843 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3844 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3845 // Handle the bias.
3846 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3847 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003848
Roland Levillain9867bc72015-08-05 10:21:34 +01003849 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3851 // reach this code path when processing a freshly built HIR
3852 // graph. However HCompare integer instructions can be synthesized
3853 // by the instruction simplifier to implement IntegerCompare and
3854 // IntegerSignum intrinsics, so we have to handle this case.
3855 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003856 }
3857 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3859 }
3860 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3861 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3862 }
3863 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3864 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003865 }
3866
Vladimir Marko372f10e2016-05-17 16:30:10 +01003867 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003869 }
3870
Vladimir Markoa1de9182016-02-25 11:37:38 +00003871 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003872
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003874 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 bool IsGtBias() const {
3876 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003877 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003879
Roland Levillain1693a1f2016-03-15 14:57:31 +00003880 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3881 // Comparisons do not require a runtime call in any back end.
3882 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003883 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003884
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003885 DECLARE_INSTRUCTION(Compare);
3886
Roland Levillain31dd3d62016-02-16 12:21:02 +00003887 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003888 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3889 static constexpr size_t kFieldComparisonBiasSize =
3890 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3891 static constexpr size_t kNumberOfComparePackedBits =
3892 kFieldComparisonBias + kFieldComparisonBiasSize;
3893 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3894 using ComparisonBiasField =
3895 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3896
Roland Levillain31dd3d62016-02-16 12:21:02 +00003897 // Return an integer constant containing the result of a comparison evaluated at compile time.
3898 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3899 DCHECK(value == -1 || value == 0 || value == 1) << value;
3900 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3901 }
3902
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003903 private:
3904 DISALLOW_COPY_AND_ASSIGN(HCompare);
3905};
3906
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003907class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003908 public:
3909 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003910 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003911 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003912 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003913 bool finalizable,
3914 QuickEntrypointEnum entrypoint)
3915 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3916 type_index_(type_index),
3917 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003918 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003919 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003920 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003921 }
3922
Andreas Gampea5b09a62016-11-17 15:21:22 -08003923 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003924 const DexFile& GetDexFile() const { return dex_file_; }
3925
3926 // Calls runtime so needs an environment.
3927 bool NeedsEnvironment() const OVERRIDE { return true; }
3928
Mingyao Yang062157f2016-03-02 10:15:36 -08003929 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3930 bool CanThrow() const OVERRIDE { return true; }
3931
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003932 bool NeedsChecks() const {
3933 return entrypoint_ == kQuickAllocObjectWithChecks;
3934 }
David Brazdil6de19382016-01-08 17:37:10 +00003935
Vladimir Markoa1de9182016-02-25 11:37:38 +00003936 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003937
3938 bool CanBeNull() const OVERRIDE { return false; }
3939
3940 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3941
3942 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3943 entrypoint_ = entrypoint;
3944 }
3945
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003946 HLoadClass* GetLoadClass() const {
3947 HInstruction* input = InputAt(0);
3948 if (input->IsClinitCheck()) {
3949 input = input->InputAt(0);
3950 }
3951 DCHECK(input->IsLoadClass());
3952 return input->AsLoadClass();
3953 }
3954
David Brazdil6de19382016-01-08 17:37:10 +00003955 bool IsStringAlloc() const;
3956
3957 DECLARE_INSTRUCTION(NewInstance);
3958
3959 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003960 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003961 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3962 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3963 "Too many packed fields.");
3964
Andreas Gampea5b09a62016-11-17 15:21:22 -08003965 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003966 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003967 QuickEntrypointEnum entrypoint_;
3968
3969 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3970};
3971
Agi Csaki05f20562015-08-19 14:58:14 -07003972enum IntrinsicNeedsEnvironmentOrCache {
3973 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3974 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003975};
3976
Aart Bik5d75afe2015-12-14 11:57:01 -08003977enum IntrinsicSideEffects {
3978 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3979 kReadSideEffects, // Intrinsic may read heap memory.
3980 kWriteSideEffects, // Intrinsic may write heap memory.
3981 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3982};
3983
3984enum IntrinsicExceptions {
3985 kNoThrow, // Intrinsic does not throw any exceptions.
3986 kCanThrow // Intrinsic may throw exceptions.
3987};
3988
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003989class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003990 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003991 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003992
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003993 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003994 SetRawInputAt(index, argument);
3995 }
3996
Roland Levillain3e3d7332015-04-28 11:00:54 +01003997 // Return the number of arguments. This number can be lower than
3998 // the number of inputs returned by InputCount(), as some invoke
3999 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4000 // inputs at the end of their list of inputs.
4001 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4002
Vladimir Markoa1de9182016-02-25 11:37:38 +00004003 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004004
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004005 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4006
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004007 InvokeType GetInvokeType() const {
4008 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004010
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004011 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004012 return intrinsic_;
4013 }
4014
Aart Bik5d75afe2015-12-14 11:57:01 -08004015 void SetIntrinsic(Intrinsics intrinsic,
4016 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4017 IntrinsicSideEffects side_effects,
4018 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004019
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004020 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004021 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004022 }
4023
Aart Bikff7d89c2016-11-07 08:49:28 -08004024 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4025
Vladimir Markoa1de9182016-02-25 11:37:38 +00004026 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004027
Aart Bik71bf7b42016-11-16 10:17:46 -08004028 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004029
Vladimir Marko372f10e2016-05-17 16:30:10 +01004030 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004031 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4032 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004033
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004034 uint32_t* GetIntrinsicOptimizations() {
4035 return &intrinsic_optimizations_;
4036 }
4037
4038 const uint32_t* GetIntrinsicOptimizations() const {
4039 return &intrinsic_optimizations_;
4040 }
4041
4042 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4043
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004044 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004045 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004046
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004047 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004048
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004049 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004050 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4051 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4053 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004054 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004055 static constexpr size_t kFieldReturnTypeSize =
4056 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4057 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4058 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4059 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004060 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004061 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4062
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004063 HInvoke(ArenaAllocator* arena,
4064 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004065 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004066 Primitive::Type return_type,
4067 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004068 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004069 ArtMethod* resolved_method,
4070 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004071 : HVariableInputSizeInstruction(
4072 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4073 dex_pc,
4074 arena,
4075 number_of_arguments + number_of_other_inputs,
4076 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004077 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004078 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004079 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004080 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004081 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004083 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004084 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004085 }
4086
Roland Levillain3e3d7332015-04-28 11:00:54 +01004087 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004088 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004089 const uint32_t dex_method_index_;
4090 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004091
4092 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4093 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004094
4095 private:
4096 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4097};
4098
Vladimir Markofcb503c2016-05-18 12:48:17 +01004099class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004100 public:
4101 HInvokeUnresolved(ArenaAllocator* arena,
4102 uint32_t number_of_arguments,
4103 Primitive::Type return_type,
4104 uint32_t dex_pc,
4105 uint32_t dex_method_index,
4106 InvokeType invoke_type)
4107 : HInvoke(arena,
4108 number_of_arguments,
4109 0u /* number_of_other_inputs */,
4110 return_type,
4111 dex_pc,
4112 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004113 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004114 invoke_type) {
4115 }
4116
4117 DECLARE_INSTRUCTION(InvokeUnresolved);
4118
4119 private:
4120 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4121};
4122
Orion Hodsonac141392017-01-13 11:53:47 +00004123class HInvokePolymorphic FINAL : public HInvoke {
4124 public:
4125 HInvokePolymorphic(ArenaAllocator* arena,
4126 uint32_t number_of_arguments,
4127 Primitive::Type return_type,
4128 uint32_t dex_pc,
4129 uint32_t dex_method_index)
4130 : HInvoke(arena,
4131 number_of_arguments,
4132 0u /* number_of_other_inputs */,
4133 return_type,
4134 dex_pc,
4135 dex_method_index,
4136 nullptr,
4137 kVirtual) {}
4138
4139 DECLARE_INSTRUCTION(InvokePolymorphic);
4140
4141 private:
4142 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4143};
4144
Vladimir Markofcb503c2016-05-18 12:48:17 +01004145class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004146 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004147 // Requirements of this method call regarding the class
4148 // initialization (clinit) check of its declaring class.
4149 enum class ClinitCheckRequirement {
4150 kNone, // Class already initialized.
4151 kExplicit, // Static call having explicit clinit check as last input.
4152 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004153 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004154 };
4155
Vladimir Marko58155012015-08-19 12:49:41 +00004156 // Determines how to load the target ArtMethod*.
4157 enum class MethodLoadKind {
4158 // Use a String init ArtMethod* loaded from Thread entrypoints.
4159 kStringInit,
4160
4161 // Use the method's own ArtMethod* loaded by the register allocator.
4162 kRecursive,
4163
Vladimir Marko65979462017-05-19 17:25:12 +01004164 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4165 // Used for boot image methods referenced by boot image code.
4166 kBootImageLinkTimePcRelative,
4167
Vladimir Marko58155012015-08-19 12:49:41 +00004168 // Use ArtMethod* at a known address, embed the direct address in the code.
4169 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4170 kDirectAddress,
4171
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004172 // Load from an entry in the .bss section using a PC-relative load.
4173 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4174 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004175
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004176 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4177 // used when other kinds are unimplemented on a particular architecture.
4178 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004179 };
4180
4181 // Determines the location of the code pointer.
4182 enum class CodePtrLocation {
4183 // Recursive call, use local PC-relative call instruction.
4184 kCallSelf,
4185
Vladimir Marko58155012015-08-19 12:49:41 +00004186 // Use code pointer from the ArtMethod*.
4187 // Used when we don't know the target code. This is also the last-resort-kind used when
4188 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4189 kCallArtMethod,
4190 };
4191
4192 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004193 MethodLoadKind method_load_kind;
4194 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004195 // The method load data holds
4196 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4197 // Note that there are multiple string init methods, each having its own offset.
4198 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004199 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004200 };
4201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004202 HInvokeStaticOrDirect(ArenaAllocator* arena,
4203 uint32_t number_of_arguments,
4204 Primitive::Type return_type,
4205 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004206 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004207 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004208 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004209 InvokeType invoke_type,
4210 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004211 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004212 : HInvoke(arena,
4213 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004214 // There is potentially one extra argument for the HCurrentMethod node, and
4215 // potentially one other if the clinit check is explicit, and potentially
4216 // one other if the method is a string factory.
4217 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004218 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004219 return_type,
4220 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004221 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004222 resolved_method,
4223 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004224 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004225 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4227 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004228
Vladimir Markodc151b22015-10-15 18:02:30 +01004229 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004230 bool had_current_method_input = HasCurrentMethodInput();
4231 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4232
4233 // Using the current method is the default and once we find a better
4234 // method load kind, we should not go back to using the current method.
4235 DCHECK(had_current_method_input || !needs_current_method_input);
4236
4237 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004238 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4239 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004240 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004241 dispatch_info_ = dispatch_info;
4242 }
4243
Aart Bik6daebeb2017-04-03 14:35:41 -07004244 DispatchInfo GetDispatchInfo() const {
4245 return dispatch_info_;
4246 }
4247
Vladimir Markoc53c0792015-11-19 15:48:33 +00004248 void AddSpecialInput(HInstruction* input) {
4249 // We allow only one special input.
4250 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4251 DCHECK(InputCount() == GetSpecialInputIndex() ||
4252 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4253 InsertInputAt(GetSpecialInputIndex(), input);
4254 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004255
Vladimir Marko372f10e2016-05-17 16:30:10 +01004256 using HInstruction::GetInputRecords; // Keep the const version visible.
4257 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4258 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4259 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4260 DCHECK(!input_records.empty());
4261 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4262 HInstruction* last_input = input_records.back().GetInstruction();
4263 // Note: `last_input` may be null during arguments setup.
4264 if (last_input != nullptr) {
4265 // `last_input` is the last input of a static invoke marked as having
4266 // an explicit clinit check. It must either be:
4267 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4268 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4269 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4270 }
4271 }
4272 return input_records;
4273 }
4274
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004275 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 // We access the method via the dex cache so we can't do an implicit null check.
4277 // TODO: for intrinsics we can generate implicit null checks.
4278 return false;
4279 }
4280
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004281 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004282 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004283 }
4284
Vladimir Markoc53c0792015-11-19 15:48:33 +00004285 // Get the index of the special input, if any.
4286 //
David Brazdil6de19382016-01-08 17:37:10 +00004287 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4288 // method pointer; otherwise there may be one platform-specific special input,
4289 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004291 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004292
Vladimir Marko58155012015-08-19 12:49:41 +00004293 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4294 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4295 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004296 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004297 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004298 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004299 bool HasPcRelativeMethodLoadKind() const {
4300 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004301 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004302 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004303 bool HasCurrentMethodInput() const {
4304 // This function can be called only after the invoke has been fully initialized by the builder.
4305 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004306 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004307 return true;
4308 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004309 DCHECK(InputCount() == GetSpecialInputIndex() ||
4310 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004311 return false;
4312 }
4313 }
Vladimir Marko58155012015-08-19 12:49:41 +00004314
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004315 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004316 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004317 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004318 }
4319
4320 uint64_t GetMethodAddress() const {
4321 DCHECK(HasMethodAddress());
4322 return dispatch_info_.method_load_data;
4323 }
4324
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004325 const DexFile& GetDexFileForPcRelativeDexCache() const;
4326
Vladimir Markoa1de9182016-02-25 11:37:38 +00004327 ClinitCheckRequirement GetClinitCheckRequirement() const {
4328 return GetPackedField<ClinitCheckRequirementField>();
4329 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004330
Roland Levillain4c0eb422015-04-24 16:43:49 +01004331 // Is this instruction a call to a static method?
4332 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004333 return GetInvokeType() == kStatic;
4334 }
4335
4336 MethodReference GetTargetMethod() const {
4337 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004338 }
4339
Vladimir Markofbb184a2015-11-13 14:47:00 +00004340 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4341 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4342 // instruction; only relevant for static calls with explicit clinit check.
4343 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004344 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004345 size_t last_input_index = inputs_.size() - 1u;
4346 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004347 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004348 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004349 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004350 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004351 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004352 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004353 }
4354
4355 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004356 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004357 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004358 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004359 }
4360
4361 // Is this a call to a static method whose declaring class has an
4362 // implicit intialization check requirement?
4363 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004364 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004365 }
4366
Vladimir Markob554b5a2015-11-06 12:57:55 +00004367 // Does this method load kind need the current method as an input?
4368 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004369 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004370 }
4371
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004372 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004373
4374 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004375 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004376 static constexpr size_t kFieldClinitCheckRequirementSize =
4377 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4378 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4379 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4380 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4381 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004382 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4383 kFieldClinitCheckRequirement,
4384 kFieldClinitCheckRequirementSize>;
4385
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004386 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004387 MethodReference target_method_;
4388 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004389
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004390 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004391};
Vladimir Markof64242a2015-12-01 14:58:23 +00004392std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004393std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004394
Vladimir Markofcb503c2016-05-18 12:48:17 +01004395class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004396 public:
4397 HInvokeVirtual(ArenaAllocator* arena,
4398 uint32_t number_of_arguments,
4399 Primitive::Type return_type,
4400 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004401 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004402 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004403 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004404 : HInvoke(arena,
4405 number_of_arguments,
4406 0u,
4407 return_type,
4408 dex_pc,
4409 dex_method_index,
4410 resolved_method,
4411 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004412 vtable_index_(vtable_index) {}
4413
Aart Bik71bf7b42016-11-16 10:17:46 -08004414 bool CanBeNull() const OVERRIDE {
4415 switch (GetIntrinsic()) {
4416 case Intrinsics::kThreadCurrentThread:
4417 case Intrinsics::kStringBufferAppend:
4418 case Intrinsics::kStringBufferToString:
4419 case Intrinsics::kStringBuilderAppend:
4420 case Intrinsics::kStringBuilderToString:
4421 return false;
4422 default:
4423 return HInvoke::CanBeNull();
4424 }
4425 }
4426
Calin Juravle641547a2015-04-21 22:08:51 +01004427 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004428 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004429 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004430 }
4431
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004432 uint32_t GetVTableIndex() const { return vtable_index_; }
4433
4434 DECLARE_INSTRUCTION(InvokeVirtual);
4435
4436 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004437 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004438 const uint32_t vtable_index_;
4439
4440 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4441};
4442
Vladimir Markofcb503c2016-05-18 12:48:17 +01004443class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004444 public:
4445 HInvokeInterface(ArenaAllocator* arena,
4446 uint32_t number_of_arguments,
4447 Primitive::Type return_type,
4448 uint32_t dex_pc,
4449 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004450 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004451 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004452 : HInvoke(arena,
4453 number_of_arguments,
4454 0u,
4455 return_type,
4456 dex_pc,
4457 dex_method_index,
4458 resolved_method,
4459 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004460 imt_index_(imt_index) {}
4461
Calin Juravle641547a2015-04-21 22:08:51 +01004462 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004463 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004464 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004465 }
4466
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004467 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4468 // The assembly stub currently needs it.
4469 return true;
4470 }
4471
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004472 uint32_t GetImtIndex() const { return imt_index_; }
4473 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4474
4475 DECLARE_INSTRUCTION(InvokeInterface);
4476
4477 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004478 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004479 const uint32_t imt_index_;
4480
4481 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4482};
4483
Vladimir Markofcb503c2016-05-18 12:48:17 +01004484class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004487 : HUnaryOperation(result_type, input, dex_pc) {
4488 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4489 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004490
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004491 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004492
4493 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
4496 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004497 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004498 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004499 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4500 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4501 }
4502 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4503 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4504 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004505
Roland Levillain88cb1752014-10-20 16:36:47 +01004506 DECLARE_INSTRUCTION(Neg);
4507
4508 private:
4509 DISALLOW_COPY_AND_ASSIGN(HNeg);
4510};
4511
Vladimir Markofcb503c2016-05-18 12:48:17 +01004512class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004513 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004514 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4515 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4516 SetRawInputAt(0, cls);
4517 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004518 }
4519
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004520 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004521 bool NeedsEnvironment() const OVERRIDE { return true; }
4522
Mingyao Yang0c365e62015-03-31 15:09:29 -07004523 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4524 bool CanThrow() const OVERRIDE { return true; }
4525
Calin Juravle10e244f2015-01-26 18:54:32 +00004526 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004527
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004528 HLoadClass* GetLoadClass() const {
4529 DCHECK(InputAt(0)->IsLoadClass());
4530 return InputAt(0)->AsLoadClass();
4531 }
4532
4533 HInstruction* GetLength() const {
4534 return InputAt(1);
4535 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004536
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004537 DECLARE_INSTRUCTION(NewArray);
4538
4539 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004540 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4541};
4542
Vladimir Markofcb503c2016-05-18 12:48:17 +01004543class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 HAdd(Primitive::Type result_type,
4546 HInstruction* left,
4547 HInstruction* right,
4548 uint32_t dex_pc = kNoDexPc)
4549 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004550
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004551 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004552
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004553 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004554
4555 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004556 return GetBlock()->GetGraph()->GetIntConstant(
4557 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004558 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004559 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 return GetBlock()->GetGraph()->GetLongConstant(
4561 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004562 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004563 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4564 return GetBlock()->GetGraph()->GetFloatConstant(
4565 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4566 }
4567 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4568 return GetBlock()->GetGraph()->GetDoubleConstant(
4569 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4570 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004571
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004572 DECLARE_INSTRUCTION(Add);
4573
4574 private:
4575 DISALLOW_COPY_AND_ASSIGN(HAdd);
4576};
4577
Vladimir Markofcb503c2016-05-18 12:48:17 +01004578class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004579 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004580 HSub(Primitive::Type result_type,
4581 HInstruction* left,
4582 HInstruction* right,
4583 uint32_t dex_pc = kNoDexPc)
4584 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004585
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004586 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004587
4588 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004589 return GetBlock()->GetGraph()->GetIntConstant(
4590 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004591 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004592 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004593 return GetBlock()->GetGraph()->GetLongConstant(
4594 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004595 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004596 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4597 return GetBlock()->GetGraph()->GetFloatConstant(
4598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4599 }
4600 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4601 return GetBlock()->GetGraph()->GetDoubleConstant(
4602 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4603 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004604
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004605 DECLARE_INSTRUCTION(Sub);
4606
4607 private:
4608 DISALLOW_COPY_AND_ASSIGN(HSub);
4609};
4610
Vladimir Markofcb503c2016-05-18 12:48:17 +01004611class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 HMul(Primitive::Type result_type,
4614 HInstruction* left,
4615 HInstruction* right,
4616 uint32_t dex_pc = kNoDexPc)
4617 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004618
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004619 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004620
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004621 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004622
4623 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004624 return GetBlock()->GetGraph()->GetIntConstant(
4625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004626 }
4627 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004628 return GetBlock()->GetGraph()->GetLongConstant(
4629 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004630 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004631 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4632 return GetBlock()->GetGraph()->GetFloatConstant(
4633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4634 }
4635 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4636 return GetBlock()->GetGraph()->GetDoubleConstant(
4637 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4638 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004639
4640 DECLARE_INSTRUCTION(Mul);
4641
4642 private:
4643 DISALLOW_COPY_AND_ASSIGN(HMul);
4644};
4645
Vladimir Markofcb503c2016-05-18 12:48:17 +01004646class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004647 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004648 HDiv(Primitive::Type result_type,
4649 HInstruction* left,
4650 HInstruction* right,
4651 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004652 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004653
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 T ComputeIntegral(T x, T y) const {
4656 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004657 // Our graph structure ensures we never have 0 for `y` during
4658 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004659 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004660 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004661 return (y == -1) ? -x : x / y;
4662 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004663
Roland Levillain31dd3d62016-02-16 12:21:02 +00004664 template <typename T>
4665 T ComputeFP(T x, T y) const {
4666 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4667 return x / y;
4668 }
4669
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
4674 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004676 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4677 }
4678 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4679 return GetBlock()->GetGraph()->GetFloatConstant(
4680 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4681 }
4682 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4683 return GetBlock()->GetGraph()->GetDoubleConstant(
4684 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004685 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004686
4687 DECLARE_INSTRUCTION(Div);
4688
4689 private:
4690 DISALLOW_COPY_AND_ASSIGN(HDiv);
4691};
4692
Vladimir Markofcb503c2016-05-18 12:48:17 +01004693class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004694 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004695 HRem(Primitive::Type result_type,
4696 HInstruction* left,
4697 HInstruction* right,
4698 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004699 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004700
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004702 T ComputeIntegral(T x, T y) const {
4703 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004704 // Our graph structure ensures we never have 0 for `y` during
4705 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004706 DCHECK_NE(y, 0);
4707 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4708 return (y == -1) ? 0 : x % y;
4709 }
4710
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 template <typename T>
4712 T ComputeFP(T x, T y) const {
4713 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4714 return std::fmod(x, y);
4715 }
4716
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004719 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004720 }
4721 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004723 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004724 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004725 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4726 return GetBlock()->GetGraph()->GetFloatConstant(
4727 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4728 }
4729 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4730 return GetBlock()->GetGraph()->GetDoubleConstant(
4731 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4732 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004733
4734 DECLARE_INSTRUCTION(Rem);
4735
4736 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004737 DISALLOW_COPY_AND_ASSIGN(HRem);
4738};
4739
Vladimir Markofcb503c2016-05-18 12:48:17 +01004740class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004741 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004742 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4743 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004744 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004745 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004746 SetRawInputAt(0, value);
4747 }
4748
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004749 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4750
Calin Juravled0d48522014-11-04 16:40:20 +00004751 bool CanBeMoved() const OVERRIDE { return true; }
4752
Vladimir Marko372f10e2016-05-17 16:30:10 +01004753 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004754 return true;
4755 }
4756
4757 bool NeedsEnvironment() const OVERRIDE { return true; }
4758 bool CanThrow() const OVERRIDE { return true; }
4759
Calin Juravled0d48522014-11-04 16:40:20 +00004760 DECLARE_INSTRUCTION(DivZeroCheck);
4761
4762 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004763 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4764};
4765
Vladimir Markofcb503c2016-05-18 12:48:17 +01004766class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HInstruction* value,
4770 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4773 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4774 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 }
4776
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004778 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 return value << (distance & max_shift_distance);
4780 }
4781
4782 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004785 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004789 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4791 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4792 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4793 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004794 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004795 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4796 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004797 LOG(FATAL) << DebugName() << " is not defined for float values";
4798 UNREACHABLE();
4799 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4801 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004802 LOG(FATAL) << DebugName() << " is not defined for double values";
4803 UNREACHABLE();
4804 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004805
4806 DECLARE_INSTRUCTION(Shl);
4807
4808 private:
4809 DISALLOW_COPY_AND_ASSIGN(HShl);
4810};
4811
Vladimir Markofcb503c2016-05-18 12:48:17 +01004812class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 HInstruction* value,
4816 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4819 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4820 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 }
4822
Roland Levillain5b5b9312016-03-22 14:57:31 +00004823 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004824 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 return value >> (distance & max_shift_distance);
4826 }
4827
4828 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004832 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004835 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004836 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4837 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4838 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4839 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004840 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004841 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4842 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004843 LOG(FATAL) << DebugName() << " is not defined for float values";
4844 UNREACHABLE();
4845 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004846 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4847 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004848 LOG(FATAL) << DebugName() << " is not defined for double values";
4849 UNREACHABLE();
4850 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004851
4852 DECLARE_INSTRUCTION(Shr);
4853
4854 private:
4855 DISALLOW_COPY_AND_ASSIGN(HShr);
4856};
4857
Vladimir Markofcb503c2016-05-18 12:48:17 +01004858class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004859 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004860 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 HInstruction* value,
4862 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4865 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4866 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004867 }
4868
Roland Levillain5b5b9312016-03-22 14:57:31 +00004869 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004870 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 typedef typename std::make_unsigned<T>::type V;
4872 V ux = static_cast<V>(value);
4873 return static_cast<T>(ux >> (distance & max_shift_distance));
4874 }
4875
4876 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004881 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004884 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4885 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4886 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4887 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004888 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004889 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4890 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004891 LOG(FATAL) << DebugName() << " is not defined for float values";
4892 UNREACHABLE();
4893 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4895 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004896 LOG(FATAL) << DebugName() << " is not defined for double values";
4897 UNREACHABLE();
4898 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004899
4900 DECLARE_INSTRUCTION(UShr);
4901
4902 private:
4903 DISALLOW_COPY_AND_ASSIGN(HUShr);
4904};
4905
Vladimir Markofcb503c2016-05-18 12:48:17 +01004906class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 HAnd(Primitive::Type result_type,
4909 HInstruction* left,
4910 HInstruction* right,
4911 uint32_t dex_pc = kNoDexPc)
4912 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004913
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004914 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004915
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004916 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004917
4918 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004919 return GetBlock()->GetGraph()->GetIntConstant(
4920 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004921 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004922 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004923 return GetBlock()->GetGraph()->GetLongConstant(
4924 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004925 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004926 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4927 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4928 LOG(FATAL) << DebugName() << " is not defined for float values";
4929 UNREACHABLE();
4930 }
4931 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4932 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4933 LOG(FATAL) << DebugName() << " is not defined for double values";
4934 UNREACHABLE();
4935 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004936
4937 DECLARE_INSTRUCTION(And);
4938
4939 private:
4940 DISALLOW_COPY_AND_ASSIGN(HAnd);
4941};
4942
Vladimir Markofcb503c2016-05-18 12:48:17 +01004943class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004944 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 HOr(Primitive::Type result_type,
4946 HInstruction* left,
4947 HInstruction* right,
4948 uint32_t dex_pc = kNoDexPc)
4949 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004950
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004951 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004953 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004954
4955 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 return GetBlock()->GetGraph()->GetIntConstant(
4957 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004958 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004959 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004960 return GetBlock()->GetGraph()->GetLongConstant(
4961 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004962 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004963 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4964 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4965 LOG(FATAL) << DebugName() << " is not defined for float values";
4966 UNREACHABLE();
4967 }
4968 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4969 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4970 LOG(FATAL) << DebugName() << " is not defined for double values";
4971 UNREACHABLE();
4972 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004973
4974 DECLARE_INSTRUCTION(Or);
4975
4976 private:
4977 DISALLOW_COPY_AND_ASSIGN(HOr);
4978};
4979
Vladimir Markofcb503c2016-05-18 12:48:17 +01004980class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004981 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004982 HXor(Primitive::Type result_type,
4983 HInstruction* left,
4984 HInstruction* right,
4985 uint32_t dex_pc = kNoDexPc)
4986 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004987
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004988 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004989
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004990 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004991
4992 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 return GetBlock()->GetGraph()->GetIntConstant(
4994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 return GetBlock()->GetGraph()->GetLongConstant(
4998 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004999 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005000 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5001 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5002 LOG(FATAL) << DebugName() << " is not defined for float values";
5003 UNREACHABLE();
5004 }
5005 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5006 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5007 LOG(FATAL) << DebugName() << " is not defined for double values";
5008 UNREACHABLE();
5009 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005010
5011 DECLARE_INSTRUCTION(Xor);
5012
5013 private:
5014 DISALLOW_COPY_AND_ASSIGN(HXor);
5015};
5016
Vladimir Markofcb503c2016-05-18 12:48:17 +01005017class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005018 public:
5019 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005020 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5022 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005023 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005024
Roland Levillain5b5b9312016-03-22 14:57:31 +00005025 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005026 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005027 typedef typename std::make_unsigned<T>::type V;
5028 V ux = static_cast<V>(value);
5029 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005030 return static_cast<T>(ux);
5031 } else {
5032 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005033 return static_cast<T>(ux >> (distance & max_shift_value)) |
5034 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005035 }
5036 }
5037
Roland Levillain5b5b9312016-03-22 14:57:31 +00005038 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005039 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005040 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005041 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005042 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005043 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005044 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005045 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005046 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5047 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5048 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5049 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005050 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005051 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5052 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005053 LOG(FATAL) << DebugName() << " is not defined for float values";
5054 UNREACHABLE();
5055 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005056 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5057 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005058 LOG(FATAL) << DebugName() << " is not defined for double values";
5059 UNREACHABLE();
5060 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005061
5062 DECLARE_INSTRUCTION(Ror);
5063
5064 private:
5065 DISALLOW_COPY_AND_ASSIGN(HRor);
5066};
5067
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005068// The value of a parameter in this method. Its location depends on
5069// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005070class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005071 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005072 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005073 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005074 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005075 Primitive::Type parameter_type,
5076 bool is_this = false)
5077 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005078 dex_file_(dex_file),
5079 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005080 index_(index) {
5081 SetPackedFlag<kFlagIsThis>(is_this);
5082 SetPackedFlag<kFlagCanBeNull>(!is_this);
5083 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005084
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005085 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005086 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005087 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005088 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005089
Vladimir Markoa1de9182016-02-25 11:37:38 +00005090 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5091 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005092
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005093 DECLARE_INSTRUCTION(ParameterValue);
5094
5095 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005096 // Whether or not the parameter value corresponds to 'this' argument.
5097 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5098 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5099 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5100 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5101 "Too many packed fields.");
5102
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005103 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005104 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005105 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005106 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005107 const uint8_t index_;
5108
5109 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5110};
5111
Vladimir Markofcb503c2016-05-18 12:48:17 +01005112class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005113 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005114 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5115 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005116
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005117 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005118 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005119 return true;
5120 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005121
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005122 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005123
5124 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005126 }
5127 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005128 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005129 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005130 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5131 LOG(FATAL) << DebugName() << " is not defined for float values";
5132 UNREACHABLE();
5133 }
5134 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5135 LOG(FATAL) << DebugName() << " is not defined for double values";
5136 UNREACHABLE();
5137 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005138
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005139 DECLARE_INSTRUCTION(Not);
5140
5141 private:
5142 DISALLOW_COPY_AND_ASSIGN(HNot);
5143};
5144
Vladimir Markofcb503c2016-05-18 12:48:17 +01005145class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005146 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005147 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5148 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005149
5150 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005151 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005152 return true;
5153 }
5154
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005155 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005156 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005157 return !x;
5158 }
5159
Roland Levillain9867bc72015-08-05 10:21:34 +01005160 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005161 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005162 }
5163 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5164 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005165 UNREACHABLE();
5166 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005167 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5168 LOG(FATAL) << DebugName() << " is not defined for float values";
5169 UNREACHABLE();
5170 }
5171 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5172 LOG(FATAL) << DebugName() << " is not defined for double values";
5173 UNREACHABLE();
5174 }
David Brazdil66d126e2015-04-03 16:02:44 +01005175
5176 DECLARE_INSTRUCTION(BooleanNot);
5177
5178 private:
5179 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5180};
5181
Vladimir Markofcb503c2016-05-18 12:48:17 +01005182class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005183 public:
5184 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005185 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005186 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005187 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005188 // Invariant: We should never generate a conversion to a Boolean value.
5189 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005190 }
5191
5192 HInstruction* GetInput() const { return InputAt(0); }
5193 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5194 Primitive::Type GetResultType() const { return GetType(); }
5195
5196 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005197 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5198 return true;
5199 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005200
Mark Mendelle82549b2015-05-06 10:55:34 -04005201 // Try to statically evaluate the conversion and return a HConstant
5202 // containing the result. If the input cannot be converted, return nullptr.
5203 HConstant* TryStaticEvaluation() const;
5204
Roland Levillaindff1f282014-11-05 14:15:05 +00005205 DECLARE_INSTRUCTION(TypeConversion);
5206
5207 private:
5208 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5209};
5210
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005211static constexpr uint32_t kNoRegNumber = -1;
5212
Vladimir Markofcb503c2016-05-18 12:48:17 +01005213class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005214 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005215 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5216 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005218 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005219 SetRawInputAt(0, value);
5220 }
5221
Calin Juravle10e244f2015-01-26 18:54:32 +00005222 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005223 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005224 return true;
5225 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005226
Calin Juravle10e244f2015-01-26 18:54:32 +00005227 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005228
Calin Juravle10e244f2015-01-26 18:54:32 +00005229 bool CanThrow() const OVERRIDE { return true; }
5230
5231 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005233
5234 DECLARE_INSTRUCTION(NullCheck);
5235
5236 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005237 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5238};
5239
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005240// Embeds an ArtField and all the information required by the compiler. We cache
5241// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005242class FieldInfo : public ValueObject {
5243 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005244 FieldInfo(ArtField* field,
5245 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005246 Primitive::Type field_type,
5247 bool is_volatile,
5248 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005249 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005250 const DexFile& dex_file)
5251 : field_(field),
5252 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005253 field_type_(field_type),
5254 is_volatile_(is_volatile),
5255 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005256 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005257 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005259 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005260 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005261 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005262 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005263 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005264 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005265 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266
5267 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005268 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005269 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005270 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005271 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005272 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005273 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005274 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005275};
5276
Vladimir Markofcb503c2016-05-18 12:48:17 +01005277class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005278 public:
5279 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005280 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005281 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005282 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005283 bool is_volatile,
5284 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005285 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005286 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005287 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005288 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005289 field_info_(field,
5290 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005291 field_type,
5292 is_volatile,
5293 field_idx,
5294 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005295 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005296 SetRawInputAt(0, value);
5297 }
5298
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005299 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005300
Vladimir Marko372f10e2016-05-17 16:30:10 +01005301 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5302 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005303 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005304 }
5305
Calin Juravle641547a2015-04-21 22:08:51 +01005306 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005307 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005308 }
5309
5310 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005311 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5312 }
5313
Calin Juravle52c48962014-12-16 17:02:57 +00005314 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005315 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005316 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005317 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005318
5319 DECLARE_INSTRUCTION(InstanceFieldGet);
5320
5321 private:
5322 const FieldInfo field_info_;
5323
5324 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5325};
5326
Vladimir Markofcb503c2016-05-18 12:48:17 +01005327class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005328 public:
5329 HInstanceFieldSet(HInstruction* object,
5330 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005331 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005332 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005333 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005334 bool is_volatile,
5335 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005336 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005337 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005338 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005339 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005340 field_info_(field,
5341 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005342 field_type,
5343 is_volatile,
5344 field_idx,
5345 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005346 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005348 SetRawInputAt(0, object);
5349 SetRawInputAt(1, value);
5350 }
5351
Calin Juravle641547a2015-04-21 22:08:51 +01005352 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005353 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005354 }
5355
Calin Juravle52c48962014-12-16 17:02:57 +00005356 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005357 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005358 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005359 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005360 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005361 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5362 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005363
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005364 DECLARE_INSTRUCTION(InstanceFieldSet);
5365
5366 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005367 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5368 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5369 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5370 "Too many packed fields.");
5371
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 const FieldInfo field_info_;
5373
5374 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5375};
5376
Vladimir Markofcb503c2016-05-18 12:48:17 +01005377class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005379 HArrayGet(HInstruction* array,
5380 HInstruction* index,
5381 Primitive::Type type,
5382 uint32_t dex_pc,
5383 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005384 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005385 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 SetRawInputAt(0, array);
5387 SetRawInputAt(1, index);
5388 }
5389
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005390 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005391 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005392 return true;
5393 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005394 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005396 // Currently, unless the array is the result of NewArray, the array access is always
5397 // preceded by some form of null NullCheck necessary for the bounds check, usually
5398 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5399 // dynamic BCE. There are cases when these could be removed to produce better code.
5400 // If we ever add optimizations to do so we should allow an implicit check here
5401 // (as long as the address falls in the first page).
5402 //
5403 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5404 // a = cond ? new int[1] : null;
5405 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005406 return false;
5407 }
5408
David Brazdil4833f5a2015-12-16 10:37:39 +00005409 bool IsEquivalentOf(HArrayGet* other) const {
5410 bool result = (GetDexPc() == other->GetDexPc());
5411 if (kIsDebugBuild && result) {
5412 DCHECK_EQ(GetBlock(), other->GetBlock());
5413 DCHECK_EQ(GetArray(), other->GetArray());
5414 DCHECK_EQ(GetIndex(), other->GetIndex());
5415 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005416 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005417 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005418 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5419 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005420 }
5421 }
5422 return result;
5423 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005424
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005425 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5426
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005427 HInstruction* GetArray() const { return InputAt(0); }
5428 HInstruction* GetIndex() const { return InputAt(1); }
5429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 DECLARE_INSTRUCTION(ArrayGet);
5431
5432 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005433 // We treat a String as an array, creating the HArrayGet from String.charAt()
5434 // intrinsic in the instruction simplifier. We can always determine whether
5435 // a particular HArrayGet is actually a String.charAt() by looking at the type
5436 // of the input but that requires holding the mutator lock, so we prefer to use
5437 // a flag, so that code generators don't need to do the locking.
5438 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5439 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5440 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5441 "Too many packed fields.");
5442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5444};
5445
Vladimir Markofcb503c2016-05-18 12:48:17 +01005446class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 public:
5448 HArraySet(HInstruction* array,
5449 HInstruction* index,
5450 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005451 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005452 uint32_t dex_pc)
5453 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005454 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5455 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5456 SetPackedFlag<kFlagValueCanBeNull>(true);
5457 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005458 SetRawInputAt(0, array);
5459 SetRawInputAt(1, index);
5460 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005461 // Make a best guess now, may be refined during SSA building.
5462 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005463 }
5464
Calin Juravle77520bc2015-01-12 18:45:46 +00005465 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005466 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005468 }
5469
Mingyao Yang81014cb2015-06-02 03:16:27 -07005470 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005473 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005474 // TODO: Same as for ArrayGet.
5475 return false;
5476 }
5477
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005478 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005479 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005480 }
5481
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005482 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005483 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005484 }
5485
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005487 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 }
5489
Vladimir Markoa1de9182016-02-25 11:37:38 +00005490 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5491 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5492 bool StaticTypeOfArrayIsObjectArray() const {
5493 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5494 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005495
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005496 HInstruction* GetArray() const { return InputAt(0); }
5497 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005498 HInstruction* GetValue() const { return InputAt(2); }
5499
5500 Primitive::Type GetComponentType() const {
5501 // The Dex format does not type floating point index operations. Since the
5502 // `expected_component_type_` is set during building and can therefore not
5503 // be correct, we also check what is the value type. If it is a floating
5504 // point type, we must use that type.
5505 Primitive::Type value_type = GetValue()->GetType();
5506 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5507 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005508 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005509 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005510
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005511 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005512 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005513 }
5514
Aart Bik18b36ab2016-04-13 16:41:35 -07005515 void ComputeSideEffects() {
5516 Primitive::Type type = GetComponentType();
5517 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5518 SideEffectsForArchRuntimeCalls(type)));
5519 }
5520
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005521 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5522 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5523 }
5524
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525 DECLARE_INSTRUCTION(ArraySet);
5526
5527 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005528 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5529 static constexpr size_t kFieldExpectedComponentTypeSize =
5530 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5531 static constexpr size_t kFlagNeedsTypeCheck =
5532 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5533 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005534 // Cached information for the reference_type_info_ so that codegen
5535 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5537 static constexpr size_t kNumberOfArraySetPackedBits =
5538 kFlagStaticTypeOfArrayIsObjectArray + 1;
5539 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5540 using ExpectedComponentTypeField =
5541 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005542
5543 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5544};
5545
Vladimir Markofcb503c2016-05-18 12:48:17 +01005546class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005548 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005549 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005550 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005551 // Note that arrays do not change length, so the instruction does not
5552 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 SetRawInputAt(0, array);
5554 }
5555
Calin Juravle77520bc2015-01-12 18:45:46 +00005556 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005557 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005558 return true;
5559 }
Calin Juravle641547a2015-04-21 22:08:51 +01005560 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5561 return obj == InputAt(0);
5562 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005563
Vladimir Markodce016e2016-04-28 13:10:02 +01005564 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5565
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566 DECLARE_INSTRUCTION(ArrayLength);
5567
5568 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005569 // We treat a String as an array, creating the HArrayLength from String.length()
5570 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5571 // determine whether a particular HArrayLength is actually a String.length() by
5572 // looking at the type of the input but that requires holding the mutator lock, so
5573 // we prefer to use a flag, so that code generators don't need to do the locking.
5574 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5575 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5576 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5577 "Too many packed fields.");
5578
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5580};
5581
Vladimir Markofcb503c2016-05-18 12:48:17 +01005582class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005584 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5585 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005586 HBoundsCheck(HInstruction* index,
5587 HInstruction* length,
5588 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005589 bool string_char_at = false)
5590 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005591 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005592 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 SetRawInputAt(0, index);
5594 SetRawInputAt(1, length);
5595 }
5596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005597 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005598 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005599 return true;
5600 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005602 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005604 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005605
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005606 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005607
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005608 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005609
5610 DECLARE_INSTRUCTION(BoundsCheck);
5611
5612 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005613 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005614
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005615 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5616};
5617
Vladimir Markofcb503c2016-05-18 12:48:17 +01005618class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005619 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005620 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005621 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005622
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005623 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005624 return true;
5625 }
5626
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005627 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5628 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005629
5630 DECLARE_INSTRUCTION(SuspendCheck);
5631
5632 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005633 // Only used for code generation, in order to share the same slow path between back edges
5634 // of a same loop.
5635 SlowPathCode* slow_path_;
5636
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005637 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5638};
5639
David Srbecky0cf44932015-12-09 14:09:59 +00005640// Pseudo-instruction which provides the native debugger with mapping information.
5641// It ensures that we can generate line number and local variables at this point.
5642class HNativeDebugInfo : public HTemplateInstruction<0> {
5643 public:
5644 explicit HNativeDebugInfo(uint32_t dex_pc)
5645 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5646
5647 bool NeedsEnvironment() const OVERRIDE {
5648 return true;
5649 }
5650
5651 DECLARE_INSTRUCTION(NativeDebugInfo);
5652
5653 private:
5654 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5655};
5656
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657/**
5658 * Instruction to load a Class object.
5659 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005660class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005661 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 // Determines how to load the Class.
5663 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005664 // We cannot load this class. See HSharpening::SharpenLoadClass.
5665 kInvalid = -1,
5666
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 // Use the Class* from the method's own ArtMethod*.
5668 kReferrersClass,
5669
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005671 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672 kBootImageLinkTimePcRelative,
5673
5674 // Use a known boot image Class* address, embedded in the code by the codegen.
5675 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005676 kBootImageAddress,
5677
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005678 // Load from an entry in the .bss section using a PC-relative load.
5679 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5680 kBssEntry,
5681
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005682 // Load from the root table associated with the JIT compiled method.
5683 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005685 // Load using a simple runtime call. This is the fall-back load kind when
5686 // the codegen is unable to use another appropriate kind.
5687 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005688
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005689 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690 };
5691
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005692 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005693 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005694 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005695 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005696 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005697 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005698 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005699 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5700 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005701 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005702 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005703 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005704 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005705 // Referrers class should not need access check. We never inline unverified
5706 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005707 DCHECK(!is_referrers_class || !needs_access_check);
5708
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005709 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005710 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005712 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005713 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005714 }
5715
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005716 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005717
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005718 LoadKind GetLoadKind() const {
5719 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005720 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005721
5722 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725
Andreas Gampea5b09a62016-11-17 15:21:22 -08005726 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005728 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005729
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005730 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005731 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732 }
5733
Calin Juravle0ba218d2015-05-19 18:46:01 +01005734 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005735 // The entrypoint the code generator is going to call does not do
5736 // clinit of the class.
5737 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005738 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005739 }
5740
5741 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005742 return NeedsAccessCheck() ||
5743 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005744 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005745 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005746 }
5747
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005748 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005749 return NeedsAccessCheck() ||
5750 MustGenerateClinitCheck() ||
5751 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5752 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5753 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005754 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005755 GetLoadKind() == LoadKind::kBssEntry) &&
5756 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005757 }
5758
Calin Juravleacf735c2015-02-12 15:25:22 +00005759 ReferenceTypeInfo GetLoadedClassRTI() {
5760 return loaded_class_rti_;
5761 }
5762
5763 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5764 // Make sure we only set exact types (the loaded class should never be merged).
5765 DCHECK(rti.IsExact());
5766 loaded_class_rti_ = rti;
5767 }
5768
Andreas Gampea5b09a62016-11-17 15:21:22 -08005769 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005770 const DexFile& GetDexFile() const { return dex_file_; }
5771
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005772 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005773 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005774 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005775
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005776 static SideEffects SideEffectsForArchRuntimeCalls() {
5777 return SideEffects::CanTriggerGC();
5778 }
5779
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005780 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005781 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005782 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005783 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005784
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005785 void MarkInBootImage() {
5786 SetPackedFlag<kFlagIsInBootImage>(true);
5787 }
5788
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 void AddSpecialInput(HInstruction* special_input);
5790
5791 using HInstruction::GetInputRecords; // Keep the const version visible.
5792 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5793 return ArrayRef<HUserRecord<HInstruction*>>(
5794 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5795 }
5796
5797 Primitive::Type GetType() const OVERRIDE {
5798 return Primitive::kPrimNot;
5799 }
5800
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005801 Handle<mirror::Class> GetClass() const {
5802 return klass_;
5803 }
5804
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005805 DECLARE_INSTRUCTION(LoadClass);
5806
5807 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005809 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005810 // Whether this instruction must generate the initialization check.
5811 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005812 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005813 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5814 static constexpr size_t kFieldLoadKindSize =
5815 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5816 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5819
5820 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005821 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005822 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005823 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005824 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825 }
5826
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 void SetLoadKindInternal(LoadKind load_kind);
5828
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005829 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005831 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005833
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005834 // A type index and dex file where the class can be accessed. The dex file can be:
5835 // - The compiling method's dex file if the class is defined there too.
5836 // - The compiling method's dex file if the class is referenced there.
5837 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005838 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005839 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005840 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005841
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005842 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005843
Calin Juravleacf735c2015-02-12 15:25:22 +00005844 ReferenceTypeInfo loaded_class_rti_;
5845
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5847};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005848std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5849
5850// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005851inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005852 // The special input is used for PC-relative loads on some architectures,
5853 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005855 GetLoadKind() == LoadKind::kBootImageAddress ||
5856 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005857 DCHECK(special_input_.GetInstruction() == nullptr);
5858 special_input_ = HUserRecord<HInstruction*>(special_input);
5859 special_input->AddUseAt(this, 0);
5860}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005861
Vladimir Marko372f10e2016-05-17 16:30:10 +01005862class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005863 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005864 // Determines how to load the String.
5865 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005867 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005868 kBootImageLinkTimePcRelative,
5869
5870 // Use a known boot image String* address, embedded in the code by the codegen.
5871 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 kBootImageAddress,
5873
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005874 // Use a PC-relative load from a boot image InternTable mmapped into the .bss
5875 // of the oat file.
5876 kBootImageInternTable,
5877
Vladimir Markoaad75c62016-10-03 08:46:48 +00005878 // Load from an entry in the .bss section using a PC-relative load.
5879 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5880 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005881
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005882 // Load from the root table associated with the JIT compiled method.
5883 kJitTableAddress,
5884
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005885 // Load using a simple runtime call. This is the fall-back load kind when
5886 // the codegen is unable to use another appropriate kind.
5887 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005889 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 };
5891
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005892 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005893 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 const DexFile& dex_file,
5895 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005896 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5897 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005898 string_index_(string_index),
5899 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005900 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005901 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005902
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005903 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005905 LoadKind GetLoadKind() const {
5906 return GetPackedField<LoadKindField>();
5907 }
5908
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005909 const DexFile& GetDexFile() const {
5910 return dex_file_;
5911 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005912
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005913 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914 return string_index_;
5915 }
5916
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005917 Handle<mirror::String> GetString() const {
5918 return string_;
5919 }
5920
5921 void SetString(Handle<mirror::String> str) {
5922 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005923 }
5924
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005925 bool CanBeMoved() const OVERRIDE { return true; }
5926
Vladimir Marko372f10e2016-05-17 16:30:10 +01005927 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005928
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005929 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005930
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005931 // Will call the runtime if we need to load the string through
5932 // the dex cache and the string is not guaranteed to be there yet.
5933 bool NeedsEnvironment() const OVERRIDE {
5934 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005935 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005936 load_kind == LoadKind::kBootImageAddress ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005937 load_kind == LoadKind::kBootImageInternTable ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005938 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005939 return false;
5940 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005941 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005943
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005944 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005945 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005946 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005947
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005948 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005950
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005951 static SideEffects SideEffectsForArchRuntimeCalls() {
5952 return SideEffects::CanTriggerGC();
5953 }
5954
Vladimir Marko372f10e2016-05-17 16:30:10 +01005955 void AddSpecialInput(HInstruction* special_input);
5956
5957 using HInstruction::GetInputRecords; // Keep the const version visible.
5958 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5959 return ArrayRef<HUserRecord<HInstruction*>>(
5960 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961 }
5962
Vladimir Marko372f10e2016-05-17 16:30:10 +01005963 Primitive::Type GetType() const OVERRIDE {
5964 return Primitive::kPrimNot;
5965 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005967 DECLARE_INSTRUCTION(LoadString);
5968
5969 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005970 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 static constexpr size_t kFieldLoadKindSize =
5972 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5973 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005974 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005975 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005976
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005977 void SetLoadKindInternal(LoadKind load_kind);
5978
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005979 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005980 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005981 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005982 HUserRecord<HInstruction*> special_input_;
5983
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005984 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005985 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005986
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005987 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005988
5989 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5990};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5992
5993// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005994inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005995 // The special input is used for PC-relative loads on some architectures,
5996 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005998 GetLoadKind() == LoadKind::kBootImageAddress ||
5999 GetLoadKind() == LoadKind::kBootImageInternTable ||
6000 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006001 // HLoadString::GetInputRecords() returns an empty array at this point,
6002 // so use the GetInputRecords() from the base class to set the input record.
6003 DCHECK(special_input_.GetInstruction() == nullptr);
6004 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006005 special_input->AddUseAt(this, 0);
6006}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006007
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008/**
6009 * Performs an initialization check on its Class object input.
6010 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006011class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006013 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006014 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006015 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006016 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6017 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018 SetRawInputAt(0, constant);
6019 }
6020
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006021 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006022 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006023 return true;
6024 }
6025
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026 bool NeedsEnvironment() const OVERRIDE {
6027 // May call runtime to initialize the class.
6028 return true;
6029 }
6030
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006031 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006033 HLoadClass* GetLoadClass() const {
6034 DCHECK(InputAt(0)->IsLoadClass());
6035 return InputAt(0)->AsLoadClass();
6036 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037
6038 DECLARE_INSTRUCTION(ClinitCheck);
6039
6040 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6042};
6043
Vladimir Markofcb503c2016-05-18 12:48:17 +01006044class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 public:
6046 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006047 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006048 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006049 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006050 bool is_volatile,
6051 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006052 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006053 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006054 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006055 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006056 field_info_(field,
6057 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006058 field_type,
6059 is_volatile,
6060 field_idx,
6061 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006062 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006063 SetRawInputAt(0, cls);
6064 }
6065
Calin Juravle52c48962014-12-16 17:02:57 +00006066
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006067 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006068
Vladimir Marko372f10e2016-05-17 16:30:10 +01006069 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6070 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006071 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006072 }
6073
6074 size_t ComputeHashCode() const OVERRIDE {
6075 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6076 }
6077
Calin Juravle52c48962014-12-16 17:02:57 +00006078 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006079 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6080 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006081 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006082
6083 DECLARE_INSTRUCTION(StaticFieldGet);
6084
6085 private:
6086 const FieldInfo field_info_;
6087
6088 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6089};
6090
Vladimir Markofcb503c2016-05-18 12:48:17 +01006091class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006092 public:
6093 HStaticFieldSet(HInstruction* cls,
6094 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006095 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006096 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006097 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006098 bool is_volatile,
6099 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006100 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006101 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006102 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006103 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006104 field_info_(field,
6105 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006106 field_type,
6107 is_volatile,
6108 field_idx,
6109 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006110 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006111 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006112 SetRawInputAt(0, cls);
6113 SetRawInputAt(1, value);
6114 }
6115
Calin Juravle52c48962014-12-16 17:02:57 +00006116 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006117 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6118 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006119 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006120
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006121 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006122 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6123 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006124
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006125 DECLARE_INSTRUCTION(StaticFieldSet);
6126
6127 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6129 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6130 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6131 "Too many packed fields.");
6132
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006133 const FieldInfo field_info_;
6134
6135 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6136};
6137
Vladimir Markofcb503c2016-05-18 12:48:17 +01006138class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006139 public:
6140 HUnresolvedInstanceFieldGet(HInstruction* obj,
6141 Primitive::Type field_type,
6142 uint32_t field_index,
6143 uint32_t dex_pc)
6144 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6145 field_index_(field_index) {
6146 SetRawInputAt(0, obj);
6147 }
6148
6149 bool NeedsEnvironment() const OVERRIDE { return true; }
6150 bool CanThrow() const OVERRIDE { return true; }
6151
6152 Primitive::Type GetFieldType() const { return GetType(); }
6153 uint32_t GetFieldIndex() const { return field_index_; }
6154
6155 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6156
6157 private:
6158 const uint32_t field_index_;
6159
6160 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6161};
6162
Vladimir Markofcb503c2016-05-18 12:48:17 +01006163class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 public:
6165 HUnresolvedInstanceFieldSet(HInstruction* obj,
6166 HInstruction* value,
6167 Primitive::Type field_type,
6168 uint32_t field_index,
6169 uint32_t dex_pc)
6170 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006171 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006173 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006174 SetRawInputAt(0, obj);
6175 SetRawInputAt(1, value);
6176 }
6177
6178 bool NeedsEnvironment() const OVERRIDE { return true; }
6179 bool CanThrow() const OVERRIDE { return true; }
6180
Vladimir Markoa1de9182016-02-25 11:37:38 +00006181 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006182 uint32_t GetFieldIndex() const { return field_index_; }
6183
6184 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6185
6186 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006187 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6188 static constexpr size_t kFieldFieldTypeSize =
6189 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6190 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6191 kFieldFieldType + kFieldFieldTypeSize;
6192 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6193 "Too many packed fields.");
6194 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6195
Calin Juravlee460d1d2015-09-29 04:52:17 +01006196 const uint32_t field_index_;
6197
6198 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6199};
6200
Vladimir Markofcb503c2016-05-18 12:48:17 +01006201class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006202 public:
6203 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6204 uint32_t field_index,
6205 uint32_t dex_pc)
6206 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6207 field_index_(field_index) {
6208 }
6209
6210 bool NeedsEnvironment() const OVERRIDE { return true; }
6211 bool CanThrow() const OVERRIDE { return true; }
6212
6213 Primitive::Type GetFieldType() const { return GetType(); }
6214 uint32_t GetFieldIndex() const { return field_index_; }
6215
6216 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6217
6218 private:
6219 const uint32_t field_index_;
6220
6221 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6222};
6223
Vladimir Markofcb503c2016-05-18 12:48:17 +01006224class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006225 public:
6226 HUnresolvedStaticFieldSet(HInstruction* value,
6227 Primitive::Type field_type,
6228 uint32_t field_index,
6229 uint32_t dex_pc)
6230 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006231 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006233 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006234 SetRawInputAt(0, value);
6235 }
6236
6237 bool NeedsEnvironment() const OVERRIDE { return true; }
6238 bool CanThrow() const OVERRIDE { return true; }
6239
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006241 uint32_t GetFieldIndex() const { return field_index_; }
6242
6243 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6244
6245 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006246 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6247 static constexpr size_t kFieldFieldTypeSize =
6248 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6249 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6250 kFieldFieldType + kFieldFieldTypeSize;
6251 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6252 "Too many packed fields.");
6253 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6254
Calin Juravlee460d1d2015-09-29 04:52:17 +01006255 const uint32_t field_index_;
6256
6257 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6258};
6259
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006260// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006261class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006263 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6264 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006265
David Brazdilbbd733e2015-08-18 17:48:17 +01006266 bool CanBeNull() const OVERRIDE { return false; }
6267
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006268 DECLARE_INSTRUCTION(LoadException);
6269
6270 private:
6271 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6272};
6273
David Brazdilcb1c0552015-08-04 16:22:25 +01006274// Implicit part of move-exception which clears thread-local exception storage.
6275// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006276class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006277 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006278 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6279 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006280
6281 DECLARE_INSTRUCTION(ClearException);
6282
6283 private:
6284 DISALLOW_COPY_AND_ASSIGN(HClearException);
6285};
6286
Vladimir Markofcb503c2016-05-18 12:48:17 +01006287class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006288 public:
6289 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006290 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006291 SetRawInputAt(0, exception);
6292 }
6293
6294 bool IsControlFlow() const OVERRIDE { return true; }
6295
6296 bool NeedsEnvironment() const OVERRIDE { return true; }
6297
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006298 bool CanThrow() const OVERRIDE { return true; }
6299
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006300
6301 DECLARE_INSTRUCTION(Throw);
6302
6303 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006304 DISALLOW_COPY_AND_ASSIGN(HThrow);
6305};
6306
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006307/**
6308 * Implementation strategies for the code generator of a HInstanceOf
6309 * or `HCheckCast`.
6310 */
6311enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006312 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 kExactCheck, // Can do a single class compare.
6314 kClassHierarchyCheck, // Can just walk the super class chain.
6315 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6316 kInterfaceCheck, // No optimization yet when checking against an interface.
6317 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 kArrayCheck, // No optimization yet when checking against a generic array.
6319 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006320};
6321
Roland Levillain86503782016-02-11 19:07:30 +00006322std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6323
Vladimir Markofcb503c2016-05-18 12:48:17 +01006324class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006325 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006326 HInstanceOf(HInstruction* object,
6327 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006329 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006330 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 dex_pc) {
6333 SetPackedField<TypeCheckKindField>(check_kind);
6334 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 SetRawInputAt(0, object);
6336 SetRawInputAt(1, constant);
6337 }
6338
6339 bool CanBeMoved() const OVERRIDE { return true; }
6340
Vladimir Marko372f10e2016-05-17 16:30:10 +01006341 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006342 return true;
6343 }
6344
6345 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006346 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006347 }
6348
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006349 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6351 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6352 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6353 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006354
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006355 static bool CanCallRuntime(TypeCheckKind check_kind) {
6356 // Mips currently does runtime calls for any other checks.
6357 return check_kind != TypeCheckKind::kExactCheck;
6358 }
6359
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006361 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006362 }
6363
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006364 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365
6366 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6368 static constexpr size_t kFieldTypeCheckKindSize =
6369 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6370 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6371 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6372 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6373 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006374
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006375 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6376};
6377
Vladimir Markofcb503c2016-05-18 12:48:17 +01006378class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006379 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006380 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006381 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6383 SetPackedFlag<kFlagUpperCanBeNull>(true);
6384 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006385 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006386 SetRawInputAt(0, input);
6387 }
6388
David Brazdilf5552582015-12-27 13:36:12 +00006389 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006390 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006392 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006393
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006394 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 DCHECK(GetUpperCanBeNull() || !can_be_null);
6396 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006397 }
6398
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006400
Calin Juravleb1498f62015-02-16 13:13:29 +00006401 DECLARE_INSTRUCTION(BoundType);
6402
6403 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006404 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6405 // is false then CanBeNull() cannot be true).
6406 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6407 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6408 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6409 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6410
Calin Juravleb1498f62015-02-16 13:13:29 +00006411 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006412 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6413 // It is used to bound the type in cases like:
6414 // if (x instanceof ClassX) {
6415 // // uper_bound_ will be ClassX
6416 // }
David Brazdilf5552582015-12-27 13:36:12 +00006417 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006418
6419 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6420};
6421
Vladimir Markofcb503c2016-05-18 12:48:17 +01006422class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006423 public:
6424 HCheckCast(HInstruction* object,
6425 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006427 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006428 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6429 SetPackedField<TypeCheckKindField>(check_kind);
6430 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006431 SetRawInputAt(0, object);
6432 SetRawInputAt(1, constant);
6433 }
6434
6435 bool CanBeMoved() const OVERRIDE { return true; }
6436
Vladimir Marko372f10e2016-05-17 16:30:10 +01006437 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006438 return true;
6439 }
6440
6441 bool NeedsEnvironment() const OVERRIDE {
6442 // Instruction may throw a CheckCastError.
6443 return true;
6444 }
6445
6446 bool CanThrow() const OVERRIDE { return true; }
6447
Vladimir Markoa1de9182016-02-25 11:37:38 +00006448 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6449 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6450 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6451 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006452
6453 DECLARE_INSTRUCTION(CheckCast);
6454
6455 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006456 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6457 static constexpr size_t kFieldTypeCheckKindSize =
6458 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6459 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6460 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6461 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6462 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006463
6464 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006465};
6466
Andreas Gampe26de38b2016-07-27 17:53:11 -07006467/**
6468 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6469 * @details We define the combined barrier types that are actually required
6470 * by the Java Memory Model, rather than using exactly the terminology from
6471 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6472 * primitives. Note that the JSR-133 cookbook generally does not deal with
6473 * store atomicity issues, and the recipes there are not always entirely sufficient.
6474 * The current recipe is as follows:
6475 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6476 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6477 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6478 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6479 * class has final fields.
6480 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6481 * store-to-memory instructions. Only generated together with non-temporal stores.
6482 */
6483enum MemBarrierKind {
6484 kAnyStore,
6485 kLoadAny,
6486 kStoreStore,
6487 kAnyAny,
6488 kNTStoreStore,
6489 kLastBarrierKind = kNTStoreStore
6490};
6491std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6492
Vladimir Markofcb503c2016-05-18 12:48:17 +01006493class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006494 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006495 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006496 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006497 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6498 SetPackedField<BarrierKindField>(barrier_kind);
6499 }
Calin Juravle27df7582015-04-17 19:12:31 +01006500
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006502
6503 DECLARE_INSTRUCTION(MemoryBarrier);
6504
6505 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006506 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6507 static constexpr size_t kFieldBarrierKindSize =
6508 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6509 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6510 kFieldBarrierKind + kFieldBarrierKindSize;
6511 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6512 "Too many packed fields.");
6513 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006514
6515 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6516};
6517
Igor Murashkind01745e2017-04-05 16:40:31 -07006518// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6519// the specified object(s), with respect to any subsequent store that might "publish"
6520// (i.e. make visible) the specified object to another thread.
6521//
6522// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6523// for all final fields (that were set) at the end of the invoked constructor.
6524//
6525// The constructor fence models the freeze actions for the final fields of an object
6526// being constructed (semantically at the end of the constructor). Constructor fences
6527// have a per-object affinity; two separate objects being constructed get two separate
6528// constructor fences.
6529//
6530// (Note: that if calling a super-constructor or forwarding to another constructor,
6531// the freezes would happen at the end of *that* constructor being invoked).
6532//
6533// The memory model guarantees that when the object being constructed is "published" after
6534// constructor completion (i.e. escapes the current thread via a store), then any final field
6535// writes must be observable on other threads (once they observe that publication).
6536//
6537// Further, anything written before the freeze, and read by dereferencing through the final field,
6538// must also be visible (so final object field could itself have an object with non-final fields;
6539// yet the freeze must also extend to them).
6540//
6541// Constructor example:
6542//
6543// class HasFinal {
6544// final int field; Optimizing IR for <init>()V:
6545// HasFinal() {
6546// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6547// // freeze(this.field); HConstructorFence(this)
6548// } HReturn
6549// }
6550//
6551// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6552// already-initialized classes; in that case the allocation must act as a "default-initializer"
6553// of the object which effectively writes the class pointer "final field".
6554//
6555// For example, we can model default-initialiation as roughly the equivalent of the following:
6556//
6557// class Object {
6558// private final Class header;
6559// }
6560//
6561// Java code: Optimizing IR:
6562//
6563// T new_instance<T>() {
6564// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6565// obj.header = T.class; // header write is done by above call.
6566// // freeze(obj.header) HConstructorFence(obj)
6567// return (T)obj;
6568// }
6569//
6570// See also:
6571// * CompilerDriver::RequiresConstructorBarrier
6572// * QuasiAtomic::ThreadFenceForConstructor
6573//
6574class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6575 // A fence has variable inputs because the inputs can be removed
6576 // after prepare_for_register_allocation phase.
6577 // (TODO: In the future a fence could freeze multiple objects
6578 // after merging two fences together.)
6579 public:
6580 // `fence_object` is the reference that needs to be protected for correct publication.
6581 //
6582 // It makes sense in the following situations:
6583 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6584 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6585 //
6586 // After construction the `fence_object` becomes the 0th input.
6587 // This is not an input in a real sense, but just a convenient place to stash the information
6588 // about the associated object.
6589 HConstructorFence(HInstruction* fence_object,
6590 uint32_t dex_pc,
6591 ArenaAllocator* arena)
6592 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6593 // constraints described in the class header. We claim that these SideEffects constraints
6594 // enforce a superset of the real constraints.
6595 //
6596 // The ordering described above is conservatively modeled with SideEffects as follows:
6597 //
6598 // * To prevent reordering of the publication stores:
6599 // ----> "Reads of objects" is the initial SideEffect.
6600 // * For every primitive final field store in the constructor:
6601 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6602 // * If there are any stores to reference final fields in the constructor:
6603 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6604 // that are reachable from `fence_object` also need to be prevented for reordering
6605 // (and we do not want to do alias analysis to figure out what those stores are).
6606 //
6607 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6608 // even more conservative approach than the one described above, and prevents all of the
6609 // above reordering without analyzing any of the instructions in the constructor.
6610 //
6611 // If in a later phase we discover that there are no writes to reference final fields,
6612 // we can refine the side effect to a smaller set of type reads (see above constraints).
6613 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6614 dex_pc,
6615 arena,
6616 /* number_of_inputs */ 1,
6617 kArenaAllocConstructorFenceInputs) {
6618 DCHECK(fence_object != nullptr);
6619 SetRawInputAt(0, fence_object);
6620 }
6621
6622 // The object associated with this constructor fence.
6623 //
6624 // (Note: This will be null after the prepare_for_register_allocation phase,
6625 // as all constructor fence inputs are removed there).
6626 HInstruction* GetFenceObject() const {
6627 return InputAt(0);
6628 }
6629
6630 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6631 // - Delete `fence_use` from `this`'s use list.
6632 // - Delete `this` from `fence_use`'s inputs list.
6633 // - If the `fence_use` is dead, remove it from the graph.
6634 //
6635 // A fence is considered dead once it no longer has any uses
6636 // and all of the inputs are dead.
6637 //
6638 // This must *not* be called during/after prepare_for_register_allocation,
6639 // because that removes all the inputs to the fences but the fence is actually
6640 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006641 //
6642 // Returns how many HConstructorFence instructions were removed from graph.
6643 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006644
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006645 // Check if this constructor fence is protecting
6646 // an HNewInstance or HNewArray that is also the immediate
6647 // predecessor of `this`.
6648 //
6649 // Returns the associated HNewArray or HNewInstance,
6650 // or null otherwise.
6651 HInstruction* GetAssociatedAllocation();
6652
Igor Murashkind01745e2017-04-05 16:40:31 -07006653 DECLARE_INSTRUCTION(ConstructorFence);
6654
6655 private:
6656 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6657};
6658
Vladimir Markofcb503c2016-05-18 12:48:17 +01006659class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006660 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006661 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006662 kEnter,
6663 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006664 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006665 };
6666
6667 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006668 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006669 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6670 dex_pc) {
6671 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006672 SetRawInputAt(0, object);
6673 }
6674
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006675 // Instruction may go into runtime, so we need an environment.
6676 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006677
6678 bool CanThrow() const OVERRIDE {
6679 // Verifier guarantees that monitor-exit cannot throw.
6680 // This is important because it allows the HGraphBuilder to remove
6681 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6682 return IsEnter();
6683 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006684
Vladimir Markoa1de9182016-02-25 11:37:38 +00006685 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6686 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006687
6688 DECLARE_INSTRUCTION(MonitorOperation);
6689
Calin Juravle52c48962014-12-16 17:02:57 +00006690 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006691 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6692 static constexpr size_t kFieldOperationKindSize =
6693 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6694 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6695 kFieldOperationKind + kFieldOperationKindSize;
6696 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6697 "Too many packed fields.");
6698 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006699
6700 private:
6701 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6702};
6703
Vladimir Markofcb503c2016-05-18 12:48:17 +01006704class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006705 public:
6706 HSelect(HInstruction* condition,
6707 HInstruction* true_value,
6708 HInstruction* false_value,
6709 uint32_t dex_pc)
6710 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6711 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6712
6713 // First input must be `true_value` or `false_value` to allow codegens to
6714 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6715 // that architectures which implement HSelect as a conditional move also
6716 // will not need to invert the condition.
6717 SetRawInputAt(0, false_value);
6718 SetRawInputAt(1, true_value);
6719 SetRawInputAt(2, condition);
6720 }
6721
6722 HInstruction* GetFalseValue() const { return InputAt(0); }
6723 HInstruction* GetTrueValue() const { return InputAt(1); }
6724 HInstruction* GetCondition() const { return InputAt(2); }
6725
6726 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006727 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6728 return true;
6729 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006730
6731 bool CanBeNull() const OVERRIDE {
6732 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6733 }
6734
6735 DECLARE_INSTRUCTION(Select);
6736
6737 private:
6738 DISALLOW_COPY_AND_ASSIGN(HSelect);
6739};
6740
Vladimir Markof9f64412015-09-02 14:05:49 +01006741class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006742 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006743 MoveOperands(Location source,
6744 Location destination,
6745 Primitive::Type type,
6746 HInstruction* instruction)
6747 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006748
6749 Location GetSource() const { return source_; }
6750 Location GetDestination() const { return destination_; }
6751
6752 void SetSource(Location value) { source_ = value; }
6753 void SetDestination(Location value) { destination_ = value; }
6754
6755 // The parallel move resolver marks moves as "in-progress" by clearing the
6756 // destination (but not the source).
6757 Location MarkPending() {
6758 DCHECK(!IsPending());
6759 Location dest = destination_;
6760 destination_ = Location::NoLocation();
6761 return dest;
6762 }
6763
6764 void ClearPending(Location dest) {
6765 DCHECK(IsPending());
6766 destination_ = dest;
6767 }
6768
6769 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006770 DCHECK(source_.IsValid() || destination_.IsInvalid());
6771 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006772 }
6773
6774 // True if this blocks a move from the given location.
6775 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006776 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006777 }
6778
6779 // A move is redundant if it's been eliminated, if its source and
6780 // destination are the same, or if its destination is unneeded.
6781 bool IsRedundant() const {
6782 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6783 }
6784
6785 // We clear both operands to indicate move that's been eliminated.
6786 void Eliminate() {
6787 source_ = destination_ = Location::NoLocation();
6788 }
6789
6790 bool IsEliminated() const {
6791 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6792 return source_.IsInvalid();
6793 }
6794
Alexey Frunze4dda3372015-06-01 18:31:49 -07006795 Primitive::Type GetType() const { return type_; }
6796
Nicolas Geoffray90218252015-04-15 11:56:51 +01006797 bool Is64BitMove() const {
6798 return Primitive::Is64BitType(type_);
6799 }
6800
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006801 HInstruction* GetInstruction() const { return instruction_; }
6802
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006803 private:
6804 Location source_;
6805 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006806 // The type this move is for.
6807 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006808 // The instruction this move is assocatied with. Null when this move is
6809 // for moving an input in the expected locations of user (including a phi user).
6810 // This is only used in debug mode, to ensure we do not connect interval siblings
6811 // in the same parallel move.
6812 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006813};
6814
Roland Levillainc9285912015-12-18 10:38:42 +00006815std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6816
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006817static constexpr size_t kDefaultNumberOfMoves = 4;
6818
Vladimir Markofcb503c2016-05-18 12:48:17 +01006819class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006821 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006822 : HTemplateInstruction(SideEffects::None(), dex_pc),
6823 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6824 moves_.reserve(kDefaultNumberOfMoves);
6825 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006826
Nicolas Geoffray90218252015-04-15 11:56:51 +01006827 void AddMove(Location source,
6828 Location destination,
6829 Primitive::Type type,
6830 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006831 DCHECK(source.IsValid());
6832 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006833 if (kIsDebugBuild) {
6834 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006835 for (const MoveOperands& move : moves_) {
6836 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006837 // Special case the situation where the move is for the spill slot
6838 // of the instruction.
6839 if ((GetPrevious() == instruction)
6840 || ((GetPrevious() == nullptr)
6841 && instruction->IsPhi()
6842 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006843 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006844 << "Doing parallel moves for the same instruction.";
6845 } else {
6846 DCHECK(false) << "Doing parallel moves for the same instruction.";
6847 }
6848 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006849 }
6850 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006851 for (const MoveOperands& move : moves_) {
6852 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006853 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006854 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006855 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006856 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006857 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006858 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006859 }
6860
Vladimir Marko225b6462015-09-28 12:17:40 +01006861 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006862 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006863 }
6864
Vladimir Marko225b6462015-09-28 12:17:40 +01006865 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006867 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006868
6869 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006870 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006871
6872 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6873};
6874
Mark Mendell0616ae02015-04-17 12:49:27 -04006875} // namespace art
6876
Aart Bikf8f5a162017-02-06 15:35:29 -08006877#include "nodes_vector.h"
6878
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006879#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6880#include "nodes_shared.h"
6881#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006882#ifdef ART_ENABLE_CODEGEN_mips
6883#include "nodes_mips.h"
6884#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006885#ifdef ART_ENABLE_CODEGEN_x86
6886#include "nodes_x86.h"
6887#endif
6888
6889namespace art {
6890
Igor Murashkin6ef45672017-08-08 13:59:55 -07006891class OptimizingCompilerStats;
6892
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006893class HGraphVisitor : public ValueObject {
6894 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006895 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6896 : stats_(stats),
6897 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07006898 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006899
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006900 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006901 virtual void VisitBasicBlock(HBasicBlock* block);
6902
Roland Levillain633021e2014-10-01 14:12:25 +01006903 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006904 void VisitInsertionOrder();
6905
Roland Levillain633021e2014-10-01 14:12:25 +01006906 // Visit the graph following dominator tree reverse post-order.
6907 void VisitReversePostOrder();
6908
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006909 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006910
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006911 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006912#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006913 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6914
6915 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6916
6917#undef DECLARE_VISIT_INSTRUCTION
6918
Igor Murashkin6ef45672017-08-08 13:59:55 -07006919 protected:
6920 OptimizingCompilerStats* stats_;
6921
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006922 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006923 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006924
6925 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6926};
6927
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006928class HGraphDelegateVisitor : public HGraphVisitor {
6929 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006930 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6931 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006932 virtual ~HGraphDelegateVisitor() {}
6933
6934 // Visit functions that delegate to to super class.
6935#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006936 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006937
6938 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6939
6940#undef DECLARE_VISIT_INSTRUCTION
6941
6942 private:
6943 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6944};
6945
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006946// Iterator over the blocks that art part of the loop. Includes blocks part
6947// of an inner loop. The order in which the blocks are iterated is on their
6948// block id.
6949class HBlocksInLoopIterator : public ValueObject {
6950 public:
6951 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6952 : blocks_in_loop_(info.GetBlocks()),
6953 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6954 index_(0) {
6955 if (!blocks_in_loop_.IsBitSet(index_)) {
6956 Advance();
6957 }
6958 }
6959
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006960 bool Done() const { return index_ == blocks_.size(); }
6961 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006962 void Advance() {
6963 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006964 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006965 if (blocks_in_loop_.IsBitSet(index_)) {
6966 break;
6967 }
6968 }
6969 }
6970
6971 private:
6972 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006973 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006974 size_t index_;
6975
6976 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6977};
6978
Mingyao Yang3584bce2015-05-19 16:01:59 -07006979// Iterator over the blocks that art part of the loop. Includes blocks part
6980// of an inner loop. The order in which the blocks are iterated is reverse
6981// post order.
6982class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6983 public:
6984 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6985 : blocks_in_loop_(info.GetBlocks()),
6986 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6987 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006988 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006989 Advance();
6990 }
6991 }
6992
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006993 bool Done() const { return index_ == blocks_.size(); }
6994 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006995 void Advance() {
6996 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006997 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6998 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006999 break;
7000 }
7001 }
7002 }
7003
7004 private:
7005 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007006 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007007 size_t index_;
7008
7009 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7010};
7011
Aart Bikf3e61ee2017-04-12 17:09:20 -07007012// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007013inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007014 if (constant->IsIntConstant()) {
7015 return constant->AsIntConstant()->GetValue();
7016 } else if (constant->IsLongConstant()) {
7017 return constant->AsLongConstant()->GetValue();
7018 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007019 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007020 return 0;
7021 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007022}
7023
Aart Bikf3e61ee2017-04-12 17:09:20 -07007024// Returns true iff instruction is an integral constant (and sets value on success).
7025inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7026 if (instruction->IsIntConstant()) {
7027 *value = instruction->AsIntConstant()->GetValue();
7028 return true;
7029 } else if (instruction->IsLongConstant()) {
7030 *value = instruction->AsLongConstant()->GetValue();
7031 return true;
7032 } else if (instruction->IsNullConstant()) {
7033 *value = 0;
7034 return true;
7035 }
7036 return false;
7037}
7038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007039#define INSTRUCTION_TYPE_CHECK(type, super) \
7040 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7041 inline const H##type* HInstruction::As##type() const { \
7042 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7043 } \
7044 inline H##type* HInstruction::As##type() { \
7045 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7046 }
7047
7048 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7049#undef INSTRUCTION_TYPE_CHECK
7050
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007051// Create space in `blocks` for adding `number_of_new_blocks` entries
7052// starting at location `at`. Blocks after `at` are moved accordingly.
7053inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7054 size_t number_of_new_blocks,
7055 size_t after) {
7056 DCHECK_LT(after, blocks->size());
7057 size_t old_size = blocks->size();
7058 size_t new_size = old_size + number_of_new_blocks;
7059 blocks->resize(new_size);
7060 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7061}
7062
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007063/*
7064 * Hunt "under the hood" of array lengths (leading to array references),
7065 * null checks (also leading to array references), and new arrays
7066 * (leading to the actual length). This makes it more likely related
7067 * instructions become actually comparable.
7068 */
7069inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7070 while (instruction->IsArrayLength() ||
7071 instruction->IsNullCheck() ||
7072 instruction->IsNewArray()) {
7073 instruction = instruction->IsNewArray()
7074 ? instruction->AsNewArray()->GetLength()
7075 : instruction->InputAt(0);
7076 }
7077 return instruction;
7078}
7079
Artem Serov21c7e6f2017-07-27 16:04:42 +01007080void RemoveEnvironmentUses(HInstruction* instruction);
7081bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7082void ResetEnvironmentInputRecords(HInstruction* instruction);
7083
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007084} // namespace art
7085
7086#endif // ART_COMPILER_OPTIMIZING_NODES_H_