blob: b4da20b558f90aa349f24becdbc0a61cccf09755 [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) \
1409 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001410#endif
1411
Vladimir Markob4536b72015-11-24 13:45:23 +00001412#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001413#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001414#else
1415#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1416 M(ArmDexCacheArraysBase, Instruction)
1417#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001418
1419#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1420
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001421#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001423#else
1424#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1425 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001426 M(MipsDexCacheArraysBase, Instruction) \
1427 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001428#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001429
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001430#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1431
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001432#ifndef ART_ENABLE_CODEGEN_x86
1433#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1434#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001435#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1436 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001437 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001438 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001439 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001440#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001441
1442#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1443
1444#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1445 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001446 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001447 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1448 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001449 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001450 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001451 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1452 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1453
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001454#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1455 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001456 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001457 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001458 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001459 M(Invoke, Instruction) \
1460 M(VecOperation, Instruction) \
1461 M(VecUnaryOperation, VecOperation) \
1462 M(VecBinaryOperation, VecOperation) \
1463 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001464
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001465#define FOR_EACH_INSTRUCTION(M) \
1466 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1467 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1468
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001469#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001470FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1471#undef FORWARD_DECLARATION
1472
Vladimir Marko372f10e2016-05-17 16:30:10 +01001473#define DECLARE_INSTRUCTION(type) \
1474 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1475 const char* DebugName() const OVERRIDE { return #type; } \
1476 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1477 return other->Is##type(); \
1478 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001479 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001480
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001481#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1482 bool Is##type() const { return As##type() != nullptr; } \
1483 const H##type* As##type() const { return this; } \
1484 H##type* As##type() { return this; }
1485
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001487class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001489 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001490 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001491 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001492 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001493 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001494 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001495
Vladimir Marko46817b82016-03-29 12:21:58 +01001496 // Hook for the IntrusiveForwardList<>.
1497 // TODO: Hide this better.
1498 IntrusiveForwardListHook hook;
1499
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500 private:
David Brazdiled596192015-01-23 10:39:45 +00001501 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001502 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001503
1504 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001505 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001506
Vladimir Marko46817b82016-03-29 12:21:58 +01001507 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001508
1509 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1510};
1511
David Brazdiled596192015-01-23 10:39:45 +00001512template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001513using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001514
David Brazdil1abb4192015-02-17 18:33:36 +00001515// This class is used by HEnvironment and HInstruction classes to record the
1516// instructions they use and pointers to the corresponding HUseListNodes kept
1517// by the used instructions.
1518template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001519class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001520 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001521 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1522 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001523
Vladimir Marko46817b82016-03-29 12:21:58 +01001524 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1525 : HUserRecord(old_record.instruction_, before_use_node) {}
1526 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1527 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001528 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001529 }
1530
1531 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001532 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1533 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001534
1535 private:
1536 // Instruction used by the user.
1537 HInstruction* instruction_;
1538
Vladimir Marko46817b82016-03-29 12:21:58 +01001539 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1540 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001541};
1542
Vladimir Markoe9004912016-06-16 16:50:52 +01001543// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1544// This is used for HInstruction::GetInputs() to return a container wrapper providing
1545// HInstruction* values even though the underlying container has HUserRecord<>s.
1546struct HInputExtractor {
1547 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1548 return record.GetInstruction();
1549 }
1550 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1551 return record.GetInstruction();
1552 }
1553};
1554
1555using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1556using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1557
Aart Bik854a02b2015-07-14 16:07:00 -07001558/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001560 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 * For write/read dependences on fields/arrays, the dependence analysis uses
1562 * type disambiguation (e.g. a float field write cannot modify the value of an
1563 * integer field read) and the access type (e.g. a reference array write cannot
1564 * modify the value of a reference field read [although it may modify the
1565 * reference fetch prior to reading the field, which is represented by its own
1566 * write/read dependence]). The analysis makes conservative points-to
1567 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1568 * the same, and any reference read depends on any reference read without
1569 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001570 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001571 * The internal representation uses 38-bit and is described in the table below.
1572 * The first line indicates the side effect, and for field/array accesses the
1573 * second line indicates the type of the access (in the order of the
1574 * Primitive::Type enum).
1575 * The two numbered lines below indicate the bit position in the bitfield (read
1576 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001577 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001578 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1579 * +-------------+---------+---------+--------------+---------+---------+
1580 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1581 * | 3 |333333322|222222221| 1 |111111110|000000000|
1582 * | 7 |654321098|765432109| 8 |765432109|876543210|
1583 *
1584 * Note that, to ease the implementation, 'changes' bits are least significant
1585 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001586 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001587class SideEffects : public ValueObject {
1588 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001589 SideEffects() : flags_(0) {}
1590
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001591 static SideEffects None() {
1592 return SideEffects(0);
1593 }
1594
1595 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 return SideEffects(kAllChangeBits | kAllDependOnBits);
1597 }
1598
1599 static SideEffects AllChanges() {
1600 return SideEffects(kAllChangeBits);
1601 }
1602
1603 static SideEffects AllDependencies() {
1604 return SideEffects(kAllDependOnBits);
1605 }
1606
1607 static SideEffects AllExceptGCDependency() {
1608 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1609 }
1610
1611 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001612 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001613 }
1614
Aart Bik34c3ba92015-07-20 14:08:59 -07001615 static SideEffects AllWrites() {
1616 return SideEffects(kAllWrites);
1617 }
1618
1619 static SideEffects AllReads() {
1620 return SideEffects(kAllReads);
1621 }
1622
1623 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1624 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001626 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001627 }
1628
Aart Bik854a02b2015-07-14 16:07:00 -07001629 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001630 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001631 }
1632
Aart Bik34c3ba92015-07-20 14:08:59 -07001633 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1634 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001636 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001637 }
1638
1639 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001640 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001641 }
1642
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001643 static SideEffects CanTriggerGC() {
1644 return SideEffects(1ULL << kCanTriggerGCBit);
1645 }
1646
1647 static SideEffects DependsOnGC() {
1648 return SideEffects(1ULL << kDependsOnGCBit);
1649 }
1650
Aart Bik854a02b2015-07-14 16:07:00 -07001651 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001652 SideEffects Union(SideEffects other) const {
1653 return SideEffects(flags_ | other.flags_);
1654 }
1655
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 SideEffects Exclusion(SideEffects other) const {
1657 return SideEffects(flags_ & ~other.flags_);
1658 }
1659
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001660 void Add(SideEffects other) {
1661 flags_ |= other.flags_;
1662 }
1663
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 bool Includes(SideEffects other) const {
1665 return (other.flags_ & flags_) == other.flags_;
1666 }
1667
1668 bool HasSideEffects() const {
1669 return (flags_ & kAllChangeBits);
1670 }
1671
1672 bool HasDependencies() const {
1673 return (flags_ & kAllDependOnBits);
1674 }
1675
1676 // Returns true if there are no side effects or dependencies.
1677 bool DoesNothing() const {
1678 return flags_ == 0;
1679 }
1680
Aart Bik854a02b2015-07-14 16:07:00 -07001681 // Returns true if something is written.
1682 bool DoesAnyWrite() const {
1683 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001684 }
1685
Aart Bik854a02b2015-07-14 16:07:00 -07001686 // Returns true if something is read.
1687 bool DoesAnyRead() const {
1688 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001689 }
1690
Aart Bik854a02b2015-07-14 16:07:00 -07001691 // Returns true if potentially everything is written and read
1692 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001693 bool DoesAllReadWrite() const {
1694 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1695 }
1696
Aart Bik854a02b2015-07-14 16:07:00 -07001697 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001699 }
1700
Roland Levillain0d5a2812015-11-13 10:07:31 +00001701 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001702 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1704 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001705 }
1706
1707 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001708 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001709 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001710 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 for (int s = kLastBit; s >= 0; s--) {
1712 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1713 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1714 // This is a bit for the GC side effect.
1715 if (current_bit_is_set) {
1716 flags += "GC";
1717 }
Aart Bik854a02b2015-07-14 16:07:00 -07001718 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001719 } else {
1720 // This is a bit for the array/field analysis.
1721 // The underscore character stands for the 'can trigger GC' bit.
1722 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1723 if (current_bit_is_set) {
1724 flags += kDebug[s];
1725 }
1726 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1727 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1728 flags += "|";
1729 }
1730 }
Aart Bik854a02b2015-07-14 16:07:00 -07001731 }
1732 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001733 }
1734
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001735 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001736
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001737 private:
1738 static constexpr int kFieldArrayAnalysisBits = 9;
1739
1740 static constexpr int kFieldWriteOffset = 0;
1741 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1742 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1743 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1744
1745 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1746
1747 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1748 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1749 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1750 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1751
1752 static constexpr int kLastBit = kDependsOnGCBit;
1753 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1754
1755 // Aliases.
1756
1757 static_assert(kChangeBits == kDependOnBits,
1758 "the 'change' bits should match the 'depend on' bits.");
1759
1760 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1761 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1762 static constexpr uint64_t kAllWrites =
1763 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1764 static constexpr uint64_t kAllReads =
1765 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001766
Aart Bik854a02b2015-07-14 16:07:00 -07001767 // Translates type to bit flag.
1768 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1769 CHECK_NE(type, Primitive::kPrimVoid);
1770 const uint64_t one = 1;
1771 const int shift = type; // 0-based consecutive enum
1772 DCHECK_LE(kFieldWriteOffset, shift);
1773 DCHECK_LT(shift, kArrayWriteOffset);
1774 return one << (type + offset);
1775 }
1776
1777 // Private constructor on direct flags value.
1778 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1779
1780 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001781};
1782
David Brazdiled596192015-01-23 10:39:45 +00001783// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001784class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001785 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001786 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1787 size_t number_of_vregs,
1788 ArtMethod* method,
1789 uint32_t dex_pc,
1790 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1792 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001794 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001795 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001796 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 }
1798
Mingyao Yang01b47b02017-02-03 12:09:57 -08001799 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001800 : HEnvironment(arena,
1801 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001802 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001803 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001804 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001805
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001807 if (parent_ != nullptr) {
1808 parent_->SetAndCopyParentChain(allocator, parent);
1809 } else {
1810 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1811 parent_->CopyFrom(parent);
1812 if (parent->GetParent() != nullptr) {
1813 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1814 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 }
David Brazdiled596192015-01-23 10:39:45 +00001816 }
1817
Vladimir Marko71bf8092015-09-15 15:33:14 +01001818 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001819 void CopyFrom(HEnvironment* environment);
1820
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001821 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1822 // input to the loop phi instead. This is for inserting instructions that
1823 // require an environment (like HDeoptimization) in the loop pre-header.
1824 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001825
1826 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001828 }
1829
1830 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001831 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001832 }
1833
David Brazdil1abb4192015-02-17 18:33:36 +00001834 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001835
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001836 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001837
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 HEnvironment* GetParent() const { return parent_; }
1839
1840 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001841 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001842 }
1843
1844 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001845 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001846 }
1847
1848 uint32_t GetDexPc() const {
1849 return dex_pc_;
1850 }
1851
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001852 ArtMethod* GetMethod() const {
1853 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001854 }
1855
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001856 HInstruction* GetHolder() const {
1857 return holder_;
1858 }
1859
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001860
1861 bool IsFromInlinedInvoke() const {
1862 return GetParent() != nullptr;
1863 }
1864
David Brazdiled596192015-01-23 10:39:45 +00001865 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001866 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1867 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001869 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001870 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001871
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001872 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001873 HInstruction* const holder_;
1874
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001875 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001876
1877 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1878};
1879
Vladimir Markof9f64412015-09-02 14:05:49 +01001880class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001881 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001882 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883 : previous_(nullptr),
1884 next_(nullptr),
1885 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001886 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001888 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001889 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001891 locations_(nullptr),
1892 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001894 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001895 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1896 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1897 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001898
Dave Allison20dfc792014-06-16 20:44:29 -07001899 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001901#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001902 enum InstructionKind {
1903 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1904 };
1905#undef DECLARE_KIND
1906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 HInstruction* GetNext() const { return next_; }
1908 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001909
Calin Juravle77520bc2015-01-12 18:45:46 +00001910 HInstruction* GetNextDisregardingMoves() const;
1911 HInstruction* GetPreviousDisregardingMoves() const;
1912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001914 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001915 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001916 bool IsInBlock() const { return block_ != nullptr; }
1917 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001918 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1919 bool IsIrreducibleLoopHeaderPhi() const {
1920 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1921 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001922
Vladimir Marko372f10e2016-05-17 16:30:10 +01001923 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1924
1925 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1926 // One virtual method is enough, just const_cast<> and then re-add the const.
1927 return ArrayRef<const HUserRecord<HInstruction*>>(
1928 const_cast<HInstruction*>(this)->GetInputRecords());
1929 }
1930
Vladimir Markoe9004912016-06-16 16:50:52 +01001931 HInputsRef GetInputs() {
1932 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001933 }
1934
Vladimir Markoe9004912016-06-16 16:50:52 +01001935 HConstInputsRef GetInputs() const {
1936 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001937 }
1938
1939 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001940 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941
Aart Bik2767f4b2016-10-28 15:03:53 -07001942 bool HasInput(HInstruction* input) const {
1943 for (const HInstruction* i : GetInputs()) {
1944 if (i == input) {
1945 return true;
1946 }
1947 }
1948 return false;
1949 }
1950
Vladimir Marko372f10e2016-05-17 16:30:10 +01001951 void SetRawInputAt(size_t index, HInstruction* input) {
1952 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1953 }
1954
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955 virtual void Accept(HGraphVisitor* visitor) = 0;
1956 virtual const char* DebugName() const = 0;
1957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001961
1962 uint32_t GetDexPc() const { return dex_pc_; }
1963
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001964 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001965
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001966 // Can the instruction throw?
1967 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1968 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001969 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001970 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001971
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001972 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001973 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Calin Juravle10e244f2015-01-26 18:54:32 +00001975 // Does not apply for all instructions, but having this at top level greatly
1976 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001977 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001978 virtual bool CanBeNull() const {
1979 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1980 return true;
1981 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001983 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001984 return false;
1985 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001986
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001987 virtual bool IsActualObject() const {
1988 return GetType() == Primitive::kPrimNot;
1989 }
1990
Calin Juravle2e768302015-07-28 14:41:11 +00001991 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001992
Calin Juravle61d544b2015-02-23 16:46:57 +00001993 ReferenceTypeInfo GetReferenceTypeInfo() const {
1994 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001995 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001996 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001997 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001999 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002000 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002001 // Note: fixup_end remains valid across push_front().
2002 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2003 HUseListNode<HInstruction*>* new_node =
2004 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2005 uses_.push_front(*new_node);
2006 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007 }
2008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002010 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002011 // Note: env_fixup_end remains valid across push_front().
2012 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2013 HUseListNode<HEnvironment*>* new_node =
2014 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2015 env_uses_.push_front(*new_node);
2016 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 }
2018
David Brazdil1abb4192015-02-17 18:33:36 +00002019 void RemoveAsUserOfInput(size_t input) {
2020 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002021 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2022 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2023 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002024 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025
Vladimir Marko372f10e2016-05-17 16:30:10 +01002026 void RemoveAsUserOfAllInputs() {
2027 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2028 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2029 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2030 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2031 }
2032 }
2033
David Brazdil1abb4192015-02-17 18:33:36 +00002034 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2035 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
Vladimir Marko46817b82016-03-29 12:21:58 +01002037 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2038 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2039 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002040 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002041 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002042 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043
Aart Bikcc42be02016-10-20 16:14:16 -07002044 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002045 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002046 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002047 !CanThrow() &&
2048 !IsSuspendCheck() &&
2049 !IsControlFlow() &&
2050 !IsNativeDebugInfo() &&
2051 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002052 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002053 !IsMemoryBarrier() &&
2054 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002055 }
2056
Aart Bikcc42be02016-10-20 16:14:16 -07002057 bool IsDeadAndRemovable() const {
2058 return IsRemovable() && !HasUses();
2059 }
2060
Roland Levillain6c82d402014-10-13 16:10:27 +01002061 // Does this instruction strictly dominate `other_instruction`?
2062 // Returns false if this instruction and `other_instruction` are the same.
2063 // Aborts if this instruction and `other_instruction` are both phis.
2064 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002066 int GetId() const { return id_; }
2067 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002069 int GetSsaIndex() const { return ssa_index_; }
2070 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2071 bool HasSsaIndex() const { return ssa_index_ != -1; }
2072
2073 bool HasEnvironment() const { return environment_ != nullptr; }
2074 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002075 // Set the `environment_` field. Raw because this method does not
2076 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002077 void SetRawEnvironment(HEnvironment* environment) {
2078 DCHECK(environment_ == nullptr);
2079 DCHECK_EQ(environment->GetHolder(), this);
2080 environment_ = environment;
2081 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002082
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002083 void InsertRawEnvironment(HEnvironment* environment) {
2084 DCHECK(environment_ != nullptr);
2085 DCHECK_EQ(environment->GetHolder(), this);
2086 DCHECK(environment->GetParent() == nullptr);
2087 environment->parent_ = environment_;
2088 environment_ = environment;
2089 }
2090
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002091 void RemoveEnvironment();
2092
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002093 // Set the environment of this instruction, copying it from `environment`. While
2094 // copying, the uses lists are being updated.
2095 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002096 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002097 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002098 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002099 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002100 if (environment->GetParent() != nullptr) {
2101 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2102 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002103 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002105 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2106 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002107 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002108 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002109 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002110 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002111 if (environment->GetParent() != nullptr) {
2112 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2113 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002114 }
2115
Nicolas Geoffray39468442014-09-02 15:17:15 +01002116 // Returns the number of entries in the environment. Typically, that is the
2117 // number of dex registers in a method. It could be more in case of inlining.
2118 size_t EnvironmentSize() const;
2119
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002120 LocationSummary* GetLocations() const { return locations_; }
2121 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002124 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126
Alexandre Rames188d4312015-04-09 18:30:21 +01002127 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2128 // uses of this instruction by `other` are *not* updated.
2129 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2130 ReplaceWith(other);
2131 other->ReplaceInput(this, use_index);
2132 }
2133
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002134 // Move `this` instruction before `cursor`
2135 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002136
Vladimir Markofb337ea2015-11-25 15:25:10 +00002137 // Move `this` before its first user and out of any loops. If there is no
2138 // out-of-loop user that dominates all other users, move the instruction
2139 // to the end of the out-of-loop common dominator of the user's blocks.
2140 //
2141 // This can be used only on non-throwing instructions with no side effects that
2142 // have at least one use but no environment uses.
2143 void MoveBeforeFirstUserAndOutOfLoops();
2144
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002145#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 bool Is##type() const; \
2147 const H##type* As##type() const; \
2148 H##type* As##type();
2149
2150 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2151#undef INSTRUCTION_TYPE_CHECK
2152
2153#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002154 bool Is##type() const { return (As##type() != nullptr); } \
2155 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002156 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002157 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002158#undef INSTRUCTION_TYPE_CHECK
2159
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002160 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002161 // TODO: this method is used by LICM and GVN with possibly different
2162 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163 virtual bool CanBeMoved() const { return false; }
2164
2165 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002166 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002167 return false;
2168 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169
2170 // Returns whether any data encoded in the two instructions is equal.
2171 // This method does not look at the inputs. Both instructions must be
2172 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002173 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002174 return false;
2175 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176
2177 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002178 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002179 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002180 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002181
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002182 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2183 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2184 // the virtual function because the __attribute__((__pure__)) doesn't really
2185 // apply the strong requirement for virtual functions, preventing optimizations.
2186 InstructionKind GetKind() const PURE;
2187 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002188
2189 virtual size_t ComputeHashCode() const {
2190 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002191 for (const HInstruction* input : GetInputs()) {
2192 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002193 }
2194 return result;
2195 }
2196
2197 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002198 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002199 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002200
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002201 size_t GetLifetimePosition() const { return lifetime_position_; }
2202 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2203 LiveInterval* GetLiveInterval() const { return live_interval_; }
2204 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2205 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2206
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002207 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2208
2209 // Returns whether the code generation of the instruction will require to have access
2210 // to the current method. Such instructions are:
2211 // (1): Instructions that require an environment, as calling the runtime requires
2212 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002213 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2214 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002215 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002216 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002217 }
2218
Vladimir Markodc151b22015-10-15 18:02:30 +01002219 // Returns whether the code generation of the instruction will require to have access
2220 // to the dex cache of the current method's declaring class via the current method.
2221 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002222
Mark Mendellc4701932015-04-10 13:18:51 -04002223 // Does this instruction have any use in an environment before
2224 // control flow hits 'other'?
2225 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2226
2227 // Remove all references to environment uses of this instruction.
2228 // The caller must ensure that this is safe to do.
2229 void RemoveEnvironmentUsers();
2230
Vladimir Markoa1de9182016-02-25 11:37:38 +00002231 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2232 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002233
David Brazdil1abb4192015-02-17 18:33:36 +00002234 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002235 // If set, the machine code for this instruction is assumed to be generated by
2236 // its users. Used by liveness analysis to compute use positions accordingly.
2237 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2238 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2239 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2240 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2241
Vladimir Marko372f10e2016-05-17 16:30:10 +01002242 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2243 return GetInputRecords()[i];
2244 }
2245
2246 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2247 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2248 input_records[index] = input;
2249 }
David Brazdil1abb4192015-02-17 18:33:36 +00002250
Vladimir Markoa1de9182016-02-25 11:37:38 +00002251 uint32_t GetPackedFields() const {
2252 return packed_fields_;
2253 }
2254
2255 template <size_t flag>
2256 bool GetPackedFlag() const {
2257 return (packed_fields_ & (1u << flag)) != 0u;
2258 }
2259
2260 template <size_t flag>
2261 void SetPackedFlag(bool value = true) {
2262 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2263 }
2264
2265 template <typename BitFieldType>
2266 typename BitFieldType::value_type GetPackedField() const {
2267 return BitFieldType::Decode(packed_fields_);
2268 }
2269
2270 template <typename BitFieldType>
2271 void SetPackedField(typename BitFieldType::value_type value) {
2272 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2273 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2274 }
2275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002277 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2278 auto before_use_node = uses_.before_begin();
2279 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2280 HInstruction* user = use_node->GetUser();
2281 size_t input_index = use_node->GetIndex();
2282 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2283 before_use_node = use_node;
2284 }
2285 }
2286
2287 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2288 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2289 if (next != uses_.end()) {
2290 HInstruction* next_user = next->GetUser();
2291 size_t next_index = next->GetIndex();
2292 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2293 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2294 }
2295 }
2296
2297 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2298 auto before_env_use_node = env_uses_.before_begin();
2299 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2300 HEnvironment* user = env_use_node->GetUser();
2301 size_t input_index = env_use_node->GetIndex();
2302 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2303 before_env_use_node = env_use_node;
2304 }
2305 }
2306
2307 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2308 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2309 if (next != env_uses_.end()) {
2310 HEnvironment* next_user = next->GetUser();
2311 size_t next_index = next->GetIndex();
2312 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2313 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2314 }
2315 }
David Brazdil1abb4192015-02-17 18:33:36 +00002316
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002317 HInstruction* previous_;
2318 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002319 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002321
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 // An instruction gets an id when it is added to the graph.
2323 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002324 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002325 int id_;
2326
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002327 // When doing liveness analysis, instructions that have uses get an SSA index.
2328 int ssa_index_;
2329
Vladimir Markoa1de9182016-02-25 11:37:38 +00002330 // Packed fields.
2331 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002332
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002333 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002334 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335
2336 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002337 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002338
Nicolas Geoffray39468442014-09-02 15:17:15 +01002339 // The environment associated with this instruction. Not null if the instruction
2340 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002341 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002342
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002343 // Set by the code generator.
2344 LocationSummary* locations_;
2345
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002346 // Set by the liveness analysis.
2347 LiveInterval* live_interval_;
2348
2349 // Set by the liveness analysis, this is the position in a linear
2350 // order of blocks where this instruction's live interval start.
2351 size_t lifetime_position_;
2352
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002353 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002354
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 // The reference handle part of the reference type info.
2356 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002357 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002358 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002359
David Brazdil1abb4192015-02-17 18:33:36 +00002360 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002361 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002362 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002363 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
2366 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2367};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002368std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002370// Iterates over the instructions, while preserving the next instruction
2371// in case the current instruction gets removed from the list by the user
2372// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002373class HInstructionIterator : public ValueObject {
2374 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002375 explicit HInstructionIterator(const HInstructionList& instructions)
2376 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002377 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002378 }
2379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002380 bool Done() const { return instruction_ == nullptr; }
2381 HInstruction* Current() const { return instruction_; }
2382 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002384 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002385 }
2386
2387 private:
2388 HInstruction* instruction_;
2389 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002390
2391 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002392};
2393
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002394// Iterates over the instructions without saving the next instruction,
2395// therefore handling changes in the graph potentially made by the user
2396// of this iterator.
2397class HInstructionIteratorHandleChanges : public ValueObject {
2398 public:
2399 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2400 : instruction_(instructions.first_instruction_) {
2401 }
2402
2403 bool Done() const { return instruction_ == nullptr; }
2404 HInstruction* Current() const { return instruction_; }
2405 void Advance() {
2406 instruction_ = instruction_->GetNext();
2407 }
2408
2409 private:
2410 HInstruction* instruction_;
2411
2412 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2413};
2414
2415
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002416class HBackwardInstructionIterator : public ValueObject {
2417 public:
2418 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2419 : instruction_(instructions.last_instruction_) {
2420 next_ = Done() ? nullptr : instruction_->GetPrevious();
2421 }
2422
2423 bool Done() const { return instruction_ == nullptr; }
2424 HInstruction* Current() const { return instruction_; }
2425 void Advance() {
2426 instruction_ = next_;
2427 next_ = Done() ? nullptr : instruction_->GetPrevious();
2428 }
2429
2430 private:
2431 HInstruction* instruction_;
2432 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002433
2434 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002435};
2436
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002437class HVariableInputSizeInstruction : public HInstruction {
2438 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002439 using HInstruction::GetInputRecords; // Keep the const version visible.
2440 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2441 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2442 }
2443
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002444 void AddInput(HInstruction* input);
2445 void InsertInputAt(size_t index, HInstruction* input);
2446 void RemoveInputAt(size_t index);
2447
Igor Murashkind01745e2017-04-05 16:40:31 -07002448 // Removes all the inputs.
2449 // Also removes this instructions from each input's use list
2450 // (for non-environment uses only).
2451 void RemoveAllInputs();
2452
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002453 protected:
2454 HVariableInputSizeInstruction(SideEffects side_effects,
2455 uint32_t dex_pc,
2456 ArenaAllocator* arena,
2457 size_t number_of_inputs,
2458 ArenaAllocKind kind)
2459 : HInstruction(side_effects, dex_pc),
2460 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2461
2462 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2463
2464 private:
2465 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2466};
2467
Vladimir Markof9f64412015-09-02 14:05:49 +01002468template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469class HTemplateInstruction: public HInstruction {
2470 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002471 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002473 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474
Vladimir Marko372f10e2016-05-17 16:30:10 +01002475 using HInstruction::GetInputRecords; // Keep the const version visible.
2476 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2477 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002478 }
2479
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002481 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482
2483 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484};
2485
Vladimir Markof9f64412015-09-02 14:05:49 +01002486// HTemplateInstruction specialization for N=0.
2487template<>
2488class HTemplateInstruction<0>: public HInstruction {
2489 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002490 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 : HInstruction(side_effects, dex_pc) {}
2492
Vladimir Markof9f64412015-09-02 14:05:49 +01002493 virtual ~HTemplateInstruction() {}
2494
Vladimir Marko372f10e2016-05-17 16:30:10 +01002495 using HInstruction::GetInputRecords; // Keep the const version visible.
2496 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2497 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002498 }
2499
2500 private:
2501 friend class SsaBuilder;
2502};
2503
Dave Allison20dfc792014-06-16 20:44:29 -07002504template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002505class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002506 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002507 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002508 : HTemplateInstruction<N>(side_effects, dex_pc) {
2509 this->template SetPackedField<TypeField>(type);
2510 }
Dave Allison20dfc792014-06-16 20:44:29 -07002511 virtual ~HExpression() {}
2512
Vladimir Markoa1de9182016-02-25 11:37:38 +00002513 Primitive::Type GetType() const OVERRIDE {
2514 return TypeField::Decode(this->GetPackedFields());
2515 }
Dave Allison20dfc792014-06-16 20:44:29 -07002516
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002517 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002518 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2519 static constexpr size_t kFieldTypeSize =
2520 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2521 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2522 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2523 "Too many packed fields.");
2524 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002525};
2526
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2528// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002529class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002531 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2532 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002534 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002536 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2540};
2541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542// Represents dex's RETURN opcodes. A HReturn is a control flow
2543// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002544class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2547 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002548 SetRawInputAt(0, value);
2549 }
2550
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002551 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002552
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002553 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554
2555 private:
2556 DISALLOW_COPY_AND_ASSIGN(HReturn);
2557};
2558
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002559class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002560 public:
2561 HPhi(ArenaAllocator* arena,
2562 uint32_t reg_number,
2563 size_t number_of_inputs,
2564 Primitive::Type type,
2565 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002566 : HVariableInputSizeInstruction(
2567 SideEffects::None(),
2568 dex_pc,
2569 arena,
2570 number_of_inputs,
2571 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002572 reg_number_(reg_number) {
2573 SetPackedField<TypeField>(ToPhiType(type));
2574 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2575 // Phis are constructed live and marked dead if conflicting or unused.
2576 // Individual steps of SsaBuilder should assume that if a phi has been
2577 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2578 SetPackedFlag<kFlagIsLive>(true);
2579 SetPackedFlag<kFlagCanBeNull>(true);
2580 }
2581
2582 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2583 static Primitive::Type ToPhiType(Primitive::Type type) {
2584 return Primitive::PrimitiveKind(type);
2585 }
2586
2587 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2588
David Brazdildee58d62016-04-07 09:54:26 +00002589 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2590 void SetType(Primitive::Type new_type) {
2591 // Make sure that only valid type changes occur. The following are allowed:
2592 // (1) int -> float/ref (primitive type propagation),
2593 // (2) long -> double (primitive type propagation).
2594 DCHECK(GetType() == new_type ||
2595 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2596 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2597 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2598 SetPackedField<TypeField>(new_type);
2599 }
2600
2601 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2602 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2603
2604 uint32_t GetRegNumber() const { return reg_number_; }
2605
2606 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2607 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2608 bool IsDead() const { return !IsLive(); }
2609 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2610
Vladimir Markoe9004912016-06-16 16:50:52 +01002611 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002612 return other != nullptr
2613 && other->IsPhi()
2614 && other->AsPhi()->GetBlock() == GetBlock()
2615 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2616 }
2617
2618 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2619 // An equivalent phi is a phi having the same dex register and type.
2620 // It assumes that phis with the same dex register are adjacent.
2621 HPhi* GetNextEquivalentPhiWithSameType() {
2622 HInstruction* next = GetNext();
2623 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2624 if (next->GetType() == GetType()) {
2625 return next->AsPhi();
2626 }
2627 next = next->GetNext();
2628 }
2629 return nullptr;
2630 }
2631
2632 DECLARE_INSTRUCTION(Phi);
2633
David Brazdildee58d62016-04-07 09:54:26 +00002634 private:
2635 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2636 static constexpr size_t kFieldTypeSize =
2637 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2638 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2639 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2640 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2641 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2642 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2643
David Brazdildee58d62016-04-07 09:54:26 +00002644 const uint32_t reg_number_;
2645
2646 DISALLOW_COPY_AND_ASSIGN(HPhi);
2647};
2648
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002649// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002650// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002651// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002652class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002653 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002654 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002655
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002656 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002658 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659
2660 private:
2661 DISALLOW_COPY_AND_ASSIGN(HExit);
2662};
2663
2664// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002665class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002667 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002668
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002669 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002670
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002671 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002672 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002673 }
2674
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002675 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676
2677 private:
2678 DISALLOW_COPY_AND_ASSIGN(HGoto);
2679};
2680
Roland Levillain9867bc72015-08-05 10:21:34 +01002681class HConstant : public HExpression<0> {
2682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002683 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2684 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002685
2686 bool CanBeMoved() const OVERRIDE { return true; }
2687
Roland Levillain1a653882016-03-18 18:05:57 +00002688 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002690 // Is this constant 0 in the arithmetic sense?
2691 virtual bool IsArithmeticZero() const { return false; }
2692 // Is this constant a 0-bit pattern?
2693 virtual bool IsZeroBitPattern() const { return false; }
2694 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 virtual bool IsOne() const { return false; }
2696
David Brazdil77a48ae2015-09-15 12:34:04 +00002697 virtual uint64_t GetValueAsUint64() const = 0;
2698
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002699 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002700
2701 private:
2702 DISALLOW_COPY_AND_ASSIGN(HConstant);
2703};
2704
Vladimir Markofcb503c2016-05-18 12:48:17 +01002705class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002706 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002707 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002708 return true;
2709 }
2710
David Brazdil77a48ae2015-09-15 12:34:04 +00002711 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2712
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 size_t ComputeHashCode() const OVERRIDE { return 0; }
2714
Roland Levillain1a653882016-03-18 18:05:57 +00002715 // The null constant representation is a 0-bit pattern.
2716 virtual bool IsZeroBitPattern() const { return true; }
2717
Roland Levillain9867bc72015-08-05 10:21:34 +01002718 DECLARE_INSTRUCTION(NullConstant);
2719
2720 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002721 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002722
2723 friend class HGraph;
2724 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2725};
2726
2727// Constants of the type int. Those can be from Dex instructions, or
2728// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002729class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002730 public:
2731 int32_t GetValue() const { return value_; }
2732
David Brazdil9f389d42015-10-01 14:32:56 +01002733 uint64_t GetValueAsUint64() const OVERRIDE {
2734 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2735 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002736
Vladimir Marko372f10e2016-05-17 16:30:10 +01002737 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002738 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 return other->AsIntConstant()->value_ == value_;
2740 }
2741
2742 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2743
2744 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002745 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2746 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002747 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2748
Roland Levillain1a653882016-03-18 18:05:57 +00002749 // Integer constants are used to encode Boolean values as well,
2750 // where 1 means true and 0 means false.
2751 bool IsTrue() const { return GetValue() == 1; }
2752 bool IsFalse() const { return GetValue() == 0; }
2753
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 DECLARE_INSTRUCTION(IntConstant);
2755
2756 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002757 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2758 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2759 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2760 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002761
2762 const int32_t value_;
2763
2764 friend class HGraph;
2765 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2766 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2767 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2768};
2769
Vladimir Markofcb503c2016-05-18 12:48:17 +01002770class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002771 public:
2772 int64_t GetValue() const { return value_; }
2773
David Brazdil77a48ae2015-09-15 12:34:04 +00002774 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2775
Vladimir Marko372f10e2016-05-17 16:30:10 +01002776 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002777 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002778 return other->AsLongConstant()->value_ == value_;
2779 }
2780
2781 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2782
2783 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002784 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2785 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002786 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2787
2788 DECLARE_INSTRUCTION(LongConstant);
2789
2790 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002791 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2792 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002793
2794 const int64_t value_;
2795
2796 friend class HGraph;
2797 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2798};
Dave Allison20dfc792014-06-16 20:44:29 -07002799
Vladimir Markofcb503c2016-05-18 12:48:17 +01002800class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002801 public:
2802 float GetValue() const { return value_; }
2803
2804 uint64_t GetValueAsUint64() const OVERRIDE {
2805 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2806 }
2807
Vladimir Marko372f10e2016-05-17 16:30:10 +01002808 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002809 DCHECK(other->IsFloatConstant()) << other->DebugName();
2810 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2811 }
2812
2813 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2814
2815 bool IsMinusOne() const OVERRIDE {
2816 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2817 }
Roland Levillain1a653882016-03-18 18:05:57 +00002818 bool IsArithmeticZero() const OVERRIDE {
2819 return std::fpclassify(value_) == FP_ZERO;
2820 }
2821 bool IsArithmeticPositiveZero() const {
2822 return IsArithmeticZero() && !std::signbit(value_);
2823 }
2824 bool IsArithmeticNegativeZero() const {
2825 return IsArithmeticZero() && std::signbit(value_);
2826 }
2827 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002828 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002829 }
2830 bool IsOne() const OVERRIDE {
2831 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2832 }
2833 bool IsNaN() const {
2834 return std::isnan(value_);
2835 }
2836
2837 DECLARE_INSTRUCTION(FloatConstant);
2838
2839 private:
2840 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2841 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2842 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2843 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2844
2845 const float value_;
2846
2847 // Only the SsaBuilder and HGraph can create floating-point constants.
2848 friend class SsaBuilder;
2849 friend class HGraph;
2850 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2851};
2852
Vladimir Markofcb503c2016-05-18 12:48:17 +01002853class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002854 public:
2855 double GetValue() const { return value_; }
2856
2857 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2858
Vladimir Marko372f10e2016-05-17 16:30:10 +01002859 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002860 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2861 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2862 }
2863
2864 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2865
2866 bool IsMinusOne() const OVERRIDE {
2867 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2868 }
Roland Levillain1a653882016-03-18 18:05:57 +00002869 bool IsArithmeticZero() const OVERRIDE {
2870 return std::fpclassify(value_) == FP_ZERO;
2871 }
2872 bool IsArithmeticPositiveZero() const {
2873 return IsArithmeticZero() && !std::signbit(value_);
2874 }
2875 bool IsArithmeticNegativeZero() const {
2876 return IsArithmeticZero() && std::signbit(value_);
2877 }
2878 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002879 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002880 }
2881 bool IsOne() const OVERRIDE {
2882 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2883 }
2884 bool IsNaN() const {
2885 return std::isnan(value_);
2886 }
2887
2888 DECLARE_INSTRUCTION(DoubleConstant);
2889
2890 private:
2891 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2892 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2893 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2894 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2895
2896 const double value_;
2897
2898 // Only the SsaBuilder and HGraph can create floating-point constants.
2899 friend class SsaBuilder;
2900 friend class HGraph;
2901 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2902};
2903
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002904// Conditional branch. A block ending with an HIf instruction must have
2905// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002906class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002907 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002908 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2909 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002910 SetRawInputAt(0, input);
2911 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002912
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002913 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002914
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002915 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002916 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002917 }
2918
2919 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002920 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002921 }
2922
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002923 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002924
2925 private:
2926 DISALLOW_COPY_AND_ASSIGN(HIf);
2927};
2928
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
2930// Abstract instruction which marks the beginning and/or end of a try block and
2931// links it to the respective exception handlers. Behaves the same as a Goto in
2932// non-exceptional control flow.
2933// Normal-flow successor is stored at index zero, exception handlers under
2934// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002935class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002936 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002937 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002938 kEntry,
2939 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002940 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002941 };
2942
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002943 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002944 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2945 SetPackedField<BoundaryKindField>(kind);
2946 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002947
2948 bool IsControlFlow() const OVERRIDE { return true; }
2949
2950 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002951 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002952
David Brazdild26a4112015-11-10 11:07:31 +00002953 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2954 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2955 }
2956
David Brazdilfc6a86a2015-06-26 10:33:45 +00002957 // Returns whether `handler` is among its exception handlers (non-zero index
2958 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002959 bool HasExceptionHandler(const HBasicBlock& handler) const {
2960 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002961 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002962 }
2963
2964 // If not present already, adds `handler` to its block's list of exception
2965 // handlers.
2966 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002967 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002968 GetBlock()->AddSuccessor(handler);
2969 }
2970 }
2971
Vladimir Markoa1de9182016-02-25 11:37:38 +00002972 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2973 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002974
David Brazdilffee3d32015-07-06 11:48:53 +01002975 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2976
David Brazdilfc6a86a2015-06-26 10:33:45 +00002977 DECLARE_INSTRUCTION(TryBoundary);
2978
2979 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002980 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2981 static constexpr size_t kFieldBoundaryKindSize =
2982 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2983 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2984 kFieldBoundaryKind + kFieldBoundaryKindSize;
2985 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2986 "Too many packed fields.");
2987 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002988
2989 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2990};
2991
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002992// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002993class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002994 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002995 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2996 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002997 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002998 : HVariableInputSizeInstruction(
2999 SideEffects::All(),
3000 dex_pc,
3001 arena,
3002 /* number_of_inputs */ 1,
3003 kArenaAllocMisc) {
3004 SetPackedFlag<kFieldCanBeMoved>(false);
3005 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003006 SetRawInputAt(0, cond);
3007 }
3008
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003009 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3010 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3011 // instead of `guard`.
3012 // We set CanTriggerGC to prevent any intermediate address to be live
3013 // at the point of the `HDeoptimize`.
3014 HDeoptimize(ArenaAllocator* arena,
3015 HInstruction* cond,
3016 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003017 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003018 uint32_t dex_pc)
3019 : HVariableInputSizeInstruction(
3020 SideEffects::CanTriggerGC(),
3021 dex_pc,
3022 arena,
3023 /* number_of_inputs */ 2,
3024 kArenaAllocMisc) {
3025 SetPackedFlag<kFieldCanBeMoved>(true);
3026 SetPackedField<DeoptimizeKindField>(kind);
3027 SetRawInputAt(0, cond);
3028 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003029 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003030
3031 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3032
3033 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3034 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3035 }
3036
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003037 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003038
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003039 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003040
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003041 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003042
3043 Primitive::Type GetType() const OVERRIDE {
3044 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3045 }
3046
3047 bool GuardsAnInput() const {
3048 return InputCount() == 2;
3049 }
3050
3051 HInstruction* GuardedInput() const {
3052 DCHECK(GuardsAnInput());
3053 return InputAt(1);
3054 }
3055
3056 void RemoveGuard() {
3057 RemoveInputAt(1);
3058 }
3059
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003060 DECLARE_INSTRUCTION(Deoptimize);
3061
3062 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003063 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3064 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3065 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003066 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003067 static constexpr size_t kNumberOfDeoptimizePackedBits =
3068 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3069 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3070 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003071 using DeoptimizeKindField =
3072 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003073
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003074 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3075};
3076
Mingyao Yang063fc772016-08-02 11:02:54 -07003077// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3078// The compiled code checks this flag value in a guard before devirtualized call and
3079// if it's true, starts to do deoptimization.
3080// It has a 4-byte slot on stack.
3081// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003082class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003083 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003084 // CHA guards are only optimized in a separate pass and it has no side effects
3085 // with regard to other passes.
3086 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3087 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003088 }
3089
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003090 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3091
3092 // We do all CHA guard elimination/motion in a single pass, after which there is no
3093 // further guard elimination/motion since a guard might have been used for justification
3094 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3095 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003096 bool CanBeMoved() const OVERRIDE { return false; }
3097
3098 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3099
3100 private:
3101 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3102};
3103
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003104// Represents the ArtMethod that was passed as a first argument to
3105// the method. It is used by instructions that depend on it, like
3106// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003107class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003108 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003109 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3110 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003111
3112 DECLARE_INSTRUCTION(CurrentMethod);
3113
3114 private:
3115 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3116};
3117
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003118// Fetches an ArtMethod from the virtual table or the interface method table
3119// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003120class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003121 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003123 kVTable,
3124 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003126 };
3127 HClassTableGet(HInstruction* cls,
3128 Primitive::Type type,
3129 TableKind kind,
3130 size_t index,
3131 uint32_t dex_pc)
3132 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 index_(index) {
3134 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003135 SetRawInputAt(0, cls);
3136 }
3137
3138 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003139 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003140 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003141 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003142 }
3143
Vladimir Markoa1de9182016-02-25 11:37:38 +00003144 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003145 size_t GetIndex() const { return index_; }
3146
3147 DECLARE_INSTRUCTION(ClassTableGet);
3148
3149 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3151 static constexpr size_t kFieldTableKindSize =
3152 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3153 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3154 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3155 "Too many packed fields.");
3156 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3157
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003158 // The index of the ArtMethod in the table.
3159 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003160
3161 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3162};
3163
Mark Mendellfe57faa2015-09-18 09:26:15 -04003164// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3165// have one successor for each entry in the switch table, and the final successor
3166// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003167class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003168 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003169 HPackedSwitch(int32_t start_value,
3170 uint32_t num_entries,
3171 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003172 uint32_t dex_pc = kNoDexPc)
3173 : HTemplateInstruction(SideEffects::None(), dex_pc),
3174 start_value_(start_value),
3175 num_entries_(num_entries) {
3176 SetRawInputAt(0, input);
3177 }
3178
3179 bool IsControlFlow() const OVERRIDE { return true; }
3180
3181 int32_t GetStartValue() const { return start_value_; }
3182
Vladimir Marko211c2112015-09-24 16:52:33 +01003183 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003184
3185 HBasicBlock* GetDefaultBlock() const {
3186 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003187 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003188 }
3189 DECLARE_INSTRUCTION(PackedSwitch);
3190
3191 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003192 const int32_t start_value_;
3193 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003194
3195 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3196};
3197
Roland Levillain88cb1752014-10-20 16:36:47 +01003198class HUnaryOperation : public HExpression<1> {
3199 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003200 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3201 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003202 SetRawInputAt(0, input);
3203 }
3204
3205 HInstruction* GetInput() const { return InputAt(0); }
3206 Primitive::Type GetResultType() const { return GetType(); }
3207
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003208 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003209 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003210 return true;
3211 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003212
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 // Try to statically evaluate `this` and return a HConstant
3214 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003215 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003216 HConstant* TryStaticEvaluation() const;
3217
3218 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003219 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3220 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003221 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3222 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003223
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003224 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003225
3226 private:
3227 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3228};
3229
Dave Allison20dfc792014-06-16 20:44:29 -07003230class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003231 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003232 HBinaryOperation(Primitive::Type result_type,
3233 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003234 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003235 SideEffects side_effects = SideEffects::None(),
3236 uint32_t dex_pc = kNoDexPc)
3237 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003238 SetRawInputAt(0, left);
3239 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003240 }
3241
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003242 HInstruction* GetLeft() const { return InputAt(0); }
3243 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003244 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003245
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003246 virtual bool IsCommutative() const { return false; }
3247
3248 // Put constant on the right.
3249 // Returns whether order is changed.
3250 bool OrderInputsWithConstantOnTheRight() {
3251 HInstruction* left = InputAt(0);
3252 HInstruction* right = InputAt(1);
3253 if (left->IsConstant() && !right->IsConstant()) {
3254 ReplaceInput(right, 0);
3255 ReplaceInput(left, 1);
3256 return true;
3257 }
3258 return false;
3259 }
3260
3261 // Order inputs by instruction id, but favor constant on the right side.
3262 // This helps GVN for commutative ops.
3263 void OrderInputs() {
3264 DCHECK(IsCommutative());
3265 HInstruction* left = InputAt(0);
3266 HInstruction* right = InputAt(1);
3267 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3268 return;
3269 }
3270 if (OrderInputsWithConstantOnTheRight()) {
3271 return;
3272 }
3273 // Order according to instruction id.
3274 if (left->GetId() > right->GetId()) {
3275 ReplaceInput(right, 0);
3276 ReplaceInput(left, 1);
3277 }
3278 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003279
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003280 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003281 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003282 return true;
3283 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003284
Roland Levillain31dd3d62016-02-16 12:21:02 +00003285 // Try to statically evaluate `this` and return a HConstant
3286 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003287 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003288 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003289
3290 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3292 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003293 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3294 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003295 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003296 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3297 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003298 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3299 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003300 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3301 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003302 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003303 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3304 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003305
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003306 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003307 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003308 HConstant* GetConstantRight() const;
3309
3310 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003311 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003312 HInstruction* GetLeastConstantLeft() const;
3313
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003314 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003315
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003316 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003317 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3318};
3319
Mark Mendellc4701932015-04-10 13:18:51 -04003320// The comparison bias applies for floating point operations and indicates how NaN
3321// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003322enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003323 kNoBias, // bias is not applicable (i.e. for long operation)
3324 kGtBias, // return 1 for NaN comparisons
3325 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003326 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003327};
3328
Roland Levillain31dd3d62016-02-16 12:21:02 +00003329std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3330
Dave Allison20dfc792014-06-16 20:44:29 -07003331class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003332 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003333 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003334 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3335 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3336 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003337
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003338 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003339 // `instruction`, and disregard moves in between.
3340 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003341
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003342 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003343
3344 virtual IfCondition GetCondition() const = 0;
3345
Mark Mendellc4701932015-04-10 13:18:51 -04003346 virtual IfCondition GetOppositeCondition() const = 0;
3347
Vladimir Markoa1de9182016-02-25 11:37:38 +00003348 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003349 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3350
Vladimir Markoa1de9182016-02-25 11:37:38 +00003351 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3352 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003353
Vladimir Marko372f10e2016-05-17 16:30:10 +01003354 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003355 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003356 }
3357
Roland Levillain4fa13f62015-07-06 18:11:54 +01003358 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003360 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003361 if (if_cond == kCondNE) {
3362 return true;
3363 } else if (if_cond == kCondEQ) {
3364 return false;
3365 }
3366 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003367 }
3368
3369 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003370 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003371 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003372 if (if_cond == kCondEQ) {
3373 return true;
3374 } else if (if_cond == kCondNE) {
3375 return false;
3376 }
3377 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003378 }
3379
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003381 // Needed if we merge a HCompare into a HCondition.
3382 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3383 static constexpr size_t kFieldComparisonBiasSize =
3384 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3385 static constexpr size_t kNumberOfConditionPackedBits =
3386 kFieldComparisonBias + kFieldComparisonBiasSize;
3387 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3388 using ComparisonBiasField =
3389 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3390
Roland Levillain31dd3d62016-02-16 12:21:02 +00003391 template <typename T>
3392 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3393
3394 template <typename T>
3395 int32_t CompareFP(T x, T y) const {
3396 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3397 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3398 // Handle the bias.
3399 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3400 }
3401
3402 // Return an integer constant containing the result of a condition evaluated at compile time.
3403 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3404 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3405 }
3406
Dave Allison20dfc792014-06-16 20:44:29 -07003407 private:
3408 DISALLOW_COPY_AND_ASSIGN(HCondition);
3409};
3410
3411// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003412class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003414 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3415 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003416
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003417 bool IsCommutative() const OVERRIDE { return true; }
3418
Vladimir Marko9e23df52015-11-10 17:14:35 +00003419 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3420 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(true, GetDexPc());
3422 }
3423 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3424 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3425 }
3426 // In the following Evaluate methods, a HCompare instruction has
3427 // been merged into this HEqual instruction; evaluate it as
3428 // `Compare(x, y) == 0`.
3429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3431 GetDexPc());
3432 }
3433 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3434 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3435 }
3436 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3437 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003438 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003439
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003440 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003442 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003443 return kCondEQ;
3444 }
3445
Mark Mendellc4701932015-04-10 13:18:51 -04003446 IfCondition GetOppositeCondition() const OVERRIDE {
3447 return kCondNE;
3448 }
3449
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003450 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003451 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003452
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003453 DISALLOW_COPY_AND_ASSIGN(HEqual);
3454};
3455
Vladimir Markofcb503c2016-05-18 12:48:17 +01003456class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003457 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003458 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3459 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003460
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003461 bool IsCommutative() const OVERRIDE { return true; }
3462
Vladimir Marko9e23df52015-11-10 17:14:35 +00003463 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3464 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003465 return MakeConstantCondition(false, GetDexPc());
3466 }
3467 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3468 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3469 }
3470 // In the following Evaluate methods, a HCompare instruction has
3471 // been merged into this HNotEqual instruction; evaluate it as
3472 // `Compare(x, y) != 0`.
3473 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3474 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3475 }
3476 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3477 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3478 }
3479 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3480 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003481 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003482
Dave Allison20dfc792014-06-16 20:44:29 -07003483 DECLARE_INSTRUCTION(NotEqual);
3484
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003485 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003486 return kCondNE;
3487 }
3488
Mark Mendellc4701932015-04-10 13:18:51 -04003489 IfCondition GetOppositeCondition() const OVERRIDE {
3490 return kCondEQ;
3491 }
3492
Dave Allison20dfc792014-06-16 20:44:29 -07003493 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003494 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003495
Dave Allison20dfc792014-06-16 20:44:29 -07003496 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3497};
3498
Vladimir Markofcb503c2016-05-18 12:48:17 +01003499class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003500 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003501 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3502 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003503
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003506 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 // In the following Evaluate methods, a HCompare instruction has
3508 // been merged into this HLessThan instruction; evaluate it as
3509 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3512 }
3513 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3514 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3515 }
3516 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3517 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003518 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003519
Dave Allison20dfc792014-06-16 20:44:29 -07003520 DECLARE_INSTRUCTION(LessThan);
3521
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003522 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003523 return kCondLT;
3524 }
3525
Mark Mendellc4701932015-04-10 13:18:51 -04003526 IfCondition GetOppositeCondition() const OVERRIDE {
3527 return kCondGE;
3528 }
3529
Dave Allison20dfc792014-06-16 20:44:29 -07003530 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003531 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003532
Dave Allison20dfc792014-06-16 20:44:29 -07003533 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3534};
3535
Vladimir Markofcb503c2016-05-18 12:48:17 +01003536class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003537 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003538 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3539 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003540
Roland Levillain9867bc72015-08-05 10:21:34 +01003541 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003542 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003543 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 // In the following Evaluate methods, a HCompare instruction has
3545 // been merged into this HLessThanOrEqual instruction; evaluate it as
3546 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3549 }
3550 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3551 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3552 }
3553 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3554 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003555 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003556
Dave Allison20dfc792014-06-16 20:44:29 -07003557 DECLARE_INSTRUCTION(LessThanOrEqual);
3558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003559 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003560 return kCondLE;
3561 }
3562
Mark Mendellc4701932015-04-10 13:18:51 -04003563 IfCondition GetOppositeCondition() const OVERRIDE {
3564 return kCondGT;
3565 }
3566
Dave Allison20dfc792014-06-16 20:44:29 -07003567 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003568 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003569
Dave Allison20dfc792014-06-16 20:44:29 -07003570 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3571};
3572
Vladimir Markofcb503c2016-05-18 12:48:17 +01003573class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003575 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3576 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003577
Roland Levillain9867bc72015-08-05 10:21:34 +01003578 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003579 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003580 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003581 // In the following Evaluate methods, a HCompare instruction has
3582 // been merged into this HGreaterThan instruction; evaluate it as
3583 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003584 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3586 }
3587 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3588 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3589 }
3590 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3591 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003592 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003593
Dave Allison20dfc792014-06-16 20:44:29 -07003594 DECLARE_INSTRUCTION(GreaterThan);
3595
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003596 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003597 return kCondGT;
3598 }
3599
Mark Mendellc4701932015-04-10 13:18:51 -04003600 IfCondition GetOppositeCondition() const OVERRIDE {
3601 return kCondLE;
3602 }
3603
Dave Allison20dfc792014-06-16 20:44:29 -07003604 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003605 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003606
Dave Allison20dfc792014-06-16 20:44:29 -07003607 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3608};
3609
Vladimir Markofcb503c2016-05-18 12:48:17 +01003610class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003612 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3613 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003614
Roland Levillain9867bc72015-08-05 10:21:34 +01003615 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003617 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 // In the following Evaluate methods, a HCompare instruction has
3619 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3620 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003621 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003622 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3623 }
3624 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3625 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3626 }
3627 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3628 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003629 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003630
Dave Allison20dfc792014-06-16 20:44:29 -07003631 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003633 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003634 return kCondGE;
3635 }
3636
Mark Mendellc4701932015-04-10 13:18:51 -04003637 IfCondition GetOppositeCondition() const OVERRIDE {
3638 return kCondLT;
3639 }
3640
Dave Allison20dfc792014-06-16 20:44:29 -07003641 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003642 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003643
Dave Allison20dfc792014-06-16 20:44:29 -07003644 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3645};
3646
Vladimir Markofcb503c2016-05-18 12:48:17 +01003647class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003648 public:
3649 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3650 : HCondition(first, second, dex_pc) {}
3651
3652 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003654 }
3655 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3657 }
3658 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3659 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3660 LOG(FATAL) << DebugName() << " is not defined for float values";
3661 UNREACHABLE();
3662 }
3663 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3664 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3665 LOG(FATAL) << DebugName() << " is not defined for double values";
3666 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003667 }
3668
3669 DECLARE_INSTRUCTION(Below);
3670
3671 IfCondition GetCondition() const OVERRIDE {
3672 return kCondB;
3673 }
3674
3675 IfCondition GetOppositeCondition() const OVERRIDE {
3676 return kCondAE;
3677 }
3678
3679 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003680 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 return MakeUnsigned(x) < MakeUnsigned(y);
3682 }
Aart Bike9f37602015-10-09 11:15:55 -07003683
3684 DISALLOW_COPY_AND_ASSIGN(HBelow);
3685};
3686
Vladimir Markofcb503c2016-05-18 12:48:17 +01003687class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003688 public:
3689 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3690 : HCondition(first, second, dex_pc) {}
3691
3692 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003693 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003694 }
3695 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003696 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3697 }
3698 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3699 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3700 LOG(FATAL) << DebugName() << " is not defined for float values";
3701 UNREACHABLE();
3702 }
3703 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3704 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3705 LOG(FATAL) << DebugName() << " is not defined for double values";
3706 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003707 }
3708
3709 DECLARE_INSTRUCTION(BelowOrEqual);
3710
3711 IfCondition GetCondition() const OVERRIDE {
3712 return kCondBE;
3713 }
3714
3715 IfCondition GetOppositeCondition() const OVERRIDE {
3716 return kCondA;
3717 }
3718
3719 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003720 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 return MakeUnsigned(x) <= MakeUnsigned(y);
3722 }
Aart Bike9f37602015-10-09 11:15:55 -07003723
3724 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3725};
3726
Vladimir Markofcb503c2016-05-18 12:48:17 +01003727class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003728 public:
3729 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3730 : HCondition(first, second, dex_pc) {}
3731
3732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003734 }
3735 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003736 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3737 }
3738 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3739 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3740 LOG(FATAL) << DebugName() << " is not defined for float values";
3741 UNREACHABLE();
3742 }
3743 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3744 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3745 LOG(FATAL) << DebugName() << " is not defined for double values";
3746 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003747 }
3748
3749 DECLARE_INSTRUCTION(Above);
3750
3751 IfCondition GetCondition() const OVERRIDE {
3752 return kCondA;
3753 }
3754
3755 IfCondition GetOppositeCondition() const OVERRIDE {
3756 return kCondBE;
3757 }
3758
3759 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003760 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 return MakeUnsigned(x) > MakeUnsigned(y);
3762 }
Aart Bike9f37602015-10-09 11:15:55 -07003763
3764 DISALLOW_COPY_AND_ASSIGN(HAbove);
3765};
3766
Vladimir Markofcb503c2016-05-18 12:48:17 +01003767class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003768 public:
3769 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3770 : HCondition(first, second, dex_pc) {}
3771
3772 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003773 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003774 }
3775 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003776 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3777 }
3778 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3779 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3780 LOG(FATAL) << DebugName() << " is not defined for float values";
3781 UNREACHABLE();
3782 }
3783 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3784 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3785 LOG(FATAL) << DebugName() << " is not defined for double values";
3786 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003787 }
3788
3789 DECLARE_INSTRUCTION(AboveOrEqual);
3790
3791 IfCondition GetCondition() const OVERRIDE {
3792 return kCondAE;
3793 }
3794
3795 IfCondition GetOppositeCondition() const OVERRIDE {
3796 return kCondB;
3797 }
3798
3799 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003800 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003801 return MakeUnsigned(x) >= MakeUnsigned(y);
3802 }
Aart Bike9f37602015-10-09 11:15:55 -07003803
3804 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3805};
Dave Allison20dfc792014-06-16 20:44:29 -07003806
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003807// Instruction to check how two inputs compare to each other.
3808// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003809class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003810 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003811 // Note that `comparison_type` is the type of comparison performed
3812 // between the comparison's inputs, not the type of the instantiated
3813 // HCompare instruction (which is always Primitive::kPrimInt).
3814 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003815 HInstruction* first,
3816 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003817 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003818 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003819 : HBinaryOperation(Primitive::kPrimInt,
3820 first,
3821 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003822 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003823 dex_pc) {
3824 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003825 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3826 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003827 }
3828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3831
3832 template <typename T>
3833 int32_t ComputeFP(T x, T y) const {
3834 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3835 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3836 // Handle the bias.
3837 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3838 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003839
Roland Levillain9867bc72015-08-05 10:21:34 +01003840 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3842 // reach this code path when processing a freshly built HIR
3843 // graph. However HCompare integer instructions can be synthesized
3844 // by the instruction simplifier to implement IntegerCompare and
3845 // IntegerSignum intrinsics, so we have to handle this case.
3846 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003847 }
3848 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003849 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3850 }
3851 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3852 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3853 }
3854 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3855 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003856 }
3857
Vladimir Marko372f10e2016-05-17 16:30:10 +01003858 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003859 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003860 }
3861
Vladimir Markoa1de9182016-02-25 11:37:38 +00003862 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003863
Roland Levillain31dd3d62016-02-16 12:21:02 +00003864 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 bool IsGtBias() const {
3867 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003870
Roland Levillain1693a1f2016-03-15 14:57:31 +00003871 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3872 // Comparisons do not require a runtime call in any back end.
3873 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003874 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003875
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003876 DECLARE_INSTRUCTION(Compare);
3877
Roland Levillain31dd3d62016-02-16 12:21:02 +00003878 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003879 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3880 static constexpr size_t kFieldComparisonBiasSize =
3881 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3882 static constexpr size_t kNumberOfComparePackedBits =
3883 kFieldComparisonBias + kFieldComparisonBiasSize;
3884 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3885 using ComparisonBiasField =
3886 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3887
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 // Return an integer constant containing the result of a comparison evaluated at compile time.
3889 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3890 DCHECK(value == -1 || value == 0 || value == 1) << value;
3891 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3892 }
3893
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003894 private:
3895 DISALLOW_COPY_AND_ASSIGN(HCompare);
3896};
3897
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003898class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003899 public:
3900 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003901 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003902 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003903 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003904 bool finalizable,
3905 QuickEntrypointEnum entrypoint)
3906 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3907 type_index_(type_index),
3908 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003909 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003910 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003911 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003912 }
3913
Andreas Gampea5b09a62016-11-17 15:21:22 -08003914 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003915 const DexFile& GetDexFile() const { return dex_file_; }
3916
3917 // Calls runtime so needs an environment.
3918 bool NeedsEnvironment() const OVERRIDE { return true; }
3919
Mingyao Yang062157f2016-03-02 10:15:36 -08003920 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3921 bool CanThrow() const OVERRIDE { return true; }
3922
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003923 bool NeedsChecks() const {
3924 return entrypoint_ == kQuickAllocObjectWithChecks;
3925 }
David Brazdil6de19382016-01-08 17:37:10 +00003926
Vladimir Markoa1de9182016-02-25 11:37:38 +00003927 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003928
3929 bool CanBeNull() const OVERRIDE { return false; }
3930
3931 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3932
3933 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3934 entrypoint_ = entrypoint;
3935 }
3936
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003937 HLoadClass* GetLoadClass() const {
3938 HInstruction* input = InputAt(0);
3939 if (input->IsClinitCheck()) {
3940 input = input->InputAt(0);
3941 }
3942 DCHECK(input->IsLoadClass());
3943 return input->AsLoadClass();
3944 }
3945
David Brazdil6de19382016-01-08 17:37:10 +00003946 bool IsStringAlloc() const;
3947
3948 DECLARE_INSTRUCTION(NewInstance);
3949
3950 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003951 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003952 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3953 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3954 "Too many packed fields.");
3955
Andreas Gampea5b09a62016-11-17 15:21:22 -08003956 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003957 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003958 QuickEntrypointEnum entrypoint_;
3959
3960 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3961};
3962
Agi Csaki05f20562015-08-19 14:58:14 -07003963enum IntrinsicNeedsEnvironmentOrCache {
3964 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3965 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003966};
3967
Aart Bik5d75afe2015-12-14 11:57:01 -08003968enum IntrinsicSideEffects {
3969 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3970 kReadSideEffects, // Intrinsic may read heap memory.
3971 kWriteSideEffects, // Intrinsic may write heap memory.
3972 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3973};
3974
3975enum IntrinsicExceptions {
3976 kNoThrow, // Intrinsic does not throw any exceptions.
3977 kCanThrow // Intrinsic may throw exceptions.
3978};
3979
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003980class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003981 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003982 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003983
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003984 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003985 SetRawInputAt(index, argument);
3986 }
3987
Roland Levillain3e3d7332015-04-28 11:00:54 +01003988 // Return the number of arguments. This number can be lower than
3989 // the number of inputs returned by InputCount(), as some invoke
3990 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3991 // inputs at the end of their list of inputs.
3992 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3993
Vladimir Markoa1de9182016-02-25 11:37:38 +00003994 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003995
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003996 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3997
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003998 InvokeType GetInvokeType() const {
3999 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004000 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004001
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004002 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004003 return intrinsic_;
4004 }
4005
Aart Bik5d75afe2015-12-14 11:57:01 -08004006 void SetIntrinsic(Intrinsics intrinsic,
4007 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4008 IntrinsicSideEffects side_effects,
4009 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004010
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004011 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004012 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004013 }
4014
Aart Bikff7d89c2016-11-07 08:49:28 -08004015 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4016
Vladimir Markoa1de9182016-02-25 11:37:38 +00004017 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004018
Aart Bik71bf7b42016-11-16 10:17:46 -08004019 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004020
Vladimir Marko372f10e2016-05-17 16:30:10 +01004021 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004022 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4023 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004024
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004025 uint32_t* GetIntrinsicOptimizations() {
4026 return &intrinsic_optimizations_;
4027 }
4028
4029 const uint32_t* GetIntrinsicOptimizations() const {
4030 return &intrinsic_optimizations_;
4031 }
4032
4033 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4034
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004035 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004036 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004037
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004038 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004039
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004040 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004041 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4042 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004043 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4044 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004045 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004046 static constexpr size_t kFieldReturnTypeSize =
4047 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4048 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4049 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4050 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4053
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004054 HInvoke(ArenaAllocator* arena,
4055 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004056 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004057 Primitive::Type return_type,
4058 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004059 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004060 ArtMethod* resolved_method,
4061 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004062 : HVariableInputSizeInstruction(
4063 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4064 dex_pc,
4065 arena,
4066 number_of_arguments + number_of_other_inputs,
4067 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004068 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004069 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004070 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004071 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004072 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004073 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004074 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004075 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004076 }
4077
Roland Levillain3e3d7332015-04-28 11:00:54 +01004078 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004079 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004080 const uint32_t dex_method_index_;
4081 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004082
4083 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4084 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004085
4086 private:
4087 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4088};
4089
Vladimir Markofcb503c2016-05-18 12:48:17 +01004090class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004091 public:
4092 HInvokeUnresolved(ArenaAllocator* arena,
4093 uint32_t number_of_arguments,
4094 Primitive::Type return_type,
4095 uint32_t dex_pc,
4096 uint32_t dex_method_index,
4097 InvokeType invoke_type)
4098 : HInvoke(arena,
4099 number_of_arguments,
4100 0u /* number_of_other_inputs */,
4101 return_type,
4102 dex_pc,
4103 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004104 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004105 invoke_type) {
4106 }
4107
4108 DECLARE_INSTRUCTION(InvokeUnresolved);
4109
4110 private:
4111 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4112};
4113
Orion Hodsonac141392017-01-13 11:53:47 +00004114class HInvokePolymorphic FINAL : public HInvoke {
4115 public:
4116 HInvokePolymorphic(ArenaAllocator* arena,
4117 uint32_t number_of_arguments,
4118 Primitive::Type return_type,
4119 uint32_t dex_pc,
4120 uint32_t dex_method_index)
4121 : HInvoke(arena,
4122 number_of_arguments,
4123 0u /* number_of_other_inputs */,
4124 return_type,
4125 dex_pc,
4126 dex_method_index,
4127 nullptr,
4128 kVirtual) {}
4129
4130 DECLARE_INSTRUCTION(InvokePolymorphic);
4131
4132 private:
4133 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4134};
4135
Vladimir Markofcb503c2016-05-18 12:48:17 +01004136class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004137 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004138 // Requirements of this method call regarding the class
4139 // initialization (clinit) check of its declaring class.
4140 enum class ClinitCheckRequirement {
4141 kNone, // Class already initialized.
4142 kExplicit, // Static call having explicit clinit check as last input.
4143 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004144 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004145 };
4146
Vladimir Marko58155012015-08-19 12:49:41 +00004147 // Determines how to load the target ArtMethod*.
4148 enum class MethodLoadKind {
4149 // Use a String init ArtMethod* loaded from Thread entrypoints.
4150 kStringInit,
4151
4152 // Use the method's own ArtMethod* loaded by the register allocator.
4153 kRecursive,
4154
4155 // Use ArtMethod* at a known address, embed the direct address in the code.
4156 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4157 kDirectAddress,
4158
Vladimir Markoa1de9182016-02-25 11:37:38 +00004159 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004160 // Used when we need to use the dex cache, for example for invoke-static that
4161 // may cause class initialization (the entry may point to a resolution method),
4162 // and we know that we can access the dex cache arrays using a PC-relative load.
4163 kDexCachePcRelative,
4164
4165 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4166 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4167 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4168 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4169 kDexCacheViaMethod,
4170 };
4171
4172 // Determines the location of the code pointer.
4173 enum class CodePtrLocation {
4174 // Recursive call, use local PC-relative call instruction.
4175 kCallSelf,
4176
Vladimir Marko58155012015-08-19 12:49:41 +00004177 // Use code pointer from the ArtMethod*.
4178 // Used when we don't know the target code. This is also the last-resort-kind used when
4179 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4180 kCallArtMethod,
4181 };
4182
4183 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004184 MethodLoadKind method_load_kind;
4185 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004186 // The method load data holds
4187 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4188 // Note that there are multiple string init methods, each having its own offset.
4189 // - the method address for kDirectAddress
4190 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004191 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004192 };
4193
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004194 HInvokeStaticOrDirect(ArenaAllocator* arena,
4195 uint32_t number_of_arguments,
4196 Primitive::Type return_type,
4197 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004198 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004199 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004200 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004201 InvokeType invoke_type,
4202 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004203 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004204 : HInvoke(arena,
4205 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004206 // There is potentially one extra argument for the HCurrentMethod node, and
4207 // potentially one other if the clinit check is explicit, and potentially
4208 // one other if the method is a string factory.
4209 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004210 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004211 return_type,
4212 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004213 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004214 resolved_method,
4215 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004216 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004217 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004218 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4219 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004220
Vladimir Markodc151b22015-10-15 18:02:30 +01004221 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004222 bool had_current_method_input = HasCurrentMethodInput();
4223 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4224
4225 // Using the current method is the default and once we find a better
4226 // method load kind, we should not go back to using the current method.
4227 DCHECK(had_current_method_input || !needs_current_method_input);
4228
4229 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004230 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4231 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004232 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004233 dispatch_info_ = dispatch_info;
4234 }
4235
Aart Bik6daebeb2017-04-03 14:35:41 -07004236 DispatchInfo GetDispatchInfo() const {
4237 return dispatch_info_;
4238 }
4239
Vladimir Markoc53c0792015-11-19 15:48:33 +00004240 void AddSpecialInput(HInstruction* input) {
4241 // We allow only one special input.
4242 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4243 DCHECK(InputCount() == GetSpecialInputIndex() ||
4244 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4245 InsertInputAt(GetSpecialInputIndex(), input);
4246 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004247
Vladimir Marko372f10e2016-05-17 16:30:10 +01004248 using HInstruction::GetInputRecords; // Keep the const version visible.
4249 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4250 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4251 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4252 DCHECK(!input_records.empty());
4253 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4254 HInstruction* last_input = input_records.back().GetInstruction();
4255 // Note: `last_input` may be null during arguments setup.
4256 if (last_input != nullptr) {
4257 // `last_input` is the last input of a static invoke marked as having
4258 // an explicit clinit check. It must either be:
4259 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4260 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4261 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4262 }
4263 }
4264 return input_records;
4265 }
4266
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004267 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004268 // We access the method via the dex cache so we can't do an implicit null check.
4269 // TODO: for intrinsics we can generate implicit null checks.
4270 return false;
4271 }
4272
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004273 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004274 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004275 }
4276
Vladimir Markoc53c0792015-11-19 15:48:33 +00004277 // Get the index of the special input, if any.
4278 //
David Brazdil6de19382016-01-08 17:37:10 +00004279 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4280 // method pointer; otherwise there may be one platform-specific special input,
4281 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004282 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004283 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004284
Vladimir Marko58155012015-08-19 12:49:41 +00004285 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4286 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4287 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004288 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004289 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004290 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004291 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004292 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4293 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004294 bool HasCurrentMethodInput() const {
4295 // This function can be called only after the invoke has been fully initialized by the builder.
4296 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004297 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004298 return true;
4299 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004300 DCHECK(InputCount() == GetSpecialInputIndex() ||
4301 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004302 return false;
4303 }
4304 }
Vladimir Marko58155012015-08-19 12:49:41 +00004305
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004306 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004307 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004308 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004309 }
4310
4311 uint64_t GetMethodAddress() const {
4312 DCHECK(HasMethodAddress());
4313 return dispatch_info_.method_load_data;
4314 }
4315
4316 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004317 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004318 return dispatch_info_.method_load_data;
4319 }
4320
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004321 const DexFile& GetDexFileForPcRelativeDexCache() const;
4322
Vladimir Markoa1de9182016-02-25 11:37:38 +00004323 ClinitCheckRequirement GetClinitCheckRequirement() const {
4324 return GetPackedField<ClinitCheckRequirementField>();
4325 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004326
Roland Levillain4c0eb422015-04-24 16:43:49 +01004327 // Is this instruction a call to a static method?
4328 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004329 return GetInvokeType() == kStatic;
4330 }
4331
4332 MethodReference GetTargetMethod() const {
4333 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004334 }
4335
Vladimir Markofbb184a2015-11-13 14:47:00 +00004336 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4337 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4338 // instruction; only relevant for static calls with explicit clinit check.
4339 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004340 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004341 size_t last_input_index = inputs_.size() - 1u;
4342 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004343 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004344 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004346 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004347 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004348 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004349 }
4350
4351 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004352 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004353 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004354 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355 }
4356
4357 // Is this a call to a static method whose declaring class has an
4358 // implicit intialization check requirement?
4359 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004360 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004361 }
4362
Vladimir Markob554b5a2015-11-06 12:57:55 +00004363 // Does this method load kind need the current method as an input?
4364 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4365 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4366 }
4367
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004368 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004369
4370 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004371 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004372 static constexpr size_t kFieldClinitCheckRequirementSize =
4373 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4374 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4375 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4376 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4377 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004378 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4379 kFieldClinitCheckRequirement,
4380 kFieldClinitCheckRequirementSize>;
4381
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004382 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004383 MethodReference target_method_;
4384 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004386 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004387};
Vladimir Markof64242a2015-12-01 14:58:23 +00004388std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004389std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004390
Vladimir Markofcb503c2016-05-18 12:48:17 +01004391class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004392 public:
4393 HInvokeVirtual(ArenaAllocator* arena,
4394 uint32_t number_of_arguments,
4395 Primitive::Type return_type,
4396 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004397 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004398 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004399 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004400 : HInvoke(arena,
4401 number_of_arguments,
4402 0u,
4403 return_type,
4404 dex_pc,
4405 dex_method_index,
4406 resolved_method,
4407 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004408 vtable_index_(vtable_index) {}
4409
Aart Bik71bf7b42016-11-16 10:17:46 -08004410 bool CanBeNull() const OVERRIDE {
4411 switch (GetIntrinsic()) {
4412 case Intrinsics::kThreadCurrentThread:
4413 case Intrinsics::kStringBufferAppend:
4414 case Intrinsics::kStringBufferToString:
4415 case Intrinsics::kStringBuilderAppend:
4416 case Intrinsics::kStringBuilderToString:
4417 return false;
4418 default:
4419 return HInvoke::CanBeNull();
4420 }
4421 }
4422
Calin Juravle641547a2015-04-21 22:08:51 +01004423 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004424 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004425 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004426 }
4427
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004428 uint32_t GetVTableIndex() const { return vtable_index_; }
4429
4430 DECLARE_INSTRUCTION(InvokeVirtual);
4431
4432 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004433 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004434 const uint32_t vtable_index_;
4435
4436 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4437};
4438
Vladimir Markofcb503c2016-05-18 12:48:17 +01004439class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004440 public:
4441 HInvokeInterface(ArenaAllocator* arena,
4442 uint32_t number_of_arguments,
4443 Primitive::Type return_type,
4444 uint32_t dex_pc,
4445 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004446 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004447 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004448 : HInvoke(arena,
4449 number_of_arguments,
4450 0u,
4451 return_type,
4452 dex_pc,
4453 dex_method_index,
4454 resolved_method,
4455 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004456 imt_index_(imt_index) {}
4457
Calin Juravle641547a2015-04-21 22:08:51 +01004458 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004459 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004460 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004461 }
4462
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004463 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4464 // The assembly stub currently needs it.
4465 return true;
4466 }
4467
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004468 uint32_t GetImtIndex() const { return imt_index_; }
4469 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4470
4471 DECLARE_INSTRUCTION(InvokeInterface);
4472
4473 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004474 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004475 const uint32_t imt_index_;
4476
4477 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4478};
4479
Vladimir Markofcb503c2016-05-18 12:48:17 +01004480class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004481 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004483 : HUnaryOperation(result_type, input, dex_pc) {
4484 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4485 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004486
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004487 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004488
4489 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004490 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004491 }
4492 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004494 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004495 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4496 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4497 }
4498 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4499 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4500 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004501
Roland Levillain88cb1752014-10-20 16:36:47 +01004502 DECLARE_INSTRUCTION(Neg);
4503
4504 private:
4505 DISALLOW_COPY_AND_ASSIGN(HNeg);
4506};
4507
Vladimir Markofcb503c2016-05-18 12:48:17 +01004508class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004509 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004510 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4511 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4512 SetRawInputAt(0, cls);
4513 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004514 }
4515
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004516 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004517 bool NeedsEnvironment() const OVERRIDE { return true; }
4518
Mingyao Yang0c365e62015-03-31 15:09:29 -07004519 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4520 bool CanThrow() const OVERRIDE { return true; }
4521
Calin Juravle10e244f2015-01-26 18:54:32 +00004522 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004523
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004524 HLoadClass* GetLoadClass() const {
4525 DCHECK(InputAt(0)->IsLoadClass());
4526 return InputAt(0)->AsLoadClass();
4527 }
4528
4529 HInstruction* GetLength() const {
4530 return InputAt(1);
4531 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004532
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004533 DECLARE_INSTRUCTION(NewArray);
4534
4535 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004536 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4537};
4538
Vladimir Markofcb503c2016-05-18 12:48:17 +01004539class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 HAdd(Primitive::Type result_type,
4542 HInstruction* left,
4543 HInstruction* right,
4544 uint32_t dex_pc = kNoDexPc)
4545 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004546
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004547 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004548
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004549 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004550
4551 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004552 return GetBlock()->GetGraph()->GetIntConstant(
4553 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004554 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004556 return GetBlock()->GetGraph()->GetLongConstant(
4557 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004558 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4560 return GetBlock()->GetGraph()->GetFloatConstant(
4561 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4562 }
4563 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4564 return GetBlock()->GetGraph()->GetDoubleConstant(
4565 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4566 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004567
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004568 DECLARE_INSTRUCTION(Add);
4569
4570 private:
4571 DISALLOW_COPY_AND_ASSIGN(HAdd);
4572};
4573
Vladimir Markofcb503c2016-05-18 12:48:17 +01004574class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004575 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 HSub(Primitive::Type result_type,
4577 HInstruction* left,
4578 HInstruction* right,
4579 uint32_t dex_pc = kNoDexPc)
4580 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004581
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004582 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004583
4584 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004585 return GetBlock()->GetGraph()->GetIntConstant(
4586 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004587 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004589 return GetBlock()->GetGraph()->GetLongConstant(
4590 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004591 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004592 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4593 return GetBlock()->GetGraph()->GetFloatConstant(
4594 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4595 }
4596 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4597 return GetBlock()->GetGraph()->GetDoubleConstant(
4598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4599 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004600
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004601 DECLARE_INSTRUCTION(Sub);
4602
4603 private:
4604 DISALLOW_COPY_AND_ASSIGN(HSub);
4605};
4606
Vladimir Markofcb503c2016-05-18 12:48:17 +01004607class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 HMul(Primitive::Type result_type,
4610 HInstruction* left,
4611 HInstruction* right,
4612 uint32_t dex_pc = kNoDexPc)
4613 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004614
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004615 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004616
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004617 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004618
4619 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004620 return GetBlock()->GetGraph()->GetIntConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004622 }
4623 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004624 return GetBlock()->GetGraph()->GetLongConstant(
4625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004626 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004627 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4628 return GetBlock()->GetGraph()->GetFloatConstant(
4629 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4630 }
4631 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4632 return GetBlock()->GetGraph()->GetDoubleConstant(
4633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4634 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004635
4636 DECLARE_INSTRUCTION(Mul);
4637
4638 private:
4639 DISALLOW_COPY_AND_ASSIGN(HMul);
4640};
4641
Vladimir Markofcb503c2016-05-18 12:48:17 +01004642class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004644 HDiv(Primitive::Type result_type,
4645 HInstruction* left,
4646 HInstruction* right,
4647 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004648 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004649
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004651 T ComputeIntegral(T x, T y) const {
4652 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 // Our graph structure ensures we never have 0 for `y` during
4654 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004655 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004656 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004657 return (y == -1) ? -x : x / y;
4658 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004659
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 template <typename T>
4661 T ComputeFP(T x, T y) const {
4662 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4663 return x / y;
4664 }
4665
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004667 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004668 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
4670 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004671 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4673 }
4674 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4675 return GetBlock()->GetGraph()->GetFloatConstant(
4676 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4677 }
4678 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4679 return GetBlock()->GetGraph()->GetDoubleConstant(
4680 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004681 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004682
4683 DECLARE_INSTRUCTION(Div);
4684
4685 private:
4686 DISALLOW_COPY_AND_ASSIGN(HDiv);
4687};
4688
Vladimir Markofcb503c2016-05-18 12:48:17 +01004689class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004690 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004691 HRem(Primitive::Type result_type,
4692 HInstruction* left,
4693 HInstruction* right,
4694 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004695 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004696
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004698 T ComputeIntegral(T x, T y) const {
4699 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 // Our graph structure ensures we never have 0 for `y` during
4701 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004702 DCHECK_NE(y, 0);
4703 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4704 return (y == -1) ? 0 : x % y;
4705 }
4706
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 template <typename T>
4708 T ComputeFP(T x, T y) const {
4709 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4710 return std::fmod(x, y);
4711 }
4712
Roland Levillain9867bc72015-08-05 10:21:34 +01004713 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004714 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004715 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004716 }
4717 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004718 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004719 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004720 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004721 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4722 return GetBlock()->GetGraph()->GetFloatConstant(
4723 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4724 }
4725 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4726 return GetBlock()->GetGraph()->GetDoubleConstant(
4727 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4728 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004729
4730 DECLARE_INSTRUCTION(Rem);
4731
4732 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004733 DISALLOW_COPY_AND_ASSIGN(HRem);
4734};
4735
Vladimir Markofcb503c2016-05-18 12:48:17 +01004736class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004737 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004738 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4739 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004740 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004741 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004742 SetRawInputAt(0, value);
4743 }
4744
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004745 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4746
Calin Juravled0d48522014-11-04 16:40:20 +00004747 bool CanBeMoved() const OVERRIDE { return true; }
4748
Vladimir Marko372f10e2016-05-17 16:30:10 +01004749 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004750 return true;
4751 }
4752
4753 bool NeedsEnvironment() const OVERRIDE { return true; }
4754 bool CanThrow() const OVERRIDE { return true; }
4755
Calin Juravled0d48522014-11-04 16:40:20 +00004756 DECLARE_INSTRUCTION(DivZeroCheck);
4757
4758 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004759 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4760};
4761
Vladimir Markofcb503c2016-05-18 12:48:17 +01004762class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004763 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004764 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 HInstruction* value,
4766 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004767 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4769 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4770 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 }
4772
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004774 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 return value << (distance & max_shift_distance);
4776 }
4777
4778 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004779 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004781 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004785 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4787 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4788 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4789 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004790 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004791 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4792 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004793 LOG(FATAL) << DebugName() << " is not defined for float values";
4794 UNREACHABLE();
4795 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004796 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4797 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004798 LOG(FATAL) << DebugName() << " is not defined for double values";
4799 UNREACHABLE();
4800 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004801
4802 DECLARE_INSTRUCTION(Shl);
4803
4804 private:
4805 DISALLOW_COPY_AND_ASSIGN(HShl);
4806};
4807
Vladimir Markofcb503c2016-05-18 12:48:17 +01004808class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004809 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004810 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004811 HInstruction* value,
4812 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004814 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4815 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4816 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 }
4818
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004820 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 return value >> (distance & max_shift_distance);
4822 }
4823
4824 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004825 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004828 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004832 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4833 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4834 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4835 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004837 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4838 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004839 LOG(FATAL) << DebugName() << " is not defined for float values";
4840 UNREACHABLE();
4841 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4843 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004844 LOG(FATAL) << DebugName() << " is not defined for double values";
4845 UNREACHABLE();
4846 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004847
4848 DECLARE_INSTRUCTION(Shr);
4849
4850 private:
4851 DISALLOW_COPY_AND_ASSIGN(HShr);
4852};
4853
Vladimir Markofcb503c2016-05-18 12:48:17 +01004854class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004855 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004856 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 HInstruction* value,
4858 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004859 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004860 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4861 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4862 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004863 }
4864
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004866 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 typedef typename std::make_unsigned<T>::type V;
4868 V ux = static_cast<V>(value);
4869 return static_cast<T>(ux >> (distance & max_shift_distance));
4870 }
4871
4872 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004873 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004874 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004875 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4881 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4882 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4883 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004884 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4886 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004887 LOG(FATAL) << DebugName() << " is not defined for float values";
4888 UNREACHABLE();
4889 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004890 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4891 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004892 LOG(FATAL) << DebugName() << " is not defined for double values";
4893 UNREACHABLE();
4894 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004895
4896 DECLARE_INSTRUCTION(UShr);
4897
4898 private:
4899 DISALLOW_COPY_AND_ASSIGN(HUShr);
4900};
4901
Vladimir Markofcb503c2016-05-18 12:48:17 +01004902class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004903 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 HAnd(Primitive::Type result_type,
4905 HInstruction* left,
4906 HInstruction* right,
4907 uint32_t dex_pc = kNoDexPc)
4908 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004909
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004910 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004911
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004912 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004913
4914 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004915 return GetBlock()->GetGraph()->GetIntConstant(
4916 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004917 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004918 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004919 return GetBlock()->GetGraph()->GetLongConstant(
4920 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004921 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004922 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4923 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4924 LOG(FATAL) << DebugName() << " is not defined for float values";
4925 UNREACHABLE();
4926 }
4927 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4928 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4929 LOG(FATAL) << DebugName() << " is not defined for double values";
4930 UNREACHABLE();
4931 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004932
4933 DECLARE_INSTRUCTION(And);
4934
4935 private:
4936 DISALLOW_COPY_AND_ASSIGN(HAnd);
4937};
4938
Vladimir Markofcb503c2016-05-18 12:48:17 +01004939class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004940 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 HOr(Primitive::Type result_type,
4942 HInstruction* left,
4943 HInstruction* right,
4944 uint32_t dex_pc = kNoDexPc)
4945 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004946
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004947 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004948
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004949 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004950
4951 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004952 return GetBlock()->GetGraph()->GetIntConstant(
4953 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004954 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004955 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004956 return GetBlock()->GetGraph()->GetLongConstant(
4957 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004958 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004959 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4960 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4961 LOG(FATAL) << DebugName() << " is not defined for float values";
4962 UNREACHABLE();
4963 }
4964 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4965 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4966 LOG(FATAL) << DebugName() << " is not defined for double values";
4967 UNREACHABLE();
4968 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969
4970 DECLARE_INSTRUCTION(Or);
4971
4972 private:
4973 DISALLOW_COPY_AND_ASSIGN(HOr);
4974};
4975
Vladimir Markofcb503c2016-05-18 12:48:17 +01004976class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004978 HXor(Primitive::Type result_type,
4979 HInstruction* left,
4980 HInstruction* right,
4981 uint32_t dex_pc = kNoDexPc)
4982 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004983
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004984 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004985
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004986 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004987
4988 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004989 return GetBlock()->GetGraph()->GetIntConstant(
4990 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004991 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004993 return GetBlock()->GetGraph()->GetLongConstant(
4994 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004996 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4997 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4998 LOG(FATAL) << DebugName() << " is not defined for float values";
4999 UNREACHABLE();
5000 }
5001 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5002 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5003 LOG(FATAL) << DebugName() << " is not defined for double values";
5004 UNREACHABLE();
5005 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005006
5007 DECLARE_INSTRUCTION(Xor);
5008
5009 private:
5010 DISALLOW_COPY_AND_ASSIGN(HXor);
5011};
5012
Vladimir Markofcb503c2016-05-18 12:48:17 +01005013class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005014 public:
5015 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005016 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005017 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5018 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005019 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005020
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005022 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 typedef typename std::make_unsigned<T>::type V;
5024 V ux = static_cast<V>(value);
5025 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026 return static_cast<T>(ux);
5027 } else {
5028 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 return static_cast<T>(ux >> (distance & max_shift_value)) |
5030 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005031 }
5032 }
5033
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005035 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005036 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005037 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005038 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005039 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005040 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005041 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005042 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5043 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5044 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5045 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005046 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005047 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5048 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005049 LOG(FATAL) << DebugName() << " is not defined for float values";
5050 UNREACHABLE();
5051 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005052 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5053 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005054 LOG(FATAL) << DebugName() << " is not defined for double values";
5055 UNREACHABLE();
5056 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005057
5058 DECLARE_INSTRUCTION(Ror);
5059
5060 private:
5061 DISALLOW_COPY_AND_ASSIGN(HRor);
5062};
5063
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005064// The value of a parameter in this method. Its location depends on
5065// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005066class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005067 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005068 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005069 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005070 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005071 Primitive::Type parameter_type,
5072 bool is_this = false)
5073 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005074 dex_file_(dex_file),
5075 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005076 index_(index) {
5077 SetPackedFlag<kFlagIsThis>(is_this);
5078 SetPackedFlag<kFlagCanBeNull>(!is_this);
5079 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005080
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005081 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005082 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005083 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005084 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005085
Vladimir Markoa1de9182016-02-25 11:37:38 +00005086 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5087 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005088
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005089 DECLARE_INSTRUCTION(ParameterValue);
5090
5091 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005092 // Whether or not the parameter value corresponds to 'this' argument.
5093 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5094 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5095 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5096 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5097 "Too many packed fields.");
5098
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005099 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005100 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005101 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005102 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005103 const uint8_t index_;
5104
5105 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5106};
5107
Vladimir Markofcb503c2016-05-18 12:48:17 +01005108class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5111 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005112
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005113 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005114 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005115 return true;
5116 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005117
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005118 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005119
5120 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005121 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005122 }
5123 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005124 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005125 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005126 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5127 LOG(FATAL) << DebugName() << " is not defined for float values";
5128 UNREACHABLE();
5129 }
5130 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5131 LOG(FATAL) << DebugName() << " is not defined for double values";
5132 UNREACHABLE();
5133 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005134
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005135 DECLARE_INSTRUCTION(Not);
5136
5137 private:
5138 DISALLOW_COPY_AND_ASSIGN(HNot);
5139};
5140
Vladimir Markofcb503c2016-05-18 12:48:17 +01005141class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005142 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005143 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5144 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005145
5146 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005147 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005148 return true;
5149 }
5150
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005151 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005152 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005153 return !x;
5154 }
5155
Roland Levillain9867bc72015-08-05 10:21:34 +01005156 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005157 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005158 }
5159 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5160 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005161 UNREACHABLE();
5162 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005163 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5164 LOG(FATAL) << DebugName() << " is not defined for float values";
5165 UNREACHABLE();
5166 }
5167 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5168 LOG(FATAL) << DebugName() << " is not defined for double values";
5169 UNREACHABLE();
5170 }
David Brazdil66d126e2015-04-03 16:02:44 +01005171
5172 DECLARE_INSTRUCTION(BooleanNot);
5173
5174 private:
5175 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5176};
5177
Vladimir Markofcb503c2016-05-18 12:48:17 +01005178class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005179 public:
5180 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005181 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005182 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005183 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005184 // Invariant: We should never generate a conversion to a Boolean value.
5185 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005186 }
5187
5188 HInstruction* GetInput() const { return InputAt(0); }
5189 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5190 Primitive::Type GetResultType() const { return GetType(); }
5191
5192 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005193 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5194 return true;
5195 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005196
Mark Mendelle82549b2015-05-06 10:55:34 -04005197 // Try to statically evaluate the conversion and return a HConstant
5198 // containing the result. If the input cannot be converted, return nullptr.
5199 HConstant* TryStaticEvaluation() const;
5200
Roland Levillaindff1f282014-11-05 14:15:05 +00005201 DECLARE_INSTRUCTION(TypeConversion);
5202
5203 private:
5204 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5205};
5206
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005207static constexpr uint32_t kNoRegNumber = -1;
5208
Vladimir Markofcb503c2016-05-18 12:48:17 +01005209class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005210 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005211 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5212 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005213 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005214 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 SetRawInputAt(0, value);
5216 }
5217
Calin Juravle10e244f2015-01-26 18:54:32 +00005218 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005219 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005220 return true;
5221 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005222
Calin Juravle10e244f2015-01-26 18:54:32 +00005223 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005224
Calin Juravle10e244f2015-01-26 18:54:32 +00005225 bool CanThrow() const OVERRIDE { return true; }
5226
5227 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005228
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005229
5230 DECLARE_INSTRUCTION(NullCheck);
5231
5232 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005233 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5234};
5235
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005236// Embeds an ArtField and all the information required by the compiler. We cache
5237// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238class FieldInfo : public ValueObject {
5239 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005240 FieldInfo(ArtField* field,
5241 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005242 Primitive::Type field_type,
5243 bool is_volatile,
5244 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005245 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005246 const DexFile& dex_file)
5247 : field_(field),
5248 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005249 field_type_(field_type),
5250 is_volatile_(is_volatile),
5251 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005252 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005253 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005254
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005255 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005256 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005257 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005258 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005259 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005260 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005261 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005262
5263 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005264 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005265 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005266 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005267 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005268 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005269 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005270 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005271};
5272
Vladimir Markofcb503c2016-05-18 12:48:17 +01005273class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005274 public:
5275 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005276 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005277 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005278 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005279 bool is_volatile,
5280 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005281 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005282 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005283 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005284 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005285 field_info_(field,
5286 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005287 field_type,
5288 is_volatile,
5289 field_idx,
5290 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005291 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005292 SetRawInputAt(0, value);
5293 }
5294
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005295 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005296
Vladimir Marko372f10e2016-05-17 16:30:10 +01005297 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5298 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005299 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005300 }
5301
Calin Juravle641547a2015-04-21 22:08:51 +01005302 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005303 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 }
5305
5306 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005307 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5308 }
5309
Calin Juravle52c48962014-12-16 17:02:57 +00005310 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005311 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005312 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005313 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005314
5315 DECLARE_INSTRUCTION(InstanceFieldGet);
5316
5317 private:
5318 const FieldInfo field_info_;
5319
5320 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5321};
5322
Vladimir Markofcb503c2016-05-18 12:48:17 +01005323class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005324 public:
5325 HInstanceFieldSet(HInstruction* object,
5326 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005327 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005328 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005329 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005330 bool is_volatile,
5331 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005332 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005333 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005334 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005335 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005336 field_info_(field,
5337 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005338 field_type,
5339 is_volatile,
5340 field_idx,
5341 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005342 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005343 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005344 SetRawInputAt(0, object);
5345 SetRawInputAt(1, value);
5346 }
5347
Calin Juravle641547a2015-04-21 22:08:51 +01005348 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005349 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005350 }
5351
Calin Juravle52c48962014-12-16 17:02:57 +00005352 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005353 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005354 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005355 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005356 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005357 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5358 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005360 DECLARE_INSTRUCTION(InstanceFieldSet);
5361
5362 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005363 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5364 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5365 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5366 "Too many packed fields.");
5367
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005368 const FieldInfo field_info_;
5369
5370 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5371};
5372
Vladimir Markofcb503c2016-05-18 12:48:17 +01005373class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005375 HArrayGet(HInstruction* array,
5376 HInstruction* index,
5377 Primitive::Type type,
5378 uint32_t dex_pc,
5379 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005380 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005381 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005382 SetRawInputAt(0, array);
5383 SetRawInputAt(1, index);
5384 }
5385
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005386 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005387 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005388 return true;
5389 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005390 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005392 // Currently, unless the array is the result of NewArray, the array access is always
5393 // preceded by some form of null NullCheck necessary for the bounds check, usually
5394 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5395 // dynamic BCE. There are cases when these could be removed to produce better code.
5396 // If we ever add optimizations to do so we should allow an implicit check here
5397 // (as long as the address falls in the first page).
5398 //
5399 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5400 // a = cond ? new int[1] : null;
5401 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005402 return false;
5403 }
5404
David Brazdil4833f5a2015-12-16 10:37:39 +00005405 bool IsEquivalentOf(HArrayGet* other) const {
5406 bool result = (GetDexPc() == other->GetDexPc());
5407 if (kIsDebugBuild && result) {
5408 DCHECK_EQ(GetBlock(), other->GetBlock());
5409 DCHECK_EQ(GetArray(), other->GetArray());
5410 DCHECK_EQ(GetIndex(), other->GetIndex());
5411 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005412 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005413 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5415 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005416 }
5417 }
5418 return result;
5419 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005420
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005421 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5422
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005423 HInstruction* GetArray() const { return InputAt(0); }
5424 HInstruction* GetIndex() const { return InputAt(1); }
5425
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 DECLARE_INSTRUCTION(ArrayGet);
5427
5428 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005429 // We treat a String as an array, creating the HArrayGet from String.charAt()
5430 // intrinsic in the instruction simplifier. We can always determine whether
5431 // a particular HArrayGet is actually a String.charAt() by looking at the type
5432 // of the input but that requires holding the mutator lock, so we prefer to use
5433 // a flag, so that code generators don't need to do the locking.
5434 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5435 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5436 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5437 "Too many packed fields.");
5438
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005439 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5440};
5441
Vladimir Markofcb503c2016-05-18 12:48:17 +01005442class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 public:
5444 HArraySet(HInstruction* array,
5445 HInstruction* index,
5446 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005447 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005448 uint32_t dex_pc)
5449 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005450 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5451 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5452 SetPackedFlag<kFlagValueCanBeNull>(true);
5453 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005454 SetRawInputAt(0, array);
5455 SetRawInputAt(1, index);
5456 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005457 // Make a best guess now, may be refined during SSA building.
5458 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005459 }
5460
Calin Juravle77520bc2015-01-12 18:45:46 +00005461 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005462 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005463 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 }
5465
Mingyao Yang81014cb2015-06-02 03:16:27 -07005466 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005469 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005470 // TODO: Same as for ArrayGet.
5471 return false;
5472 }
5473
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005474 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005476 }
5477
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005478 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005479 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005480 }
5481
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005483 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 }
5485
Vladimir Markoa1de9182016-02-25 11:37:38 +00005486 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5487 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5488 bool StaticTypeOfArrayIsObjectArray() const {
5489 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5490 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005491
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005492 HInstruction* GetArray() const { return InputAt(0); }
5493 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005494 HInstruction* GetValue() const { return InputAt(2); }
5495
5496 Primitive::Type GetComponentType() const {
5497 // The Dex format does not type floating point index operations. Since the
5498 // `expected_component_type_` is set during building and can therefore not
5499 // be correct, we also check what is the value type. If it is a floating
5500 // point type, we must use that type.
5501 Primitive::Type value_type = GetValue()->GetType();
5502 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5503 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005504 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005505 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005506
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005507 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005508 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005509 }
5510
Aart Bik18b36ab2016-04-13 16:41:35 -07005511 void ComputeSideEffects() {
5512 Primitive::Type type = GetComponentType();
5513 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5514 SideEffectsForArchRuntimeCalls(type)));
5515 }
5516
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005517 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5518 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5519 }
5520
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521 DECLARE_INSTRUCTION(ArraySet);
5522
5523 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005524 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5525 static constexpr size_t kFieldExpectedComponentTypeSize =
5526 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5527 static constexpr size_t kFlagNeedsTypeCheck =
5528 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5529 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005530 // Cached information for the reference_type_info_ so that codegen
5531 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5533 static constexpr size_t kNumberOfArraySetPackedBits =
5534 kFlagStaticTypeOfArrayIsObjectArray + 1;
5535 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5536 using ExpectedComponentTypeField =
5537 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538
5539 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5540};
5541
Vladimir Markofcb503c2016-05-18 12:48:17 +01005542class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005544 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005545 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005546 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005547 // Note that arrays do not change length, so the instruction does not
5548 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005549 SetRawInputAt(0, array);
5550 }
5551
Calin Juravle77520bc2015-01-12 18:45:46 +00005552 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005553 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005554 return true;
5555 }
Calin Juravle641547a2015-04-21 22:08:51 +01005556 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5557 return obj == InputAt(0);
5558 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005559
Vladimir Markodce016e2016-04-28 13:10:02 +01005560 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5561
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005562 DECLARE_INSTRUCTION(ArrayLength);
5563
5564 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005565 // We treat a String as an array, creating the HArrayLength from String.length()
5566 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5567 // determine whether a particular HArrayLength is actually a String.length() by
5568 // looking at the type of the input but that requires holding the mutator lock, so
5569 // we prefer to use a flag, so that code generators don't need to do the locking.
5570 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5571 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5572 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5573 "Too many packed fields.");
5574
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5576};
5577
Vladimir Markofcb503c2016-05-18 12:48:17 +01005578class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005579 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005580 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5581 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005582 HBoundsCheck(HInstruction* index,
5583 HInstruction* length,
5584 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005585 bool string_char_at = false)
5586 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005587 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005588 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 SetRawInputAt(0, index);
5590 SetRawInputAt(1, length);
5591 }
5592
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005593 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005594 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005595 return true;
5596 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005598 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005600 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005601
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005602 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005603
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005604 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005605
5606 DECLARE_INSTRUCTION(BoundsCheck);
5607
5608 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005609 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5612};
5613
Vladimir Markofcb503c2016-05-18 12:48:17 +01005614class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005615 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005616 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005617 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005618
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005619 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005620 return true;
5621 }
5622
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005623 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5624 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005625
5626 DECLARE_INSTRUCTION(SuspendCheck);
5627
5628 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005629 // Only used for code generation, in order to share the same slow path between back edges
5630 // of a same loop.
5631 SlowPathCode* slow_path_;
5632
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005633 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5634};
5635
David Srbecky0cf44932015-12-09 14:09:59 +00005636// Pseudo-instruction which provides the native debugger with mapping information.
5637// It ensures that we can generate line number and local variables at this point.
5638class HNativeDebugInfo : public HTemplateInstruction<0> {
5639 public:
5640 explicit HNativeDebugInfo(uint32_t dex_pc)
5641 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5642
5643 bool NeedsEnvironment() const OVERRIDE {
5644 return true;
5645 }
5646
5647 DECLARE_INSTRUCTION(NativeDebugInfo);
5648
5649 private:
5650 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5651};
5652
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005653/**
5654 * Instruction to load a Class object.
5655 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005657 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 // Determines how to load the Class.
5659 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005660 // We cannot load this class. See HSharpening::SharpenLoadClass.
5661 kInvalid = -1,
5662
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005663 // Use the Class* from the method's own ArtMethod*.
5664 kReferrersClass,
5665
5666 // Use boot image Class* address that will be known at link time.
5667 // Used for boot image classes referenced by boot image code in non-PIC mode.
5668 kBootImageLinkTimeAddress,
5669
5670 // Use PC-relative boot image Class* address that will be known at link time.
5671 // Used for boot image classes referenced by boot image code in PIC mode.
5672 kBootImageLinkTimePcRelative,
5673
5674 // Use a known boot image Class* address, embedded in the code by the codegen.
5675 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005676 kBootImageAddress,
5677
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005678 // Load from an entry in the .bss section using a PC-relative load.
5679 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5680 kBssEntry,
5681
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005682 // Load from the root table associated with the JIT compiled method.
5683 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005685 // Load from resolved types array accessed through the class loaded from
5686 // the compiled method's own ArtMethod*. This is the default access type when
5687 // all other types are unavailable.
5688 kDexCacheViaMethod,
5689
5690 kLast = kDexCacheViaMethod
5691 };
5692
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005693 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005694 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005695 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005696 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005697 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005698 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005699 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5701 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005703 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005704 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005705 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005706 // Referrers class should not need access check. We never inline unverified
5707 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005708 DCHECK(!is_referrers_class || !needs_access_check);
5709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005710 SetPackedField<LoadKindField>(
5711 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005713 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005714 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715 }
5716
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005717 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005718
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 LoadKind GetLoadKind() const {
5720 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005721 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005722
5723 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005726
Andreas Gampea5b09a62016-11-17 15:21:22 -08005727 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005729 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005731 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005732 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005733 }
5734
Calin Juravle0ba218d2015-05-19 18:46:01 +01005735 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005736 // The entrypoint the code generator is going to call does not do
5737 // clinit of the class.
5738 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005739 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005740 }
5741
5742 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005743 return NeedsAccessCheck() ||
5744 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005745 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5746 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005747 }
5748
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005749 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005750 return NeedsAccessCheck() ||
5751 MustGenerateClinitCheck() ||
5752 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5753 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5754 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005755 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5756 GetLoadKind() == LoadKind::kBssEntry) &&
5757 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005758 }
5759
Calin Juravleacf735c2015-02-12 15:25:22 +00005760 ReferenceTypeInfo GetLoadedClassRTI() {
5761 return loaded_class_rti_;
5762 }
5763
5764 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5765 // Make sure we only set exact types (the loaded class should never be merged).
5766 DCHECK(rti.IsExact());
5767 loaded_class_rti_ = rti;
5768 }
5769
Andreas Gampea5b09a62016-11-17 15:21:22 -08005770 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 const DexFile& GetDexFile() const { return dex_file_; }
5772
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005773 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005774 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005775 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005777 static SideEffects SideEffectsForArchRuntimeCalls() {
5778 return SideEffects::CanTriggerGC();
5779 }
5780
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005781 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005782 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005783 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005784 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005785
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005786 void MarkInBootImage() {
5787 SetPackedFlag<kFlagIsInBootImage>(true);
5788 }
5789
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 void AddSpecialInput(HInstruction* special_input);
5791
5792 using HInstruction::GetInputRecords; // Keep the const version visible.
5793 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5794 return ArrayRef<HUserRecord<HInstruction*>>(
5795 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5796 }
5797
5798 Primitive::Type GetType() const OVERRIDE {
5799 return Primitive::kPrimNot;
5800 }
5801
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005802 Handle<mirror::Class> GetClass() const {
5803 return klass_;
5804 }
5805
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005806 DECLARE_INSTRUCTION(LoadClass);
5807
5808 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005809 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005810 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005811 // Whether this instruction must generate the initialization check.
5812 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005813 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005814 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5815 static constexpr size_t kFieldLoadKindSize =
5816 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5817 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005818 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005819 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5820
5821 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005822 return load_kind == LoadKind::kReferrersClass ||
5823 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005824 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005825 load_kind == LoadKind::kBssEntry ||
5826 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 }
5828
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005829 void SetLoadKindInternal(LoadKind load_kind);
5830
5831 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5832 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005833 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005835
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005836 // A type index and dex file where the class can be accessed. The dex file can be:
5837 // - The compiling method's dex file if the class is defined there too.
5838 // - The compiling method's dex file if the class is referenced there.
5839 // - The dex file where the class is defined. When the load kind can only be
5840 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005841 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005842 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005843
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005844 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845
Calin Juravleacf735c2015-02-12 15:25:22 +00005846 ReferenceTypeInfo loaded_class_rti_;
5847
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005848 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5849};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005850std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5851
5852// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005853inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005854 // The special input is used for PC-relative loads on some architectures,
5855 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005856 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005857 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005858 GetLoadKind() == LoadKind::kBootImageAddress ||
5859 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005860 DCHECK(special_input_.GetInstruction() == nullptr);
5861 special_input_ = HUserRecord<HInstruction*>(special_input);
5862 special_input->AddUseAt(this, 0);
5863}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005864
Vladimir Marko372f10e2016-05-17 16:30:10 +01005865class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005866 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867 // Determines how to load the String.
5868 enum class LoadKind {
5869 // Use boot image String* address that will be known at link time.
5870 // Used for boot image strings referenced by boot image code in non-PIC mode.
5871 kBootImageLinkTimeAddress,
5872
5873 // Use PC-relative boot image String* address that will be known at link time.
5874 // Used for boot image strings referenced by boot image code in PIC mode.
5875 kBootImageLinkTimePcRelative,
5876
5877 // Use a known boot image String* address, embedded in the code by the codegen.
5878 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 kBootImageAddress,
5880
Vladimir Markoaad75c62016-10-03 08:46:48 +00005881 // Load from an entry in the .bss section using a PC-relative load.
5882 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5883 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005885 // Load from the root table associated with the JIT compiled method.
5886 kJitTableAddress,
5887
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888 // Load from resolved strings array accessed through the class loaded from
5889 // the compiled method's own ArtMethod*. This is the default access type when
5890 // all other types are unavailable.
5891 kDexCacheViaMethod,
5892
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005893 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 };
5895
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005896 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005897 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898 const DexFile& dex_file,
5899 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005900 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5901 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 string_index_(string_index),
5903 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005905 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005906
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005907 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 LoadKind GetLoadKind() const {
5910 return GetPackedField<LoadKindField>();
5911 }
5912
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005913 const DexFile& GetDexFile() const {
5914 return dex_file_;
5915 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005917 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005918 return string_index_;
5919 }
5920
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005921 Handle<mirror::String> GetString() const {
5922 return string_;
5923 }
5924
5925 void SetString(Handle<mirror::String> str) {
5926 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 }
5928
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005929 bool CanBeMoved() const OVERRIDE { return true; }
5930
Vladimir Marko372f10e2016-05-17 16:30:10 +01005931 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005932
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005933 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005934
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 // Will call the runtime if we need to load the string through
5936 // the dex cache and the string is not guaranteed to be there yet.
5937 bool NeedsEnvironment() const OVERRIDE {
5938 LoadKind load_kind = GetLoadKind();
5939 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5940 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005941 load_kind == LoadKind::kBootImageAddress ||
5942 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 return false;
5944 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005945 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005946 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005947
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005948 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5949 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5950 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005951
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005952 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005953 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005954
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005955 static SideEffects SideEffectsForArchRuntimeCalls() {
5956 return SideEffects::CanTriggerGC();
5957 }
5958
Vladimir Marko372f10e2016-05-17 16:30:10 +01005959 void AddSpecialInput(HInstruction* special_input);
5960
5961 using HInstruction::GetInputRecords; // Keep the const version visible.
5962 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5963 return ArrayRef<HUserRecord<HInstruction*>>(
5964 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005965 }
5966
Vladimir Marko372f10e2016-05-17 16:30:10 +01005967 Primitive::Type GetType() const OVERRIDE {
5968 return Primitive::kPrimNot;
5969 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005971 DECLARE_INSTRUCTION(LoadString);
5972
5973 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005974 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005975 static constexpr size_t kFieldLoadKindSize =
5976 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5977 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005978 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005980
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981 void SetLoadKindInternal(LoadKind load_kind);
5982
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005983 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5984 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005985 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005986 HUserRecord<HInstruction*> special_input_;
5987
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005988 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005989 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005991 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005992
5993 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5994};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5996
5997// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005999 // The special input is used for PC-relative loads on some architectures,
6000 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006002 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07006003 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
6004 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006005 // HLoadString::GetInputRecords() returns an empty array at this point,
6006 // so use the GetInputRecords() from the base class to set the input record.
6007 DCHECK(special_input_.GetInstruction() == nullptr);
6008 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006009 special_input->AddUseAt(this, 0);
6010}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006011
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012/**
6013 * Performs an initialization check on its Class object input.
6014 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006015class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006016 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006017 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006018 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006019 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006020 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6021 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022 SetRawInputAt(0, constant);
6023 }
6024
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006025 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006026 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006027 return true;
6028 }
6029
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 bool NeedsEnvironment() const OVERRIDE {
6031 // May call runtime to initialize the class.
6032 return true;
6033 }
6034
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006035 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006036
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006037 HLoadClass* GetLoadClass() const {
6038 DCHECK(InputAt(0)->IsLoadClass());
6039 return InputAt(0)->AsLoadClass();
6040 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041
6042 DECLARE_INSTRUCTION(ClinitCheck);
6043
6044 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6046};
6047
Vladimir Markofcb503c2016-05-18 12:48:17 +01006048class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006049 public:
6050 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006051 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006053 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006054 bool is_volatile,
6055 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006056 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006057 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006058 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006059 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006060 field_info_(field,
6061 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006062 field_type,
6063 is_volatile,
6064 field_idx,
6065 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006066 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067 SetRawInputAt(0, cls);
6068 }
6069
Calin Juravle52c48962014-12-16 17:02:57 +00006070
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006071 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006072
Vladimir Marko372f10e2016-05-17 16:30:10 +01006073 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6074 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006075 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006076 }
6077
6078 size_t ComputeHashCode() const OVERRIDE {
6079 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6080 }
6081
Calin Juravle52c48962014-12-16 17:02:57 +00006082 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006083 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6084 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006085 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006086
6087 DECLARE_INSTRUCTION(StaticFieldGet);
6088
6089 private:
6090 const FieldInfo field_info_;
6091
6092 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6093};
6094
Vladimir Markofcb503c2016-05-18 12:48:17 +01006095class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006096 public:
6097 HStaticFieldSet(HInstruction* cls,
6098 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006099 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006100 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006101 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006102 bool is_volatile,
6103 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006104 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006105 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006106 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006107 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006108 field_info_(field,
6109 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006110 field_type,
6111 is_volatile,
6112 field_idx,
6113 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006114 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006115 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006116 SetRawInputAt(0, cls);
6117 SetRawInputAt(1, value);
6118 }
6119
Calin Juravle52c48962014-12-16 17:02:57 +00006120 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006121 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6122 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006123 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006124
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006125 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6127 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006128
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006129 DECLARE_INSTRUCTION(StaticFieldSet);
6130
6131 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006132 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6133 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6134 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6135 "Too many packed fields.");
6136
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006137 const FieldInfo field_info_;
6138
6139 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6140};
6141
Vladimir Markofcb503c2016-05-18 12:48:17 +01006142class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006143 public:
6144 HUnresolvedInstanceFieldGet(HInstruction* obj,
6145 Primitive::Type field_type,
6146 uint32_t field_index,
6147 uint32_t dex_pc)
6148 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6149 field_index_(field_index) {
6150 SetRawInputAt(0, obj);
6151 }
6152
6153 bool NeedsEnvironment() const OVERRIDE { return true; }
6154 bool CanThrow() const OVERRIDE { return true; }
6155
6156 Primitive::Type GetFieldType() const { return GetType(); }
6157 uint32_t GetFieldIndex() const { return field_index_; }
6158
6159 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6160
6161 private:
6162 const uint32_t field_index_;
6163
6164 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6165};
6166
Vladimir Markofcb503c2016-05-18 12:48:17 +01006167class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006168 public:
6169 HUnresolvedInstanceFieldSet(HInstruction* obj,
6170 HInstruction* value,
6171 Primitive::Type field_type,
6172 uint32_t field_index,
6173 uint32_t dex_pc)
6174 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006175 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006176 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006177 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006178 SetRawInputAt(0, obj);
6179 SetRawInputAt(1, value);
6180 }
6181
6182 bool NeedsEnvironment() const OVERRIDE { return true; }
6183 bool CanThrow() const OVERRIDE { return true; }
6184
Vladimir Markoa1de9182016-02-25 11:37:38 +00006185 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006186 uint32_t GetFieldIndex() const { return field_index_; }
6187
6188 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6189
6190 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6192 static constexpr size_t kFieldFieldTypeSize =
6193 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6194 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6195 kFieldFieldType + kFieldFieldTypeSize;
6196 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6197 "Too many packed fields.");
6198 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6199
Calin Juravlee460d1d2015-09-29 04:52:17 +01006200 const uint32_t field_index_;
6201
6202 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6203};
6204
Vladimir Markofcb503c2016-05-18 12:48:17 +01006205class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006206 public:
6207 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6208 uint32_t field_index,
6209 uint32_t dex_pc)
6210 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6211 field_index_(field_index) {
6212 }
6213
6214 bool NeedsEnvironment() const OVERRIDE { return true; }
6215 bool CanThrow() const OVERRIDE { return true; }
6216
6217 Primitive::Type GetFieldType() const { return GetType(); }
6218 uint32_t GetFieldIndex() const { return field_index_; }
6219
6220 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6221
6222 private:
6223 const uint32_t field_index_;
6224
6225 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6226};
6227
Vladimir Markofcb503c2016-05-18 12:48:17 +01006228class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006229 public:
6230 HUnresolvedStaticFieldSet(HInstruction* value,
6231 Primitive::Type field_type,
6232 uint32_t field_index,
6233 uint32_t dex_pc)
6234 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006235 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006237 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006238 SetRawInputAt(0, value);
6239 }
6240
6241 bool NeedsEnvironment() const OVERRIDE { return true; }
6242 bool CanThrow() const OVERRIDE { return true; }
6243
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006245 uint32_t GetFieldIndex() const { return field_index_; }
6246
6247 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6248
6249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6251 static constexpr size_t kFieldFieldTypeSize =
6252 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6253 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6254 kFieldFieldType + kFieldFieldTypeSize;
6255 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6256 "Too many packed fields.");
6257 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6258
Calin Juravlee460d1d2015-09-29 04:52:17 +01006259 const uint32_t field_index_;
6260
6261 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6262};
6263
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006264// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006265class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006266 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006267 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6268 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006269
David Brazdilbbd733e2015-08-18 17:48:17 +01006270 bool CanBeNull() const OVERRIDE { return false; }
6271
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006272 DECLARE_INSTRUCTION(LoadException);
6273
6274 private:
6275 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6276};
6277
David Brazdilcb1c0552015-08-04 16:22:25 +01006278// Implicit part of move-exception which clears thread-local exception storage.
6279// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006280class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006281 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006282 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6283 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006284
6285 DECLARE_INSTRUCTION(ClearException);
6286
6287 private:
6288 DISALLOW_COPY_AND_ASSIGN(HClearException);
6289};
6290
Vladimir Markofcb503c2016-05-18 12:48:17 +01006291class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006292 public:
6293 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006294 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006295 SetRawInputAt(0, exception);
6296 }
6297
6298 bool IsControlFlow() const OVERRIDE { return true; }
6299
6300 bool NeedsEnvironment() const OVERRIDE { return true; }
6301
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006302 bool CanThrow() const OVERRIDE { return true; }
6303
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006304
6305 DECLARE_INSTRUCTION(Throw);
6306
6307 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006308 DISALLOW_COPY_AND_ASSIGN(HThrow);
6309};
6310
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006311/**
6312 * Implementation strategies for the code generator of a HInstanceOf
6313 * or `HCheckCast`.
6314 */
6315enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006316 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317 kExactCheck, // Can do a single class compare.
6318 kClassHierarchyCheck, // Can just walk the super class chain.
6319 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6320 kInterfaceCheck, // No optimization yet when checking against an interface.
6321 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006322 kArrayCheck, // No optimization yet when checking against a generic array.
6323 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324};
6325
Roland Levillain86503782016-02-11 19:07:30 +00006326std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6327
Vladimir Markofcb503c2016-05-18 12:48:17 +01006328class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006330 HInstanceOf(HInstruction* object,
6331 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006333 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006334 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 dex_pc) {
6337 SetPackedField<TypeCheckKindField>(check_kind);
6338 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006339 SetRawInputAt(0, object);
6340 SetRawInputAt(1, constant);
6341 }
6342
6343 bool CanBeMoved() const OVERRIDE { return true; }
6344
Vladimir Marko372f10e2016-05-17 16:30:10 +01006345 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006346 return true;
6347 }
6348
6349 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006350 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006351 }
6352
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006353 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6355 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6356 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6357 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006358
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006359 static bool CanCallRuntime(TypeCheckKind check_kind) {
6360 // Mips currently does runtime calls for any other checks.
6361 return check_kind != TypeCheckKind::kExactCheck;
6362 }
6363
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006365 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006366 }
6367
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006369
6370 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006371 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6372 static constexpr size_t kFieldTypeCheckKindSize =
6373 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6374 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6375 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6376 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6377 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006378
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006379 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6380};
6381
Vladimir Markofcb503c2016-05-18 12:48:17 +01006382class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006383 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006384 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006385 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6387 SetPackedFlag<kFlagUpperCanBeNull>(true);
6388 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006389 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006390 SetRawInputAt(0, input);
6391 }
6392
David Brazdilf5552582015-12-27 13:36:12 +00006393 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006394 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006395 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006396 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006397
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006398 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 DCHECK(GetUpperCanBeNull() || !can_be_null);
6400 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006401 }
6402
Vladimir Markoa1de9182016-02-25 11:37:38 +00006403 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006404
Calin Juravleb1498f62015-02-16 13:13:29 +00006405 DECLARE_INSTRUCTION(BoundType);
6406
6407 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6409 // is false then CanBeNull() cannot be true).
6410 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6411 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6412 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6413 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6414
Calin Juravleb1498f62015-02-16 13:13:29 +00006415 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006416 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6417 // It is used to bound the type in cases like:
6418 // if (x instanceof ClassX) {
6419 // // uper_bound_ will be ClassX
6420 // }
David Brazdilf5552582015-12-27 13:36:12 +00006421 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006422
6423 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6424};
6425
Vladimir Markofcb503c2016-05-18 12:48:17 +01006426class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006427 public:
6428 HCheckCast(HInstruction* object,
6429 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006431 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006432 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6433 SetPackedField<TypeCheckKindField>(check_kind);
6434 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006435 SetRawInputAt(0, object);
6436 SetRawInputAt(1, constant);
6437 }
6438
6439 bool CanBeMoved() const OVERRIDE { return true; }
6440
Vladimir Marko372f10e2016-05-17 16:30:10 +01006441 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006442 return true;
6443 }
6444
6445 bool NeedsEnvironment() const OVERRIDE {
6446 // Instruction may throw a CheckCastError.
6447 return true;
6448 }
6449
6450 bool CanThrow() const OVERRIDE { return true; }
6451
Vladimir Markoa1de9182016-02-25 11:37:38 +00006452 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6453 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6454 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6455 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006456
6457 DECLARE_INSTRUCTION(CheckCast);
6458
6459 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006460 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6461 static constexpr size_t kFieldTypeCheckKindSize =
6462 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6463 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6464 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6465 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6466 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006467
6468 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006469};
6470
Andreas Gampe26de38b2016-07-27 17:53:11 -07006471/**
6472 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6473 * @details We define the combined barrier types that are actually required
6474 * by the Java Memory Model, rather than using exactly the terminology from
6475 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6476 * primitives. Note that the JSR-133 cookbook generally does not deal with
6477 * store atomicity issues, and the recipes there are not always entirely sufficient.
6478 * The current recipe is as follows:
6479 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6480 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6481 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6482 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6483 * class has final fields.
6484 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6485 * store-to-memory instructions. Only generated together with non-temporal stores.
6486 */
6487enum MemBarrierKind {
6488 kAnyStore,
6489 kLoadAny,
6490 kStoreStore,
6491 kAnyAny,
6492 kNTStoreStore,
6493 kLastBarrierKind = kNTStoreStore
6494};
6495std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6496
Vladimir Markofcb503c2016-05-18 12:48:17 +01006497class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006499 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006500 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6502 SetPackedField<BarrierKindField>(barrier_kind);
6503 }
Calin Juravle27df7582015-04-17 19:12:31 +01006504
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006506
6507 DECLARE_INSTRUCTION(MemoryBarrier);
6508
6509 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006510 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6511 static constexpr size_t kFieldBarrierKindSize =
6512 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6513 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6514 kFieldBarrierKind + kFieldBarrierKindSize;
6515 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6516 "Too many packed fields.");
6517 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006518
6519 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6520};
6521
Igor Murashkind01745e2017-04-05 16:40:31 -07006522// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6523// the specified object(s), with respect to any subsequent store that might "publish"
6524// (i.e. make visible) the specified object to another thread.
6525//
6526// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6527// for all final fields (that were set) at the end of the invoked constructor.
6528//
6529// The constructor fence models the freeze actions for the final fields of an object
6530// being constructed (semantically at the end of the constructor). Constructor fences
6531// have a per-object affinity; two separate objects being constructed get two separate
6532// constructor fences.
6533//
6534// (Note: that if calling a super-constructor or forwarding to another constructor,
6535// the freezes would happen at the end of *that* constructor being invoked).
6536//
6537// The memory model guarantees that when the object being constructed is "published" after
6538// constructor completion (i.e. escapes the current thread via a store), then any final field
6539// writes must be observable on other threads (once they observe that publication).
6540//
6541// Further, anything written before the freeze, and read by dereferencing through the final field,
6542// must also be visible (so final object field could itself have an object with non-final fields;
6543// yet the freeze must also extend to them).
6544//
6545// Constructor example:
6546//
6547// class HasFinal {
6548// final int field; Optimizing IR for <init>()V:
6549// HasFinal() {
6550// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6551// // freeze(this.field); HConstructorFence(this)
6552// } HReturn
6553// }
6554//
6555// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6556// already-initialized classes; in that case the allocation must act as a "default-initializer"
6557// of the object which effectively writes the class pointer "final field".
6558//
6559// For example, we can model default-initialiation as roughly the equivalent of the following:
6560//
6561// class Object {
6562// private final Class header;
6563// }
6564//
6565// Java code: Optimizing IR:
6566//
6567// T new_instance<T>() {
6568// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6569// obj.header = T.class; // header write is done by above call.
6570// // freeze(obj.header) HConstructorFence(obj)
6571// return (T)obj;
6572// }
6573//
6574// See also:
6575// * CompilerDriver::RequiresConstructorBarrier
6576// * QuasiAtomic::ThreadFenceForConstructor
6577//
6578class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6579 // A fence has variable inputs because the inputs can be removed
6580 // after prepare_for_register_allocation phase.
6581 // (TODO: In the future a fence could freeze multiple objects
6582 // after merging two fences together.)
6583 public:
6584 // `fence_object` is the reference that needs to be protected for correct publication.
6585 //
6586 // It makes sense in the following situations:
6587 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6588 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6589 //
6590 // After construction the `fence_object` becomes the 0th input.
6591 // This is not an input in a real sense, but just a convenient place to stash the information
6592 // about the associated object.
6593 HConstructorFence(HInstruction* fence_object,
6594 uint32_t dex_pc,
6595 ArenaAllocator* arena)
6596 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6597 // constraints described in the class header. We claim that these SideEffects constraints
6598 // enforce a superset of the real constraints.
6599 //
6600 // The ordering described above is conservatively modeled with SideEffects as follows:
6601 //
6602 // * To prevent reordering of the publication stores:
6603 // ----> "Reads of objects" is the initial SideEffect.
6604 // * For every primitive final field store in the constructor:
6605 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6606 // * If there are any stores to reference final fields in the constructor:
6607 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6608 // that are reachable from `fence_object` also need to be prevented for reordering
6609 // (and we do not want to do alias analysis to figure out what those stores are).
6610 //
6611 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6612 // even more conservative approach than the one described above, and prevents all of the
6613 // above reordering without analyzing any of the instructions in the constructor.
6614 //
6615 // If in a later phase we discover that there are no writes to reference final fields,
6616 // we can refine the side effect to a smaller set of type reads (see above constraints).
6617 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6618 dex_pc,
6619 arena,
6620 /* number_of_inputs */ 1,
6621 kArenaAllocConstructorFenceInputs) {
6622 DCHECK(fence_object != nullptr);
6623 SetRawInputAt(0, fence_object);
6624 }
6625
6626 // The object associated with this constructor fence.
6627 //
6628 // (Note: This will be null after the prepare_for_register_allocation phase,
6629 // as all constructor fence inputs are removed there).
6630 HInstruction* GetFenceObject() const {
6631 return InputAt(0);
6632 }
6633
6634 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6635 // - Delete `fence_use` from `this`'s use list.
6636 // - Delete `this` from `fence_use`'s inputs list.
6637 // - If the `fence_use` is dead, remove it from the graph.
6638 //
6639 // A fence is considered dead once it no longer has any uses
6640 // and all of the inputs are dead.
6641 //
6642 // This must *not* be called during/after prepare_for_register_allocation,
6643 // because that removes all the inputs to the fences but the fence is actually
6644 // still considered live.
6645 static void RemoveConstructorFences(HInstruction* instruction);
6646
6647 DECLARE_INSTRUCTION(ConstructorFence);
6648
6649 private:
6650 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6651};
6652
Vladimir Markofcb503c2016-05-18 12:48:17 +01006653class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006654 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006655 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006656 kEnter,
6657 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006658 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006659 };
6660
6661 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006662 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006663 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6664 dex_pc) {
6665 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006666 SetRawInputAt(0, object);
6667 }
6668
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006669 // Instruction may go into runtime, so we need an environment.
6670 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006671
6672 bool CanThrow() const OVERRIDE {
6673 // Verifier guarantees that monitor-exit cannot throw.
6674 // This is important because it allows the HGraphBuilder to remove
6675 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6676 return IsEnter();
6677 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006678
Vladimir Markoa1de9182016-02-25 11:37:38 +00006679 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6680 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006681
6682 DECLARE_INSTRUCTION(MonitorOperation);
6683
Calin Juravle52c48962014-12-16 17:02:57 +00006684 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006685 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6686 static constexpr size_t kFieldOperationKindSize =
6687 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6688 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6689 kFieldOperationKind + kFieldOperationKindSize;
6690 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6691 "Too many packed fields.");
6692 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006693
6694 private:
6695 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6696};
6697
Vladimir Markofcb503c2016-05-18 12:48:17 +01006698class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006699 public:
6700 HSelect(HInstruction* condition,
6701 HInstruction* true_value,
6702 HInstruction* false_value,
6703 uint32_t dex_pc)
6704 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6705 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6706
6707 // First input must be `true_value` or `false_value` to allow codegens to
6708 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6709 // that architectures which implement HSelect as a conditional move also
6710 // will not need to invert the condition.
6711 SetRawInputAt(0, false_value);
6712 SetRawInputAt(1, true_value);
6713 SetRawInputAt(2, condition);
6714 }
6715
6716 HInstruction* GetFalseValue() const { return InputAt(0); }
6717 HInstruction* GetTrueValue() const { return InputAt(1); }
6718 HInstruction* GetCondition() const { return InputAt(2); }
6719
6720 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006721 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6722 return true;
6723 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006724
6725 bool CanBeNull() const OVERRIDE {
6726 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6727 }
6728
6729 DECLARE_INSTRUCTION(Select);
6730
6731 private:
6732 DISALLOW_COPY_AND_ASSIGN(HSelect);
6733};
6734
Vladimir Markof9f64412015-09-02 14:05:49 +01006735class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006736 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006737 MoveOperands(Location source,
6738 Location destination,
6739 Primitive::Type type,
6740 HInstruction* instruction)
6741 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006742
6743 Location GetSource() const { return source_; }
6744 Location GetDestination() const { return destination_; }
6745
6746 void SetSource(Location value) { source_ = value; }
6747 void SetDestination(Location value) { destination_ = value; }
6748
6749 // The parallel move resolver marks moves as "in-progress" by clearing the
6750 // destination (but not the source).
6751 Location MarkPending() {
6752 DCHECK(!IsPending());
6753 Location dest = destination_;
6754 destination_ = Location::NoLocation();
6755 return dest;
6756 }
6757
6758 void ClearPending(Location dest) {
6759 DCHECK(IsPending());
6760 destination_ = dest;
6761 }
6762
6763 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006764 DCHECK(source_.IsValid() || destination_.IsInvalid());
6765 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006766 }
6767
6768 // True if this blocks a move from the given location.
6769 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006770 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006771 }
6772
6773 // A move is redundant if it's been eliminated, if its source and
6774 // destination are the same, or if its destination is unneeded.
6775 bool IsRedundant() const {
6776 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6777 }
6778
6779 // We clear both operands to indicate move that's been eliminated.
6780 void Eliminate() {
6781 source_ = destination_ = Location::NoLocation();
6782 }
6783
6784 bool IsEliminated() const {
6785 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6786 return source_.IsInvalid();
6787 }
6788
Alexey Frunze4dda3372015-06-01 18:31:49 -07006789 Primitive::Type GetType() const { return type_; }
6790
Nicolas Geoffray90218252015-04-15 11:56:51 +01006791 bool Is64BitMove() const {
6792 return Primitive::Is64BitType(type_);
6793 }
6794
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006795 HInstruction* GetInstruction() const { return instruction_; }
6796
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006797 private:
6798 Location source_;
6799 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006800 // The type this move is for.
6801 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006802 // The instruction this move is assocatied with. Null when this move is
6803 // for moving an input in the expected locations of user (including a phi user).
6804 // This is only used in debug mode, to ensure we do not connect interval siblings
6805 // in the same parallel move.
6806 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006807};
6808
Roland Levillainc9285912015-12-18 10:38:42 +00006809std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6810
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006811static constexpr size_t kDefaultNumberOfMoves = 4;
6812
Vladimir Markofcb503c2016-05-18 12:48:17 +01006813class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006815 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006816 : HTemplateInstruction(SideEffects::None(), dex_pc),
6817 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6818 moves_.reserve(kDefaultNumberOfMoves);
6819 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006820
Nicolas Geoffray90218252015-04-15 11:56:51 +01006821 void AddMove(Location source,
6822 Location destination,
6823 Primitive::Type type,
6824 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006825 DCHECK(source.IsValid());
6826 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006827 if (kIsDebugBuild) {
6828 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006829 for (const MoveOperands& move : moves_) {
6830 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006831 // Special case the situation where the move is for the spill slot
6832 // of the instruction.
6833 if ((GetPrevious() == instruction)
6834 || ((GetPrevious() == nullptr)
6835 && instruction->IsPhi()
6836 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006837 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006838 << "Doing parallel moves for the same instruction.";
6839 } else {
6840 DCHECK(false) << "Doing parallel moves for the same instruction.";
6841 }
6842 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006843 }
6844 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006845 for (const MoveOperands& move : moves_) {
6846 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006847 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006848 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006849 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006850 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006851 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006852 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006853 }
6854
Vladimir Marko225b6462015-09-28 12:17:40 +01006855 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006856 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006857 }
6858
Vladimir Marko225b6462015-09-28 12:17:40 +01006859 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006860
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006861 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006862
6863 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006864 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006865
6866 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6867};
6868
Mark Mendell0616ae02015-04-17 12:49:27 -04006869} // namespace art
6870
Aart Bikf8f5a162017-02-06 15:35:29 -08006871#include "nodes_vector.h"
6872
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006873#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6874#include "nodes_shared.h"
6875#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006876#ifdef ART_ENABLE_CODEGEN_arm
6877#include "nodes_arm.h"
6878#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006879#ifdef ART_ENABLE_CODEGEN_mips
6880#include "nodes_mips.h"
6881#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006882#ifdef ART_ENABLE_CODEGEN_x86
6883#include "nodes_x86.h"
6884#endif
6885
6886namespace art {
6887
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006888class HGraphVisitor : public ValueObject {
6889 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006890 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6891 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006892
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006893 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006894 virtual void VisitBasicBlock(HBasicBlock* block);
6895
Roland Levillain633021e2014-10-01 14:12:25 +01006896 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006897 void VisitInsertionOrder();
6898
Roland Levillain633021e2014-10-01 14:12:25 +01006899 // Visit the graph following dominator tree reverse post-order.
6900 void VisitReversePostOrder();
6901
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006902 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006903
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006904 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006905#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006906 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6907
6908 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6909
6910#undef DECLARE_VISIT_INSTRUCTION
6911
6912 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006913 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006914
6915 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6916};
6917
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006918class HGraphDelegateVisitor : public HGraphVisitor {
6919 public:
6920 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6921 virtual ~HGraphDelegateVisitor() {}
6922
6923 // Visit functions that delegate to to super class.
6924#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006925 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006926
6927 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6928
6929#undef DECLARE_VISIT_INSTRUCTION
6930
6931 private:
6932 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6933};
6934
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006935// Iterator over the blocks that art part of the loop. Includes blocks part
6936// of an inner loop. The order in which the blocks are iterated is on their
6937// block id.
6938class HBlocksInLoopIterator : public ValueObject {
6939 public:
6940 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6941 : blocks_in_loop_(info.GetBlocks()),
6942 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6943 index_(0) {
6944 if (!blocks_in_loop_.IsBitSet(index_)) {
6945 Advance();
6946 }
6947 }
6948
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006949 bool Done() const { return index_ == blocks_.size(); }
6950 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006951 void Advance() {
6952 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006953 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006954 if (blocks_in_loop_.IsBitSet(index_)) {
6955 break;
6956 }
6957 }
6958 }
6959
6960 private:
6961 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006962 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006963 size_t index_;
6964
6965 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6966};
6967
Mingyao Yang3584bce2015-05-19 16:01:59 -07006968// Iterator over the blocks that art part of the loop. Includes blocks part
6969// of an inner loop. The order in which the blocks are iterated is reverse
6970// post order.
6971class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6972 public:
6973 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6974 : blocks_in_loop_(info.GetBlocks()),
6975 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6976 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006977 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006978 Advance();
6979 }
6980 }
6981
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006982 bool Done() const { return index_ == blocks_.size(); }
6983 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006984 void Advance() {
6985 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006986 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6987 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006988 break;
6989 }
6990 }
6991 }
6992
6993 private:
6994 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006995 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006996 size_t index_;
6997
6998 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6999};
7000
Aart Bikf3e61ee2017-04-12 17:09:20 -07007001// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007002inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007003 if (constant->IsIntConstant()) {
7004 return constant->AsIntConstant()->GetValue();
7005 } else if (constant->IsLongConstant()) {
7006 return constant->AsLongConstant()->GetValue();
7007 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007008 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007009 return 0;
7010 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007011}
7012
Aart Bikf3e61ee2017-04-12 17:09:20 -07007013// Returns true iff instruction is an integral constant (and sets value on success).
7014inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7015 if (instruction->IsIntConstant()) {
7016 *value = instruction->AsIntConstant()->GetValue();
7017 return true;
7018 } else if (instruction->IsLongConstant()) {
7019 *value = instruction->AsLongConstant()->GetValue();
7020 return true;
7021 } else if (instruction->IsNullConstant()) {
7022 *value = 0;
7023 return true;
7024 }
7025 return false;
7026}
7027
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007028#define INSTRUCTION_TYPE_CHECK(type, super) \
7029 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7030 inline const H##type* HInstruction::As##type() const { \
7031 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7032 } \
7033 inline H##type* HInstruction::As##type() { \
7034 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7035 }
7036
7037 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7038#undef INSTRUCTION_TYPE_CHECK
7039
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007040// Create space in `blocks` for adding `number_of_new_blocks` entries
7041// starting at location `at`. Blocks after `at` are moved accordingly.
7042inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7043 size_t number_of_new_blocks,
7044 size_t after) {
7045 DCHECK_LT(after, blocks->size());
7046 size_t old_size = blocks->size();
7047 size_t new_size = old_size + number_of_new_blocks;
7048 blocks->resize(new_size);
7049 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7050}
7051
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007052/*
7053 * Hunt "under the hood" of array lengths (leading to array references),
7054 * null checks (also leading to array references), and new arrays
7055 * (leading to the actual length). This makes it more likely related
7056 * instructions become actually comparable.
7057 */
7058inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7059 while (instruction->IsArrayLength() ||
7060 instruction->IsNullCheck() ||
7061 instruction->IsNewArray()) {
7062 instruction = instruction->IsNewArray()
7063 ? instruction->AsNewArray()->GetLength()
7064 : instruction->InputAt(0);
7065 }
7066 return instruction;
7067}
7068
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007069} // namespace art
7070
7071#endif // ART_COMPILER_OPTIMIZING_NODES_H_