blob: ffa16dd7871a7a750461d26d4427cab3614e99bf [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"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +010033#include "deoptimization_kind.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 Geoffraye53798a2014-12-01 10:31:54 +000037#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.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);
421 void SimplifyLoop(HBasicBlock* header);
422
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 int32_t GetNextInstructionId() {
424 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000425 return current_instruction_id_++;
426 }
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t GetCurrentInstructionId() const {
429 return current_instruction_id_;
430 }
431
432 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000433 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 current_instruction_id_ = id;
435 }
436
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100437 uint16_t GetMaximumNumberOfOutVRegs() const {
438 return maximum_number_of_out_vregs_;
439 }
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
442 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100443 }
444
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
446 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
447 }
448
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000449 void UpdateTemporariesVRegSlots(size_t slots) {
450 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100451 }
452
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000453 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100454 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000455 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100458 void SetNumberOfVRegs(uint16_t number_of_vregs) {
459 number_of_vregs_ = number_of_vregs;
460 }
461
462 uint16_t GetNumberOfVRegs() const {
463 return number_of_vregs_;
464 }
465
466 void SetNumberOfInVRegs(uint16_t value) {
467 number_of_in_vregs_ = value;
468 }
469
David Brazdildee58d62016-04-07 09:54:26 +0000470 uint16_t GetNumberOfInVRegs() const {
471 return number_of_in_vregs_;
472 }
473
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100474 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100475 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100476 return number_of_vregs_ - number_of_in_vregs_;
477 }
478
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100479 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100482
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100483 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
484 DCHECK(GetReversePostOrder()[0] == entry_block_);
485 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
486 }
487
488 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
489 return ReverseRange(GetReversePostOrder());
490 }
491
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100492 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100493 return linear_order_;
494 }
495
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100496 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
497 return ReverseRange(GetLinearOrder());
498 }
499
Mark Mendell1152c922015-04-24 17:06:35 -0400500 bool HasBoundsChecks() const {
501 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800502 }
503
Mark Mendell1152c922015-04-24 17:06:35 -0400504 void SetHasBoundsChecks(bool value) {
505 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800506 }
507
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000508 bool IsDebuggable() const { return debuggable_; }
509
David Brazdil8d5b8b22015-03-24 10:51:52 +0000510 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 // already, it is created and inserted into the graph. This method is only for
512 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000514
515 // TODO: This is problematic for the consistency of reference type propagation
516 // because it can be created anytime after the pass and thus it will be left
517 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
530 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000532
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100533 HCurrentMethod* GetCurrentMethod();
534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100535 const DexFile& GetDexFile() const {
536 return dex_file_;
537 }
538
539 uint32_t GetMethodIdx() const {
540 return method_idx_;
541 }
542
Igor Murashkind01745e2017-04-05 16:40:31 -0700543 // Get the method name (without the signature), e.g. "<init>"
544 const char* GetMethodName() const;
545
546 // Get the pretty method name (class + name + optionally signature).
547 std::string PrettyMethod(bool with_signature = true) const;
548
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100549 InvokeType GetInvokeType() const {
550 return invoke_type_;
551 }
552
Mark Mendellc4701932015-04-10 13:18:51 -0400553 InstructionSet GetInstructionSet() const {
554 return instruction_set_;
555 }
556
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000557 bool IsCompilingOsr() const { return osr_; }
558
Mingyao Yang063fc772016-08-02 11:02:54 -0700559 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
560 return cha_single_implementation_list_;
561 }
562
563 void AddCHASingleImplementationDependency(ArtMethod* method) {
564 cha_single_implementation_list_.insert(method);
565 }
566
567 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800568 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700569 }
570
David Brazdil77a48ae2015-09-15 12:34:04 +0000571 bool HasTryCatch() const { return has_try_catch_; }
572 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100573
Aart Bikb13c65b2017-03-21 20:14:07 -0700574 bool HasSIMD() const { return has_simd_; }
575 void SetHasSIMD(bool value) { has_simd_ = value; }
576
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800577 bool HasLoops() const { return has_loops_; }
578 void SetHasLoops(bool value) { has_loops_ = value; }
579
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000580 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
581 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
582
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100583 ArtMethod* GetArtMethod() const { return art_method_; }
584 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
585
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100586 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500587 // The instruction has been inserted into the graph, either as a constant, or
588 // before cursor.
589 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
590
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000591 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
592
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800593 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
594 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
595 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
596
David Brazdil2d7352b2015-04-20 14:52:42 +0100597 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000598 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100599 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000600
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 template <class InstructionType, typename ValueType>
602 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600603 ArenaSafeMap<ValueType, InstructionType*>* cache,
604 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 // Try to find an existing constant of the given value.
606 InstructionType* constant = nullptr;
607 auto cached_constant = cache->find(value);
608 if (cached_constant != cache->end()) {
609 constant = cached_constant->second;
610 }
611
612 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100613 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000614 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600615 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000616 cache->Overwrite(value, constant);
617 InsertConstant(constant);
618 }
619 return constant;
620 }
621
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 void InsertConstant(HConstant* instruction);
623
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 // Cache a float constant into the graph. This method should only be
625 // called by the SsaBuilder when creating "equivalent" instructions.
626 void CacheFloatConstant(HFloatConstant* constant);
627
628 // See CacheFloatConstant comment.
629 void CacheDoubleConstant(HDoubleConstant* constant);
630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000632
633 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100634 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
Aart Bik281c6812016-08-26 11:31:48 -0700639 // List of blocks to perform a linear order tree traversal. Unlike the reverse
640 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100641 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100642
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000643 HBasicBlock* entry_block_;
644 HBasicBlock* exit_block_;
645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100646 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100647 uint16_t maximum_number_of_out_vregs_;
648
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100649 // The number of virtual registers in this method. Contains the parameters.
650 uint16_t number_of_vregs_;
651
652 // The number of virtual registers used by parameters of this method.
653 uint16_t number_of_in_vregs_;
654
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000655 // Number of vreg size slots that the temporaries use (used in baseline compiler).
656 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800658 // Flag whether there are bounds checks in the graph. We can skip
659 // BCE if it's false. It's only best effort to keep it up to date in
660 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400661 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800662
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800663 // Flag whether there are try/catch blocks in the graph. We will skip
664 // try/catch-related passes if it's false. It's only best effort to keep
665 // it up to date in the presence of code elimination so there might be
666 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000667 bool has_try_catch_;
668
Aart Bikb13c65b2017-03-21 20:14:07 -0700669 // Flag whether SIMD instructions appear in the graph. If true, the
670 // code generators may have to be more careful spilling the wider
671 // contents of SIMD registers.
672 bool has_simd_;
673
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800674 // Flag whether there are any loops in the graph. We can skip loop
675 // optimization if it's false. It's only best effort to keep it up
676 // to date in the presence of code elimination so there might be false
677 // positives.
678 bool has_loops_;
679
680 // Flag whether there are any irreducible loops in the graph. It's only
681 // best effort to keep it up to date in the presence of code elimination
682 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000683 bool has_irreducible_loops_;
684
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000685 // Indicates whether the graph should be compiled in a way that
686 // ensures full debuggability. If false, we can apply more
687 // aggressive optimizations that may limit the level of debugging.
688 const bool debuggable_;
689
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000691 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000692
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100693 // The dex file from which the method is from.
694 const DexFile& dex_file_;
695
696 // The method index in the dex file.
697 const uint32_t method_idx_;
698
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100699 // If inlined, this encodes how the callee is being invoked.
700 const InvokeType invoke_type_;
701
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100702 // Whether the graph has been transformed to SSA form. Only used
703 // in debug mode to ensure we are not using properties only valid
704 // for non-SSA form (like the number of temporaries).
705 bool in_ssa_form_;
706
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800707 // Number of CHA guards in the graph. Used to short-circuit the
708 // CHA guard optimization pass when there is no CHA guard left.
709 uint32_t number_of_cha_guards_;
710
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711 const InstructionSet instruction_set_;
712
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000713 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000714 HNullConstant* cached_null_constant_;
715 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000716 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000717 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000718 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000719
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100720 HCurrentMethod* cached_current_method_;
721
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100722 // The ArtMethod this graph is for. Note that for AOT, it may be null,
723 // for example for methods whose declaring class could not be resolved
724 // (such as when the superclass could not be found).
725 ArtMethod* art_method_;
726
David Brazdil4833f5a2015-12-16 10:37:39 +0000727 // Keep the RTI of inexact Object to avoid having to pass stack handle
728 // collection pointer to passes which may create NullConstant.
729 ReferenceTypeInfo inexact_object_rti_;
730
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000731 // Whether we are compiling this graph for on stack replacement: this will
732 // make all loops seen as irreducible and emit special stack maps to mark
733 // compiled code entries which the interpreter can directly jump to.
734 const bool osr_;
735
Mingyao Yang063fc772016-08-02 11:02:54 -0700736 // List of methods that are assumed to have single implementation.
737 ArenaSet<ArtMethod*> cha_single_implementation_list_;
738
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000739 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100740 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000741 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000742 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743 DISALLOW_COPY_AND_ASSIGN(HGraph);
744};
745
Vladimir Markof9f64412015-09-02 14:05:49 +0100746class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747 public:
748 HLoopInformation(HBasicBlock* header, HGraph* graph)
749 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100750 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000751 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100753 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100754 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000755 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 back_edges_.reserve(kDefaultNumberOfBackEdges);
757 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000759 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100760 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000761
762 void Dump(std::ostream& os);
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 HBasicBlock* GetHeader() const {
765 return header_;
766 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 void SetHeader(HBasicBlock* block) {
769 header_ = block;
770 }
771
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
773 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
774 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
775
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000778 }
779
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 }
783
David Brazdil46e2a392015-03-16 17:31:52 +0000784 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 }
787
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000788 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 }
791
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100792 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 }
797
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100798 // Returns the lifetime position of the back edge that has the
799 // greatest lifetime position.
800 size_t GetLifetimeEnd() const;
801
802 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100804 }
805
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000806 // Finds blocks that are part of this loop.
807 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100808
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 // Returns whether this loop information contains `block`.
810 // Note that this loop information *must* be populated before entering this function.
811 bool Contains(const HBasicBlock& block) const;
812
813 // Returns whether this loop information is an inner loop of `other`.
814 // Note that `other` *must* be populated before entering this function.
815 bool IsIn(const HLoopInformation& other) const;
816
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800817 // Returns true if instruction is not defined within this loop.
818 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700819
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 const ArenaBitVector& GetBlocks() const { return blocks_; }
821
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000822 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000823 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000824
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000825 void ClearAllBlocks() {
826 blocks_.ClearAllBits();
827 }
828
David Brazdil3f4a5222016-05-06 12:46:21 +0100829 bool HasBackEdgeNotDominatedByHeader() const;
830
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100831 bool IsPopulated() const {
832 return blocks_.GetHighestBitSet() != -1;
833 }
834
Anton Shaminf89381f2016-05-16 16:44:13 +0600835 bool DominatesAllBackEdges(HBasicBlock* block);
836
David Sehrc757dec2016-11-04 15:48:34 -0700837 bool HasExitEdge() const;
838
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 // Internal recursive implementation of `Populate`.
841 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100842 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000846 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100847 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850
851 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
852};
853
David Brazdilec16f792015-08-19 15:04:01 +0100854// Stores try/catch information for basic blocks.
855// Note that HGraph is constructed so that catch blocks cannot simultaneously
856// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100857class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100858 public:
859 // Try block information constructor.
860 explicit TryCatchInformation(const HTryBoundary& try_entry)
861 : try_entry_(&try_entry),
862 catch_dex_file_(nullptr),
863 catch_type_index_(DexFile::kDexNoIndex16) {
864 DCHECK(try_entry_ != nullptr);
865 }
866
867 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800868 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100869 : try_entry_(nullptr),
870 catch_dex_file_(&dex_file),
871 catch_type_index_(catch_type_index) {}
872
873 bool IsTryBlock() const { return try_entry_ != nullptr; }
874
875 const HTryBoundary& GetTryEntry() const {
876 DCHECK(IsTryBlock());
877 return *try_entry_;
878 }
879
880 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
881
882 bool IsCatchAllTypeIndex() const {
883 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800884 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100885 }
886
Andreas Gampea5b09a62016-11-17 15:21:22 -0800887 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100888 DCHECK(IsCatchBlock());
889 return catch_type_index_;
890 }
891
892 const DexFile& GetCatchDexFile() const {
893 DCHECK(IsCatchBlock());
894 return *catch_dex_file_;
895 }
896
897 private:
898 // One of possibly several TryBoundary instructions entering the block's try.
899 // Only set for try blocks.
900 const HTryBoundary* try_entry_;
901
902 // Exception type information. Only set for catch blocks.
903 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800904 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100905};
906
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100907static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100908static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910// A block in a method. Contains the list of instructions represented
911// as a double linked list. Each block knows its predecessors and
912// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100913
Vladimir Markof9f64412015-09-02 14:05:49 +0100914class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700916 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000918 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
919 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920 loop_information_(nullptr),
921 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000922 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100923 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100924 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000926 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000927 try_catch_information_(nullptr) {
928 predecessors_.reserve(kDefaultNumberOfPredecessors);
929 successors_.reserve(kDefaultNumberOfSuccessors);
930 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
931 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932
Vladimir Marko60584552015-09-03 13:35:12 +0000933 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
Vladimir Marko60584552015-09-03 13:35:12 +0000937 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939 }
940
David Brazdild26a4112015-11-10 11:07:31 +0000941 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
942 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
943
Vladimir Marko60584552015-09-03 13:35:12 +0000944 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
945 return ContainsElement(successors_, block, start_from);
946 }
947
948 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100949 return dominated_blocks_;
950 }
951
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952 bool IsEntryBlock() const {
953 return graph_->GetEntryBlock() == this;
954 }
955
956 bool IsExitBlock() const {
957 return graph_->GetExitBlock() == this;
958 }
959
David Brazdil46e2a392015-03-16 17:31:52 +0000960 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 bool IsSingleTryBoundary() const;
962
963 // Returns true if this block emits nothing but a jump.
964 bool IsSingleJump() const {
965 HLoopInformation* loop_info = GetLoopInformation();
966 return (IsSingleGoto() || IsSingleTryBoundary())
967 // Back edges generate a suspend check.
968 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
969 }
David Brazdil46e2a392015-03-16 17:31:52 +0000970
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 void AddBackEdge(HBasicBlock* back_edge) {
972 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100975 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 loop_information_->AddBackEdge(back_edge);
977 }
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000980 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600984 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986 HBasicBlock* GetDominator() const { return dominator_; }
987 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000988 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
989
990 void RemoveDominatedBlock(HBasicBlock* block) {
991 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100992 }
Vladimir Marko60584552015-09-03 13:35:12 +0000993
994 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
995 ReplaceElement(dominated_blocks_, existing, new_block);
996 }
997
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100998 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000999
1000 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001001 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001002 }
1003
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1005 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001006 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001007 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001008 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1009 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001011 HInstruction* GetFirstInstructionDisregardMoves() const;
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001014 successors_.push_back(block);
1015 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 }
1017
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001018 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1019 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001021 new_block->predecessors_.push_back(this);
1022 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001023 }
1024
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1026 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001028 new_block->successors_.push_back(this);
1029 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001030 }
1031
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001032 // Insert `this` between `predecessor` and `successor. This method
1033 // preserves the indicies, and will update the first edge found between
1034 // `predecessor` and `successor`.
1035 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1036 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001037 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001038 successor->predecessors_[predecessor_index] = this;
1039 predecessor->successors_[successor_index] = this;
1040 successors_.push_back(successor);
1041 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001042 }
1043
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001045 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001046 }
1047
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001049 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 }
1051
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001053 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 }
1055
1056 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001057 predecessors_.push_back(block);
1058 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 }
1060
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001061 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001062 DCHECK_EQ(predecessors_.size(), 2u);
1063 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001064 }
1065
David Brazdil769c9e52015-04-27 13:54:09 +01001066 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 DCHECK_EQ(successors_.size(), 2u);
1068 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001069 }
1070
David Brazdilfc6a86a2015-06-26 10:33:45 +00001071 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001072 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001073 }
1074
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001076 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001077 }
1078
David Brazdilfc6a86a2015-06-26 10:33:45 +00001079 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001080 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001081 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 }
1083
1084 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001085 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001086 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001087 }
1088
1089 // Returns whether the first occurrence of `predecessor` in the list of
1090 // predecessors is at index `idx`.
1091 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 return GetPredecessorIndexOf(predecessor) == idx;
1094 }
1095
David Brazdild7558da2015-09-22 13:04:14 +01001096 // Create a new block between this block and its predecessors. The new block
1097 // is added to the graph, all predecessor edges are relinked to it and an edge
1098 // is created to `this`. Returns the new empty block. Reverse post order or
1099 // loop and try/catch information are not updated.
1100 HBasicBlock* CreateImmediateDominator();
1101
David Brazdilfc6a86a2015-06-26 10:33:45 +00001102 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001103 // created, latter block. Note that this method will add the block to the
1104 // graph, create a Goto at the end of the former block and will create an edge
1105 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001106 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001107 HBasicBlock* SplitBefore(HInstruction* cursor);
1108
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001109 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001110 // created block. Note that this method just updates raw block information,
1111 // like predecessors, successors, dominators, and instruction list. It does not
1112 // update the graph, reverse post order, loop information, nor make sure the
1113 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001114 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1115
1116 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1117 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118
1119 // Merge `other` at the end of `this`. Successors and dominated blocks of
1120 // `other` are changed to be successors and dominated blocks of `this`. Note
1121 // that this method does not update the graph, reverse post order, loop
1122 // information, nor make sure the blocks are consistent (for example ending
1123 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001124 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001125
1126 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1127 // of `this` are moved to `other`.
1128 // Note that this method does not update the graph, reverse post order, loop
1129 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001130 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 void ReplaceWith(HBasicBlock* other);
1132
Aart Bik6b69e0a2017-01-11 10:20:43 -08001133 // Merges the instructions of `other` at the end of `this`.
1134 void MergeInstructionsWith(HBasicBlock* other);
1135
David Brazdil2d7352b2015-04-20 14:52:42 +01001136 // Merge `other` at the end of `this`. This method updates loops, reverse post
1137 // order, links to predecessors, successors, dominators and deletes the block
1138 // from the graph. The two blocks must be successive, i.e. `this` the only
1139 // predecessor of `other` and vice versa.
1140 void MergeWith(HBasicBlock* other);
1141
1142 // Disconnects `this` from all its predecessors, successors and dominator,
1143 // removes it from all loops it is included in and eventually from the graph.
1144 // The block must not dominate any other block. Predecessors and successors
1145 // are safely updated.
1146 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001149 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001150 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001152 // Replace instruction `initial` with `replacement` within this block.
1153 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1154 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001156 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001157 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1158 // instruction list. With 'ensure_safety' set to true, it verifies that the
1159 // instruction is not in use and removes it from the use lists of its inputs.
1160 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1161 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001162 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001163
1164 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001165 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 }
1167
Roland Levillain6b879dd2014-09-22 17:13:44 +01001168 bool IsLoopPreHeaderFirstPredecessor() const {
1169 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001170 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001171 }
1172
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001173 bool IsFirstPredecessorBackEdge() const {
1174 DCHECK(IsLoopHeader());
1175 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1176 }
1177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001178 HLoopInformation* GetLoopInformation() const {
1179 return loop_information_;
1180 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001182 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001183 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 void SetInLoop(HLoopInformation* info) {
1186 if (IsLoopHeader()) {
1187 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001188 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001189 loop_information_ = info;
1190 } else if (loop_information_->Contains(*info->GetHeader())) {
1191 // Block is currently part of an outer loop. Make it part of this inner loop.
1192 // Note that a non loop header having a loop information means this loop information
1193 // has already been populated
1194 loop_information_ = info;
1195 } else {
1196 // Block is part of an inner loop. Do not update the loop information.
1197 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1198 // at this point, because this method is being called while populating `info`.
1199 }
1200 }
1201
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001202 // Raw update of the loop information.
1203 void SetLoopInformation(HLoopInformation* info) {
1204 loop_information_ = info;
1205 }
1206
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001207 bool IsInLoop() const { return loop_information_ != nullptr; }
1208
David Brazdilec16f792015-08-19 15:04:01 +01001209 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1210
1211 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1212 try_catch_information_ = try_catch_information;
1213 }
1214
1215 bool IsTryBlock() const {
1216 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1217 }
1218
1219 bool IsCatchBlock() const {
1220 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1221 }
David Brazdilffee3d32015-07-06 11:48:53 +01001222
1223 // Returns the try entry that this block's successors should have. They will
1224 // be in the same try, unless the block ends in a try boundary. In that case,
1225 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001226 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001227
David Brazdild7558da2015-09-22 13:04:14 +01001228 bool HasThrowingInstructions() const;
1229
David Brazdila4b8c212015-05-07 09:59:30 +01001230 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 bool Dominates(HBasicBlock* block) const;
1232
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001233 size_t GetLifetimeStart() const { return lifetime_start_; }
1234 size_t GetLifetimeEnd() const { return lifetime_end_; }
1235
1236 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1237 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1238
David Brazdil8d5b8b22015-03-24 10:51:52 +00001239 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001240 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001241 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool HasSinglePhi() const;
1243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001245 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001246 ArenaVector<HBasicBlock*> predecessors_;
1247 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248 HInstructionList instructions_;
1249 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001250 HLoopInformation* loop_information_;
1251 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001252 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001253 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001254 // The dex program counter of the first instruction of this block.
1255 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001256 size_t lifetime_start_;
1257 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001258 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260 friend class HGraph;
1261 friend class HInstruction;
1262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001263 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1264};
1265
David Brazdilb2bd1c52015-03-25 11:17:37 +00001266// Iterates over the LoopInformation of all loops which contain 'block'
1267// from the innermost to the outermost.
1268class HLoopInformationOutwardIterator : public ValueObject {
1269 public:
1270 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1271 : current_(block.GetLoopInformation()) {}
1272
1273 bool Done() const { return current_ == nullptr; }
1274
1275 void Advance() {
1276 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001277 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001278 }
1279
1280 HLoopInformation* Current() const {
1281 DCHECK(!Done());
1282 return current_;
1283 }
1284
1285 private:
1286 HLoopInformation* current_;
1287
1288 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1289};
1290
Alexandre Ramesef20f712015-06-09 10:29:30 +01001291#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001292 M(Above, Condition) \
1293 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001295 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001296 M(ArrayGet, Instruction) \
1297 M(ArrayLength, Instruction) \
1298 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001299 M(Below, Condition) \
1300 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001301 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001302 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001303 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001304 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001305 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001306 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001307 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001309 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001310 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001311 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001312 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001314 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(Exit, Instruction) \
1318 M(FloatConstant, Constant) \
1319 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(GreaterThan, Condition) \
1321 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001323 M(InstanceFieldGet, Instruction) \
1324 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001325 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001327 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001329 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001331 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(LessThan, Condition) \
1333 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001334 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001335 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001338 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001339 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001340 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001341 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Neg, UnaryOperation) \
1343 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001345 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001347 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001349 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001350 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001354 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Return, Instruction) \
1356 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001357 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001358 M(Shl, BinaryOperation) \
1359 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360 M(StaticFieldGet, Instruction) \
1361 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 M(UnresolvedInstanceFieldGet, Instruction) \
1363 M(UnresolvedInstanceFieldSet, Instruction) \
1364 M(UnresolvedStaticFieldGet, Instruction) \
1365 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001366 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001369 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001370 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001371 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001372 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001373 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001374 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001375 M(VecSumReduce, VecUnaryOperation) \
1376 M(VecCnv, VecUnaryOperation) \
1377 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001378 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001379 M(VecNot, VecUnaryOperation) \
1380 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001381 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001382 M(VecSub, VecBinaryOperation) \
1383 M(VecMul, VecBinaryOperation) \
1384 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001385 M(VecMin, VecBinaryOperation) \
1386 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001387 M(VecAnd, VecBinaryOperation) \
1388 M(VecAndNot, VecBinaryOperation) \
1389 M(VecOr, VecBinaryOperation) \
1390 M(VecXor, VecBinaryOperation) \
1391 M(VecShl, VecBinaryOperation) \
1392 M(VecShr, VecBinaryOperation) \
1393 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001394 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001395 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001396 M(VecLoad, VecMemoryOperation) \
1397 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001399/*
1400 * Instructions, shared across several (not all) architectures.
1401 */
1402#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1403#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1404#else
1405#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001406 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001407 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001408 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001409 M(IntermediateAddress, Instruction) \
1410 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001411#endif
1412
Alexandre Ramesef20f712015-06-09 10:29:30 +01001413#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1414
1415#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1416
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001417#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001418#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001419#else
1420#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1421 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001422 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001423#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001424
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001425#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1426
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001427#ifndef ART_ENABLE_CODEGEN_x86
1428#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1429#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001430#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1431 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001432 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001433 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001434 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001435#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001436
1437#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1438
1439#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1440 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001441 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001442 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1443 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001444 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001445 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001446 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1447 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1448
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001449#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1450 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001451 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001452 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001453 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001454 M(Invoke, Instruction) \
1455 M(VecOperation, Instruction) \
1456 M(VecUnaryOperation, VecOperation) \
1457 M(VecBinaryOperation, VecOperation) \
1458 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001459
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001460#define FOR_EACH_INSTRUCTION(M) \
1461 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1462 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1463
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001464#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1466#undef FORWARD_DECLARATION
1467
Vladimir Marko372f10e2016-05-17 16:30:10 +01001468#define DECLARE_INSTRUCTION(type) \
1469 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1470 const char* DebugName() const OVERRIDE { return #type; } \
1471 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1472 return other->Is##type(); \
1473 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001474 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001476#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1477 bool Is##type() const { return As##type() != nullptr; } \
1478 const H##type* As##type() const { return this; } \
1479 H##type* As##type() { return this; }
1480
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001481template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001482class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1483 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001484 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001485 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001486 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001487 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001488 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001489 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001490 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001491
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001492 private:
David Brazdiled596192015-01-23 10:39:45 +00001493 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001494 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001495
1496 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001497 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001498
Vladimir Marko46817b82016-03-29 12:21:58 +01001499 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
1501 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1502};
1503
David Brazdiled596192015-01-23 10:39:45 +00001504template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001505using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001506
David Brazdil1abb4192015-02-17 18:33:36 +00001507// This class is used by HEnvironment and HInstruction classes to record the
1508// instructions they use and pointers to the corresponding HUseListNodes kept
1509// by the used instructions.
1510template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001511class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001512 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001513 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1514 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001515
Vladimir Marko46817b82016-03-29 12:21:58 +01001516 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1517 : HUserRecord(old_record.instruction_, before_use_node) {}
1518 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1519 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001520 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001521 }
1522
1523 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001524 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1525 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001526
1527 private:
1528 // Instruction used by the user.
1529 HInstruction* instruction_;
1530
Vladimir Marko46817b82016-03-29 12:21:58 +01001531 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1532 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001533};
1534
Vladimir Markoe9004912016-06-16 16:50:52 +01001535// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1536// This is used for HInstruction::GetInputs() to return a container wrapper providing
1537// HInstruction* values even though the underlying container has HUserRecord<>s.
1538struct HInputExtractor {
1539 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1540 return record.GetInstruction();
1541 }
1542 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1543 return record.GetInstruction();
1544 }
1545};
1546
1547using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1548using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1549
Aart Bik854a02b2015-07-14 16:07:00 -07001550/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001552 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 * For write/read dependences on fields/arrays, the dependence analysis uses
1554 * type disambiguation (e.g. a float field write cannot modify the value of an
1555 * integer field read) and the access type (e.g. a reference array write cannot
1556 * modify the value of a reference field read [although it may modify the
1557 * reference fetch prior to reading the field, which is represented by its own
1558 * write/read dependence]). The analysis makes conservative points-to
1559 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1560 * the same, and any reference read depends on any reference read without
1561 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001562 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 * The internal representation uses 38-bit and is described in the table below.
1564 * The first line indicates the side effect, and for field/array accesses the
1565 * second line indicates the type of the access (in the order of the
1566 * Primitive::Type enum).
1567 * The two numbered lines below indicate the bit position in the bitfield (read
1568 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001569 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1571 * +-------------+---------+---------+--------------+---------+---------+
1572 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1573 * | 3 |333333322|222222221| 1 |111111110|000000000|
1574 * | 7 |654321098|765432109| 8 |765432109|876543210|
1575 *
1576 * Note that, to ease the implementation, 'changes' bits are least significant
1577 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001578 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579class SideEffects : public ValueObject {
1580 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001581 SideEffects() : flags_(0) {}
1582
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 static SideEffects None() {
1584 return SideEffects(0);
1585 }
1586
1587 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 return SideEffects(kAllChangeBits | kAllDependOnBits);
1589 }
1590
1591 static SideEffects AllChanges() {
1592 return SideEffects(kAllChangeBits);
1593 }
1594
1595 static SideEffects AllDependencies() {
1596 return SideEffects(kAllDependOnBits);
1597 }
1598
1599 static SideEffects AllExceptGCDependency() {
1600 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1601 }
1602
1603 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001604 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001605 }
1606
Aart Bik34c3ba92015-07-20 14:08:59 -07001607 static SideEffects AllWrites() {
1608 return SideEffects(kAllWrites);
1609 }
1610
1611 static SideEffects AllReads() {
1612 return SideEffects(kAllReads);
1613 }
1614
1615 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1616 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001618 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619 }
1620
Aart Bik854a02b2015-07-14 16:07:00 -07001621 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001622 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001623 }
1624
Aart Bik34c3ba92015-07-20 14:08:59 -07001625 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1626 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001628 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001629 }
1630
1631 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001632 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 static SideEffects CanTriggerGC() {
1636 return SideEffects(1ULL << kCanTriggerGCBit);
1637 }
1638
1639 static SideEffects DependsOnGC() {
1640 return SideEffects(1ULL << kDependsOnGCBit);
1641 }
1642
Aart Bik854a02b2015-07-14 16:07:00 -07001643 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001644 SideEffects Union(SideEffects other) const {
1645 return SideEffects(flags_ | other.flags_);
1646 }
1647
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001648 SideEffects Exclusion(SideEffects other) const {
1649 return SideEffects(flags_ & ~other.flags_);
1650 }
1651
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001652 void Add(SideEffects other) {
1653 flags_ |= other.flags_;
1654 }
1655
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 bool Includes(SideEffects other) const {
1657 return (other.flags_ & flags_) == other.flags_;
1658 }
1659
1660 bool HasSideEffects() const {
1661 return (flags_ & kAllChangeBits);
1662 }
1663
1664 bool HasDependencies() const {
1665 return (flags_ & kAllDependOnBits);
1666 }
1667
1668 // Returns true if there are no side effects or dependencies.
1669 bool DoesNothing() const {
1670 return flags_ == 0;
1671 }
1672
Aart Bik854a02b2015-07-14 16:07:00 -07001673 // Returns true if something is written.
1674 bool DoesAnyWrite() const {
1675 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001676 }
1677
Aart Bik854a02b2015-07-14 16:07:00 -07001678 // Returns true if something is read.
1679 bool DoesAnyRead() const {
1680 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 // Returns true if potentially everything is written and read
1684 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001685 bool DoesAllReadWrite() const {
1686 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1687 }
1688
Aart Bik854a02b2015-07-14 16:07:00 -07001689 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
Roland Levillain0d5a2812015-11-13 10:07:31 +00001693 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001694 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1696 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001697 }
1698
1699 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001701 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001702 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 for (int s = kLastBit; s >= 0; s--) {
1704 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1705 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1706 // This is a bit for the GC side effect.
1707 if (current_bit_is_set) {
1708 flags += "GC";
1709 }
Aart Bik854a02b2015-07-14 16:07:00 -07001710 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 } else {
1712 // This is a bit for the array/field analysis.
1713 // The underscore character stands for the 'can trigger GC' bit.
1714 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1715 if (current_bit_is_set) {
1716 flags += kDebug[s];
1717 }
1718 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1719 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1720 flags += "|";
1721 }
1722 }
Aart Bik854a02b2015-07-14 16:07:00 -07001723 }
1724 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001725 }
1726
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001727 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 private:
1730 static constexpr int kFieldArrayAnalysisBits = 9;
1731
1732 static constexpr int kFieldWriteOffset = 0;
1733 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1734 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1735 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1736
1737 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1738
1739 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1740 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1741 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1742 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1743
1744 static constexpr int kLastBit = kDependsOnGCBit;
1745 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1746
1747 // Aliases.
1748
1749 static_assert(kChangeBits == kDependOnBits,
1750 "the 'change' bits should match the 'depend on' bits.");
1751
1752 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1753 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1754 static constexpr uint64_t kAllWrites =
1755 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1756 static constexpr uint64_t kAllReads =
1757 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758
Aart Bik854a02b2015-07-14 16:07:00 -07001759 // Translates type to bit flag.
1760 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1761 CHECK_NE(type, Primitive::kPrimVoid);
1762 const uint64_t one = 1;
1763 const int shift = type; // 0-based consecutive enum
1764 DCHECK_LE(kFieldWriteOffset, shift);
1765 DCHECK_LT(shift, kArrayWriteOffset);
1766 return one << (type + offset);
1767 }
1768
1769 // Private constructor on direct flags value.
1770 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1771
1772 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001773};
1774
David Brazdiled596192015-01-23 10:39:45 +00001775// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001776class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001777 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001778 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1779 size_t number_of_vregs,
1780 ArtMethod* method,
1781 uint32_t dex_pc,
1782 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001783 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001784 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001786 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001787 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001788 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 }
1790
Mingyao Yang01b47b02017-02-03 12:09:57 -08001791 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001792 : HEnvironment(arena,
1793 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001794 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001795 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001796 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001797
Vladimir Markoec32f642017-06-02 10:51:55 +01001798 void AllocateLocations() {
1799 DCHECK(locations_.empty());
1800 locations_.resize(vregs_.size());
1801 }
1802
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001804 if (parent_ != nullptr) {
1805 parent_->SetAndCopyParentChain(allocator, parent);
1806 } else {
1807 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1808 parent_->CopyFrom(parent);
1809 if (parent->GetParent() != nullptr) {
1810 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1811 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001812 }
David Brazdiled596192015-01-23 10:39:45 +00001813 }
1814
Vladimir Marko71bf8092015-09-15 15:33:14 +01001815 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001816 void CopyFrom(HEnvironment* environment);
1817
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001818 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1819 // input to the loop phi instead. This is for inserting instructions that
1820 // require an environment (like HDeoptimization) in the loop pre-header.
1821 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001822
1823 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001824 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001825 }
1826
1827 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001829 }
1830
David Brazdil1abb4192015-02-17 18:33:36 +00001831 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001832
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001834
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001835 HEnvironment* GetParent() const { return parent_; }
1836
1837 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001838 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001839 }
1840
1841 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001842 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 }
1844
1845 uint32_t GetDexPc() const {
1846 return dex_pc_;
1847 }
1848
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001849 ArtMethod* GetMethod() const {
1850 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001851 }
1852
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001853 HInstruction* GetHolder() const {
1854 return holder_;
1855 }
1856
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001857
1858 bool IsFromInlinedInvoke() const {
1859 return GetParent() != nullptr;
1860 }
1861
David Brazdiled596192015-01-23 10:39:45 +00001862 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001863 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1864 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001865 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001866 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001868
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001869 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 HInstruction* const holder_;
1871
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001872 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001873
1874 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1875};
1876
Vladimir Markof9f64412015-09-02 14:05:49 +01001877class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001878 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001879 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880 : previous_(nullptr),
1881 next_(nullptr),
1882 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001883 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001884 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001885 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001886 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001888 locations_(nullptr),
1889 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001891 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001892 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1893 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1894 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895
Dave Allison20dfc792014-06-16 20:44:29 -07001896 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001898#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001899 enum InstructionKind {
1900 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1901 };
1902#undef DECLARE_KIND
1903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904 HInstruction* GetNext() const { return next_; }
1905 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Calin Juravle77520bc2015-01-12 18:45:46 +00001907 HInstruction* GetNextDisregardingMoves() const;
1908 HInstruction* GetPreviousDisregardingMoves() const;
1909
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001911 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001913 bool IsInBlock() const { return block_ != nullptr; }
1914 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001915 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1916 bool IsIrreducibleLoopHeaderPhi() const {
1917 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1918 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001919
Vladimir Marko372f10e2016-05-17 16:30:10 +01001920 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1921
1922 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1923 // One virtual method is enough, just const_cast<> and then re-add the const.
1924 return ArrayRef<const HUserRecord<HInstruction*>>(
1925 const_cast<HInstruction*>(this)->GetInputRecords());
1926 }
1927
Vladimir Markoe9004912016-06-16 16:50:52 +01001928 HInputsRef GetInputs() {
1929 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001930 }
1931
Vladimir Markoe9004912016-06-16 16:50:52 +01001932 HConstInputsRef GetInputs() const {
1933 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001934 }
1935
1936 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001937 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938
Aart Bik2767f4b2016-10-28 15:03:53 -07001939 bool HasInput(HInstruction* input) const {
1940 for (const HInstruction* i : GetInputs()) {
1941 if (i == input) {
1942 return true;
1943 }
1944 }
1945 return false;
1946 }
1947
Vladimir Marko372f10e2016-05-17 16:30:10 +01001948 void SetRawInputAt(size_t index, HInstruction* input) {
1949 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1950 }
1951
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001952 virtual void Accept(HGraphVisitor* visitor) = 0;
1953 virtual const char* DebugName() const = 0;
1954
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001958
1959 uint32_t GetDexPc() const { return dex_pc_; }
1960
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001961 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001962
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001963 // Can the instruction throw?
1964 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1965 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001966 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001967 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001968
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001969 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001970 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971
Calin Juravle10e244f2015-01-26 18:54:32 +00001972 // Does not apply for all instructions, but having this at top level greatly
1973 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001974 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001975 virtual bool CanBeNull() const {
1976 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1977 return true;
1978 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001980 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001981 return false;
1982 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001983
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001984 virtual bool IsActualObject() const {
1985 return GetType() == Primitive::kPrimNot;
1986 }
1987
Calin Juravle2e768302015-07-28 14:41:11 +00001988 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001989
Calin Juravle61d544b2015-02-23 16:46:57 +00001990 ReferenceTypeInfo GetReferenceTypeInfo() const {
1991 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001992 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001993 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001994 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001997 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001998 // Note: fixup_end remains valid across push_front().
1999 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2000 HUseListNode<HInstruction*>* new_node =
2001 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2002 uses_.push_front(*new_node);
2003 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004 }
2005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002007 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002008 // Note: env_fixup_end remains valid across push_front().
2009 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2010 HUseListNode<HEnvironment*>* new_node =
2011 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2012 env_uses_.push_front(*new_node);
2013 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014 }
2015
David Brazdil1abb4192015-02-17 18:33:36 +00002016 void RemoveAsUserOfInput(size_t input) {
2017 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002018 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2019 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2020 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002021 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022
Vladimir Marko372f10e2016-05-17 16:30:10 +01002023 void RemoveAsUserOfAllInputs() {
2024 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2025 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2026 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2027 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2028 }
2029 }
2030
David Brazdil1abb4192015-02-17 18:33:36 +00002031 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2032 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
Vladimir Marko46817b82016-03-29 12:21:58 +01002034 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2035 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2036 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002037 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002038 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002039 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040
Aart Bikcc42be02016-10-20 16:14:16 -07002041 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002042 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002043 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002044 !CanThrow() &&
2045 !IsSuspendCheck() &&
2046 !IsControlFlow() &&
2047 !IsNativeDebugInfo() &&
2048 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002049 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002050 !IsMemoryBarrier() &&
2051 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002052 }
2053
Aart Bikcc42be02016-10-20 16:14:16 -07002054 bool IsDeadAndRemovable() const {
2055 return IsRemovable() && !HasUses();
2056 }
2057
Roland Levillain6c82d402014-10-13 16:10:27 +01002058 // Does this instruction strictly dominate `other_instruction`?
2059 // Returns false if this instruction and `other_instruction` are the same.
2060 // Aborts if this instruction and `other_instruction` are both phis.
2061 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002062
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002063 int GetId() const { return id_; }
2064 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002066 int GetSsaIndex() const { return ssa_index_; }
2067 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2068 bool HasSsaIndex() const { return ssa_index_ != -1; }
2069
2070 bool HasEnvironment() const { return environment_ != nullptr; }
2071 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002072 // Set the `environment_` field. Raw because this method does not
2073 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002074 void SetRawEnvironment(HEnvironment* environment) {
2075 DCHECK(environment_ == nullptr);
2076 DCHECK_EQ(environment->GetHolder(), this);
2077 environment_ = environment;
2078 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002079
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002080 void InsertRawEnvironment(HEnvironment* environment) {
2081 DCHECK(environment_ != nullptr);
2082 DCHECK_EQ(environment->GetHolder(), this);
2083 DCHECK(environment->GetParent() == nullptr);
2084 environment->parent_ = environment_;
2085 environment_ = environment;
2086 }
2087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002088 void RemoveEnvironment();
2089
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002090 // Set the environment of this instruction, copying it from `environment`. While
2091 // copying, the uses lists are being updated.
2092 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002093 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002094 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002095 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002096 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002097 if (environment->GetParent() != nullptr) {
2098 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2099 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002100 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002102 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2103 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002104 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002105 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002106 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002107 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002108 if (environment->GetParent() != nullptr) {
2109 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2110 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002111 }
2112
Nicolas Geoffray39468442014-09-02 15:17:15 +01002113 // Returns the number of entries in the environment. Typically, that is the
2114 // number of dex registers in a method. It could be more in case of inlining.
2115 size_t EnvironmentSize() const;
2116
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002117 LocationSummary* GetLocations() const { return locations_; }
2118 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002119
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002121 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002122 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123
Alexandre Rames188d4312015-04-09 18:30:21 +01002124 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2125 // uses of this instruction by `other` are *not* updated.
2126 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2127 ReplaceWith(other);
2128 other->ReplaceInput(this, use_index);
2129 }
2130
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002131 // Move `this` instruction before `cursor`
2132 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002133
Vladimir Markofb337ea2015-11-25 15:25:10 +00002134 // Move `this` before its first user and out of any loops. If there is no
2135 // out-of-loop user that dominates all other users, move the instruction
2136 // to the end of the out-of-loop common dominator of the user's blocks.
2137 //
2138 // This can be used only on non-throwing instructions with no side effects that
2139 // have at least one use but no environment uses.
2140 void MoveBeforeFirstUserAndOutOfLoops();
2141
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002142#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002143 bool Is##type() const; \
2144 const H##type* As##type() const; \
2145 H##type* As##type();
2146
2147 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2148#undef INSTRUCTION_TYPE_CHECK
2149
2150#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002151 bool Is##type() const { return (As##type() != nullptr); } \
2152 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002153 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002154 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155#undef INSTRUCTION_TYPE_CHECK
2156
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002158 // TODO: this method is used by LICM and GVN with possibly different
2159 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002160 virtual bool CanBeMoved() const { return false; }
2161
2162 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002163 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 return false;
2165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
2167 // Returns whether any data encoded in the two instructions is equal.
2168 // This method does not look at the inputs. Both instructions must be
2169 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002170 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002171 return false;
2172 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173
2174 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002175 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002177 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002178
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002179 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2180 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2181 // the virtual function because the __attribute__((__pure__)) doesn't really
2182 // apply the strong requirement for virtual functions, preventing optimizations.
2183 InstructionKind GetKind() const PURE;
2184 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002185
2186 virtual size_t ComputeHashCode() const {
2187 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002188 for (const HInstruction* input : GetInputs()) {
2189 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002190 }
2191 return result;
2192 }
2193
2194 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002195 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002196 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002197
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198 size_t GetLifetimePosition() const { return lifetime_position_; }
2199 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2200 LiveInterval* GetLiveInterval() const { return live_interval_; }
2201 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2202 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2203
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002204 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2205
2206 // Returns whether the code generation of the instruction will require to have access
2207 // to the current method. Such instructions are:
2208 // (1): Instructions that require an environment, as calling the runtime requires
2209 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002210 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2211 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002212 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002213 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002214 }
2215
Vladimir Markodc151b22015-10-15 18:02:30 +01002216 // Returns whether the code generation of the instruction will require to have access
2217 // to the dex cache of the current method's declaring class via the current method.
2218 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002219
Mark Mendellc4701932015-04-10 13:18:51 -04002220 // Does this instruction have any use in an environment before
2221 // control flow hits 'other'?
2222 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2223
2224 // Remove all references to environment uses of this instruction.
2225 // The caller must ensure that this is safe to do.
2226 void RemoveEnvironmentUsers();
2227
Vladimir Markoa1de9182016-02-25 11:37:38 +00002228 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2229 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002230
David Brazdil1abb4192015-02-17 18:33:36 +00002231 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002232 // If set, the machine code for this instruction is assumed to be generated by
2233 // its users. Used by liveness analysis to compute use positions accordingly.
2234 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2235 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2236 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2237 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2238
Vladimir Marko372f10e2016-05-17 16:30:10 +01002239 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2240 return GetInputRecords()[i];
2241 }
2242
2243 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2244 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2245 input_records[index] = input;
2246 }
David Brazdil1abb4192015-02-17 18:33:36 +00002247
Vladimir Markoa1de9182016-02-25 11:37:38 +00002248 uint32_t GetPackedFields() const {
2249 return packed_fields_;
2250 }
2251
2252 template <size_t flag>
2253 bool GetPackedFlag() const {
2254 return (packed_fields_ & (1u << flag)) != 0u;
2255 }
2256
2257 template <size_t flag>
2258 void SetPackedFlag(bool value = true) {
2259 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2260 }
2261
2262 template <typename BitFieldType>
2263 typename BitFieldType::value_type GetPackedField() const {
2264 return BitFieldType::Decode(packed_fields_);
2265 }
2266
2267 template <typename BitFieldType>
2268 void SetPackedField(typename BitFieldType::value_type value) {
2269 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2270 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2271 }
2272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002274 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2275 auto before_use_node = uses_.before_begin();
2276 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2277 HInstruction* user = use_node->GetUser();
2278 size_t input_index = use_node->GetIndex();
2279 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2280 before_use_node = use_node;
2281 }
2282 }
2283
2284 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2285 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2286 if (next != uses_.end()) {
2287 HInstruction* next_user = next->GetUser();
2288 size_t next_index = next->GetIndex();
2289 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2290 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2291 }
2292 }
2293
2294 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2295 auto before_env_use_node = env_uses_.before_begin();
2296 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2297 HEnvironment* user = env_use_node->GetUser();
2298 size_t input_index = env_use_node->GetIndex();
2299 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2300 before_env_use_node = env_use_node;
2301 }
2302 }
2303
2304 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2305 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2306 if (next != env_uses_.end()) {
2307 HEnvironment* next_user = next->GetUser();
2308 size_t next_index = next->GetIndex();
2309 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2310 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2311 }
2312 }
David Brazdil1abb4192015-02-17 18:33:36 +00002313
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314 HInstruction* previous_;
2315 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002316 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002317 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002319 // An instruction gets an id when it is added to the graph.
2320 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002321 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 int id_;
2323
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002324 // When doing liveness analysis, instructions that have uses get an SSA index.
2325 int ssa_index_;
2326
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 // Packed fields.
2328 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002329
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002330 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002331 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002332
2333 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002334 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335
Nicolas Geoffray39468442014-09-02 15:17:15 +01002336 // The environment associated with this instruction. Not null if the instruction
2337 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002338 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002339
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002340 // Set by the code generator.
2341 LocationSummary* locations_;
2342
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002343 // Set by the liveness analysis.
2344 LiveInterval* live_interval_;
2345
2346 // Set by the liveness analysis, this is the position in a linear
2347 // order of blocks where this instruction's live interval start.
2348 size_t lifetime_position_;
2349
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002350 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 // The reference handle part of the reference type info.
2353 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002354 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002356
David Brazdil1abb4192015-02-17 18:33:36 +00002357 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002359 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002360 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002361 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362
2363 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2364};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002365std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002367// Iterates over the instructions, while preserving the next instruction
2368// in case the current instruction gets removed from the list by the user
2369// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370class HInstructionIterator : public ValueObject {
2371 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002372 explicit HInstructionIterator(const HInstructionList& instructions)
2373 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002374 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002375 }
2376
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002377 bool Done() const { return instruction_ == nullptr; }
2378 HInstruction* Current() const { return instruction_; }
2379 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002380 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002381 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382 }
2383
2384 private:
2385 HInstruction* instruction_;
2386 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002387
2388 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389};
2390
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002391// Iterates over the instructions without saving the next instruction,
2392// therefore handling changes in the graph potentially made by the user
2393// of this iterator.
2394class HInstructionIteratorHandleChanges : public ValueObject {
2395 public:
2396 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2397 : instruction_(instructions.first_instruction_) {
2398 }
2399
2400 bool Done() const { return instruction_ == nullptr; }
2401 HInstruction* Current() const { return instruction_; }
2402 void Advance() {
2403 instruction_ = instruction_->GetNext();
2404 }
2405
2406 private:
2407 HInstruction* instruction_;
2408
2409 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2410};
2411
2412
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002413class HBackwardInstructionIterator : public ValueObject {
2414 public:
2415 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2416 : instruction_(instructions.last_instruction_) {
2417 next_ = Done() ? nullptr : instruction_->GetPrevious();
2418 }
2419
2420 bool Done() const { return instruction_ == nullptr; }
2421 HInstruction* Current() const { return instruction_; }
2422 void Advance() {
2423 instruction_ = next_;
2424 next_ = Done() ? nullptr : instruction_->GetPrevious();
2425 }
2426
2427 private:
2428 HInstruction* instruction_;
2429 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002430
2431 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002432};
2433
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002434class HVariableInputSizeInstruction : public HInstruction {
2435 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002436 using HInstruction::GetInputRecords; // Keep the const version visible.
2437 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2438 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2439 }
2440
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002441 void AddInput(HInstruction* input);
2442 void InsertInputAt(size_t index, HInstruction* input);
2443 void RemoveInputAt(size_t index);
2444
Igor Murashkind01745e2017-04-05 16:40:31 -07002445 // Removes all the inputs.
2446 // Also removes this instructions from each input's use list
2447 // (for non-environment uses only).
2448 void RemoveAllInputs();
2449
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002450 protected:
2451 HVariableInputSizeInstruction(SideEffects side_effects,
2452 uint32_t dex_pc,
2453 ArenaAllocator* arena,
2454 size_t number_of_inputs,
2455 ArenaAllocKind kind)
2456 : HInstruction(side_effects, dex_pc),
2457 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2458
2459 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2460
2461 private:
2462 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2463};
2464
Vladimir Markof9f64412015-09-02 14:05:49 +01002465template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466class HTemplateInstruction: public HInstruction {
2467 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002468 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002470 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471
Vladimir Marko372f10e2016-05-17 16:30:10 +01002472 using HInstruction::GetInputRecords; // Keep the const version visible.
2473 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2474 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002475 }
2476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002478 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002479
2480 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481};
2482
Vladimir Markof9f64412015-09-02 14:05:49 +01002483// HTemplateInstruction specialization for N=0.
2484template<>
2485class HTemplateInstruction<0>: public HInstruction {
2486 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002487 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 : HInstruction(side_effects, dex_pc) {}
2489
Vladimir Markof9f64412015-09-02 14:05:49 +01002490 virtual ~HTemplateInstruction() {}
2491
Vladimir Marko372f10e2016-05-17 16:30:10 +01002492 using HInstruction::GetInputRecords; // Keep the const version visible.
2493 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2494 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002495 }
2496
2497 private:
2498 friend class SsaBuilder;
2499};
2500
Dave Allison20dfc792014-06-16 20:44:29 -07002501template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002502class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002503 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002504 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002505 : HTemplateInstruction<N>(side_effects, dex_pc) {
2506 this->template SetPackedField<TypeField>(type);
2507 }
Dave Allison20dfc792014-06-16 20:44:29 -07002508 virtual ~HExpression() {}
2509
Vladimir Markoa1de9182016-02-25 11:37:38 +00002510 Primitive::Type GetType() const OVERRIDE {
2511 return TypeField::Decode(this->GetPackedFields());
2512 }
Dave Allison20dfc792014-06-16 20:44:29 -07002513
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002514 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002515 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2516 static constexpr size_t kFieldTypeSize =
2517 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2518 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2519 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2520 "Too many packed fields.");
2521 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002522};
2523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2525// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002526class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2529 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002533 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2537};
2538
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539// Represents dex's RETURN opcodes. A HReturn is a control flow
2540// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002541class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2544 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002545 SetRawInputAt(0, value);
2546 }
2547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002550 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HReturn);
2554};
2555
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002556class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002557 public:
2558 HPhi(ArenaAllocator* arena,
2559 uint32_t reg_number,
2560 size_t number_of_inputs,
2561 Primitive::Type type,
2562 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002563 : HVariableInputSizeInstruction(
2564 SideEffects::None(),
2565 dex_pc,
2566 arena,
2567 number_of_inputs,
2568 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002569 reg_number_(reg_number) {
2570 SetPackedField<TypeField>(ToPhiType(type));
2571 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2572 // Phis are constructed live and marked dead if conflicting or unused.
2573 // Individual steps of SsaBuilder should assume that if a phi has been
2574 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2575 SetPackedFlag<kFlagIsLive>(true);
2576 SetPackedFlag<kFlagCanBeNull>(true);
2577 }
2578
2579 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2580 static Primitive::Type ToPhiType(Primitive::Type type) {
2581 return Primitive::PrimitiveKind(type);
2582 }
2583
2584 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2585
David Brazdildee58d62016-04-07 09:54:26 +00002586 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2587 void SetType(Primitive::Type new_type) {
2588 // Make sure that only valid type changes occur. The following are allowed:
2589 // (1) int -> float/ref (primitive type propagation),
2590 // (2) long -> double (primitive type propagation).
2591 DCHECK(GetType() == new_type ||
2592 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2593 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2594 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2595 SetPackedField<TypeField>(new_type);
2596 }
2597
2598 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2599 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2600
2601 uint32_t GetRegNumber() const { return reg_number_; }
2602
2603 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2604 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2605 bool IsDead() const { return !IsLive(); }
2606 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2607
Vladimir Markoe9004912016-06-16 16:50:52 +01002608 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002609 return other != nullptr
2610 && other->IsPhi()
2611 && other->AsPhi()->GetBlock() == GetBlock()
2612 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2613 }
2614
2615 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2616 // An equivalent phi is a phi having the same dex register and type.
2617 // It assumes that phis with the same dex register are adjacent.
2618 HPhi* GetNextEquivalentPhiWithSameType() {
2619 HInstruction* next = GetNext();
2620 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2621 if (next->GetType() == GetType()) {
2622 return next->AsPhi();
2623 }
2624 next = next->GetNext();
2625 }
2626 return nullptr;
2627 }
2628
2629 DECLARE_INSTRUCTION(Phi);
2630
David Brazdildee58d62016-04-07 09:54:26 +00002631 private:
2632 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2633 static constexpr size_t kFieldTypeSize =
2634 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2635 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2636 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2637 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2638 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2639 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2640
David Brazdildee58d62016-04-07 09:54:26 +00002641 const uint32_t reg_number_;
2642
2643 DISALLOW_COPY_AND_ASSIGN(HPhi);
2644};
2645
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002647// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002649class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002652
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002653 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002655 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656
2657 private:
2658 DISALLOW_COPY_AND_ASSIGN(HExit);
2659};
2660
2661// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002662class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002663 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002665
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002666 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002667
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002668 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002669 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002670 }
2671
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002672 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673
2674 private:
2675 DISALLOW_COPY_AND_ASSIGN(HGoto);
2676};
2677
Roland Levillain9867bc72015-08-05 10:21:34 +01002678class HConstant : public HExpression<0> {
2679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002680 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2681 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002682
2683 bool CanBeMoved() const OVERRIDE { return true; }
2684
Roland Levillain1a653882016-03-18 18:05:57 +00002685 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002687 // Is this constant 0 in the arithmetic sense?
2688 virtual bool IsArithmeticZero() const { return false; }
2689 // Is this constant a 0-bit pattern?
2690 virtual bool IsZeroBitPattern() const { return false; }
2691 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 virtual bool IsOne() const { return false; }
2693
David Brazdil77a48ae2015-09-15 12:34:04 +00002694 virtual uint64_t GetValueAsUint64() const = 0;
2695
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002696 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002697
2698 private:
2699 DISALLOW_COPY_AND_ASSIGN(HConstant);
2700};
2701
Vladimir Markofcb503c2016-05-18 12:48:17 +01002702class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002703 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002704 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002705 return true;
2706 }
2707
David Brazdil77a48ae2015-09-15 12:34:04 +00002708 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2709
Roland Levillain9867bc72015-08-05 10:21:34 +01002710 size_t ComputeHashCode() const OVERRIDE { return 0; }
2711
Roland Levillain1a653882016-03-18 18:05:57 +00002712 // The null constant representation is a 0-bit pattern.
2713 virtual bool IsZeroBitPattern() const { return true; }
2714
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 DECLARE_INSTRUCTION(NullConstant);
2716
2717 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002718 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002719
2720 friend class HGraph;
2721 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2722};
2723
2724// Constants of the type int. Those can be from Dex instructions, or
2725// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002726class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 public:
2728 int32_t GetValue() const { return value_; }
2729
David Brazdil9f389d42015-10-01 14:32:56 +01002730 uint64_t GetValueAsUint64() const OVERRIDE {
2731 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2732 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002733
Vladimir Marko372f10e2016-05-17 16:30:10 +01002734 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002735 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002736 return other->AsIntConstant()->value_ == value_;
2737 }
2738
2739 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2740
2741 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002742 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2743 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002744 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2745
Roland Levillain1a653882016-03-18 18:05:57 +00002746 // Integer constants are used to encode Boolean values as well,
2747 // where 1 means true and 0 means false.
2748 bool IsTrue() const { return GetValue() == 1; }
2749 bool IsFalse() const { return GetValue() == 0; }
2750
Roland Levillain9867bc72015-08-05 10:21:34 +01002751 DECLARE_INSTRUCTION(IntConstant);
2752
2753 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002754 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2755 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2756 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2757 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002758
2759 const int32_t value_;
2760
2761 friend class HGraph;
2762 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2763 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2764 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2765};
2766
Vladimir Markofcb503c2016-05-18 12:48:17 +01002767class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002768 public:
2769 int64_t GetValue() const { return value_; }
2770
David Brazdil77a48ae2015-09-15 12:34:04 +00002771 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2772
Vladimir Marko372f10e2016-05-17 16:30:10 +01002773 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002774 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002775 return other->AsLongConstant()->value_ == value_;
2776 }
2777
2778 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2779
2780 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002781 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2782 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002783 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2784
2785 DECLARE_INSTRUCTION(LongConstant);
2786
2787 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002788 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2789 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002790
2791 const int64_t value_;
2792
2793 friend class HGraph;
2794 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2795};
Dave Allison20dfc792014-06-16 20:44:29 -07002796
Vladimir Markofcb503c2016-05-18 12:48:17 +01002797class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002798 public:
2799 float GetValue() const { return value_; }
2800
2801 uint64_t GetValueAsUint64() const OVERRIDE {
2802 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2803 }
2804
Vladimir Marko372f10e2016-05-17 16:30:10 +01002805 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002806 DCHECK(other->IsFloatConstant()) << other->DebugName();
2807 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2808 }
2809
2810 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2811
2812 bool IsMinusOne() const OVERRIDE {
2813 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2814 }
Roland Levillain1a653882016-03-18 18:05:57 +00002815 bool IsArithmeticZero() const OVERRIDE {
2816 return std::fpclassify(value_) == FP_ZERO;
2817 }
2818 bool IsArithmeticPositiveZero() const {
2819 return IsArithmeticZero() && !std::signbit(value_);
2820 }
2821 bool IsArithmeticNegativeZero() const {
2822 return IsArithmeticZero() && std::signbit(value_);
2823 }
2824 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002825 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002826 }
2827 bool IsOne() const OVERRIDE {
2828 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2829 }
2830 bool IsNaN() const {
2831 return std::isnan(value_);
2832 }
2833
2834 DECLARE_INSTRUCTION(FloatConstant);
2835
2836 private:
2837 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2838 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2839 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2840 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2841
2842 const float value_;
2843
2844 // Only the SsaBuilder and HGraph can create floating-point constants.
2845 friend class SsaBuilder;
2846 friend class HGraph;
2847 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2848};
2849
Vladimir Markofcb503c2016-05-18 12:48:17 +01002850class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002851 public:
2852 double GetValue() const { return value_; }
2853
2854 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2855
Vladimir Marko372f10e2016-05-17 16:30:10 +01002856 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002857 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2858 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2859 }
2860
2861 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2862
2863 bool IsMinusOne() const OVERRIDE {
2864 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2865 }
Roland Levillain1a653882016-03-18 18:05:57 +00002866 bool IsArithmeticZero() const OVERRIDE {
2867 return std::fpclassify(value_) == FP_ZERO;
2868 }
2869 bool IsArithmeticPositiveZero() const {
2870 return IsArithmeticZero() && !std::signbit(value_);
2871 }
2872 bool IsArithmeticNegativeZero() const {
2873 return IsArithmeticZero() && std::signbit(value_);
2874 }
2875 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002876 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002877 }
2878 bool IsOne() const OVERRIDE {
2879 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2880 }
2881 bool IsNaN() const {
2882 return std::isnan(value_);
2883 }
2884
2885 DECLARE_INSTRUCTION(DoubleConstant);
2886
2887 private:
2888 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2889 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2890 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2891 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2892
2893 const double value_;
2894
2895 // Only the SsaBuilder and HGraph can create floating-point constants.
2896 friend class SsaBuilder;
2897 friend class HGraph;
2898 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2899};
2900
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002901// Conditional branch. A block ending with an HIf instruction must have
2902// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002903class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002904 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002905 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2906 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002907 SetRawInputAt(0, input);
2908 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002909
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002910 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002911
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002912 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002913 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002914 }
2915
2916 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002917 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002918 }
2919
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002920 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002921
2922 private:
2923 DISALLOW_COPY_AND_ASSIGN(HIf);
2924};
2925
David Brazdilfc6a86a2015-06-26 10:33:45 +00002926
2927// Abstract instruction which marks the beginning and/or end of a try block and
2928// links it to the respective exception handlers. Behaves the same as a Goto in
2929// non-exceptional control flow.
2930// Normal-flow successor is stored at index zero, exception handlers under
2931// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002932class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002933 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002934 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002935 kEntry,
2936 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002937 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002938 };
2939
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002940 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002941 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2942 SetPackedField<BoundaryKindField>(kind);
2943 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002944
2945 bool IsControlFlow() const OVERRIDE { return true; }
2946
2947 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002948 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002949
David Brazdild26a4112015-11-10 11:07:31 +00002950 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2951 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2952 }
2953
David Brazdilfc6a86a2015-06-26 10:33:45 +00002954 // Returns whether `handler` is among its exception handlers (non-zero index
2955 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002956 bool HasExceptionHandler(const HBasicBlock& handler) const {
2957 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002958 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002959 }
2960
2961 // If not present already, adds `handler` to its block's list of exception
2962 // handlers.
2963 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002964 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002965 GetBlock()->AddSuccessor(handler);
2966 }
2967 }
2968
Vladimir Markoa1de9182016-02-25 11:37:38 +00002969 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2970 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002971
David Brazdilffee3d32015-07-06 11:48:53 +01002972 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2973
David Brazdilfc6a86a2015-06-26 10:33:45 +00002974 DECLARE_INSTRUCTION(TryBoundary);
2975
2976 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002977 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2978 static constexpr size_t kFieldBoundaryKindSize =
2979 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2980 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2981 kFieldBoundaryKind + kFieldBoundaryKindSize;
2982 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2983 "Too many packed fields.");
2984 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002985
2986 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2987};
2988
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002989// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002990class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002991 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002992 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2993 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002994 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002995 : HVariableInputSizeInstruction(
2996 SideEffects::All(),
2997 dex_pc,
2998 arena,
2999 /* number_of_inputs */ 1,
3000 kArenaAllocMisc) {
3001 SetPackedFlag<kFieldCanBeMoved>(false);
3002 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003003 SetRawInputAt(0, cond);
3004 }
3005
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003006 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3007 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3008 // instead of `guard`.
3009 // We set CanTriggerGC to prevent any intermediate address to be live
3010 // at the point of the `HDeoptimize`.
3011 HDeoptimize(ArenaAllocator* arena,
3012 HInstruction* cond,
3013 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003014 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003015 uint32_t dex_pc)
3016 : HVariableInputSizeInstruction(
3017 SideEffects::CanTriggerGC(),
3018 dex_pc,
3019 arena,
3020 /* number_of_inputs */ 2,
3021 kArenaAllocMisc) {
3022 SetPackedFlag<kFieldCanBeMoved>(true);
3023 SetPackedField<DeoptimizeKindField>(kind);
3024 SetRawInputAt(0, cond);
3025 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003026 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003027
3028 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3029
3030 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3031 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3032 }
3033
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003034 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003035
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003036 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003037
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003038 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003039
3040 Primitive::Type GetType() const OVERRIDE {
3041 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3042 }
3043
3044 bool GuardsAnInput() const {
3045 return InputCount() == 2;
3046 }
3047
3048 HInstruction* GuardedInput() const {
3049 DCHECK(GuardsAnInput());
3050 return InputAt(1);
3051 }
3052
3053 void RemoveGuard() {
3054 RemoveInputAt(1);
3055 }
3056
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003057 DECLARE_INSTRUCTION(Deoptimize);
3058
3059 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003060 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3061 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3062 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003063 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003064 static constexpr size_t kNumberOfDeoptimizePackedBits =
3065 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3066 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3067 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003068 using DeoptimizeKindField =
3069 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003070
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003071 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3072};
3073
Mingyao Yang063fc772016-08-02 11:02:54 -07003074// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3075// The compiled code checks this flag value in a guard before devirtualized call and
3076// if it's true, starts to do deoptimization.
3077// It has a 4-byte slot on stack.
3078// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003079class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003080 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003081 // CHA guards are only optimized in a separate pass and it has no side effects
3082 // with regard to other passes.
3083 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3084 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003085 }
3086
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003087 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3088
3089 // We do all CHA guard elimination/motion in a single pass, after which there is no
3090 // further guard elimination/motion since a guard might have been used for justification
3091 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3092 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003093 bool CanBeMoved() const OVERRIDE { return false; }
3094
3095 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3096
3097 private:
3098 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3099};
3100
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003101// Represents the ArtMethod that was passed as a first argument to
3102// the method. It is used by instructions that depend on it, like
3103// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003104class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003105 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003106 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3107 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003108
3109 DECLARE_INSTRUCTION(CurrentMethod);
3110
3111 private:
3112 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3113};
3114
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003115// Fetches an ArtMethod from the virtual table or the interface method table
3116// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003117class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003118 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003119 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003120 kVTable,
3121 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003123 };
3124 HClassTableGet(HInstruction* cls,
3125 Primitive::Type type,
3126 TableKind kind,
3127 size_t index,
3128 uint32_t dex_pc)
3129 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003130 index_(index) {
3131 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 SetRawInputAt(0, cls);
3133 }
3134
3135 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003136 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003137 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003138 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003139 }
3140
Vladimir Markoa1de9182016-02-25 11:37:38 +00003141 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003142 size_t GetIndex() const { return index_; }
3143
3144 DECLARE_INSTRUCTION(ClassTableGet);
3145
3146 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003147 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3148 static constexpr size_t kFieldTableKindSize =
3149 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3150 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3151 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3152 "Too many packed fields.");
3153 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3154
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003155 // The index of the ArtMethod in the table.
3156 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003157
3158 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3159};
3160
Mark Mendellfe57faa2015-09-18 09:26:15 -04003161// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3162// have one successor for each entry in the switch table, and the final successor
3163// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003164class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003165 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003166 HPackedSwitch(int32_t start_value,
3167 uint32_t num_entries,
3168 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003169 uint32_t dex_pc = kNoDexPc)
3170 : HTemplateInstruction(SideEffects::None(), dex_pc),
3171 start_value_(start_value),
3172 num_entries_(num_entries) {
3173 SetRawInputAt(0, input);
3174 }
3175
3176 bool IsControlFlow() const OVERRIDE { return true; }
3177
3178 int32_t GetStartValue() const { return start_value_; }
3179
Vladimir Marko211c2112015-09-24 16:52:33 +01003180 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003181
3182 HBasicBlock* GetDefaultBlock() const {
3183 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003184 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003185 }
3186 DECLARE_INSTRUCTION(PackedSwitch);
3187
3188 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003189 const int32_t start_value_;
3190 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003191
3192 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3193};
3194
Roland Levillain88cb1752014-10-20 16:36:47 +01003195class HUnaryOperation : public HExpression<1> {
3196 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003197 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3198 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003199 SetRawInputAt(0, input);
3200 }
3201
3202 HInstruction* GetInput() const { return InputAt(0); }
3203 Primitive::Type GetResultType() const { return GetType(); }
3204
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003205 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003206 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003207 return true;
3208 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003209
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 // Try to statically evaluate `this` and return a HConstant
3211 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003212 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003213 HConstant* TryStaticEvaluation() const;
3214
3215 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003216 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3217 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003218 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3219 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003220
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003221 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003222
3223 private:
3224 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3225};
3226
Dave Allison20dfc792014-06-16 20:44:29 -07003227class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003228 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003229 HBinaryOperation(Primitive::Type result_type,
3230 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003231 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003232 SideEffects side_effects = SideEffects::None(),
3233 uint32_t dex_pc = kNoDexPc)
3234 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003235 SetRawInputAt(0, left);
3236 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003237 }
3238
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003239 HInstruction* GetLeft() const { return InputAt(0); }
3240 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003241 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003242
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003243 virtual bool IsCommutative() const { return false; }
3244
3245 // Put constant on the right.
3246 // Returns whether order is changed.
3247 bool OrderInputsWithConstantOnTheRight() {
3248 HInstruction* left = InputAt(0);
3249 HInstruction* right = InputAt(1);
3250 if (left->IsConstant() && !right->IsConstant()) {
3251 ReplaceInput(right, 0);
3252 ReplaceInput(left, 1);
3253 return true;
3254 }
3255 return false;
3256 }
3257
3258 // Order inputs by instruction id, but favor constant on the right side.
3259 // This helps GVN for commutative ops.
3260 void OrderInputs() {
3261 DCHECK(IsCommutative());
3262 HInstruction* left = InputAt(0);
3263 HInstruction* right = InputAt(1);
3264 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3265 return;
3266 }
3267 if (OrderInputsWithConstantOnTheRight()) {
3268 return;
3269 }
3270 // Order according to instruction id.
3271 if (left->GetId() > right->GetId()) {
3272 ReplaceInput(right, 0);
3273 ReplaceInput(left, 1);
3274 }
3275 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003276
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003277 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003278 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003279 return true;
3280 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003281
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 // Try to statically evaluate `this` and return a HConstant
3283 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003284 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003285 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003286
3287 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003288 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3289 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003290 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3291 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003292 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003293 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3294 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003295 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3296 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003297 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3298 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003299 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3301 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003302
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003303 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003304 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003305 HConstant* GetConstantRight() const;
3306
3307 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003308 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003309 HInstruction* GetLeastConstantLeft() const;
3310
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003311 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003312
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003313 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003314 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3315};
3316
Mark Mendellc4701932015-04-10 13:18:51 -04003317// The comparison bias applies for floating point operations and indicates how NaN
3318// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003319enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003320 kNoBias, // bias is not applicable (i.e. for long operation)
3321 kGtBias, // return 1 for NaN comparisons
3322 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003323 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003324};
3325
Roland Levillain31dd3d62016-02-16 12:21:02 +00003326std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3327
Dave Allison20dfc792014-06-16 20:44:29 -07003328class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003329 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003330 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003331 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3332 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3333 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003334
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003335 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003336 // `instruction`, and disregard moves in between.
3337 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003338
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003339 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003340
3341 virtual IfCondition GetCondition() const = 0;
3342
Mark Mendellc4701932015-04-10 13:18:51 -04003343 virtual IfCondition GetOppositeCondition() const = 0;
3344
Vladimir Markoa1de9182016-02-25 11:37:38 +00003345 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003346 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3347
Vladimir Markoa1de9182016-02-25 11:37:38 +00003348 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3349 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003350
Vladimir Marko372f10e2016-05-17 16:30:10 +01003351 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003352 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003353 }
3354
Roland Levillain4fa13f62015-07-06 18:11:54 +01003355 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003357 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003358 if (if_cond == kCondNE) {
3359 return true;
3360 } else if (if_cond == kCondEQ) {
3361 return false;
3362 }
3363 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003364 }
3365
3366 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003367 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003368 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003369 if (if_cond == kCondEQ) {
3370 return true;
3371 } else if (if_cond == kCondNE) {
3372 return false;
3373 }
3374 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003375 }
3376
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003378 // Needed if we merge a HCompare into a HCondition.
3379 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3380 static constexpr size_t kFieldComparisonBiasSize =
3381 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3382 static constexpr size_t kNumberOfConditionPackedBits =
3383 kFieldComparisonBias + kFieldComparisonBiasSize;
3384 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3385 using ComparisonBiasField =
3386 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3387
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 template <typename T>
3389 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3390
3391 template <typename T>
3392 int32_t CompareFP(T x, T y) const {
3393 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3394 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3395 // Handle the bias.
3396 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3397 }
3398
3399 // Return an integer constant containing the result of a condition evaluated at compile time.
3400 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3401 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3402 }
3403
Dave Allison20dfc792014-06-16 20:44:29 -07003404 private:
3405 DISALLOW_COPY_AND_ASSIGN(HCondition);
3406};
3407
3408// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003409class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003410 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003411 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3412 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003413
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003414 bool IsCommutative() const OVERRIDE { return true; }
3415
Vladimir Marko9e23df52015-11-10 17:14:35 +00003416 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3417 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003418 return MakeConstantCondition(true, GetDexPc());
3419 }
3420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3422 }
3423 // In the following Evaluate methods, a HCompare instruction has
3424 // been merged into this HEqual instruction; evaluate it as
3425 // `Compare(x, y) == 0`.
3426 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3427 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3428 GetDexPc());
3429 }
3430 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3431 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3432 }
3433 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3434 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003435 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003436
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003437 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003439 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003440 return kCondEQ;
3441 }
3442
Mark Mendellc4701932015-04-10 13:18:51 -04003443 IfCondition GetOppositeCondition() const OVERRIDE {
3444 return kCondNE;
3445 }
3446
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003447 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003448 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003450 DISALLOW_COPY_AND_ASSIGN(HEqual);
3451};
3452
Vladimir Markofcb503c2016-05-18 12:48:17 +01003453class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003454 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003455 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3456 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003457
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003458 bool IsCommutative() const OVERRIDE { return true; }
3459
Vladimir Marko9e23df52015-11-10 17:14:35 +00003460 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3461 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(false, GetDexPc());
3463 }
3464 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3465 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3466 }
3467 // In the following Evaluate methods, a HCompare instruction has
3468 // been merged into this HNotEqual instruction; evaluate it as
3469 // `Compare(x, y) != 0`.
3470 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3471 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3472 }
3473 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3474 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3475 }
3476 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3477 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003478 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003479
Dave Allison20dfc792014-06-16 20:44:29 -07003480 DECLARE_INSTRUCTION(NotEqual);
3481
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003482 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003483 return kCondNE;
3484 }
3485
Mark Mendellc4701932015-04-10 13:18:51 -04003486 IfCondition GetOppositeCondition() const OVERRIDE {
3487 return kCondEQ;
3488 }
3489
Dave Allison20dfc792014-06-16 20:44:29 -07003490 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003491 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003492
Dave Allison20dfc792014-06-16 20:44:29 -07003493 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3494};
3495
Vladimir Markofcb503c2016-05-18 12:48:17 +01003496class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003497 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003498 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3499 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003500
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003503 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 // In the following Evaluate methods, a HCompare instruction has
3505 // been merged into this HLessThan instruction; evaluate it as
3506 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003507 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3509 }
3510 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3511 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3512 }
3513 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3514 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003515 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003516
Dave Allison20dfc792014-06-16 20:44:29 -07003517 DECLARE_INSTRUCTION(LessThan);
3518
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003519 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003520 return kCondLT;
3521 }
3522
Mark Mendellc4701932015-04-10 13:18:51 -04003523 IfCondition GetOppositeCondition() const OVERRIDE {
3524 return kCondGE;
3525 }
3526
Dave Allison20dfc792014-06-16 20:44:29 -07003527 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003528 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003529
Dave Allison20dfc792014-06-16 20:44:29 -07003530 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3531};
3532
Vladimir Markofcb503c2016-05-18 12:48:17 +01003533class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003534 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003535 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3536 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003537
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003540 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003541 // In the following Evaluate methods, a HCompare instruction has
3542 // been merged into this HLessThanOrEqual instruction; evaluate it as
3543 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3546 }
3547 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3548 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3549 }
3550 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3551 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003552 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003553
Dave Allison20dfc792014-06-16 20:44:29 -07003554 DECLARE_INSTRUCTION(LessThanOrEqual);
3555
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003556 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003557 return kCondLE;
3558 }
3559
Mark Mendellc4701932015-04-10 13:18:51 -04003560 IfCondition GetOppositeCondition() const OVERRIDE {
3561 return kCondGT;
3562 }
3563
Dave Allison20dfc792014-06-16 20:44:29 -07003564 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003565 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003566
Dave Allison20dfc792014-06-16 20:44:29 -07003567 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3568};
3569
Vladimir Markofcb503c2016-05-18 12:48:17 +01003570class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003571 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003572 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3573 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003574
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003577 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 // In the following Evaluate methods, a HCompare instruction has
3579 // been merged into this HGreaterThan instruction; evaluate it as
3580 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3583 }
3584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3585 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3586 }
3587 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3588 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003589 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003590
Dave Allison20dfc792014-06-16 20:44:29 -07003591 DECLARE_INSTRUCTION(GreaterThan);
3592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003593 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003594 return kCondGT;
3595 }
3596
Mark Mendellc4701932015-04-10 13:18:51 -04003597 IfCondition GetOppositeCondition() const OVERRIDE {
3598 return kCondLE;
3599 }
3600
Dave Allison20dfc792014-06-16 20:44:29 -07003601 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003602 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003603
Dave Allison20dfc792014-06-16 20:44:29 -07003604 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3605};
3606
Vladimir Markofcb503c2016-05-18 12:48:17 +01003607class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003609 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3610 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003611
Roland Levillain9867bc72015-08-05 10:21:34 +01003612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003614 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 // In the following Evaluate methods, a HCompare instruction has
3616 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3617 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003618 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003619 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3620 }
3621 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3622 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3623 }
3624 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3625 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003626 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003627
Dave Allison20dfc792014-06-16 20:44:29 -07003628 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3629
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003630 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003631 return kCondGE;
3632 }
3633
Mark Mendellc4701932015-04-10 13:18:51 -04003634 IfCondition GetOppositeCondition() const OVERRIDE {
3635 return kCondLT;
3636 }
3637
Dave Allison20dfc792014-06-16 20:44:29 -07003638 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003639 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003640
Dave Allison20dfc792014-06-16 20:44:29 -07003641 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3642};
3643
Vladimir Markofcb503c2016-05-18 12:48:17 +01003644class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003645 public:
3646 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3647 : HCondition(first, second, dex_pc) {}
3648
3649 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003650 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003651 }
3652 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3654 }
3655 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3656 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3657 LOG(FATAL) << DebugName() << " is not defined for float values";
3658 UNREACHABLE();
3659 }
3660 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3661 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3662 LOG(FATAL) << DebugName() << " is not defined for double values";
3663 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003664 }
3665
3666 DECLARE_INSTRUCTION(Below);
3667
3668 IfCondition GetCondition() const OVERRIDE {
3669 return kCondB;
3670 }
3671
3672 IfCondition GetOppositeCondition() const OVERRIDE {
3673 return kCondAE;
3674 }
3675
3676 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003677 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003678 return MakeUnsigned(x) < MakeUnsigned(y);
3679 }
Aart Bike9f37602015-10-09 11:15:55 -07003680
3681 DISALLOW_COPY_AND_ASSIGN(HBelow);
3682};
3683
Vladimir Markofcb503c2016-05-18 12:48:17 +01003684class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003685 public:
3686 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3687 : HCondition(first, second, dex_pc) {}
3688
3689 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003690 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003691 }
3692 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003693 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3694 }
3695 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3696 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3697 LOG(FATAL) << DebugName() << " is not defined for float values";
3698 UNREACHABLE();
3699 }
3700 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3701 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3702 LOG(FATAL) << DebugName() << " is not defined for double values";
3703 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003704 }
3705
3706 DECLARE_INSTRUCTION(BelowOrEqual);
3707
3708 IfCondition GetCondition() const OVERRIDE {
3709 return kCondBE;
3710 }
3711
3712 IfCondition GetOppositeCondition() const OVERRIDE {
3713 return kCondA;
3714 }
3715
3716 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003717 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003718 return MakeUnsigned(x) <= MakeUnsigned(y);
3719 }
Aart Bike9f37602015-10-09 11:15:55 -07003720
3721 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3722};
3723
Vladimir Markofcb503c2016-05-18 12:48:17 +01003724class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003725 public:
3726 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3727 : HCondition(first, second, dex_pc) {}
3728
3729 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003730 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003731 }
3732 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3734 }
3735 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3736 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3737 LOG(FATAL) << DebugName() << " is not defined for float values";
3738 UNREACHABLE();
3739 }
3740 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3741 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3742 LOG(FATAL) << DebugName() << " is not defined for double values";
3743 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003744 }
3745
3746 DECLARE_INSTRUCTION(Above);
3747
3748 IfCondition GetCondition() const OVERRIDE {
3749 return kCondA;
3750 }
3751
3752 IfCondition GetOppositeCondition() const OVERRIDE {
3753 return kCondBE;
3754 }
3755
3756 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003757 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003758 return MakeUnsigned(x) > MakeUnsigned(y);
3759 }
Aart Bike9f37602015-10-09 11:15:55 -07003760
3761 DISALLOW_COPY_AND_ASSIGN(HAbove);
3762};
3763
Vladimir Markofcb503c2016-05-18 12:48:17 +01003764class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003765 public:
3766 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3767 : HCondition(first, second, dex_pc) {}
3768
3769 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003771 }
3772 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003773 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3774 }
3775 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3776 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3777 LOG(FATAL) << DebugName() << " is not defined for float values";
3778 UNREACHABLE();
3779 }
3780 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3781 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3782 LOG(FATAL) << DebugName() << " is not defined for double values";
3783 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003784 }
3785
3786 DECLARE_INSTRUCTION(AboveOrEqual);
3787
3788 IfCondition GetCondition() const OVERRIDE {
3789 return kCondAE;
3790 }
3791
3792 IfCondition GetOppositeCondition() const OVERRIDE {
3793 return kCondB;
3794 }
3795
3796 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003797 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003798 return MakeUnsigned(x) >= MakeUnsigned(y);
3799 }
Aart Bike9f37602015-10-09 11:15:55 -07003800
3801 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3802};
Dave Allison20dfc792014-06-16 20:44:29 -07003803
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003804// Instruction to check how two inputs compare to each other.
3805// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003806class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003807 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003808 // Note that `comparison_type` is the type of comparison performed
3809 // between the comparison's inputs, not the type of the instantiated
3810 // HCompare instruction (which is always Primitive::kPrimInt).
3811 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003812 HInstruction* first,
3813 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003814 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003815 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003816 : HBinaryOperation(Primitive::kPrimInt,
3817 first,
3818 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003819 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 dex_pc) {
3821 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003822 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3823 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003824 }
3825
Roland Levillain9867bc72015-08-05 10:21:34 +01003826 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003827 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3828
3829 template <typename T>
3830 int32_t ComputeFP(T x, T y) const {
3831 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3832 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3833 // Handle the bias.
3834 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3835 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003836
Roland Levillain9867bc72015-08-05 10:21:34 +01003837 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003838 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3839 // reach this code path when processing a freshly built HIR
3840 // graph. However HCompare integer instructions can be synthesized
3841 // by the instruction simplifier to implement IntegerCompare and
3842 // IntegerSignum intrinsics, so we have to handle this case.
3843 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003844 }
3845 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003846 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3847 }
3848 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3849 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3850 }
3851 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3852 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003853 }
3854
Vladimir Marko372f10e2016-05-17 16:30:10 +01003855 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003856 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003857 }
3858
Vladimir Markoa1de9182016-02-25 11:37:38 +00003859 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003860
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003862 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003863 bool IsGtBias() const {
3864 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003867
Roland Levillain1693a1f2016-03-15 14:57:31 +00003868 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3869 // Comparisons do not require a runtime call in any back end.
3870 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003871 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003872
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003873 DECLARE_INSTRUCTION(Compare);
3874
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003876 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3877 static constexpr size_t kFieldComparisonBiasSize =
3878 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3879 static constexpr size_t kNumberOfComparePackedBits =
3880 kFieldComparisonBias + kFieldComparisonBiasSize;
3881 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3882 using ComparisonBiasField =
3883 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3884
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 // Return an integer constant containing the result of a comparison evaluated at compile time.
3886 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3887 DCHECK(value == -1 || value == 0 || value == 1) << value;
3888 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3889 }
3890
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003891 private:
3892 DISALLOW_COPY_AND_ASSIGN(HCompare);
3893};
3894
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003895class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003896 public:
3897 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003898 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003899 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003900 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003901 bool finalizable,
3902 QuickEntrypointEnum entrypoint)
3903 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3904 type_index_(type_index),
3905 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003906 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003907 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003908 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003909 }
3910
Andreas Gampea5b09a62016-11-17 15:21:22 -08003911 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003912 const DexFile& GetDexFile() const { return dex_file_; }
3913
3914 // Calls runtime so needs an environment.
3915 bool NeedsEnvironment() const OVERRIDE { return true; }
3916
Mingyao Yang062157f2016-03-02 10:15:36 -08003917 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3918 bool CanThrow() const OVERRIDE { return true; }
3919
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003920 bool NeedsChecks() const {
3921 return entrypoint_ == kQuickAllocObjectWithChecks;
3922 }
David Brazdil6de19382016-01-08 17:37:10 +00003923
Vladimir Markoa1de9182016-02-25 11:37:38 +00003924 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003925
3926 bool CanBeNull() const OVERRIDE { return false; }
3927
3928 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3929
3930 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3931 entrypoint_ = entrypoint;
3932 }
3933
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003934 HLoadClass* GetLoadClass() const {
3935 HInstruction* input = InputAt(0);
3936 if (input->IsClinitCheck()) {
3937 input = input->InputAt(0);
3938 }
3939 DCHECK(input->IsLoadClass());
3940 return input->AsLoadClass();
3941 }
3942
David Brazdil6de19382016-01-08 17:37:10 +00003943 bool IsStringAlloc() const;
3944
3945 DECLARE_INSTRUCTION(NewInstance);
3946
3947 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003948 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3950 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3951 "Too many packed fields.");
3952
Andreas Gampea5b09a62016-11-17 15:21:22 -08003953 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003954 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003955 QuickEntrypointEnum entrypoint_;
3956
3957 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3958};
3959
Agi Csaki05f20562015-08-19 14:58:14 -07003960enum IntrinsicNeedsEnvironmentOrCache {
3961 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3962 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003963};
3964
Aart Bik5d75afe2015-12-14 11:57:01 -08003965enum IntrinsicSideEffects {
3966 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3967 kReadSideEffects, // Intrinsic may read heap memory.
3968 kWriteSideEffects, // Intrinsic may write heap memory.
3969 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3970};
3971
3972enum IntrinsicExceptions {
3973 kNoThrow, // Intrinsic does not throw any exceptions.
3974 kCanThrow // Intrinsic may throw exceptions.
3975};
3976
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003977class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003978 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003979 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003980
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003981 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003982 SetRawInputAt(index, argument);
3983 }
3984
Roland Levillain3e3d7332015-04-28 11:00:54 +01003985 // Return the number of arguments. This number can be lower than
3986 // the number of inputs returned by InputCount(), as some invoke
3987 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3988 // inputs at the end of their list of inputs.
3989 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3990
Vladimir Markoa1de9182016-02-25 11:37:38 +00003991 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003992
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003993 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3994
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003995 InvokeType GetInvokeType() const {
3996 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003997 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003998
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003999 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004000 return intrinsic_;
4001 }
4002
Aart Bik5d75afe2015-12-14 11:57:01 -08004003 void SetIntrinsic(Intrinsics intrinsic,
4004 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4005 IntrinsicSideEffects side_effects,
4006 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004007
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004008 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004009 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004010 }
4011
Aart Bikff7d89c2016-11-07 08:49:28 -08004012 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4013
Vladimir Markoa1de9182016-02-25 11:37:38 +00004014 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004015
Aart Bik71bf7b42016-11-16 10:17:46 -08004016 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004017
Vladimir Marko372f10e2016-05-17 16:30:10 +01004018 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004019 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4020 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004021
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004022 uint32_t* GetIntrinsicOptimizations() {
4023 return &intrinsic_optimizations_;
4024 }
4025
4026 const uint32_t* GetIntrinsicOptimizations() const {
4027 return &intrinsic_optimizations_;
4028 }
4029
4030 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4031
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004032 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004033 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004034
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004035 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004036
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004037 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004038 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4039 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004040 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4041 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004042 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004043 static constexpr size_t kFieldReturnTypeSize =
4044 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4045 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4046 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4047 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004048 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4050
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004051 HInvoke(ArenaAllocator* arena,
4052 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004053 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004054 Primitive::Type return_type,
4055 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004056 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004057 ArtMethod* resolved_method,
4058 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004059 : HVariableInputSizeInstruction(
4060 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4061 dex_pc,
4062 arena,
4063 number_of_arguments + number_of_other_inputs,
4064 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004065 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004067 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004068 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004069 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004070 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004071 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004073 }
4074
Roland Levillain3e3d7332015-04-28 11:00:54 +01004075 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004076 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004077 const uint32_t dex_method_index_;
4078 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004079
4080 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4081 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004082
4083 private:
4084 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4085};
4086
Vladimir Markofcb503c2016-05-18 12:48:17 +01004087class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004088 public:
4089 HInvokeUnresolved(ArenaAllocator* arena,
4090 uint32_t number_of_arguments,
4091 Primitive::Type return_type,
4092 uint32_t dex_pc,
4093 uint32_t dex_method_index,
4094 InvokeType invoke_type)
4095 : HInvoke(arena,
4096 number_of_arguments,
4097 0u /* number_of_other_inputs */,
4098 return_type,
4099 dex_pc,
4100 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004101 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004102 invoke_type) {
4103 }
4104
4105 DECLARE_INSTRUCTION(InvokeUnresolved);
4106
4107 private:
4108 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4109};
4110
Orion Hodsonac141392017-01-13 11:53:47 +00004111class HInvokePolymorphic FINAL : public HInvoke {
4112 public:
4113 HInvokePolymorphic(ArenaAllocator* arena,
4114 uint32_t number_of_arguments,
4115 Primitive::Type return_type,
4116 uint32_t dex_pc,
4117 uint32_t dex_method_index)
4118 : HInvoke(arena,
4119 number_of_arguments,
4120 0u /* number_of_other_inputs */,
4121 return_type,
4122 dex_pc,
4123 dex_method_index,
4124 nullptr,
4125 kVirtual) {}
4126
4127 DECLARE_INSTRUCTION(InvokePolymorphic);
4128
4129 private:
4130 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4131};
4132
Vladimir Markofcb503c2016-05-18 12:48:17 +01004133class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004134 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004135 // Requirements of this method call regarding the class
4136 // initialization (clinit) check of its declaring class.
4137 enum class ClinitCheckRequirement {
4138 kNone, // Class already initialized.
4139 kExplicit, // Static call having explicit clinit check as last input.
4140 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004141 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004142 };
4143
Vladimir Marko58155012015-08-19 12:49:41 +00004144 // Determines how to load the target ArtMethod*.
4145 enum class MethodLoadKind {
4146 // Use a String init ArtMethod* loaded from Thread entrypoints.
4147 kStringInit,
4148
4149 // Use the method's own ArtMethod* loaded by the register allocator.
4150 kRecursive,
4151
Vladimir Marko65979462017-05-19 17:25:12 +01004152 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4153 // Used for boot image methods referenced by boot image code.
4154 kBootImageLinkTimePcRelative,
4155
Vladimir Marko58155012015-08-19 12:49:41 +00004156 // Use ArtMethod* at a known address, embed the direct address in the code.
4157 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4158 kDirectAddress,
4159
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004160 // Load from an entry in the .bss section using a PC-relative load.
4161 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4162 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004163
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004164 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4165 // used when other kinds are unimplemented on a particular architecture.
4166 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004167 };
4168
4169 // Determines the location of the code pointer.
4170 enum class CodePtrLocation {
4171 // Recursive call, use local PC-relative call instruction.
4172 kCallSelf,
4173
Vladimir Marko58155012015-08-19 12:49:41 +00004174 // Use code pointer from the ArtMethod*.
4175 // Used when we don't know the target code. This is also the last-resort-kind used when
4176 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4177 kCallArtMethod,
4178 };
4179
4180 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004181 MethodLoadKind method_load_kind;
4182 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004183 // The method load data holds
4184 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4185 // Note that there are multiple string init methods, each having its own offset.
4186 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004187 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004188 };
4189
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004190 HInvokeStaticOrDirect(ArenaAllocator* arena,
4191 uint32_t number_of_arguments,
4192 Primitive::Type return_type,
4193 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004194 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004195 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004196 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004197 InvokeType invoke_type,
4198 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004199 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004200 : HInvoke(arena,
4201 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004202 // There is potentially one extra argument for the HCurrentMethod node, and
4203 // potentially one other if the clinit check is explicit, and potentially
4204 // one other if the method is a string factory.
4205 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004206 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004207 return_type,
4208 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004209 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004210 resolved_method,
4211 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004212 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004213 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004214 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4215 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004216
Vladimir Markodc151b22015-10-15 18:02:30 +01004217 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004218 bool had_current_method_input = HasCurrentMethodInput();
4219 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4220
4221 // Using the current method is the default and once we find a better
4222 // method load kind, we should not go back to using the current method.
4223 DCHECK(had_current_method_input || !needs_current_method_input);
4224
4225 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004226 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4227 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004228 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004229 dispatch_info_ = dispatch_info;
4230 }
4231
Aart Bik6daebeb2017-04-03 14:35:41 -07004232 DispatchInfo GetDispatchInfo() const {
4233 return dispatch_info_;
4234 }
4235
Vladimir Markoc53c0792015-11-19 15:48:33 +00004236 void AddSpecialInput(HInstruction* input) {
4237 // We allow only one special input.
4238 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4239 DCHECK(InputCount() == GetSpecialInputIndex() ||
4240 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4241 InsertInputAt(GetSpecialInputIndex(), input);
4242 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004243
Vladimir Marko372f10e2016-05-17 16:30:10 +01004244 using HInstruction::GetInputRecords; // Keep the const version visible.
4245 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4246 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4247 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4248 DCHECK(!input_records.empty());
4249 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4250 HInstruction* last_input = input_records.back().GetInstruction();
4251 // Note: `last_input` may be null during arguments setup.
4252 if (last_input != nullptr) {
4253 // `last_input` is the last input of a static invoke marked as having
4254 // an explicit clinit check. It must either be:
4255 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4256 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4257 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4258 }
4259 }
4260 return input_records;
4261 }
4262
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004263 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004264 // We access the method via the dex cache so we can't do an implicit null check.
4265 // TODO: for intrinsics we can generate implicit null checks.
4266 return false;
4267 }
4268
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004269 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004270 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004271 }
4272
Vladimir Markoc53c0792015-11-19 15:48:33 +00004273 // Get the index of the special input, if any.
4274 //
David Brazdil6de19382016-01-08 17:37:10 +00004275 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4276 // method pointer; otherwise there may be one platform-specific special input,
4277 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004278 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004279 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004280
Vladimir Marko58155012015-08-19 12:49:41 +00004281 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4282 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4283 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004284 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004285 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004286 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004287 bool HasPcRelativeMethodLoadKind() const {
4288 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004289 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004290 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004291 bool HasCurrentMethodInput() const {
4292 // This function can be called only after the invoke has been fully initialized by the builder.
4293 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004294 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004295 return true;
4296 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004297 DCHECK(InputCount() == GetSpecialInputIndex() ||
4298 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004299 return false;
4300 }
4301 }
Vladimir Marko58155012015-08-19 12:49:41 +00004302
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004303 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004304 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004305 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004306 }
4307
4308 uint64_t GetMethodAddress() const {
4309 DCHECK(HasMethodAddress());
4310 return dispatch_info_.method_load_data;
4311 }
4312
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004313 const DexFile& GetDexFileForPcRelativeDexCache() const;
4314
Vladimir Markoa1de9182016-02-25 11:37:38 +00004315 ClinitCheckRequirement GetClinitCheckRequirement() const {
4316 return GetPackedField<ClinitCheckRequirementField>();
4317 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004318
Roland Levillain4c0eb422015-04-24 16:43:49 +01004319 // Is this instruction a call to a static method?
4320 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 return GetInvokeType() == kStatic;
4322 }
4323
4324 MethodReference GetTargetMethod() const {
4325 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004326 }
4327
Vladimir Markofbb184a2015-11-13 14:47:00 +00004328 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4329 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4330 // instruction; only relevant for static calls with explicit clinit check.
4331 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004333 size_t last_input_index = inputs_.size() - 1u;
4334 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004335 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004336 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004337 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004338 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004339 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004340 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004341 }
4342
4343 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004344 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004346 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004347 }
4348
4349 // Is this a call to a static method whose declaring class has an
4350 // implicit intialization check requirement?
4351 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004352 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004353 }
4354
Vladimir Markob554b5a2015-11-06 12:57:55 +00004355 // Does this method load kind need the current method as an input?
4356 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004357 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004358 }
4359
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004360 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004361
4362 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004364 static constexpr size_t kFieldClinitCheckRequirementSize =
4365 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4366 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4367 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4368 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4369 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004370 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4371 kFieldClinitCheckRequirement,
4372 kFieldClinitCheckRequirementSize>;
4373
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004374 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004375 MethodReference target_method_;
4376 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004378 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379};
Vladimir Markof64242a2015-12-01 14:58:23 +00004380std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004381std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004382
Vladimir Markofcb503c2016-05-18 12:48:17 +01004383class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004384 public:
4385 HInvokeVirtual(ArenaAllocator* arena,
4386 uint32_t number_of_arguments,
4387 Primitive::Type return_type,
4388 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004389 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004390 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004391 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004392 : HInvoke(arena,
4393 number_of_arguments,
4394 0u,
4395 return_type,
4396 dex_pc,
4397 dex_method_index,
4398 resolved_method,
4399 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004400 vtable_index_(vtable_index) {}
4401
Aart Bik71bf7b42016-11-16 10:17:46 -08004402 bool CanBeNull() const OVERRIDE {
4403 switch (GetIntrinsic()) {
4404 case Intrinsics::kThreadCurrentThread:
4405 case Intrinsics::kStringBufferAppend:
4406 case Intrinsics::kStringBufferToString:
4407 case Intrinsics::kStringBuilderAppend:
4408 case Intrinsics::kStringBuilderToString:
4409 return false;
4410 default:
4411 return HInvoke::CanBeNull();
4412 }
4413 }
4414
Calin Juravle641547a2015-04-21 22:08:51 +01004415 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004416 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004417 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004418 }
4419
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004420 uint32_t GetVTableIndex() const { return vtable_index_; }
4421
4422 DECLARE_INSTRUCTION(InvokeVirtual);
4423
4424 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004425 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004426 const uint32_t vtable_index_;
4427
4428 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4429};
4430
Vladimir Markofcb503c2016-05-18 12:48:17 +01004431class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004432 public:
4433 HInvokeInterface(ArenaAllocator* arena,
4434 uint32_t number_of_arguments,
4435 Primitive::Type return_type,
4436 uint32_t dex_pc,
4437 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004439 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004440 : HInvoke(arena,
4441 number_of_arguments,
4442 0u,
4443 return_type,
4444 dex_pc,
4445 dex_method_index,
4446 resolved_method,
4447 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004448 imt_index_(imt_index) {}
4449
Calin Juravle641547a2015-04-21 22:08:51 +01004450 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004451 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004452 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004453 }
4454
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004455 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4456 // The assembly stub currently needs it.
4457 return true;
4458 }
4459
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004460 uint32_t GetImtIndex() const { return imt_index_; }
4461 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4462
4463 DECLARE_INSTRUCTION(InvokeInterface);
4464
4465 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004466 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004467 const uint32_t imt_index_;
4468
4469 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4470};
4471
Vladimir Markofcb503c2016-05-18 12:48:17 +01004472class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004473 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004474 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004475 : HUnaryOperation(result_type, input, dex_pc) {
4476 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4477 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004478
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004479 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004480
4481 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
4484 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004487 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4488 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4489 }
4490 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4491 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4492 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004493
Roland Levillain88cb1752014-10-20 16:36:47 +01004494 DECLARE_INSTRUCTION(Neg);
4495
4496 private:
4497 DISALLOW_COPY_AND_ASSIGN(HNeg);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004501 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004502 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4503 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4504 SetRawInputAt(0, cls);
4505 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004506 }
4507
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004508 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004509 bool NeedsEnvironment() const OVERRIDE { return true; }
4510
Mingyao Yang0c365e62015-03-31 15:09:29 -07004511 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4512 bool CanThrow() const OVERRIDE { return true; }
4513
Calin Juravle10e244f2015-01-26 18:54:32 +00004514 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004515
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004516 HLoadClass* GetLoadClass() const {
4517 DCHECK(InputAt(0)->IsLoadClass());
4518 return InputAt(0)->AsLoadClass();
4519 }
4520
4521 HInstruction* GetLength() const {
4522 return InputAt(1);
4523 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004524
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525 DECLARE_INSTRUCTION(NewArray);
4526
4527 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004528 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4529};
4530
Vladimir Markofcb503c2016-05-18 12:48:17 +01004531class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004532 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 HAdd(Primitive::Type result_type,
4534 HInstruction* left,
4535 HInstruction* right,
4536 uint32_t dex_pc = kNoDexPc)
4537 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004539 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004540
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004541 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004542
4543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 return GetBlock()->GetGraph()->GetIntConstant(
4545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004546 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 return GetBlock()->GetGraph()->GetLongConstant(
4549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004550 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004551 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4552 return GetBlock()->GetGraph()->GetFloatConstant(
4553 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4554 }
4555 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4556 return GetBlock()->GetGraph()->GetDoubleConstant(
4557 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4558 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004559
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004560 DECLARE_INSTRUCTION(Add);
4561
4562 private:
4563 DISALLOW_COPY_AND_ASSIGN(HAdd);
4564};
4565
Vladimir Markofcb503c2016-05-18 12:48:17 +01004566class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004567 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004568 HSub(Primitive::Type result_type,
4569 HInstruction* left,
4570 HInstruction* right,
4571 uint32_t dex_pc = kNoDexPc)
4572 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004573
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004574 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575
4576 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 return GetBlock()->GetGraph()->GetIntConstant(
4578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004579 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 return GetBlock()->GetGraph()->GetLongConstant(
4582 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004583 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4585 return GetBlock()->GetGraph()->GetFloatConstant(
4586 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4587 }
4588 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4589 return GetBlock()->GetGraph()->GetDoubleConstant(
4590 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4591 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004592
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004593 DECLARE_INSTRUCTION(Sub);
4594
4595 private:
4596 DISALLOW_COPY_AND_ASSIGN(HSub);
4597};
4598
Vladimir Markofcb503c2016-05-18 12:48:17 +01004599class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004600 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 HMul(Primitive::Type result_type,
4602 HInstruction* left,
4603 HInstruction* right,
4604 uint32_t dex_pc = kNoDexPc)
4605 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004606
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004607 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004608
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004609 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004610
4611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 return GetBlock()->GetGraph()->GetIntConstant(
4613 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
4615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 return GetBlock()->GetGraph()->GetLongConstant(
4617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004618 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004619 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4620 return GetBlock()->GetGraph()->GetFloatConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4622 }
4623 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4624 return GetBlock()->GetGraph()->GetDoubleConstant(
4625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4626 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004627
4628 DECLARE_INSTRUCTION(Mul);
4629
4630 private:
4631 DISALLOW_COPY_AND_ASSIGN(HMul);
4632};
4633
Vladimir Markofcb503c2016-05-18 12:48:17 +01004634class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004636 HDiv(Primitive::Type result_type,
4637 HInstruction* left,
4638 HInstruction* right,
4639 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004640 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004641
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004643 T ComputeIntegral(T x, T y) const {
4644 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 // Our graph structure ensures we never have 0 for `y` during
4646 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004647 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004648 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004649 return (y == -1) ? -x : x / y;
4650 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004651
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 template <typename T>
4653 T ComputeFP(T x, T y) const {
4654 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4655 return x / y;
4656 }
4657
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004661 }
4662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004664 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4665 }
4666 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4667 return GetBlock()->GetGraph()->GetFloatConstant(
4668 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4669 }
4670 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4671 return GetBlock()->GetGraph()->GetDoubleConstant(
4672 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004673 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004674
4675 DECLARE_INSTRUCTION(Div);
4676
4677 private:
4678 DISALLOW_COPY_AND_ASSIGN(HDiv);
4679};
4680
Vladimir Markofcb503c2016-05-18 12:48:17 +01004681class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 HRem(Primitive::Type result_type,
4684 HInstruction* left,
4685 HInstruction* right,
4686 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004687 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004688
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004690 T ComputeIntegral(T x, T y) const {
4691 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004692 // Our graph structure ensures we never have 0 for `y` during
4693 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004694 DCHECK_NE(y, 0);
4695 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4696 return (y == -1) ? 0 : x % y;
4697 }
4698
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 template <typename T>
4700 T ComputeFP(T x, T y) const {
4701 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4702 return std::fmod(x, y);
4703 }
4704
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004708 }
4709 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004710 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004712 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004713 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4714 return GetBlock()->GetGraph()->GetFloatConstant(
4715 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4716 }
4717 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4718 return GetBlock()->GetGraph()->GetDoubleConstant(
4719 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4720 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004721
4722 DECLARE_INSTRUCTION(Rem);
4723
4724 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004725 DISALLOW_COPY_AND_ASSIGN(HRem);
4726};
4727
Vladimir Markofcb503c2016-05-18 12:48:17 +01004728class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004729 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004730 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4731 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004732 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004733 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004734 SetRawInputAt(0, value);
4735 }
4736
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004737 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4738
Calin Juravled0d48522014-11-04 16:40:20 +00004739 bool CanBeMoved() const OVERRIDE { return true; }
4740
Vladimir Marko372f10e2016-05-17 16:30:10 +01004741 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004742 return true;
4743 }
4744
4745 bool NeedsEnvironment() const OVERRIDE { return true; }
4746 bool CanThrow() const OVERRIDE { return true; }
4747
Calin Juravled0d48522014-11-04 16:40:20 +00004748 DECLARE_INSTRUCTION(DivZeroCheck);
4749
4750 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004751 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4752};
4753
Vladimir Markofcb503c2016-05-18 12:48:17 +01004754class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 HInstruction* value,
4758 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004759 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4761 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4762 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 }
4764
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004766 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 return value << (distance & max_shift_distance);
4768 }
4769
4770 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4779 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4780 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4781 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4784 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for float values";
4786 UNREACHABLE();
4787 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4789 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004790 LOG(FATAL) << DebugName() << " is not defined for double values";
4791 UNREACHABLE();
4792 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004793
4794 DECLARE_INSTRUCTION(Shl);
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HShl);
4798};
4799
Vladimir Markofcb503c2016-05-18 12:48:17 +01004800class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 HInstruction* value,
4804 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004805 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4807 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4808 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 }
4810
Roland Levillain5b5b9312016-03-22 14:57:31 +00004811 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004812 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 return value >> (distance & max_shift_distance);
4814 }
4815
4816 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004822 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4825 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4826 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4827 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004829 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4830 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 LOG(FATAL) << DebugName() << " is not defined for float values";
4832 UNREACHABLE();
4833 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4835 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004836 LOG(FATAL) << DebugName() << " is not defined for double values";
4837 UNREACHABLE();
4838 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004839
4840 DECLARE_INSTRUCTION(Shr);
4841
4842 private:
4843 DISALLOW_COPY_AND_ASSIGN(HShr);
4844};
4845
Vladimir Markofcb503c2016-05-18 12:48:17 +01004846class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 HInstruction* value,
4850 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4853 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4854 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004855 }
4856
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004858 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 typedef typename std::make_unsigned<T>::type V;
4860 V ux = static_cast<V>(value);
4861 return static_cast<T>(ux >> (distance & max_shift_distance));
4862 }
4863
4864 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004868 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4873 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4874 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4875 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004876 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4878 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 LOG(FATAL) << DebugName() << " is not defined for float values";
4880 UNREACHABLE();
4881 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4883 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004884 LOG(FATAL) << DebugName() << " is not defined for double values";
4885 UNREACHABLE();
4886 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004887
4888 DECLARE_INSTRUCTION(UShr);
4889
4890 private:
4891 DISALLOW_COPY_AND_ASSIGN(HUShr);
4892};
4893
Vladimir Markofcb503c2016-05-18 12:48:17 +01004894class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004896 HAnd(Primitive::Type result_type,
4897 HInstruction* left,
4898 HInstruction* right,
4899 uint32_t dex_pc = kNoDexPc)
4900 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004901
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004902 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004903
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004904 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004905
4906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 return GetBlock()->GetGraph()->GetIntConstant(
4908 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004909 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004911 return GetBlock()->GetGraph()->GetLongConstant(
4912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004913 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004914 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4915 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for float values";
4917 UNREACHABLE();
4918 }
4919 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4920 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4921 LOG(FATAL) << DebugName() << " is not defined for double values";
4922 UNREACHABLE();
4923 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004924
4925 DECLARE_INSTRUCTION(And);
4926
4927 private:
4928 DISALLOW_COPY_AND_ASSIGN(HAnd);
4929};
4930
Vladimir Markofcb503c2016-05-18 12:48:17 +01004931class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 HOr(Primitive::Type result_type,
4934 HInstruction* left,
4935 HInstruction* right,
4936 uint32_t dex_pc = kNoDexPc)
4937 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004939 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004940
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004941 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004942
4943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 return GetBlock()->GetGraph()->GetIntConstant(
4945 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004947 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 return GetBlock()->GetGraph()->GetLongConstant(
4949 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004950 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004951 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4952 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4953 LOG(FATAL) << DebugName() << " is not defined for float values";
4954 UNREACHABLE();
4955 }
4956 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4957 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4958 LOG(FATAL) << DebugName() << " is not defined for double values";
4959 UNREACHABLE();
4960 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004961
4962 DECLARE_INSTRUCTION(Or);
4963
4964 private:
4965 DISALLOW_COPY_AND_ASSIGN(HOr);
4966};
4967
Vladimir Markofcb503c2016-05-18 12:48:17 +01004968class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 HXor(Primitive::Type result_type,
4971 HInstruction* left,
4972 HInstruction* right,
4973 uint32_t dex_pc = kNoDexPc)
4974 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004975
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004976 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004978 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004979
4980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetIntConstant(
4982 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 return GetBlock()->GetGraph()->GetLongConstant(
4986 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004987 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4989 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4990 LOG(FATAL) << DebugName() << " is not defined for float values";
4991 UNREACHABLE();
4992 }
4993 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4994 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4995 LOG(FATAL) << DebugName() << " is not defined for double values";
4996 UNREACHABLE();
4997 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004998
4999 DECLARE_INSTRUCTION(Xor);
5000
5001 private:
5002 DISALLOW_COPY_AND_ASSIGN(HXor);
5003};
5004
Vladimir Markofcb503c2016-05-18 12:48:17 +01005005class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005006 public:
5007 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005008 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005009 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5010 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005011 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005012
Roland Levillain5b5b9312016-03-22 14:57:31 +00005013 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005014 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005015 typedef typename std::make_unsigned<T>::type V;
5016 V ux = static_cast<V>(value);
5017 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005018 return static_cast<T>(ux);
5019 } else {
5020 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 return static_cast<T>(ux >> (distance & max_shift_value)) |
5022 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005023 }
5024 }
5025
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005027 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005029 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005030 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005031 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005032 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5035 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5036 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5037 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005038 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5040 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 LOG(FATAL) << DebugName() << " is not defined for float values";
5042 UNREACHABLE();
5043 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005044 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5045 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005046 LOG(FATAL) << DebugName() << " is not defined for double values";
5047 UNREACHABLE();
5048 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005049
5050 DECLARE_INSTRUCTION(Ror);
5051
5052 private:
5053 DISALLOW_COPY_AND_ASSIGN(HRor);
5054};
5055
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005056// The value of a parameter in this method. Its location depends on
5057// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005058class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005059 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005060 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005061 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005062 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 Primitive::Type parameter_type,
5064 bool is_this = false)
5065 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005066 dex_file_(dex_file),
5067 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005068 index_(index) {
5069 SetPackedFlag<kFlagIsThis>(is_this);
5070 SetPackedFlag<kFlagCanBeNull>(!is_this);
5071 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005073 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005074 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005075 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005076 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005077
Vladimir Markoa1de9182016-02-25 11:37:38 +00005078 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5079 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005080
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005081 DECLARE_INSTRUCTION(ParameterValue);
5082
5083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005084 // Whether or not the parameter value corresponds to 'this' argument.
5085 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5086 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5087 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5088 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5089 "Too many packed fields.");
5090
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005091 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005092 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005093 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005094 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005095 const uint8_t index_;
5096
5097 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005102 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5103 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005105 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005106 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005107 return true;
5108 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005109
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005110 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005111
5112 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 }
5115 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005117 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5119 LOG(FATAL) << DebugName() << " is not defined for float values";
5120 UNREACHABLE();
5121 }
5122 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5123 LOG(FATAL) << DebugName() << " is not defined for double values";
5124 UNREACHABLE();
5125 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005126
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005127 DECLARE_INSTRUCTION(Not);
5128
5129 private:
5130 DISALLOW_COPY_AND_ASSIGN(HNot);
5131};
5132
Vladimir Markofcb503c2016-05-18 12:48:17 +01005133class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005135 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5136 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005137
5138 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005139 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005140 return true;
5141 }
5142
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005143 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005145 return !x;
5146 }
5147
Roland Levillain9867bc72015-08-05 10:21:34 +01005148 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 }
5151 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5152 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005153 UNREACHABLE();
5154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005155 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5156 LOG(FATAL) << DebugName() << " is not defined for float values";
5157 UNREACHABLE();
5158 }
5159 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5160 LOG(FATAL) << DebugName() << " is not defined for double values";
5161 UNREACHABLE();
5162 }
David Brazdil66d126e2015-04-03 16:02:44 +01005163
5164 DECLARE_INSTRUCTION(BooleanNot);
5165
5166 private:
5167 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5168};
5169
Vladimir Markofcb503c2016-05-18 12:48:17 +01005170class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005171 public:
5172 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005173 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005174 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005175 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005176 // Invariant: We should never generate a conversion to a Boolean value.
5177 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005178 }
5179
5180 HInstruction* GetInput() const { return InputAt(0); }
5181 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5182 Primitive::Type GetResultType() const { return GetType(); }
5183
5184 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005185 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5186 return true;
5187 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005188
Mark Mendelle82549b2015-05-06 10:55:34 -04005189 // Try to statically evaluate the conversion and return a HConstant
5190 // containing the result. If the input cannot be converted, return nullptr.
5191 HConstant* TryStaticEvaluation() const;
5192
Roland Levillaindff1f282014-11-05 14:15:05 +00005193 DECLARE_INSTRUCTION(TypeConversion);
5194
5195 private:
5196 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5197};
5198
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005199static constexpr uint32_t kNoRegNumber = -1;
5200
Vladimir Markofcb503c2016-05-18 12:48:17 +01005201class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005203 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5204 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005205 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005206 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 SetRawInputAt(0, value);
5208 }
5209
Calin Juravle10e244f2015-01-26 18:54:32 +00005210 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005211 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005212 return true;
5213 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005214
Calin Juravle10e244f2015-01-26 18:54:32 +00005215 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216
Calin Juravle10e244f2015-01-26 18:54:32 +00005217 bool CanThrow() const OVERRIDE { return true; }
5218
5219 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005221
5222 DECLARE_INSTRUCTION(NullCheck);
5223
5224 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5226};
5227
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005228// Embeds an ArtField and all the information required by the compiler. We cache
5229// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230class FieldInfo : public ValueObject {
5231 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005232 FieldInfo(ArtField* field,
5233 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005234 Primitive::Type field_type,
5235 bool is_volatile,
5236 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005237 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005238 const DexFile& dex_file)
5239 : field_(field),
5240 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005241 field_type_(field_type),
5242 is_volatile_(is_volatile),
5243 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005244 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005245 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005246
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005247 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005248 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005249 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005250 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005251 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005252 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005253 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005254
5255 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005256 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005257 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005258 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005259 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005260 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005261 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005262 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005263};
5264
Vladimir Markofcb503c2016-05-18 12:48:17 +01005265class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266 public:
5267 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005268 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005269 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005270 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005271 bool is_volatile,
5272 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005273 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005274 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005275 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005276 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005277 field_info_(field,
5278 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005279 field_type,
5280 is_volatile,
5281 field_idx,
5282 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005283 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005284 SetRawInputAt(0, value);
5285 }
5286
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005287 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005288
Vladimir Marko372f10e2016-05-17 16:30:10 +01005289 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5290 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005291 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005292 }
5293
Calin Juravle641547a2015-04-21 22:08:51 +01005294 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005295 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 }
5297
5298 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005299 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5300 }
5301
Calin Juravle52c48962014-12-16 17:02:57 +00005302 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005303 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005304 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005305 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005306
5307 DECLARE_INSTRUCTION(InstanceFieldGet);
5308
5309 private:
5310 const FieldInfo field_info_;
5311
5312 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5313};
5314
Vladimir Markofcb503c2016-05-18 12:48:17 +01005315class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005316 public:
5317 HInstanceFieldSet(HInstruction* object,
5318 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005319 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005320 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005321 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005322 bool is_volatile,
5323 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005324 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005325 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005326 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005327 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005328 field_info_(field,
5329 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005330 field_type,
5331 is_volatile,
5332 field_idx,
5333 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005334 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005336 SetRawInputAt(0, object);
5337 SetRawInputAt(1, value);
5338 }
5339
Calin Juravle641547a2015-04-21 22:08:51 +01005340 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005341 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 }
5343
Calin Juravle52c48962014-12-16 17:02:57 +00005344 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005345 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005346 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005347 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005348 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005349 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5350 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005352 DECLARE_INSTRUCTION(InstanceFieldSet);
5353
5354 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005355 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5356 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5357 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5358 "Too many packed fields.");
5359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005360 const FieldInfo field_info_;
5361
5362 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5363};
5364
Vladimir Markofcb503c2016-05-18 12:48:17 +01005365class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005367 HArrayGet(HInstruction* array,
5368 HInstruction* index,
5369 Primitive::Type type,
5370 uint32_t dex_pc,
5371 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005372 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005373 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 SetRawInputAt(0, array);
5375 SetRawInputAt(1, index);
5376 }
5377
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005378 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005379 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005380 return true;
5381 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005382 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005384 // Currently, unless the array is the result of NewArray, the array access is always
5385 // preceded by some form of null NullCheck necessary for the bounds check, usually
5386 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5387 // dynamic BCE. There are cases when these could be removed to produce better code.
5388 // If we ever add optimizations to do so we should allow an implicit check here
5389 // (as long as the address falls in the first page).
5390 //
5391 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5392 // a = cond ? new int[1] : null;
5393 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005394 return false;
5395 }
5396
David Brazdil4833f5a2015-12-16 10:37:39 +00005397 bool IsEquivalentOf(HArrayGet* other) const {
5398 bool result = (GetDexPc() == other->GetDexPc());
5399 if (kIsDebugBuild && result) {
5400 DCHECK_EQ(GetBlock(), other->GetBlock());
5401 DCHECK_EQ(GetArray(), other->GetArray());
5402 DCHECK_EQ(GetIndex(), other->GetIndex());
5403 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005404 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005405 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005406 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5407 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005408 }
5409 }
5410 return result;
5411 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005412
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005413 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5414
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005415 HInstruction* GetArray() const { return InputAt(0); }
5416 HInstruction* GetIndex() const { return InputAt(1); }
5417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 DECLARE_INSTRUCTION(ArrayGet);
5419
5420 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005421 // We treat a String as an array, creating the HArrayGet from String.charAt()
5422 // intrinsic in the instruction simplifier. We can always determine whether
5423 // a particular HArrayGet is actually a String.charAt() by looking at the type
5424 // of the input but that requires holding the mutator lock, so we prefer to use
5425 // a flag, so that code generators don't need to do the locking.
5426 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5427 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5428 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5429 "Too many packed fields.");
5430
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5432};
5433
Vladimir Markofcb503c2016-05-18 12:48:17 +01005434class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 public:
5436 HArraySet(HInstruction* array,
5437 HInstruction* index,
5438 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005439 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005440 uint32_t dex_pc)
5441 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005442 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5443 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5444 SetPackedFlag<kFlagValueCanBeNull>(true);
5445 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 SetRawInputAt(0, array);
5447 SetRawInputAt(1, index);
5448 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005449 // Make a best guess now, may be refined during SSA building.
5450 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005451 }
5452
Calin Juravle77520bc2015-01-12 18:45:46 +00005453 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005454 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005455 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456 }
5457
Mingyao Yang81014cb2015-06-02 03:16:27 -07005458 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005459 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005460
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005461 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005462 // TODO: Same as for ArrayGet.
5463 return false;
5464 }
5465
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005466 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005468 }
5469
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005470 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005471 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005472 }
5473
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005474 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005476 }
5477
Vladimir Markoa1de9182016-02-25 11:37:38 +00005478 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5479 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5480 bool StaticTypeOfArrayIsObjectArray() const {
5481 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5482 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005483
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005484 HInstruction* GetArray() const { return InputAt(0); }
5485 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005486 HInstruction* GetValue() const { return InputAt(2); }
5487
5488 Primitive::Type GetComponentType() const {
5489 // The Dex format does not type floating point index operations. Since the
5490 // `expected_component_type_` is set during building and can therefore not
5491 // be correct, we also check what is the value type. If it is a floating
5492 // point type, we must use that type.
5493 Primitive::Type value_type = GetValue()->GetType();
5494 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5495 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005496 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005497 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005498
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005499 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005500 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005501 }
5502
Aart Bik18b36ab2016-04-13 16:41:35 -07005503 void ComputeSideEffects() {
5504 Primitive::Type type = GetComponentType();
5505 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5506 SideEffectsForArchRuntimeCalls(type)));
5507 }
5508
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005509 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5510 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5511 }
5512
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005513 DECLARE_INSTRUCTION(ArraySet);
5514
5515 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005516 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5517 static constexpr size_t kFieldExpectedComponentTypeSize =
5518 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5519 static constexpr size_t kFlagNeedsTypeCheck =
5520 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5521 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 // Cached information for the reference_type_info_ so that codegen
5523 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5525 static constexpr size_t kNumberOfArraySetPackedBits =
5526 kFlagStaticTypeOfArrayIsObjectArray + 1;
5527 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5528 using ExpectedComponentTypeField =
5529 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530
5531 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5532};
5533
Vladimir Markofcb503c2016-05-18 12:48:17 +01005534class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005536 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005537 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005538 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005539 // Note that arrays do not change length, so the instruction does not
5540 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 SetRawInputAt(0, array);
5542 }
5543
Calin Juravle77520bc2015-01-12 18:45:46 +00005544 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005545 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005546 return true;
5547 }
Calin Juravle641547a2015-04-21 22:08:51 +01005548 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5549 return obj == InputAt(0);
5550 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005551
Vladimir Markodce016e2016-04-28 13:10:02 +01005552 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5553
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554 DECLARE_INSTRUCTION(ArrayLength);
5555
5556 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005557 // We treat a String as an array, creating the HArrayLength from String.length()
5558 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5559 // determine whether a particular HArrayLength is actually a String.length() by
5560 // looking at the type of the input but that requires holding the mutator lock, so
5561 // we prefer to use a flag, so that code generators don't need to do the locking.
5562 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5563 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5564 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5565 "Too many packed fields.");
5566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5568};
5569
Vladimir Markofcb503c2016-05-18 12:48:17 +01005570class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005572 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5573 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005574 HBoundsCheck(HInstruction* index,
5575 HInstruction* length,
5576 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005577 bool string_char_at = false)
5578 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005579 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005580 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581 SetRawInputAt(0, index);
5582 SetRawInputAt(1, length);
5583 }
5584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005585 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005586 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005587 return true;
5588 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005589
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005590 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005592 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005593
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005594 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005595
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005596 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005597
5598 DECLARE_INSTRUCTION(BoundsCheck);
5599
5600 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005601 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005602
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5604};
5605
Vladimir Markofcb503c2016-05-18 12:48:17 +01005606class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005607 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005608 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005609 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005611 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005612 return true;
5613 }
5614
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005615 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5616 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005617
5618 DECLARE_INSTRUCTION(SuspendCheck);
5619
5620 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005621 // Only used for code generation, in order to share the same slow path between back edges
5622 // of a same loop.
5623 SlowPathCode* slow_path_;
5624
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005625 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5626};
5627
David Srbecky0cf44932015-12-09 14:09:59 +00005628// Pseudo-instruction which provides the native debugger with mapping information.
5629// It ensures that we can generate line number and local variables at this point.
5630class HNativeDebugInfo : public HTemplateInstruction<0> {
5631 public:
5632 explicit HNativeDebugInfo(uint32_t dex_pc)
5633 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5634
5635 bool NeedsEnvironment() const OVERRIDE {
5636 return true;
5637 }
5638
5639 DECLARE_INSTRUCTION(NativeDebugInfo);
5640
5641 private:
5642 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5643};
5644
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005645/**
5646 * Instruction to load a Class object.
5647 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005648class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005650 // Determines how to load the Class.
5651 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005652 // We cannot load this class. See HSharpening::SharpenLoadClass.
5653 kInvalid = -1,
5654
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005655 // Use the Class* from the method's own ArtMethod*.
5656 kReferrersClass,
5657
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005659 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005660 kBootImageLinkTimePcRelative,
5661
5662 // Use a known boot image Class* address, embedded in the code by the codegen.
5663 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005664 kBootImageAddress,
5665
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005666 // Load from an entry in the .bss section using a PC-relative load.
5667 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5668 kBssEntry,
5669
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005670 // Load from the root table associated with the JIT compiled method.
5671 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005673 // Load using a simple runtime call. This is the fall-back load kind when
5674 // the codegen is unable to use another appropriate kind.
5675 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005676
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005677 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005678 };
5679
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005680 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005681 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005682 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005683 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005684 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005685 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005686 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005687 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5688 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005689 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005690 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005691 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005692 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005693 // Referrers class should not need access check. We never inline unverified
5694 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 DCHECK(!is_referrers_class || !needs_access_check);
5696
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005698 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005699 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005700 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005701 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005702 }
5703
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005704 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706 LoadKind GetLoadKind() const {
5707 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005708 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005709
5710 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005712 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005713
Andreas Gampea5b09a62016-11-17 15:21:22 -08005714 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005716 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005717
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005718 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005719 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005720 }
5721
Calin Juravle0ba218d2015-05-19 18:46:01 +01005722 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005723 // The entrypoint the code generator is going to call does not do
5724 // clinit of the class.
5725 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005726 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005727 }
5728
5729 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005730 return NeedsAccessCheck() ||
5731 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005732 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005733 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005734 }
5735
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005736 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005737 return NeedsAccessCheck() ||
5738 MustGenerateClinitCheck() ||
5739 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5740 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5741 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005742 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005743 GetLoadKind() == LoadKind::kBssEntry) &&
5744 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005745 }
5746
Calin Juravleacf735c2015-02-12 15:25:22 +00005747 ReferenceTypeInfo GetLoadedClassRTI() {
5748 return loaded_class_rti_;
5749 }
5750
5751 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5752 // Make sure we only set exact types (the loaded class should never be merged).
5753 DCHECK(rti.IsExact());
5754 loaded_class_rti_ = rti;
5755 }
5756
Andreas Gampea5b09a62016-11-17 15:21:22 -08005757 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005758 const DexFile& GetDexFile() const { return dex_file_; }
5759
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005760 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005761 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005762 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005763
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005764 static SideEffects SideEffectsForArchRuntimeCalls() {
5765 return SideEffects::CanTriggerGC();
5766 }
5767
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005769 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005770 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005772
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005773 void MarkInBootImage() {
5774 SetPackedFlag<kFlagIsInBootImage>(true);
5775 }
5776
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005777 void AddSpecialInput(HInstruction* special_input);
5778
5779 using HInstruction::GetInputRecords; // Keep the const version visible.
5780 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5781 return ArrayRef<HUserRecord<HInstruction*>>(
5782 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5783 }
5784
5785 Primitive::Type GetType() const OVERRIDE {
5786 return Primitive::kPrimNot;
5787 }
5788
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005789 Handle<mirror::Class> GetClass() const {
5790 return klass_;
5791 }
5792
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005793 DECLARE_INSTRUCTION(LoadClass);
5794
5795 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005796 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005797 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005798 // Whether this instruction must generate the initialization check.
5799 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005800 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005801 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5802 static constexpr size_t kFieldLoadKindSize =
5803 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5804 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005805 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5807
5808 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005809 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005810 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005811 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005812 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005813 }
5814
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 void SetLoadKindInternal(LoadKind load_kind);
5816
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005817 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005819 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005820 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005821
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005822 // A type index and dex file where the class can be accessed. The dex file can be:
5823 // - The compiling method's dex file if the class is defined there too.
5824 // - The compiling method's dex file if the class is referenced there.
5825 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005826 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005827 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005829
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005830 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831
Calin Juravleacf735c2015-02-12 15:25:22 +00005832 ReferenceTypeInfo loaded_class_rti_;
5833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005834 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5835};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005836std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5837
5838// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005839inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005840 // The special input is used for PC-relative loads on some architectures,
5841 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005842 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005843 GetLoadKind() == LoadKind::kBootImageAddress ||
5844 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845 DCHECK(special_input_.GetInstruction() == nullptr);
5846 special_input_ = HUserRecord<HInstruction*>(special_input);
5847 special_input->AddUseAt(this, 0);
5848}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849
Vladimir Marko372f10e2016-05-17 16:30:10 +01005850class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005851 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005852 // Determines how to load the String.
5853 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005855 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 kBootImageLinkTimePcRelative,
5857
5858 // Use a known boot image String* address, embedded in the code by the codegen.
5859 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 kBootImageAddress,
5861
Vladimir Markoaad75c62016-10-03 08:46:48 +00005862 // Load from an entry in the .bss section using a PC-relative load.
5863 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5864 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005866 // Load from the root table associated with the JIT compiled method.
5867 kJitTableAddress,
5868
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005869 // Load using a simple runtime call. This is the fall-back load kind when
5870 // the codegen is unable to use another appropriate kind.
5871 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005873 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 };
5875
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005876 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005877 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005878 const DexFile& dex_file,
5879 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005880 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5881 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005882 string_index_(string_index),
5883 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005884 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005885 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005886
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005887 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889 LoadKind GetLoadKind() const {
5890 return GetPackedField<LoadKindField>();
5891 }
5892
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005893 const DexFile& GetDexFile() const {
5894 return dex_file_;
5895 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005897 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898 return string_index_;
5899 }
5900
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005901 Handle<mirror::String> GetString() const {
5902 return string_;
5903 }
5904
5905 void SetString(Handle<mirror::String> str) {
5906 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005907 }
5908
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005909 bool CanBeMoved() const OVERRIDE { return true; }
5910
Vladimir Marko372f10e2016-05-17 16:30:10 +01005911 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005913 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005914
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005915 // Will call the runtime if we need to load the string through
5916 // the dex cache and the string is not guaranteed to be there yet.
5917 bool NeedsEnvironment() const OVERRIDE {
5918 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005919 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005920 load_kind == LoadKind::kBootImageAddress ||
5921 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005922 return false;
5923 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005924 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005926
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005928 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005930
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005931 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005933
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005934 static SideEffects SideEffectsForArchRuntimeCalls() {
5935 return SideEffects::CanTriggerGC();
5936 }
5937
Vladimir Marko372f10e2016-05-17 16:30:10 +01005938 void AddSpecialInput(HInstruction* special_input);
5939
5940 using HInstruction::GetInputRecords; // Keep the const version visible.
5941 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5942 return ArrayRef<HUserRecord<HInstruction*>>(
5943 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005944 }
5945
Vladimir Marko372f10e2016-05-17 16:30:10 +01005946 Primitive::Type GetType() const OVERRIDE {
5947 return Primitive::kPrimNot;
5948 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005950 DECLARE_INSTRUCTION(LoadString);
5951
5952 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005953 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 static constexpr size_t kFieldLoadKindSize =
5955 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5956 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005957 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005958 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960 void SetLoadKindInternal(LoadKind load_kind);
5961
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005962 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005963 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005964 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005965 HUserRecord<HInstruction*> special_input_;
5966
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005967 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005968 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005970 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005971
5972 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5973};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005974std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5975
5976// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005977inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005978 // The special input is used for PC-relative loads on some architectures,
5979 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005981 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005982 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005983 // HLoadString::GetInputRecords() returns an empty array at this point,
5984 // so use the GetInputRecords() from the base class to set the input record.
5985 DCHECK(special_input_.GetInstruction() == nullptr);
5986 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 special_input->AddUseAt(this, 0);
5988}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005989
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005990/**
5991 * Performs an initialization check on its Class object input.
5992 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005993class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005995 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005996 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005997 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005998 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5999 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006000 SetRawInputAt(0, constant);
6001 }
6002
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006003 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006004 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006005 return true;
6006 }
6007
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008 bool NeedsEnvironment() const OVERRIDE {
6009 // May call runtime to initialize the class.
6010 return true;
6011 }
6012
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006013 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006015 HLoadClass* GetLoadClass() const {
6016 DCHECK(InputAt(0)->IsLoadClass());
6017 return InputAt(0)->AsLoadClass();
6018 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019
6020 DECLARE_INSTRUCTION(ClinitCheck);
6021
6022 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006023 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6024};
6025
Vladimir Markofcb503c2016-05-18 12:48:17 +01006026class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 public:
6028 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006029 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006031 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006032 bool is_volatile,
6033 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006034 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006035 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006036 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006037 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006038 field_info_(field,
6039 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006040 field_type,
6041 is_volatile,
6042 field_idx,
6043 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006044 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 SetRawInputAt(0, cls);
6046 }
6047
Calin Juravle52c48962014-12-16 17:02:57 +00006048
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006049 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006050
Vladimir Marko372f10e2016-05-17 16:30:10 +01006051 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6052 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006053 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006054 }
6055
6056 size_t ComputeHashCode() const OVERRIDE {
6057 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6058 }
6059
Calin Juravle52c48962014-12-16 17:02:57 +00006060 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006061 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6062 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006063 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064
6065 DECLARE_INSTRUCTION(StaticFieldGet);
6066
6067 private:
6068 const FieldInfo field_info_;
6069
6070 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6071};
6072
Vladimir Markofcb503c2016-05-18 12:48:17 +01006073class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074 public:
6075 HStaticFieldSet(HInstruction* cls,
6076 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006077 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006078 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006079 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006080 bool is_volatile,
6081 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006082 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006083 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006084 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006085 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006086 field_info_(field,
6087 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006088 field_type,
6089 is_volatile,
6090 field_idx,
6091 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006092 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094 SetRawInputAt(0, cls);
6095 SetRawInputAt(1, value);
6096 }
6097
Calin Juravle52c48962014-12-16 17:02:57 +00006098 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6100 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006101 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006103 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6105 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006106
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006107 DECLARE_INSTRUCTION(StaticFieldSet);
6108
6109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6111 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6112 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6113 "Too many packed fields.");
6114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006115 const FieldInfo field_info_;
6116
6117 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6118};
6119
Vladimir Markofcb503c2016-05-18 12:48:17 +01006120class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006121 public:
6122 HUnresolvedInstanceFieldGet(HInstruction* obj,
6123 Primitive::Type field_type,
6124 uint32_t field_index,
6125 uint32_t dex_pc)
6126 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6127 field_index_(field_index) {
6128 SetRawInputAt(0, obj);
6129 }
6130
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
6132 bool CanThrow() const OVERRIDE { return true; }
6133
6134 Primitive::Type GetFieldType() const { return GetType(); }
6135 uint32_t GetFieldIndex() const { return field_index_; }
6136
6137 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6138
6139 private:
6140 const uint32_t field_index_;
6141
6142 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6143};
6144
Vladimir Markofcb503c2016-05-18 12:48:17 +01006145class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006146 public:
6147 HUnresolvedInstanceFieldSet(HInstruction* obj,
6148 HInstruction* value,
6149 Primitive::Type field_type,
6150 uint32_t field_index,
6151 uint32_t dex_pc)
6152 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006153 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006155 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006156 SetRawInputAt(0, obj);
6157 SetRawInputAt(1, value);
6158 }
6159
6160 bool NeedsEnvironment() const OVERRIDE { return true; }
6161 bool CanThrow() const OVERRIDE { return true; }
6162
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 uint32_t GetFieldIndex() const { return field_index_; }
6165
6166 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6167
6168 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006169 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6170 static constexpr size_t kFieldFieldTypeSize =
6171 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6172 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6173 kFieldFieldType + kFieldFieldTypeSize;
6174 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6175 "Too many packed fields.");
6176 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6177
Calin Juravlee460d1d2015-09-29 04:52:17 +01006178 const uint32_t field_index_;
6179
6180 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6181};
6182
Vladimir Markofcb503c2016-05-18 12:48:17 +01006183class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006184 public:
6185 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6186 uint32_t field_index,
6187 uint32_t dex_pc)
6188 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6189 field_index_(field_index) {
6190 }
6191
6192 bool NeedsEnvironment() const OVERRIDE { return true; }
6193 bool CanThrow() const OVERRIDE { return true; }
6194
6195 Primitive::Type GetFieldType() const { return GetType(); }
6196 uint32_t GetFieldIndex() const { return field_index_; }
6197
6198 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6199
6200 private:
6201 const uint32_t field_index_;
6202
6203 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6204};
6205
Vladimir Markofcb503c2016-05-18 12:48:17 +01006206class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006207 public:
6208 HUnresolvedStaticFieldSet(HInstruction* value,
6209 Primitive::Type field_type,
6210 uint32_t field_index,
6211 uint32_t dex_pc)
6212 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006213 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006215 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006216 SetRawInputAt(0, value);
6217 }
6218
6219 bool NeedsEnvironment() const OVERRIDE { return true; }
6220 bool CanThrow() const OVERRIDE { return true; }
6221
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006223 uint32_t GetFieldIndex() const { return field_index_; }
6224
6225 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6226
6227 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6229 static constexpr size_t kFieldFieldTypeSize =
6230 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6231 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6232 kFieldFieldType + kFieldFieldTypeSize;
6233 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6234 "Too many packed fields.");
6235 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6236
Calin Juravlee460d1d2015-09-29 04:52:17 +01006237 const uint32_t field_index_;
6238
6239 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6240};
6241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006242// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006243class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006245 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6246 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006247
David Brazdilbbd733e2015-08-18 17:48:17 +01006248 bool CanBeNull() const OVERRIDE { return false; }
6249
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250 DECLARE_INSTRUCTION(LoadException);
6251
6252 private:
6253 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6254};
6255
David Brazdilcb1c0552015-08-04 16:22:25 +01006256// Implicit part of move-exception which clears thread-local exception storage.
6257// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006258class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006260 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6261 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006262
6263 DECLARE_INSTRUCTION(ClearException);
6264
6265 private:
6266 DISALLOW_COPY_AND_ASSIGN(HClearException);
6267};
6268
Vladimir Markofcb503c2016-05-18 12:48:17 +01006269class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006270 public:
6271 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006272 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006273 SetRawInputAt(0, exception);
6274 }
6275
6276 bool IsControlFlow() const OVERRIDE { return true; }
6277
6278 bool NeedsEnvironment() const OVERRIDE { return true; }
6279
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006280 bool CanThrow() const OVERRIDE { return true; }
6281
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006282
6283 DECLARE_INSTRUCTION(Throw);
6284
6285 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006286 DISALLOW_COPY_AND_ASSIGN(HThrow);
6287};
6288
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289/**
6290 * Implementation strategies for the code generator of a HInstanceOf
6291 * or `HCheckCast`.
6292 */
6293enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006294 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295 kExactCheck, // Can do a single class compare.
6296 kClassHierarchyCheck, // Can just walk the super class chain.
6297 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6298 kInterfaceCheck, // No optimization yet when checking against an interface.
6299 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 kArrayCheck, // No optimization yet when checking against a generic array.
6301 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302};
6303
Roland Levillain86503782016-02-11 19:07:30 +00006304std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6305
Vladimir Markofcb503c2016-05-18 12:48:17 +01006306class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308 HInstanceOf(HInstruction* object,
6309 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006311 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006312 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006314 dex_pc) {
6315 SetPackedField<TypeCheckKindField>(check_kind);
6316 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006317 SetRawInputAt(0, object);
6318 SetRawInputAt(1, constant);
6319 }
6320
6321 bool CanBeMoved() const OVERRIDE { return true; }
6322
Vladimir Marko372f10e2016-05-17 16:30:10 +01006323 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006324 return true;
6325 }
6326
6327 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329 }
6330
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006331 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6333 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6334 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6335 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006336
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006337 static bool CanCallRuntime(TypeCheckKind check_kind) {
6338 // Mips currently does runtime calls for any other checks.
6339 return check_kind != TypeCheckKind::kExactCheck;
6340 }
6341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006343 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006344 }
6345
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006346 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006347
6348 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006349 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6350 static constexpr size_t kFieldTypeCheckKindSize =
6351 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6352 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6353 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6354 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6355 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6358};
6359
Vladimir Markofcb503c2016-05-18 12:48:17 +01006360class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006361 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006362 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006363 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6365 SetPackedFlag<kFlagUpperCanBeNull>(true);
6366 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006367 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006368 SetRawInputAt(0, input);
6369 }
6370
David Brazdilf5552582015-12-27 13:36:12 +00006371 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006372 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006373 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006374 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006375
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006376 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 DCHECK(GetUpperCanBeNull() || !can_be_null);
6378 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006379 }
6380
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006382
Calin Juravleb1498f62015-02-16 13:13:29 +00006383 DECLARE_INSTRUCTION(BoundType);
6384
6385 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6387 // is false then CanBeNull() cannot be true).
6388 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6389 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6390 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6391 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6392
Calin Juravleb1498f62015-02-16 13:13:29 +00006393 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006394 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6395 // It is used to bound the type in cases like:
6396 // if (x instanceof ClassX) {
6397 // // uper_bound_ will be ClassX
6398 // }
David Brazdilf5552582015-12-27 13:36:12 +00006399 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006400
6401 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6402};
6403
Vladimir Markofcb503c2016-05-18 12:48:17 +01006404class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006405 public:
6406 HCheckCast(HInstruction* object,
6407 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006409 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6411 SetPackedField<TypeCheckKindField>(check_kind);
6412 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 SetRawInputAt(0, object);
6414 SetRawInputAt(1, constant);
6415 }
6416
6417 bool CanBeMoved() const OVERRIDE { return true; }
6418
Vladimir Marko372f10e2016-05-17 16:30:10 +01006419 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006420 return true;
6421 }
6422
6423 bool NeedsEnvironment() const OVERRIDE {
6424 // Instruction may throw a CheckCastError.
6425 return true;
6426 }
6427
6428 bool CanThrow() const OVERRIDE { return true; }
6429
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6431 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6432 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6433 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006434
6435 DECLARE_INSTRUCTION(CheckCast);
6436
6437 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6439 static constexpr size_t kFieldTypeCheckKindSize =
6440 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6441 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6442 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6443 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6444 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006445
6446 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006447};
6448
Andreas Gampe26de38b2016-07-27 17:53:11 -07006449/**
6450 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6451 * @details We define the combined barrier types that are actually required
6452 * by the Java Memory Model, rather than using exactly the terminology from
6453 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6454 * primitives. Note that the JSR-133 cookbook generally does not deal with
6455 * store atomicity issues, and the recipes there are not always entirely sufficient.
6456 * The current recipe is as follows:
6457 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6458 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6459 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6460 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6461 * class has final fields.
6462 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6463 * store-to-memory instructions. Only generated together with non-temporal stores.
6464 */
6465enum MemBarrierKind {
6466 kAnyStore,
6467 kLoadAny,
6468 kStoreStore,
6469 kAnyAny,
6470 kNTStoreStore,
6471 kLastBarrierKind = kNTStoreStore
6472};
6473std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6474
Vladimir Markofcb503c2016-05-18 12:48:17 +01006475class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006476 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006477 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006478 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6480 SetPackedField<BarrierKindField>(barrier_kind);
6481 }
Calin Juravle27df7582015-04-17 19:12:31 +01006482
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006484
6485 DECLARE_INSTRUCTION(MemoryBarrier);
6486
6487 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006488 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6489 static constexpr size_t kFieldBarrierKindSize =
6490 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6491 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6492 kFieldBarrierKind + kFieldBarrierKindSize;
6493 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6494 "Too many packed fields.");
6495 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006496
6497 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6498};
6499
Igor Murashkind01745e2017-04-05 16:40:31 -07006500// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6501// the specified object(s), with respect to any subsequent store that might "publish"
6502// (i.e. make visible) the specified object to another thread.
6503//
6504// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6505// for all final fields (that were set) at the end of the invoked constructor.
6506//
6507// The constructor fence models the freeze actions for the final fields of an object
6508// being constructed (semantically at the end of the constructor). Constructor fences
6509// have a per-object affinity; two separate objects being constructed get two separate
6510// constructor fences.
6511//
6512// (Note: that if calling a super-constructor or forwarding to another constructor,
6513// the freezes would happen at the end of *that* constructor being invoked).
6514//
6515// The memory model guarantees that when the object being constructed is "published" after
6516// constructor completion (i.e. escapes the current thread via a store), then any final field
6517// writes must be observable on other threads (once they observe that publication).
6518//
6519// Further, anything written before the freeze, and read by dereferencing through the final field,
6520// must also be visible (so final object field could itself have an object with non-final fields;
6521// yet the freeze must also extend to them).
6522//
6523// Constructor example:
6524//
6525// class HasFinal {
6526// final int field; Optimizing IR for <init>()V:
6527// HasFinal() {
6528// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6529// // freeze(this.field); HConstructorFence(this)
6530// } HReturn
6531// }
6532//
6533// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6534// already-initialized classes; in that case the allocation must act as a "default-initializer"
6535// of the object which effectively writes the class pointer "final field".
6536//
6537// For example, we can model default-initialiation as roughly the equivalent of the following:
6538//
6539// class Object {
6540// private final Class header;
6541// }
6542//
6543// Java code: Optimizing IR:
6544//
6545// T new_instance<T>() {
6546// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6547// obj.header = T.class; // header write is done by above call.
6548// // freeze(obj.header) HConstructorFence(obj)
6549// return (T)obj;
6550// }
6551//
6552// See also:
6553// * CompilerDriver::RequiresConstructorBarrier
6554// * QuasiAtomic::ThreadFenceForConstructor
6555//
6556class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6557 // A fence has variable inputs because the inputs can be removed
6558 // after prepare_for_register_allocation phase.
6559 // (TODO: In the future a fence could freeze multiple objects
6560 // after merging two fences together.)
6561 public:
6562 // `fence_object` is the reference that needs to be protected for correct publication.
6563 //
6564 // It makes sense in the following situations:
6565 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6566 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6567 //
6568 // After construction the `fence_object` becomes the 0th input.
6569 // This is not an input in a real sense, but just a convenient place to stash the information
6570 // about the associated object.
6571 HConstructorFence(HInstruction* fence_object,
6572 uint32_t dex_pc,
6573 ArenaAllocator* arena)
6574 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6575 // constraints described in the class header. We claim that these SideEffects constraints
6576 // enforce a superset of the real constraints.
6577 //
6578 // The ordering described above is conservatively modeled with SideEffects as follows:
6579 //
6580 // * To prevent reordering of the publication stores:
6581 // ----> "Reads of objects" is the initial SideEffect.
6582 // * For every primitive final field store in the constructor:
6583 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6584 // * If there are any stores to reference final fields in the constructor:
6585 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6586 // that are reachable from `fence_object` also need to be prevented for reordering
6587 // (and we do not want to do alias analysis to figure out what those stores are).
6588 //
6589 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6590 // even more conservative approach than the one described above, and prevents all of the
6591 // above reordering without analyzing any of the instructions in the constructor.
6592 //
6593 // If in a later phase we discover that there are no writes to reference final fields,
6594 // we can refine the side effect to a smaller set of type reads (see above constraints).
6595 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6596 dex_pc,
6597 arena,
6598 /* number_of_inputs */ 1,
6599 kArenaAllocConstructorFenceInputs) {
6600 DCHECK(fence_object != nullptr);
6601 SetRawInputAt(0, fence_object);
6602 }
6603
6604 // The object associated with this constructor fence.
6605 //
6606 // (Note: This will be null after the prepare_for_register_allocation phase,
6607 // as all constructor fence inputs are removed there).
6608 HInstruction* GetFenceObject() const {
6609 return InputAt(0);
6610 }
6611
6612 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6613 // - Delete `fence_use` from `this`'s use list.
6614 // - Delete `this` from `fence_use`'s inputs list.
6615 // - If the `fence_use` is dead, remove it from the graph.
6616 //
6617 // A fence is considered dead once it no longer has any uses
6618 // and all of the inputs are dead.
6619 //
6620 // This must *not* be called during/after prepare_for_register_allocation,
6621 // because that removes all the inputs to the fences but the fence is actually
6622 // still considered live.
6623 static void RemoveConstructorFences(HInstruction* instruction);
6624
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006625 // Check if this constructor fence is protecting
6626 // an HNewInstance or HNewArray that is also the immediate
6627 // predecessor of `this`.
6628 //
6629 // Returns the associated HNewArray or HNewInstance,
6630 // or null otherwise.
6631 HInstruction* GetAssociatedAllocation();
6632
Igor Murashkind01745e2017-04-05 16:40:31 -07006633 DECLARE_INSTRUCTION(ConstructorFence);
6634
6635 private:
6636 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6637};
6638
Vladimir Markofcb503c2016-05-18 12:48:17 +01006639class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006640 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006641 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006642 kEnter,
6643 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006644 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006645 };
6646
6647 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006648 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006649 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6650 dex_pc) {
6651 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006652 SetRawInputAt(0, object);
6653 }
6654
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006655 // Instruction may go into runtime, so we need an environment.
6656 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006657
6658 bool CanThrow() const OVERRIDE {
6659 // Verifier guarantees that monitor-exit cannot throw.
6660 // This is important because it allows the HGraphBuilder to remove
6661 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6662 return IsEnter();
6663 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006664
Vladimir Markoa1de9182016-02-25 11:37:38 +00006665 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6666 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006667
6668 DECLARE_INSTRUCTION(MonitorOperation);
6669
Calin Juravle52c48962014-12-16 17:02:57 +00006670 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006671 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6672 static constexpr size_t kFieldOperationKindSize =
6673 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6674 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6675 kFieldOperationKind + kFieldOperationKindSize;
6676 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6677 "Too many packed fields.");
6678 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006679
6680 private:
6681 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6682};
6683
Vladimir Markofcb503c2016-05-18 12:48:17 +01006684class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006685 public:
6686 HSelect(HInstruction* condition,
6687 HInstruction* true_value,
6688 HInstruction* false_value,
6689 uint32_t dex_pc)
6690 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6691 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6692
6693 // First input must be `true_value` or `false_value` to allow codegens to
6694 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6695 // that architectures which implement HSelect as a conditional move also
6696 // will not need to invert the condition.
6697 SetRawInputAt(0, false_value);
6698 SetRawInputAt(1, true_value);
6699 SetRawInputAt(2, condition);
6700 }
6701
6702 HInstruction* GetFalseValue() const { return InputAt(0); }
6703 HInstruction* GetTrueValue() const { return InputAt(1); }
6704 HInstruction* GetCondition() const { return InputAt(2); }
6705
6706 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006707 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6708 return true;
6709 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006710
6711 bool CanBeNull() const OVERRIDE {
6712 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6713 }
6714
6715 DECLARE_INSTRUCTION(Select);
6716
6717 private:
6718 DISALLOW_COPY_AND_ASSIGN(HSelect);
6719};
6720
Vladimir Markof9f64412015-09-02 14:05:49 +01006721class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006722 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006723 MoveOperands(Location source,
6724 Location destination,
6725 Primitive::Type type,
6726 HInstruction* instruction)
6727 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006728
6729 Location GetSource() const { return source_; }
6730 Location GetDestination() const { return destination_; }
6731
6732 void SetSource(Location value) { source_ = value; }
6733 void SetDestination(Location value) { destination_ = value; }
6734
6735 // The parallel move resolver marks moves as "in-progress" by clearing the
6736 // destination (but not the source).
6737 Location MarkPending() {
6738 DCHECK(!IsPending());
6739 Location dest = destination_;
6740 destination_ = Location::NoLocation();
6741 return dest;
6742 }
6743
6744 void ClearPending(Location dest) {
6745 DCHECK(IsPending());
6746 destination_ = dest;
6747 }
6748
6749 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006750 DCHECK(source_.IsValid() || destination_.IsInvalid());
6751 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006752 }
6753
6754 // True if this blocks a move from the given location.
6755 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006756 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006757 }
6758
6759 // A move is redundant if it's been eliminated, if its source and
6760 // destination are the same, or if its destination is unneeded.
6761 bool IsRedundant() const {
6762 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6763 }
6764
6765 // We clear both operands to indicate move that's been eliminated.
6766 void Eliminate() {
6767 source_ = destination_ = Location::NoLocation();
6768 }
6769
6770 bool IsEliminated() const {
6771 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6772 return source_.IsInvalid();
6773 }
6774
Alexey Frunze4dda3372015-06-01 18:31:49 -07006775 Primitive::Type GetType() const { return type_; }
6776
Nicolas Geoffray90218252015-04-15 11:56:51 +01006777 bool Is64BitMove() const {
6778 return Primitive::Is64BitType(type_);
6779 }
6780
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006781 HInstruction* GetInstruction() const { return instruction_; }
6782
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006783 private:
6784 Location source_;
6785 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006786 // The type this move is for.
6787 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006788 // The instruction this move is assocatied with. Null when this move is
6789 // for moving an input in the expected locations of user (including a phi user).
6790 // This is only used in debug mode, to ensure we do not connect interval siblings
6791 // in the same parallel move.
6792 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006793};
6794
Roland Levillainc9285912015-12-18 10:38:42 +00006795std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6796
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006797static constexpr size_t kDefaultNumberOfMoves = 4;
6798
Vladimir Markofcb503c2016-05-18 12:48:17 +01006799class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006800 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006801 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006802 : HTemplateInstruction(SideEffects::None(), dex_pc),
6803 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6804 moves_.reserve(kDefaultNumberOfMoves);
6805 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006806
Nicolas Geoffray90218252015-04-15 11:56:51 +01006807 void AddMove(Location source,
6808 Location destination,
6809 Primitive::Type type,
6810 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006811 DCHECK(source.IsValid());
6812 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006813 if (kIsDebugBuild) {
6814 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006815 for (const MoveOperands& move : moves_) {
6816 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006817 // Special case the situation where the move is for the spill slot
6818 // of the instruction.
6819 if ((GetPrevious() == instruction)
6820 || ((GetPrevious() == nullptr)
6821 && instruction->IsPhi()
6822 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006823 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006824 << "Doing parallel moves for the same instruction.";
6825 } else {
6826 DCHECK(false) << "Doing parallel moves for the same instruction.";
6827 }
6828 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006829 }
6830 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006831 for (const MoveOperands& move : moves_) {
6832 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006833 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006834 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006835 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006836 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006837 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006838 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006839 }
6840
Vladimir Marko225b6462015-09-28 12:17:40 +01006841 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006842 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006843 }
6844
Vladimir Marko225b6462015-09-28 12:17:40 +01006845 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006846
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006847 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006848
6849 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006850 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006851
6852 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6853};
6854
Mark Mendell0616ae02015-04-17 12:49:27 -04006855} // namespace art
6856
Aart Bikf8f5a162017-02-06 15:35:29 -08006857#include "nodes_vector.h"
6858
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006859#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6860#include "nodes_shared.h"
6861#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006862#ifdef ART_ENABLE_CODEGEN_mips
6863#include "nodes_mips.h"
6864#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006865#ifdef ART_ENABLE_CODEGEN_x86
6866#include "nodes_x86.h"
6867#endif
6868
6869namespace art {
6870
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006871class HGraphVisitor : public ValueObject {
6872 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006873 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6874 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006875
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006876 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006877 virtual void VisitBasicBlock(HBasicBlock* block);
6878
Roland Levillain633021e2014-10-01 14:12:25 +01006879 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006880 void VisitInsertionOrder();
6881
Roland Levillain633021e2014-10-01 14:12:25 +01006882 // Visit the graph following dominator tree reverse post-order.
6883 void VisitReversePostOrder();
6884
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006885 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006886
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006887 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006888#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006889 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6890
6891 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6892
6893#undef DECLARE_VISIT_INSTRUCTION
6894
6895 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006896 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006897
6898 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6899};
6900
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006901class HGraphDelegateVisitor : public HGraphVisitor {
6902 public:
6903 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6904 virtual ~HGraphDelegateVisitor() {}
6905
6906 // Visit functions that delegate to to super class.
6907#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006908 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006909
6910 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6911
6912#undef DECLARE_VISIT_INSTRUCTION
6913
6914 private:
6915 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6916};
6917
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006918// Iterator over the blocks that art part of the loop. Includes blocks part
6919// of an inner loop. The order in which the blocks are iterated is on their
6920// block id.
6921class HBlocksInLoopIterator : public ValueObject {
6922 public:
6923 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6924 : blocks_in_loop_(info.GetBlocks()),
6925 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6926 index_(0) {
6927 if (!blocks_in_loop_.IsBitSet(index_)) {
6928 Advance();
6929 }
6930 }
6931
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006932 bool Done() const { return index_ == blocks_.size(); }
6933 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006934 void Advance() {
6935 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006936 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006937 if (blocks_in_loop_.IsBitSet(index_)) {
6938 break;
6939 }
6940 }
6941 }
6942
6943 private:
6944 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006945 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006946 size_t index_;
6947
6948 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6949};
6950
Mingyao Yang3584bce2015-05-19 16:01:59 -07006951// Iterator over the blocks that art part of the loop. Includes blocks part
6952// of an inner loop. The order in which the blocks are iterated is reverse
6953// post order.
6954class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6955 public:
6956 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6957 : blocks_in_loop_(info.GetBlocks()),
6958 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6959 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006960 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006961 Advance();
6962 }
6963 }
6964
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006965 bool Done() const { return index_ == blocks_.size(); }
6966 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006967 void Advance() {
6968 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006969 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6970 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006971 break;
6972 }
6973 }
6974 }
6975
6976 private:
6977 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006978 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006979 size_t index_;
6980
6981 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6982};
6983
Aart Bikf3e61ee2017-04-12 17:09:20 -07006984// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006985inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006986 if (constant->IsIntConstant()) {
6987 return constant->AsIntConstant()->GetValue();
6988 } else if (constant->IsLongConstant()) {
6989 return constant->AsLongConstant()->GetValue();
6990 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006991 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006992 return 0;
6993 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006994}
6995
Aart Bikf3e61ee2017-04-12 17:09:20 -07006996// Returns true iff instruction is an integral constant (and sets value on success).
6997inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
6998 if (instruction->IsIntConstant()) {
6999 *value = instruction->AsIntConstant()->GetValue();
7000 return true;
7001 } else if (instruction->IsLongConstant()) {
7002 *value = instruction->AsLongConstant()->GetValue();
7003 return true;
7004 } else if (instruction->IsNullConstant()) {
7005 *value = 0;
7006 return true;
7007 }
7008 return false;
7009}
7010
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007011#define INSTRUCTION_TYPE_CHECK(type, super) \
7012 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7013 inline const H##type* HInstruction::As##type() const { \
7014 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7015 } \
7016 inline H##type* HInstruction::As##type() { \
7017 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7018 }
7019
7020 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7021#undef INSTRUCTION_TYPE_CHECK
7022
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007023// Create space in `blocks` for adding `number_of_new_blocks` entries
7024// starting at location `at`. Blocks after `at` are moved accordingly.
7025inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7026 size_t number_of_new_blocks,
7027 size_t after) {
7028 DCHECK_LT(after, blocks->size());
7029 size_t old_size = blocks->size();
7030 size_t new_size = old_size + number_of_new_blocks;
7031 blocks->resize(new_size);
7032 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7033}
7034
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007035/*
7036 * Hunt "under the hood" of array lengths (leading to array references),
7037 * null checks (also leading to array references), and new arrays
7038 * (leading to the actual length). This makes it more likely related
7039 * instructions become actually comparable.
7040 */
7041inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7042 while (instruction->IsArrayLength() ||
7043 instruction->IsNullCheck() ||
7044 instruction->IsNewArray()) {
7045 instruction = instruction->IsNewArray()
7046 ? instruction->AsNewArray()->GetLength()
7047 : instruction->InputAt(0);
7048 }
7049 return instruction;
7050}
7051
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007052} // namespace art
7053
7054#endif // ART_COMPILER_OPTIMIZING_NODES_H_