blob: 3e4928bd652c49ba80f0c461ab1813007aa02d17 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010032#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080033#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000034#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "handle.h"
36#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010039#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000040#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010042#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070043#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
David Brazdil1abb4192015-02-17 18:33:36 +000048class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070050class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000055class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000061class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070062class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010064class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010065class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010068class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Mathieu Chartier736b5602015-09-02 14:54:11 -070071namespace mirror {
72class DexCache;
73} // namespace mirror
74
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075static const int kDefaultNumberOfBlocks = 8;
76static const int kDefaultNumberOfSuccessors = 2;
77static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010078static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010079static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Roland Levillain5b5b9312016-03-22 14:57:31 +000082// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
83static constexpr int32_t kMaxIntShiftDistance = 0x1f;
84// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
85static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000086
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010087static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070088static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010090static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
91
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060092static constexpr uint32_t kNoDexPc = -1;
93
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010094inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
95 // For the purposes of the compiler, the dex files must actually be the same object
96 // if we want to safely treat them as the same. This is especially important for JIT
97 // as custom class loaders can open the same underlying file (or memory) multiple
98 // times and provide different class resolution but no two class loaders should ever
99 // use the same DexFile object - doing so is an unsupported hack that can lead to
100 // all sorts of weird failures.
101 return &lhs == &rhs;
102}
103
Dave Allison20dfc792014-06-16 20:44:29 -0700104enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700105 // All types.
106 kCondEQ, // ==
107 kCondNE, // !=
108 // Signed integers and floating-point numbers.
109 kCondLT, // <
110 kCondLE, // <=
111 kCondGT, // >
112 kCondGE, // >=
113 // Unsigned integers.
114 kCondB, // <
115 kCondBE, // <=
116 kCondA, // >
117 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100118 // First and last aliases.
119 kCondFirst = kCondEQ,
120 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700121};
122
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000125 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 kAnalysisFailThrowCatchLoop,
127 kAnalysisFailAmbiguousArrayOp,
128 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000129};
130
Andreas Gampe9186ced2016-12-12 14:28:21 -0800131template <typename T>
132static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
133 return static_cast<typename std::make_unsigned<T>::type>(x);
134}
135
Vladimir Markof9f64412015-09-02 14:05:49 +0100136class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 public:
138 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
139
140 void AddInstruction(HInstruction* instruction);
141 void RemoveInstruction(HInstruction* instruction);
142
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 // Insert `instruction` before/after an existing instruction `cursor`.
144 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
145 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
146
Roland Levillain6b469232014-09-25 10:10:38 +0100147 // Return true if this list contains `instruction`.
148 bool Contains(HInstruction* instruction) const;
149
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 // Return true if `instruction1` is found before `instruction2` in
151 // this instruction list and false otherwise. Abort if none
152 // of these instructions is found.
153 bool FoundBefore(const HInstruction* instruction1,
154 const HInstruction* instruction2) const;
155
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 bool IsEmpty() const { return first_instruction_ == nullptr; }
157 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
158
159 // Update the block of all instructions to be `block`.
160 void SetBlockOfInstructions(HBasicBlock* block) const;
161
162 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000163 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 void Add(const HInstructionList& instruction_list);
165
David Brazdil2d7352b2015-04-20 14:52:42 +0100166 // Return the number of instructions in the list. This is an expensive operation.
167 size_t CountSize() const;
168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 private:
170 HInstruction* first_instruction_;
171 HInstruction* last_instruction_;
172
173 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 friend class HGraph;
175 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000177 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
180 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
181};
182
David Brazdil4833f5a2015-12-16 10:37:39 +0000183class ReferenceTypeInfo : ValueObject {
184 public:
185 typedef Handle<mirror::Class> TypeHandle;
186
Vladimir Markoa1de9182016-02-25 11:37:38 +0000187 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100190 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
191 }
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return ReferenceTypeInfo(type_handle, is_exact);
195 }
196
197 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
198
Vladimir Markof39745e2016-01-26 12:16:55 +0000199 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return handle.GetReference() != nullptr;
201 }
202
Vladimir Marko456307a2016-04-19 14:12:13 +0000203 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return IsValidHandle(type_handle_);
205 }
206
207 bool IsExact() const { return is_exact_; }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsStringClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsInterface();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsPrimitiveArray();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 if (!IsExact()) return false;
247 if (!IsArrayClass()) return false;
248 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 if (!rti.IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
257 rti.GetTypeHandle()->GetComponentType());
258 }
259
260 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
261
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000263 DCHECK(IsValid());
264 DCHECK(rti.IsValid());
265 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
272 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100308 HGraph(ArenaAllocator* arena,
309 const DexFile& dex_file,
310 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_(arena->Adapter(kArenaAllocBlockList)),
318 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
319 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 entry_block_(nullptr),
321 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100322 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 number_of_vregs_(0),
324 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000325 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400326 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000327 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700328 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800329 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000331 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100333 dex_file_(dex_file),
334 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800337 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100340 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
343 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100345 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000346 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700347 osr_(osr),
348 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100349 blocks_.reserve(kDefaultNumberOfBlocks);
350 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351
David Brazdilbadd8262016-02-02 16:28:56 +0000352 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700353 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
357
David Brazdil69ba7b72015-06-23 18:27:30 +0100358 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000359 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 HBasicBlock* GetEntryBlock() const { return entry_block_; }
362 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100363 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
366 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100370 void ComputeDominanceInformation();
371 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000372 void ClearLoopInformation();
373 void FindBackEdges(ArenaBitVector* visited);
374 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100376 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // Analyze all natural loops in this graph. Returns a code specifying that it
379 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000380 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Iterate over blocks to compute try block membership. Needs reverse post
384 // order and loop information.
385 void ComputeTryBlockInformation();
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000388 // Returns the instruction to replace the invoke expression or null if the
389 // invoke is for a void method. Note that the caller is responsible for replacing
390 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000391 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000393 // Update the loop and try membership of `block`, which was spawned from `reference`.
394 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
395 // should be the new back edge.
396 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
397 HBasicBlock* reference,
398 bool replace_if_back_edge);
399
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 // Need to add a couple of blocks to test if the loop body is entered and
401 // put deoptimization instructions, etc.
402 void TransformLoopHeaderForBCE(HBasicBlock* header);
403
Aart Bikf8f5a162017-02-06 15:35:29 -0800404 // Adds a new loop directly after the loop with the given header and exit.
405 // Returns the new preheader.
406 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
407 HBasicBlock* body,
408 HBasicBlock* exit);
409
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000410 // Removes `block` from the graph. Assumes `block` has been disconnected from
411 // other blocks and has no instructions or phis.
412 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000413
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 // Splits the edge between `block` and `successor` while preserving the
415 // indices in the predecessor/successor lists. If there are multiple edges
416 // between the blocks, the lowest indices are used.
417 // Returns the new block which is empty and has the same dex pc as `successor`.
418 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
421 void SimplifyLoop(HBasicBlock* header);
422
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100424 CHECK_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) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100433 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 current_instruction_id_ = id;
435 }
436
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100437 uint16_t GetMaximumNumberOfOutVRegs() const {
438 return maximum_number_of_out_vregs_;
439 }
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
442 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100443 }
444
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
446 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
447 }
448
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000449 void UpdateTemporariesVRegSlots(size_t slots) {
450 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100451 }
452
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000453 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100454 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000455 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100458 void SetNumberOfVRegs(uint16_t number_of_vregs) {
459 number_of_vregs_ = number_of_vregs;
460 }
461
462 uint16_t GetNumberOfVRegs() const {
463 return number_of_vregs_;
464 }
465
466 void SetNumberOfInVRegs(uint16_t value) {
467 number_of_in_vregs_ = value;
468 }
469
David Brazdildee58d62016-04-07 09:54:26 +0000470 uint16_t GetNumberOfInVRegs() const {
471 return number_of_in_vregs_;
472 }
473
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100474 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100475 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100476 return number_of_vregs_ - number_of_in_vregs_;
477 }
478
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100479 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100482
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100483 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
484 DCHECK(GetReversePostOrder()[0] == entry_block_);
485 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
486 }
487
488 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
489 return ReverseRange(GetReversePostOrder());
490 }
491
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100492 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100493 return linear_order_;
494 }
495
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100496 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
497 return ReverseRange(GetLinearOrder());
498 }
499
Mark Mendell1152c922015-04-24 17:06:35 -0400500 bool HasBoundsChecks() const {
501 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800502 }
503
Mark Mendell1152c922015-04-24 17:06:35 -0400504 void SetHasBoundsChecks(bool value) {
505 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800506 }
507
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000508 bool IsDebuggable() const { return debuggable_; }
509
David Brazdil8d5b8b22015-03-24 10:51:52 +0000510 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 // already, it is created and inserted into the graph. This method is only for
512 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000514
515 // TODO: This is problematic for the consistency of reference type propagation
516 // because it can be created anytime after the pass and thus it will be left
517 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
530 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000532
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100533 HCurrentMethod* GetCurrentMethod();
534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100535 const DexFile& GetDexFile() const {
536 return dex_file_;
537 }
538
539 uint32_t GetMethodIdx() const {
540 return method_idx_;
541 }
542
Igor Murashkind01745e2017-04-05 16:40:31 -0700543 // Get the method name (without the signature), e.g. "<init>"
544 const char* GetMethodName() const;
545
546 // Get the pretty method name (class + name + optionally signature).
547 std::string PrettyMethod(bool with_signature = true) const;
548
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100549 InvokeType GetInvokeType() const {
550 return invoke_type_;
551 }
552
Mark Mendellc4701932015-04-10 13:18:51 -0400553 InstructionSet GetInstructionSet() const {
554 return instruction_set_;
555 }
556
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000557 bool IsCompilingOsr() const { return osr_; }
558
Mingyao Yang063fc772016-08-02 11:02:54 -0700559 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
560 return cha_single_implementation_list_;
561 }
562
563 void AddCHASingleImplementationDependency(ArtMethod* method) {
564 cha_single_implementation_list_.insert(method);
565 }
566
567 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800568 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700569 }
570
David Brazdil77a48ae2015-09-15 12:34:04 +0000571 bool HasTryCatch() const { return has_try_catch_; }
572 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100573
Aart Bikb13c65b2017-03-21 20:14:07 -0700574 bool HasSIMD() const { return has_simd_; }
575 void SetHasSIMD(bool value) { has_simd_ = value; }
576
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800577 bool HasLoops() const { return has_loops_; }
578 void SetHasLoops(bool value) { has_loops_ = value; }
579
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000580 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
581 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
582
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100583 ArtMethod* GetArtMethod() const { return art_method_; }
584 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
585
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100586 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500587 // The instruction has been inserted into the graph, either as a constant, or
588 // before cursor.
589 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
590
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000591 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
592
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800593 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
594 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
595 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
596
David Brazdil2d7352b2015-04-20 14:52:42 +0100597 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000598 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100599 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000600
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 template <class InstructionType, typename ValueType>
602 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600603 ArenaSafeMap<ValueType, InstructionType*>* cache,
604 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 // Try to find an existing constant of the given value.
606 InstructionType* constant = nullptr;
607 auto cached_constant = cache->find(value);
608 if (cached_constant != cache->end()) {
609 constant = cached_constant->second;
610 }
611
612 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100613 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000614 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600615 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000616 cache->Overwrite(value, constant);
617 InsertConstant(constant);
618 }
619 return constant;
620 }
621
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 void InsertConstant(HConstant* instruction);
623
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 // Cache a float constant into the graph. This method should only be
625 // called by the SsaBuilder when creating "equivalent" instructions.
626 void CacheFloatConstant(HFloatConstant* constant);
627
628 // See CacheFloatConstant comment.
629 void CacheDoubleConstant(HDoubleConstant* constant);
630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000632
633 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100634 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
Aart Bik281c6812016-08-26 11:31:48 -0700639 // List of blocks to perform a linear order tree traversal. Unlike the reverse
640 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100641 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100642
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000643 HBasicBlock* entry_block_;
644 HBasicBlock* exit_block_;
645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100646 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100647 uint16_t maximum_number_of_out_vregs_;
648
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100649 // The number of virtual registers in this method. Contains the parameters.
650 uint16_t number_of_vregs_;
651
652 // The number of virtual registers used by parameters of this method.
653 uint16_t number_of_in_vregs_;
654
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000655 // Number of vreg size slots that the temporaries use (used in baseline compiler).
656 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800658 // Flag whether there are bounds checks in the graph. We can skip
659 // BCE if it's false. It's only best effort to keep it up to date in
660 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400661 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800662
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800663 // Flag whether there are try/catch blocks in the graph. We will skip
664 // try/catch-related passes if it's false. It's only best effort to keep
665 // it up to date in the presence of code elimination so there might be
666 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000667 bool has_try_catch_;
668
Aart Bikb13c65b2017-03-21 20:14:07 -0700669 // Flag whether SIMD instructions appear in the graph. If true, the
670 // code generators may have to be more careful spilling the wider
671 // contents of SIMD registers.
672 bool has_simd_;
673
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800674 // Flag whether there are any loops in the graph. We can skip loop
675 // optimization if it's false. It's only best effort to keep it up
676 // to date in the presence of code elimination so there might be false
677 // positives.
678 bool has_loops_;
679
680 // Flag whether there are any irreducible loops in the graph. It's only
681 // best effort to keep it up to date in the presence of code elimination
682 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000683 bool has_irreducible_loops_;
684
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000685 // Indicates whether the graph should be compiled in a way that
686 // ensures full debuggability. If false, we can apply more
687 // aggressive optimizations that may limit the level of debugging.
688 const bool debuggable_;
689
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000691 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000692
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100693 // The dex file from which the method is from.
694 const DexFile& dex_file_;
695
696 // The method index in the dex file.
697 const uint32_t method_idx_;
698
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100699 // If inlined, this encodes how the callee is being invoked.
700 const InvokeType invoke_type_;
701
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100702 // Whether the graph has been transformed to SSA form. Only used
703 // in debug mode to ensure we are not using properties only valid
704 // for non-SSA form (like the number of temporaries).
705 bool in_ssa_form_;
706
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800707 // Number of CHA guards in the graph. Used to short-circuit the
708 // CHA guard optimization pass when there is no CHA guard left.
709 uint32_t number_of_cha_guards_;
710
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711 const InstructionSet instruction_set_;
712
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000713 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000714 HNullConstant* cached_null_constant_;
715 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000716 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000717 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000718 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000719
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100720 HCurrentMethod* cached_current_method_;
721
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100722 // The ArtMethod this graph is for. Note that for AOT, it may be null,
723 // for example for methods whose declaring class could not be resolved
724 // (such as when the superclass could not be found).
725 ArtMethod* art_method_;
726
David Brazdil4833f5a2015-12-16 10:37:39 +0000727 // Keep the RTI of inexact Object to avoid having to pass stack handle
728 // collection pointer to passes which may create NullConstant.
729 ReferenceTypeInfo inexact_object_rti_;
730
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000731 // Whether we are compiling this graph for on stack replacement: this will
732 // make all loops seen as irreducible and emit special stack maps to mark
733 // compiled code entries which the interpreter can directly jump to.
734 const bool osr_;
735
Mingyao Yang063fc772016-08-02 11:02:54 -0700736 // List of methods that are assumed to have single implementation.
737 ArenaSet<ArtMethod*> cha_single_implementation_list_;
738
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000739 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100740 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000741 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000742 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743 DISALLOW_COPY_AND_ASSIGN(HGraph);
744};
745
Vladimir Markof9f64412015-09-02 14:05:49 +0100746class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747 public:
748 HLoopInformation(HBasicBlock* header, HGraph* graph)
749 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100750 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000751 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100753 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100754 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000755 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 back_edges_.reserve(kDefaultNumberOfBackEdges);
757 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000759 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100760 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000761
762 void Dump(std::ostream& os);
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 HBasicBlock* GetHeader() const {
765 return header_;
766 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 void SetHeader(HBasicBlock* block) {
769 header_ = block;
770 }
771
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
773 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
774 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
775
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000778 }
779
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 }
783
David Brazdil46e2a392015-03-16 17:31:52 +0000784 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 }
787
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000788 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 }
791
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100792 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 }
797
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100798 // Returns the lifetime position of the back edge that has the
799 // greatest lifetime position.
800 size_t GetLifetimeEnd() const;
801
802 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100804 }
805
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000806 // Finds blocks that are part of this loop.
807 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100808
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 // Returns whether this loop information contains `block`.
810 // Note that this loop information *must* be populated before entering this function.
811 bool Contains(const HBasicBlock& block) const;
812
813 // Returns whether this loop information is an inner loop of `other`.
814 // Note that `other` *must* be populated before entering this function.
815 bool IsIn(const HLoopInformation& other) const;
816
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800817 // Returns true if instruction is not defined within this loop.
818 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700819
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 const ArenaBitVector& GetBlocks() const { return blocks_; }
821
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000822 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000823 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000824
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000825 void ClearAllBlocks() {
826 blocks_.ClearAllBits();
827 }
828
David Brazdil3f4a5222016-05-06 12:46:21 +0100829 bool HasBackEdgeNotDominatedByHeader() const;
830
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100831 bool IsPopulated() const {
832 return blocks_.GetHighestBitSet() != -1;
833 }
834
Anton Shaminf89381f2016-05-16 16:44:13 +0600835 bool DominatesAllBackEdges(HBasicBlock* block);
836
David Sehrc757dec2016-11-04 15:48:34 -0700837 bool HasExitEdge() const;
838
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 // Internal recursive implementation of `Populate`.
841 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100842 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000846 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100847 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850
851 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
852};
853
David Brazdilec16f792015-08-19 15:04:01 +0100854// Stores try/catch information for basic blocks.
855// Note that HGraph is constructed so that catch blocks cannot simultaneously
856// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100857class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100858 public:
859 // Try block information constructor.
860 explicit TryCatchInformation(const HTryBoundary& try_entry)
861 : try_entry_(&try_entry),
862 catch_dex_file_(nullptr),
863 catch_type_index_(DexFile::kDexNoIndex16) {
864 DCHECK(try_entry_ != nullptr);
865 }
866
867 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800868 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100869 : try_entry_(nullptr),
870 catch_dex_file_(&dex_file),
871 catch_type_index_(catch_type_index) {}
872
873 bool IsTryBlock() const { return try_entry_ != nullptr; }
874
875 const HTryBoundary& GetTryEntry() const {
876 DCHECK(IsTryBlock());
877 return *try_entry_;
878 }
879
880 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
881
882 bool IsCatchAllTypeIndex() const {
883 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800884 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100885 }
886
Andreas Gampea5b09a62016-11-17 15:21:22 -0800887 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100888 DCHECK(IsCatchBlock());
889 return catch_type_index_;
890 }
891
892 const DexFile& GetCatchDexFile() const {
893 DCHECK(IsCatchBlock());
894 return *catch_dex_file_;
895 }
896
897 private:
898 // One of possibly several TryBoundary instructions entering the block's try.
899 // Only set for try blocks.
900 const HTryBoundary* try_entry_;
901
902 // Exception type information. Only set for catch blocks.
903 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800904 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100905};
906
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100907static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100908static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910// A block in a method. Contains the list of instructions represented
911// as a double linked list. Each block knows its predecessors and
912// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100913
Vladimir Markof9f64412015-09-02 14:05:49 +0100914class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700916 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000918 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
919 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920 loop_information_(nullptr),
921 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000922 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100923 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100924 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000926 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000927 try_catch_information_(nullptr) {
928 predecessors_.reserve(kDefaultNumberOfPredecessors);
929 successors_.reserve(kDefaultNumberOfSuccessors);
930 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
931 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932
Vladimir Marko60584552015-09-03 13:35:12 +0000933 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
Vladimir Marko60584552015-09-03 13:35:12 +0000937 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939 }
940
David Brazdild26a4112015-11-10 11:07:31 +0000941 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
942 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
943
Vladimir Marko60584552015-09-03 13:35:12 +0000944 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
945 return ContainsElement(successors_, block, start_from);
946 }
947
948 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100949 return dominated_blocks_;
950 }
951
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952 bool IsEntryBlock() const {
953 return graph_->GetEntryBlock() == this;
954 }
955
956 bool IsExitBlock() const {
957 return graph_->GetExitBlock() == this;
958 }
959
David Brazdil46e2a392015-03-16 17:31:52 +0000960 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 bool IsSingleTryBoundary() const;
962
963 // Returns true if this block emits nothing but a jump.
964 bool IsSingleJump() const {
965 HLoopInformation* loop_info = GetLoopInformation();
966 return (IsSingleGoto() || IsSingleTryBoundary())
967 // Back edges generate a suspend check.
968 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
969 }
David Brazdil46e2a392015-03-16 17:31:52 +0000970
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 void AddBackEdge(HBasicBlock* back_edge) {
972 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100975 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 loop_information_->AddBackEdge(back_edge);
977 }
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000980 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600984 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986 HBasicBlock* GetDominator() const { return dominator_; }
987 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000988 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
989
990 void RemoveDominatedBlock(HBasicBlock* block) {
991 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100992 }
Vladimir Marko60584552015-09-03 13:35:12 +0000993
994 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
995 ReplaceElement(dominated_blocks_, existing, new_block);
996 }
997
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100998 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000999
1000 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001001 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001002 }
1003
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1005 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001006 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001007 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001008 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1009 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001011 HInstruction* GetFirstInstructionDisregardMoves() const;
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001014 successors_.push_back(block);
1015 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 }
1017
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001018 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1019 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001021 new_block->predecessors_.push_back(this);
1022 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001023 }
1024
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1026 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001028 new_block->successors_.push_back(this);
1029 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001030 }
1031
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001032 // Insert `this` between `predecessor` and `successor. This method
1033 // preserves the indicies, and will update the first edge found between
1034 // `predecessor` and `successor`.
1035 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1036 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001037 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001038 successor->predecessors_[predecessor_index] = this;
1039 predecessor->successors_[successor_index] = this;
1040 successors_.push_back(successor);
1041 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001042 }
1043
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001045 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001046 }
1047
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001049 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 }
1051
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001053 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 }
1055
1056 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001057 predecessors_.push_back(block);
1058 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 }
1060
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001061 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001062 DCHECK_EQ(predecessors_.size(), 2u);
1063 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001064 }
1065
David Brazdil769c9e52015-04-27 13:54:09 +01001066 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 DCHECK_EQ(successors_.size(), 2u);
1068 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001069 }
1070
David Brazdilfc6a86a2015-06-26 10:33:45 +00001071 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001072 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001073 }
1074
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001076 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001077 }
1078
David Brazdilfc6a86a2015-06-26 10:33:45 +00001079 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001080 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001081 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 }
1083
1084 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001085 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001086 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001087 }
1088
1089 // Returns whether the first occurrence of `predecessor` in the list of
1090 // predecessors is at index `idx`.
1091 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 return GetPredecessorIndexOf(predecessor) == idx;
1094 }
1095
David Brazdild7558da2015-09-22 13:04:14 +01001096 // Create a new block between this block and its predecessors. The new block
1097 // is added to the graph, all predecessor edges are relinked to it and an edge
1098 // is created to `this`. Returns the new empty block. Reverse post order or
1099 // loop and try/catch information are not updated.
1100 HBasicBlock* CreateImmediateDominator();
1101
David Brazdilfc6a86a2015-06-26 10:33:45 +00001102 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001103 // created, latter block. Note that this method will add the block to the
1104 // graph, create a Goto at the end of the former block and will create an edge
1105 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001106 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001107 HBasicBlock* SplitBefore(HInstruction* cursor);
1108
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001109 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001110 // created block. Note that this method just updates raw block information,
1111 // like predecessors, successors, dominators, and instruction list. It does not
1112 // update the graph, reverse post order, loop information, nor make sure the
1113 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001114 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1115
1116 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1117 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118
1119 // Merge `other` at the end of `this`. Successors and dominated blocks of
1120 // `other` are changed to be successors and dominated blocks of `this`. Note
1121 // that this method does not update the graph, reverse post order, loop
1122 // information, nor make sure the blocks are consistent (for example ending
1123 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001124 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001125
1126 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1127 // of `this` are moved to `other`.
1128 // Note that this method does not update the graph, reverse post order, loop
1129 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001130 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 void ReplaceWith(HBasicBlock* other);
1132
Aart Bik6b69e0a2017-01-11 10:20:43 -08001133 // Merges the instructions of `other` at the end of `this`.
1134 void MergeInstructionsWith(HBasicBlock* other);
1135
David Brazdil2d7352b2015-04-20 14:52:42 +01001136 // Merge `other` at the end of `this`. This method updates loops, reverse post
1137 // order, links to predecessors, successors, dominators and deletes the block
1138 // from the graph. The two blocks must be successive, i.e. `this` the only
1139 // predecessor of `other` and vice versa.
1140 void MergeWith(HBasicBlock* other);
1141
1142 // Disconnects `this` from all its predecessors, successors and dominator,
1143 // removes it from all loops it is included in and eventually from the graph.
1144 // The block must not dominate any other block. Predecessors and successors
1145 // are safely updated.
1146 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001149 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001150 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001152 // Replace instruction `initial` with `replacement` within this block.
1153 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1154 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001156 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001157 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1158 // instruction list. With 'ensure_safety' set to true, it verifies that the
1159 // instruction is not in use and removes it from the use lists of its inputs.
1160 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1161 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001162 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001163
1164 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001165 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 }
1167
Roland Levillain6b879dd2014-09-22 17:13:44 +01001168 bool IsLoopPreHeaderFirstPredecessor() const {
1169 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001170 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001171 }
1172
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001173 bool IsFirstPredecessorBackEdge() const {
1174 DCHECK(IsLoopHeader());
1175 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1176 }
1177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001178 HLoopInformation* GetLoopInformation() const {
1179 return loop_information_;
1180 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001182 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001183 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 void SetInLoop(HLoopInformation* info) {
1186 if (IsLoopHeader()) {
1187 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001188 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001189 loop_information_ = info;
1190 } else if (loop_information_->Contains(*info->GetHeader())) {
1191 // Block is currently part of an outer loop. Make it part of this inner loop.
1192 // Note that a non loop header having a loop information means this loop information
1193 // has already been populated
1194 loop_information_ = info;
1195 } else {
1196 // Block is part of an inner loop. Do not update the loop information.
1197 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1198 // at this point, because this method is being called while populating `info`.
1199 }
1200 }
1201
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001202 // Raw update of the loop information.
1203 void SetLoopInformation(HLoopInformation* info) {
1204 loop_information_ = info;
1205 }
1206
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001207 bool IsInLoop() const { return loop_information_ != nullptr; }
1208
David Brazdilec16f792015-08-19 15:04:01 +01001209 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1210
1211 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1212 try_catch_information_ = try_catch_information;
1213 }
1214
1215 bool IsTryBlock() const {
1216 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1217 }
1218
1219 bool IsCatchBlock() const {
1220 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1221 }
David Brazdilffee3d32015-07-06 11:48:53 +01001222
1223 // Returns the try entry that this block's successors should have. They will
1224 // be in the same try, unless the block ends in a try boundary. In that case,
1225 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001226 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001227
David Brazdild7558da2015-09-22 13:04:14 +01001228 bool HasThrowingInstructions() const;
1229
David Brazdila4b8c212015-05-07 09:59:30 +01001230 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 bool Dominates(HBasicBlock* block) const;
1232
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001233 size_t GetLifetimeStart() const { return lifetime_start_; }
1234 size_t GetLifetimeEnd() const { return lifetime_end_; }
1235
1236 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1237 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1238
David Brazdil8d5b8b22015-03-24 10:51:52 +00001239 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001240 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001241 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool HasSinglePhi() const;
1243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001245 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001246 ArenaVector<HBasicBlock*> predecessors_;
1247 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248 HInstructionList instructions_;
1249 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001250 HLoopInformation* loop_information_;
1251 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001252 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001253 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001254 // The dex program counter of the first instruction of this block.
1255 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001256 size_t lifetime_start_;
1257 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001258 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260 friend class HGraph;
1261 friend class HInstruction;
1262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001263 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1264};
1265
David Brazdilb2bd1c52015-03-25 11:17:37 +00001266// Iterates over the LoopInformation of all loops which contain 'block'
1267// from the innermost to the outermost.
1268class HLoopInformationOutwardIterator : public ValueObject {
1269 public:
1270 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1271 : current_(block.GetLoopInformation()) {}
1272
1273 bool Done() const { return current_ == nullptr; }
1274
1275 void Advance() {
1276 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001277 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001278 }
1279
1280 HLoopInformation* Current() const {
1281 DCHECK(!Done());
1282 return current_;
1283 }
1284
1285 private:
1286 HLoopInformation* current_;
1287
1288 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1289};
1290
Alexandre Ramesef20f712015-06-09 10:29:30 +01001291#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001292 M(Above, Condition) \
1293 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001295 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001296 M(ArrayGet, Instruction) \
1297 M(ArrayLength, Instruction) \
1298 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001299 M(Below, Condition) \
1300 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001301 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001302 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001303 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001304 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001305 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001306 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001307 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001309 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001310 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001311 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001312 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001314 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(Exit, Instruction) \
1318 M(FloatConstant, Constant) \
1319 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(GreaterThan, Condition) \
1321 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001323 M(InstanceFieldGet, Instruction) \
1324 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001325 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001327 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001329 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001331 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(LessThan, Condition) \
1333 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001334 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001335 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001338 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001339 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001340 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001341 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Neg, UnaryOperation) \
1343 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001345 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001347 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001349 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001350 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001354 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Return, Instruction) \
1356 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001357 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001358 M(Shl, BinaryOperation) \
1359 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360 M(StaticFieldGet, Instruction) \
1361 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 M(UnresolvedInstanceFieldGet, Instruction) \
1363 M(UnresolvedInstanceFieldSet, Instruction) \
1364 M(UnresolvedStaticFieldGet, Instruction) \
1365 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001366 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001369 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001370 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001371 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001372 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001373 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001374 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001375 M(VecSumReduce, VecUnaryOperation) \
1376 M(VecCnv, VecUnaryOperation) \
1377 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001378 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001379 M(VecNot, VecUnaryOperation) \
1380 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001381 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001382 M(VecSub, VecBinaryOperation) \
1383 M(VecMul, VecBinaryOperation) \
1384 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001385 M(VecMin, VecBinaryOperation) \
1386 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001387 M(VecAnd, VecBinaryOperation) \
1388 M(VecAndNot, VecBinaryOperation) \
1389 M(VecOr, VecBinaryOperation) \
1390 M(VecXor, VecBinaryOperation) \
1391 M(VecShl, VecBinaryOperation) \
1392 M(VecShr, VecBinaryOperation) \
1393 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001394 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001395 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001396 M(VecLoad, VecMemoryOperation) \
1397 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001399/*
1400 * Instructions, shared across several (not all) architectures.
1401 */
1402#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1403#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1404#else
1405#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001406 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001407 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001408 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001409 M(IntermediateAddress, Instruction) \
1410 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001411#endif
1412
Alexandre Ramesef20f712015-06-09 10:29:30 +01001413#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1414
1415#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1416
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001417#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001418#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001419#else
1420#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1421 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001422 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001423#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001424
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001425#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1426
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001427#ifndef ART_ENABLE_CODEGEN_x86
1428#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1429#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001430#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1431 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001432 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001433 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001434 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001435#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001436
1437#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1438
1439#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1440 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001441 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001442 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1443 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001444 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001445 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001446 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1447 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1448
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001449#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1450 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001451 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001452 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001453 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001454 M(Invoke, Instruction) \
1455 M(VecOperation, Instruction) \
1456 M(VecUnaryOperation, VecOperation) \
1457 M(VecBinaryOperation, VecOperation) \
1458 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001459
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001460#define FOR_EACH_INSTRUCTION(M) \
1461 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1462 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1463
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001464#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1466#undef FORWARD_DECLARATION
1467
Vladimir Marko372f10e2016-05-17 16:30:10 +01001468#define DECLARE_INSTRUCTION(type) \
1469 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1470 const char* DebugName() const OVERRIDE { return #type; } \
1471 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1472 return other->Is##type(); \
1473 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001474 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001475
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001476#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1477 bool Is##type() const { return As##type() != nullptr; } \
1478 const H##type* As##type() const { return this; } \
1479 H##type* As##type() { return this; }
1480
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001481template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001482class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1483 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001484 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001485 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001486 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001487 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001488 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001489 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001490 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001491
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001492 private:
David Brazdiled596192015-01-23 10:39:45 +00001493 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001494 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001495
1496 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001497 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001498
Vladimir Marko46817b82016-03-29 12:21:58 +01001499 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001500
1501 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1502};
1503
David Brazdiled596192015-01-23 10:39:45 +00001504template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001505using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001506
David Brazdil1abb4192015-02-17 18:33:36 +00001507// This class is used by HEnvironment and HInstruction classes to record the
1508// instructions they use and pointers to the corresponding HUseListNodes kept
1509// by the used instructions.
1510template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001511class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001512 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001513 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1514 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001515
Vladimir Marko46817b82016-03-29 12:21:58 +01001516 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1517 : HUserRecord(old_record.instruction_, before_use_node) {}
1518 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1519 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001520 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001521 }
1522
1523 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001524 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1525 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001526
1527 private:
1528 // Instruction used by the user.
1529 HInstruction* instruction_;
1530
Vladimir Marko46817b82016-03-29 12:21:58 +01001531 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1532 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001533};
1534
Vladimir Markoe9004912016-06-16 16:50:52 +01001535// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1536// This is used for HInstruction::GetInputs() to return a container wrapper providing
1537// HInstruction* values even though the underlying container has HUserRecord<>s.
1538struct HInputExtractor {
1539 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1540 return record.GetInstruction();
1541 }
1542 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1543 return record.GetInstruction();
1544 }
1545};
1546
1547using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1548using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1549
Aart Bik854a02b2015-07-14 16:07:00 -07001550/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001552 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001553 * For write/read dependences on fields/arrays, the dependence analysis uses
1554 * type disambiguation (e.g. a float field write cannot modify the value of an
1555 * integer field read) and the access type (e.g. a reference array write cannot
1556 * modify the value of a reference field read [although it may modify the
1557 * reference fetch prior to reading the field, which is represented by its own
1558 * write/read dependence]). The analysis makes conservative points-to
1559 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1560 * the same, and any reference read depends on any reference read without
1561 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001562 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001563 * The internal representation uses 38-bit and is described in the table below.
1564 * The first line indicates the side effect, and for field/array accesses the
1565 * second line indicates the type of the access (in the order of the
1566 * Primitive::Type enum).
1567 * The two numbered lines below indicate the bit position in the bitfield (read
1568 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001569 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1571 * +-------------+---------+---------+--------------+---------+---------+
1572 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1573 * | 3 |333333322|222222221| 1 |111111110|000000000|
1574 * | 7 |654321098|765432109| 8 |765432109|876543210|
1575 *
1576 * Note that, to ease the implementation, 'changes' bits are least significant
1577 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001578 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579class SideEffects : public ValueObject {
1580 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001581 SideEffects() : flags_(0) {}
1582
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001583 static SideEffects None() {
1584 return SideEffects(0);
1585 }
1586
1587 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001588 return SideEffects(kAllChangeBits | kAllDependOnBits);
1589 }
1590
1591 static SideEffects AllChanges() {
1592 return SideEffects(kAllChangeBits);
1593 }
1594
1595 static SideEffects AllDependencies() {
1596 return SideEffects(kAllDependOnBits);
1597 }
1598
1599 static SideEffects AllExceptGCDependency() {
1600 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1601 }
1602
1603 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001604 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001605 }
1606
Aart Bik34c3ba92015-07-20 14:08:59 -07001607 static SideEffects AllWrites() {
1608 return SideEffects(kAllWrites);
1609 }
1610
1611 static SideEffects AllReads() {
1612 return SideEffects(kAllReads);
1613 }
1614
1615 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1616 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001618 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619 }
1620
Aart Bik854a02b2015-07-14 16:07:00 -07001621 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001622 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001623 }
1624
Aart Bik34c3ba92015-07-20 14:08:59 -07001625 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1626 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001627 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001628 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001629 }
1630
1631 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001632 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001633 }
1634
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 static SideEffects CanTriggerGC() {
1636 return SideEffects(1ULL << kCanTriggerGCBit);
1637 }
1638
1639 static SideEffects DependsOnGC() {
1640 return SideEffects(1ULL << kDependsOnGCBit);
1641 }
1642
Aart Bik854a02b2015-07-14 16:07:00 -07001643 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001644 SideEffects Union(SideEffects other) const {
1645 return SideEffects(flags_ | other.flags_);
1646 }
1647
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001648 SideEffects Exclusion(SideEffects other) const {
1649 return SideEffects(flags_ & ~other.flags_);
1650 }
1651
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001652 void Add(SideEffects other) {
1653 flags_ |= other.flags_;
1654 }
1655
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001656 bool Includes(SideEffects other) const {
1657 return (other.flags_ & flags_) == other.flags_;
1658 }
1659
1660 bool HasSideEffects() const {
1661 return (flags_ & kAllChangeBits);
1662 }
1663
1664 bool HasDependencies() const {
1665 return (flags_ & kAllDependOnBits);
1666 }
1667
1668 // Returns true if there are no side effects or dependencies.
1669 bool DoesNothing() const {
1670 return flags_ == 0;
1671 }
1672
Aart Bik854a02b2015-07-14 16:07:00 -07001673 // Returns true if something is written.
1674 bool DoesAnyWrite() const {
1675 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001676 }
1677
Aart Bik854a02b2015-07-14 16:07:00 -07001678 // Returns true if something is read.
1679 bool DoesAnyRead() const {
1680 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 // Returns true if potentially everything is written and read
1684 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001685 bool DoesAllReadWrite() const {
1686 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1687 }
1688
Aart Bik854a02b2015-07-14 16:07:00 -07001689 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
Roland Levillain0d5a2812015-11-13 10:07:31 +00001693 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001694 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1696 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001697 }
1698
1699 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001700 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001701 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001702 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 for (int s = kLastBit; s >= 0; s--) {
1704 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1705 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1706 // This is a bit for the GC side effect.
1707 if (current_bit_is_set) {
1708 flags += "GC";
1709 }
Aart Bik854a02b2015-07-14 16:07:00 -07001710 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001711 } else {
1712 // This is a bit for the array/field analysis.
1713 // The underscore character stands for the 'can trigger GC' bit.
1714 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1715 if (current_bit_is_set) {
1716 flags += kDebug[s];
1717 }
1718 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1719 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1720 flags += "|";
1721 }
1722 }
Aart Bik854a02b2015-07-14 16:07:00 -07001723 }
1724 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001725 }
1726
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001727 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001728
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001729 private:
1730 static constexpr int kFieldArrayAnalysisBits = 9;
1731
1732 static constexpr int kFieldWriteOffset = 0;
1733 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1734 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1735 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1736
1737 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1738
1739 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1740 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1741 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1742 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1743
1744 static constexpr int kLastBit = kDependsOnGCBit;
1745 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1746
1747 // Aliases.
1748
1749 static_assert(kChangeBits == kDependOnBits,
1750 "the 'change' bits should match the 'depend on' bits.");
1751
1752 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1753 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1754 static constexpr uint64_t kAllWrites =
1755 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1756 static constexpr uint64_t kAllReads =
1757 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001758
Aart Bik854a02b2015-07-14 16:07:00 -07001759 // Translates type to bit flag.
1760 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1761 CHECK_NE(type, Primitive::kPrimVoid);
1762 const uint64_t one = 1;
1763 const int shift = type; // 0-based consecutive enum
1764 DCHECK_LE(kFieldWriteOffset, shift);
1765 DCHECK_LT(shift, kArrayWriteOffset);
1766 return one << (type + offset);
1767 }
1768
1769 // Private constructor on direct flags value.
1770 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1771
1772 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001773};
1774
David Brazdiled596192015-01-23 10:39:45 +00001775// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001776class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001777 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001778 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1779 size_t number_of_vregs,
1780 ArtMethod* method,
1781 uint32_t dex_pc,
1782 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001783 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001784 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001786 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001787 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001788 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 }
1790
Mingyao Yang01b47b02017-02-03 12:09:57 -08001791 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001792 : HEnvironment(arena,
1793 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001794 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001795 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001796 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001797
Vladimir Markoec32f642017-06-02 10:51:55 +01001798 void AllocateLocations() {
1799 DCHECK(locations_.empty());
1800 locations_.resize(vregs_.size());
1801 }
1802
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001804 if (parent_ != nullptr) {
1805 parent_->SetAndCopyParentChain(allocator, parent);
1806 } else {
1807 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1808 parent_->CopyFrom(parent);
1809 if (parent->GetParent() != nullptr) {
1810 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1811 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001812 }
David Brazdiled596192015-01-23 10:39:45 +00001813 }
1814
Vladimir Marko71bf8092015-09-15 15:33:14 +01001815 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001816 void CopyFrom(HEnvironment* environment);
1817
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001818 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1819 // input to the loop phi instead. This is for inserting instructions that
1820 // require an environment (like HDeoptimization) in the loop pre-header.
1821 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001822
1823 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001824 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001825 }
1826
1827 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001829 }
1830
David Brazdil1abb4192015-02-17 18:33:36 +00001831 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001832
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001834
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001835 HEnvironment* GetParent() const { return parent_; }
1836
1837 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001838 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001839 }
1840
1841 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001842 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 }
1844
1845 uint32_t GetDexPc() const {
1846 return dex_pc_;
1847 }
1848
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001849 ArtMethod* GetMethod() const {
1850 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001851 }
1852
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001853 HInstruction* GetHolder() const {
1854 return holder_;
1855 }
1856
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001857
1858 bool IsFromInlinedInvoke() const {
1859 return GetParent() != nullptr;
1860 }
1861
David Brazdiled596192015-01-23 10:39:45 +00001862 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001863 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1864 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001865 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001866 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001868
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001869 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001870 HInstruction* const holder_;
1871
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001872 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001873
1874 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1875};
1876
Vladimir Markof9f64412015-09-02 14:05:49 +01001877class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001878 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001879 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001880 : previous_(nullptr),
1881 next_(nullptr),
1882 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001883 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001884 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001885 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001886 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001887 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001888 locations_(nullptr),
1889 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001890 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001891 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001892 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1893 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1894 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001895
Dave Allison20dfc792014-06-16 20:44:29 -07001896 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001898#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001899 enum InstructionKind {
1900 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1901 };
1902#undef DECLARE_KIND
1903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904 HInstruction* GetNext() const { return next_; }
1905 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Calin Juravle77520bc2015-01-12 18:45:46 +00001907 HInstruction* GetNextDisregardingMoves() const;
1908 HInstruction* GetPreviousDisregardingMoves() const;
1909
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001911 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001913 bool IsInBlock() const { return block_ != nullptr; }
1914 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001915 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1916 bool IsIrreducibleLoopHeaderPhi() const {
1917 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1918 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001919
Vladimir Marko372f10e2016-05-17 16:30:10 +01001920 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1921
1922 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1923 // One virtual method is enough, just const_cast<> and then re-add the const.
1924 return ArrayRef<const HUserRecord<HInstruction*>>(
1925 const_cast<HInstruction*>(this)->GetInputRecords());
1926 }
1927
Vladimir Markoe9004912016-06-16 16:50:52 +01001928 HInputsRef GetInputs() {
1929 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001930 }
1931
Vladimir Markoe9004912016-06-16 16:50:52 +01001932 HConstInputsRef GetInputs() const {
1933 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001934 }
1935
1936 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001937 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938
Aart Bik2767f4b2016-10-28 15:03:53 -07001939 bool HasInput(HInstruction* input) const {
1940 for (const HInstruction* i : GetInputs()) {
1941 if (i == input) {
1942 return true;
1943 }
1944 }
1945 return false;
1946 }
1947
Vladimir Marko372f10e2016-05-17 16:30:10 +01001948 void SetRawInputAt(size_t index, HInstruction* input) {
1949 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1950 }
1951
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001952 virtual void Accept(HGraphVisitor* visitor) = 0;
1953 virtual const char* DebugName() const = 0;
1954
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001958
1959 uint32_t GetDexPc() const { return dex_pc_; }
1960
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001961 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001962
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001963 // Can the instruction throw?
1964 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1965 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001966 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001967 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001968
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001969 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001970 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001971
Calin Juravle10e244f2015-01-26 18:54:32 +00001972 // Does not apply for all instructions, but having this at top level greatly
1973 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001974 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001975 virtual bool CanBeNull() const {
1976 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1977 return true;
1978 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001979
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001980 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001981 return false;
1982 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001983
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001984 virtual bool IsActualObject() const {
1985 return GetType() == Primitive::kPrimNot;
1986 }
1987
Calin Juravle2e768302015-07-28 14:41:11 +00001988 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001989
Calin Juravle61d544b2015-02-23 16:46:57 +00001990 ReferenceTypeInfo GetReferenceTypeInfo() const {
1991 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001992 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001993 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001994 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001997 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001998 // Note: fixup_end remains valid across push_front().
1999 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2000 HUseListNode<HInstruction*>* new_node =
2001 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2002 uses_.push_front(*new_node);
2003 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004 }
2005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002006 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002007 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002008 // Note: env_fixup_end remains valid across push_front().
2009 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2010 HUseListNode<HEnvironment*>* new_node =
2011 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2012 env_uses_.push_front(*new_node);
2013 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002014 }
2015
David Brazdil1abb4192015-02-17 18:33:36 +00002016 void RemoveAsUserOfInput(size_t input) {
2017 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002018 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2019 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2020 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002021 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002022
Vladimir Marko372f10e2016-05-17 16:30:10 +01002023 void RemoveAsUserOfAllInputs() {
2024 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2025 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2026 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2027 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2028 }
2029 }
2030
David Brazdil1abb4192015-02-17 18:33:36 +00002031 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2032 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
Vladimir Marko46817b82016-03-29 12:21:58 +01002034 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2035 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2036 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002037 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002038 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002039 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002040
Aart Bikcc42be02016-10-20 16:14:16 -07002041 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002042 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002043 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002044 !CanThrow() &&
2045 !IsSuspendCheck() &&
2046 !IsControlFlow() &&
2047 !IsNativeDebugInfo() &&
2048 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002049 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002050 !IsMemoryBarrier() &&
2051 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002052 }
2053
Aart Bikcc42be02016-10-20 16:14:16 -07002054 bool IsDeadAndRemovable() const {
2055 return IsRemovable() && !HasUses();
2056 }
2057
Roland Levillain6c82d402014-10-13 16:10:27 +01002058 // Does this instruction strictly dominate `other_instruction`?
2059 // Returns false if this instruction and `other_instruction` are the same.
2060 // Aborts if this instruction and `other_instruction` are both phis.
2061 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002062
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002063 int GetId() const { return id_; }
2064 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002065
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002066 int GetSsaIndex() const { return ssa_index_; }
2067 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2068 bool HasSsaIndex() const { return ssa_index_ != -1; }
2069
2070 bool HasEnvironment() const { return environment_ != nullptr; }
2071 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002072 // Set the `environment_` field. Raw because this method does not
2073 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002074 void SetRawEnvironment(HEnvironment* environment) {
2075 DCHECK(environment_ == nullptr);
2076 DCHECK_EQ(environment->GetHolder(), this);
2077 environment_ = environment;
2078 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002079
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002080 void InsertRawEnvironment(HEnvironment* environment) {
2081 DCHECK(environment_ != nullptr);
2082 DCHECK_EQ(environment->GetHolder(), this);
2083 DCHECK(environment->GetParent() == nullptr);
2084 environment->parent_ = environment_;
2085 environment_ = environment;
2086 }
2087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002088 void RemoveEnvironment();
2089
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002090 // Set the environment of this instruction, copying it from `environment`. While
2091 // copying, the uses lists are being updated.
2092 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002093 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002094 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002095 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002096 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002097 if (environment->GetParent() != nullptr) {
2098 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2099 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002100 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002101
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002102 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2103 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002104 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002105 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002106 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002107 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002108 if (environment->GetParent() != nullptr) {
2109 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2110 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002111 }
2112
Nicolas Geoffray39468442014-09-02 15:17:15 +01002113 // Returns the number of entries in the environment. Typically, that is the
2114 // number of dex registers in a method. It could be more in case of inlining.
2115 size_t EnvironmentSize() const;
2116
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002117 LocationSummary* GetLocations() const { return locations_; }
2118 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002119
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002120 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002121 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002122 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123
Alexandre Rames188d4312015-04-09 18:30:21 +01002124 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2125 // uses of this instruction by `other` are *not* updated.
2126 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2127 ReplaceWith(other);
2128 other->ReplaceInput(this, use_index);
2129 }
2130
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002131 // Move `this` instruction before `cursor`
2132 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002133
Vladimir Markofb337ea2015-11-25 15:25:10 +00002134 // Move `this` before its first user and out of any loops. If there is no
2135 // out-of-loop user that dominates all other users, move the instruction
2136 // to the end of the out-of-loop common dominator of the user's blocks.
2137 //
2138 // This can be used only on non-throwing instructions with no side effects that
2139 // have at least one use but no environment uses.
2140 void MoveBeforeFirstUserAndOutOfLoops();
2141
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002142#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002143 bool Is##type() const; \
2144 const H##type* As##type() const; \
2145 H##type* As##type();
2146
2147 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2148#undef INSTRUCTION_TYPE_CHECK
2149
2150#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002151 bool Is##type() const { return (As##type() != nullptr); } \
2152 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002153 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002154 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155#undef INSTRUCTION_TYPE_CHECK
2156
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002157 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002158 // TODO: this method is used by LICM and GVN with possibly different
2159 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002160 virtual bool CanBeMoved() const { return false; }
2161
2162 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002163 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 return false;
2165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
2167 // Returns whether any data encoded in the two instructions is equal.
2168 // This method does not look at the inputs. Both instructions must be
2169 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002170 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002171 return false;
2172 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002173
2174 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002175 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002177 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002178
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002179 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2180 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2181 // the virtual function because the __attribute__((__pure__)) doesn't really
2182 // apply the strong requirement for virtual functions, preventing optimizations.
2183 InstructionKind GetKind() const PURE;
2184 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002185
2186 virtual size_t ComputeHashCode() const {
2187 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002188 for (const HInstruction* input : GetInputs()) {
2189 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002190 }
2191 return result;
2192 }
2193
2194 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002195 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002196 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002197
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002198 size_t GetLifetimePosition() const { return lifetime_position_; }
2199 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2200 LiveInterval* GetLiveInterval() const { return live_interval_; }
2201 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2202 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2203
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002204 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2205
2206 // Returns whether the code generation of the instruction will require to have access
2207 // to the current method. Such instructions are:
2208 // (1): Instructions that require an environment, as calling the runtime requires
2209 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002210 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2211 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002212 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002213 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002214 }
2215
Vladimir Markodc151b22015-10-15 18:02:30 +01002216 // Returns whether the code generation of the instruction will require to have access
2217 // to the dex cache of the current method's declaring class via the current method.
2218 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002219
Mark Mendellc4701932015-04-10 13:18:51 -04002220 // Does this instruction have any use in an environment before
2221 // control flow hits 'other'?
2222 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2223
2224 // Remove all references to environment uses of this instruction.
2225 // The caller must ensure that this is safe to do.
2226 void RemoveEnvironmentUsers();
2227
Vladimir Markoa1de9182016-02-25 11:37:38 +00002228 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2229 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002230
David Brazdil1abb4192015-02-17 18:33:36 +00002231 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002232 // If set, the machine code for this instruction is assumed to be generated by
2233 // its users. Used by liveness analysis to compute use positions accordingly.
2234 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2235 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2236 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2237 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2238
Vladimir Marko372f10e2016-05-17 16:30:10 +01002239 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2240 return GetInputRecords()[i];
2241 }
2242
2243 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2244 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2245 input_records[index] = input;
2246 }
David Brazdil1abb4192015-02-17 18:33:36 +00002247
Vladimir Markoa1de9182016-02-25 11:37:38 +00002248 uint32_t GetPackedFields() const {
2249 return packed_fields_;
2250 }
2251
2252 template <size_t flag>
2253 bool GetPackedFlag() const {
2254 return (packed_fields_ & (1u << flag)) != 0u;
2255 }
2256
2257 template <size_t flag>
2258 void SetPackedFlag(bool value = true) {
2259 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2260 }
2261
2262 template <typename BitFieldType>
2263 typename BitFieldType::value_type GetPackedField() const {
2264 return BitFieldType::Decode(packed_fields_);
2265 }
2266
2267 template <typename BitFieldType>
2268 void SetPackedField(typename BitFieldType::value_type value) {
2269 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2270 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2271 }
2272
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002273 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002274 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2275 auto before_use_node = uses_.before_begin();
2276 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2277 HInstruction* user = use_node->GetUser();
2278 size_t input_index = use_node->GetIndex();
2279 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2280 before_use_node = use_node;
2281 }
2282 }
2283
2284 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2285 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2286 if (next != uses_.end()) {
2287 HInstruction* next_user = next->GetUser();
2288 size_t next_index = next->GetIndex();
2289 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2290 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2291 }
2292 }
2293
2294 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2295 auto before_env_use_node = env_uses_.before_begin();
2296 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2297 HEnvironment* user = env_use_node->GetUser();
2298 size_t input_index = env_use_node->GetIndex();
2299 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2300 before_env_use_node = env_use_node;
2301 }
2302 }
2303
2304 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2305 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2306 if (next != env_uses_.end()) {
2307 HEnvironment* next_user = next->GetUser();
2308 size_t next_index = next->GetIndex();
2309 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2310 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2311 }
2312 }
David Brazdil1abb4192015-02-17 18:33:36 +00002313
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002314 HInstruction* previous_;
2315 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002316 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002317 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002319 // An instruction gets an id when it is added to the graph.
2320 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002321 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 int id_;
2323
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002324 // When doing liveness analysis, instructions that have uses get an SSA index.
2325 int ssa_index_;
2326
Vladimir Markoa1de9182016-02-25 11:37:38 +00002327 // Packed fields.
2328 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002329
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002330 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002331 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002332
2333 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002334 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335
Nicolas Geoffray39468442014-09-02 15:17:15 +01002336 // The environment associated with this instruction. Not null if the instruction
2337 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002338 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002339
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002340 // Set by the code generator.
2341 LocationSummary* locations_;
2342
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002343 // Set by the liveness analysis.
2344 LiveInterval* live_interval_;
2345
2346 // Set by the liveness analysis, this is the position in a linear
2347 // order of blocks where this instruction's live interval start.
2348 size_t lifetime_position_;
2349
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002350 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002351
Vladimir Markoa1de9182016-02-25 11:37:38 +00002352 // The reference handle part of the reference type info.
2353 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002354 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002356
David Brazdil1abb4192015-02-17 18:33:36 +00002357 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002359 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002360 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002361 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362
2363 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2364};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002365std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002367// Iterates over the instructions, while preserving the next instruction
2368// in case the current instruction gets removed from the list by the user
2369// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370class HInstructionIterator : public ValueObject {
2371 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002372 explicit HInstructionIterator(const HInstructionList& instructions)
2373 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002374 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002375 }
2376
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002377 bool Done() const { return instruction_ == nullptr; }
2378 HInstruction* Current() const { return instruction_; }
2379 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002380 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002381 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382 }
2383
2384 private:
2385 HInstruction* instruction_;
2386 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002387
2388 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389};
2390
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002391// Iterates over the instructions without saving the next instruction,
2392// therefore handling changes in the graph potentially made by the user
2393// of this iterator.
2394class HInstructionIteratorHandleChanges : public ValueObject {
2395 public:
2396 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2397 : instruction_(instructions.first_instruction_) {
2398 }
2399
2400 bool Done() const { return instruction_ == nullptr; }
2401 HInstruction* Current() const { return instruction_; }
2402 void Advance() {
2403 instruction_ = instruction_->GetNext();
2404 }
2405
2406 private:
2407 HInstruction* instruction_;
2408
2409 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2410};
2411
2412
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002413class HBackwardInstructionIterator : public ValueObject {
2414 public:
2415 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2416 : instruction_(instructions.last_instruction_) {
2417 next_ = Done() ? nullptr : instruction_->GetPrevious();
2418 }
2419
2420 bool Done() const { return instruction_ == nullptr; }
2421 HInstruction* Current() const { return instruction_; }
2422 void Advance() {
2423 instruction_ = next_;
2424 next_ = Done() ? nullptr : instruction_->GetPrevious();
2425 }
2426
2427 private:
2428 HInstruction* instruction_;
2429 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002430
2431 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002432};
2433
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002434class HVariableInputSizeInstruction : public HInstruction {
2435 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002436 using HInstruction::GetInputRecords; // Keep the const version visible.
2437 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2438 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2439 }
2440
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002441 void AddInput(HInstruction* input);
2442 void InsertInputAt(size_t index, HInstruction* input);
2443 void RemoveInputAt(size_t index);
2444
Igor Murashkind01745e2017-04-05 16:40:31 -07002445 // Removes all the inputs.
2446 // Also removes this instructions from each input's use list
2447 // (for non-environment uses only).
2448 void RemoveAllInputs();
2449
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002450 protected:
2451 HVariableInputSizeInstruction(SideEffects side_effects,
2452 uint32_t dex_pc,
2453 ArenaAllocator* arena,
2454 size_t number_of_inputs,
2455 ArenaAllocKind kind)
2456 : HInstruction(side_effects, dex_pc),
2457 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2458
2459 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2460
2461 private:
2462 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2463};
2464
Vladimir Markof9f64412015-09-02 14:05:49 +01002465template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466class HTemplateInstruction: public HInstruction {
2467 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002468 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002469 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002470 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471
Vladimir Marko372f10e2016-05-17 16:30:10 +01002472 using HInstruction::GetInputRecords; // Keep the const version visible.
2473 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2474 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002475 }
2476
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002477 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002478 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002479
2480 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481};
2482
Vladimir Markof9f64412015-09-02 14:05:49 +01002483// HTemplateInstruction specialization for N=0.
2484template<>
2485class HTemplateInstruction<0>: public HInstruction {
2486 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002487 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002488 : HInstruction(side_effects, dex_pc) {}
2489
Vladimir Markof9f64412015-09-02 14:05:49 +01002490 virtual ~HTemplateInstruction() {}
2491
Vladimir Marko372f10e2016-05-17 16:30:10 +01002492 using HInstruction::GetInputRecords; // Keep the const version visible.
2493 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2494 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002495 }
2496
2497 private:
2498 friend class SsaBuilder;
2499};
2500
Dave Allison20dfc792014-06-16 20:44:29 -07002501template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002502class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002503 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002504 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002505 : HTemplateInstruction<N>(side_effects, dex_pc) {
2506 this->template SetPackedField<TypeField>(type);
2507 }
Dave Allison20dfc792014-06-16 20:44:29 -07002508 virtual ~HExpression() {}
2509
Vladimir Markoa1de9182016-02-25 11:37:38 +00002510 Primitive::Type GetType() const OVERRIDE {
2511 return TypeField::Decode(this->GetPackedFields());
2512 }
Dave Allison20dfc792014-06-16 20:44:29 -07002513
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002514 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002515 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2516 static constexpr size_t kFieldTypeSize =
2517 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2518 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2519 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2520 "Too many packed fields.");
2521 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002522};
2523
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002524// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2525// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002526class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2529 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002533 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2537};
2538
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539// Represents dex's RETURN opcodes. A HReturn is a control flow
2540// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002541class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002543 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2544 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002545 SetRawInputAt(0, value);
2546 }
2547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002548 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002549
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002550 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002551
2552 private:
2553 DISALLOW_COPY_AND_ASSIGN(HReturn);
2554};
2555
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002556class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002557 public:
2558 HPhi(ArenaAllocator* arena,
2559 uint32_t reg_number,
2560 size_t number_of_inputs,
2561 Primitive::Type type,
2562 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002563 : HVariableInputSizeInstruction(
2564 SideEffects::None(),
2565 dex_pc,
2566 arena,
2567 number_of_inputs,
2568 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002569 reg_number_(reg_number) {
2570 SetPackedField<TypeField>(ToPhiType(type));
2571 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2572 // Phis are constructed live and marked dead if conflicting or unused.
2573 // Individual steps of SsaBuilder should assume that if a phi has been
2574 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2575 SetPackedFlag<kFlagIsLive>(true);
2576 SetPackedFlag<kFlagCanBeNull>(true);
2577 }
2578
2579 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2580 static Primitive::Type ToPhiType(Primitive::Type type) {
2581 return Primitive::PrimitiveKind(type);
2582 }
2583
2584 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2585
David Brazdildee58d62016-04-07 09:54:26 +00002586 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2587 void SetType(Primitive::Type new_type) {
2588 // Make sure that only valid type changes occur. The following are allowed:
2589 // (1) int -> float/ref (primitive type propagation),
2590 // (2) long -> double (primitive type propagation).
2591 DCHECK(GetType() == new_type ||
2592 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2593 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2594 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2595 SetPackedField<TypeField>(new_type);
2596 }
2597
2598 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2599 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2600
2601 uint32_t GetRegNumber() const { return reg_number_; }
2602
2603 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2604 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2605 bool IsDead() const { return !IsLive(); }
2606 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2607
Vladimir Markoe9004912016-06-16 16:50:52 +01002608 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002609 return other != nullptr
2610 && other->IsPhi()
2611 && other->AsPhi()->GetBlock() == GetBlock()
2612 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2613 }
2614
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002615 bool HasEquivalentPhi() const {
2616 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2617 return true;
2618 }
2619 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2620 return true;
2621 }
2622 return false;
2623 }
2624
David Brazdildee58d62016-04-07 09:54:26 +00002625 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2626 // An equivalent phi is a phi having the same dex register and type.
2627 // It assumes that phis with the same dex register are adjacent.
2628 HPhi* GetNextEquivalentPhiWithSameType() {
2629 HInstruction* next = GetNext();
2630 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2631 if (next->GetType() == GetType()) {
2632 return next->AsPhi();
2633 }
2634 next = next->GetNext();
2635 }
2636 return nullptr;
2637 }
2638
2639 DECLARE_INSTRUCTION(Phi);
2640
David Brazdildee58d62016-04-07 09:54:26 +00002641 private:
2642 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2643 static constexpr size_t kFieldTypeSize =
2644 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2645 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2646 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2647 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2648 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2649 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2650
David Brazdildee58d62016-04-07 09:54:26 +00002651 const uint32_t reg_number_;
2652
2653 DISALLOW_COPY_AND_ASSIGN(HPhi);
2654};
2655
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002657// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002659class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002661 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002662
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002663 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002664
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002665 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002666
2667 private:
2668 DISALLOW_COPY_AND_ASSIGN(HExit);
2669};
2670
2671// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002672class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002673 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002674 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002675
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002676 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002678 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002679 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002680 }
2681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002682 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002683
2684 private:
2685 DISALLOW_COPY_AND_ASSIGN(HGoto);
2686};
2687
Roland Levillain9867bc72015-08-05 10:21:34 +01002688class HConstant : public HExpression<0> {
2689 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002690 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2691 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002692
2693 bool CanBeMoved() const OVERRIDE { return true; }
2694
Roland Levillain1a653882016-03-18 18:05:57 +00002695 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002697 // Is this constant 0 in the arithmetic sense?
2698 virtual bool IsArithmeticZero() const { return false; }
2699 // Is this constant a 0-bit pattern?
2700 virtual bool IsZeroBitPattern() const { return false; }
2701 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 virtual bool IsOne() const { return false; }
2703
David Brazdil77a48ae2015-09-15 12:34:04 +00002704 virtual uint64_t GetValueAsUint64() const = 0;
2705
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002706 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002707
2708 private:
2709 DISALLOW_COPY_AND_ASSIGN(HConstant);
2710};
2711
Vladimir Markofcb503c2016-05-18 12:48:17 +01002712class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002713 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002714 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 return true;
2716 }
2717
David Brazdil77a48ae2015-09-15 12:34:04 +00002718 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2719
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 size_t ComputeHashCode() const OVERRIDE { return 0; }
2721
Roland Levillain1a653882016-03-18 18:05:57 +00002722 // The null constant representation is a 0-bit pattern.
2723 virtual bool IsZeroBitPattern() const { return true; }
2724
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 DECLARE_INSTRUCTION(NullConstant);
2726
2727 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002728 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002729
2730 friend class HGraph;
2731 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2732};
2733
2734// Constants of the type int. Those can be from Dex instructions, or
2735// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002736class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002737 public:
2738 int32_t GetValue() const { return value_; }
2739
David Brazdil9f389d42015-10-01 14:32:56 +01002740 uint64_t GetValueAsUint64() const OVERRIDE {
2741 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2742 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002743
Vladimir Marko372f10e2016-05-17 16:30:10 +01002744 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002745 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002746 return other->AsIntConstant()->value_ == value_;
2747 }
2748
2749 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2750
2751 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002752 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2753 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002754 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2755
Roland Levillain1a653882016-03-18 18:05:57 +00002756 // Integer constants are used to encode Boolean values as well,
2757 // where 1 means true and 0 means false.
2758 bool IsTrue() const { return GetValue() == 1; }
2759 bool IsFalse() const { return GetValue() == 0; }
2760
Roland Levillain9867bc72015-08-05 10:21:34 +01002761 DECLARE_INSTRUCTION(IntConstant);
2762
2763 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002764 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2765 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2766 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2767 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002768
2769 const int32_t value_;
2770
2771 friend class HGraph;
2772 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2773 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2774 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2775};
2776
Vladimir Markofcb503c2016-05-18 12:48:17 +01002777class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002778 public:
2779 int64_t GetValue() const { return value_; }
2780
David Brazdil77a48ae2015-09-15 12:34:04 +00002781 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2782
Vladimir Marko372f10e2016-05-17 16:30:10 +01002783 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002784 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 return other->AsLongConstant()->value_ == value_;
2786 }
2787
2788 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2789
2790 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002791 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2792 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002793 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2794
2795 DECLARE_INSTRUCTION(LongConstant);
2796
2797 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002798 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2799 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002800
2801 const int64_t value_;
2802
2803 friend class HGraph;
2804 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2805};
Dave Allison20dfc792014-06-16 20:44:29 -07002806
Vladimir Markofcb503c2016-05-18 12:48:17 +01002807class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002808 public:
2809 float GetValue() const { return value_; }
2810
2811 uint64_t GetValueAsUint64() const OVERRIDE {
2812 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2813 }
2814
Vladimir Marko372f10e2016-05-17 16:30:10 +01002815 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002816 DCHECK(other->IsFloatConstant()) << other->DebugName();
2817 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2818 }
2819
2820 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2821
2822 bool IsMinusOne() const OVERRIDE {
2823 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2824 }
Roland Levillain1a653882016-03-18 18:05:57 +00002825 bool IsArithmeticZero() const OVERRIDE {
2826 return std::fpclassify(value_) == FP_ZERO;
2827 }
2828 bool IsArithmeticPositiveZero() const {
2829 return IsArithmeticZero() && !std::signbit(value_);
2830 }
2831 bool IsArithmeticNegativeZero() const {
2832 return IsArithmeticZero() && std::signbit(value_);
2833 }
2834 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002835 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002836 }
2837 bool IsOne() const OVERRIDE {
2838 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2839 }
2840 bool IsNaN() const {
2841 return std::isnan(value_);
2842 }
2843
2844 DECLARE_INSTRUCTION(FloatConstant);
2845
2846 private:
2847 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2848 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2849 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2850 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2851
2852 const float value_;
2853
2854 // Only the SsaBuilder and HGraph can create floating-point constants.
2855 friend class SsaBuilder;
2856 friend class HGraph;
2857 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2858};
2859
Vladimir Markofcb503c2016-05-18 12:48:17 +01002860class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002861 public:
2862 double GetValue() const { return value_; }
2863
2864 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2865
Vladimir Marko372f10e2016-05-17 16:30:10 +01002866 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002867 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2868 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2869 }
2870
2871 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2872
2873 bool IsMinusOne() const OVERRIDE {
2874 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2875 }
Roland Levillain1a653882016-03-18 18:05:57 +00002876 bool IsArithmeticZero() const OVERRIDE {
2877 return std::fpclassify(value_) == FP_ZERO;
2878 }
2879 bool IsArithmeticPositiveZero() const {
2880 return IsArithmeticZero() && !std::signbit(value_);
2881 }
2882 bool IsArithmeticNegativeZero() const {
2883 return IsArithmeticZero() && std::signbit(value_);
2884 }
2885 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002886 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002887 }
2888 bool IsOne() const OVERRIDE {
2889 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2890 }
2891 bool IsNaN() const {
2892 return std::isnan(value_);
2893 }
2894
2895 DECLARE_INSTRUCTION(DoubleConstant);
2896
2897 private:
2898 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2899 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2900 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2901 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2902
2903 const double value_;
2904
2905 // Only the SsaBuilder and HGraph can create floating-point constants.
2906 friend class SsaBuilder;
2907 friend class HGraph;
2908 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2909};
2910
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002911// Conditional branch. A block ending with an HIf instruction must have
2912// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002913class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002914 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002915 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2916 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002917 SetRawInputAt(0, input);
2918 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002919
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002920 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002921
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002922 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002923 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002924 }
2925
2926 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002927 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002928 }
2929
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002930 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002931
2932 private:
2933 DISALLOW_COPY_AND_ASSIGN(HIf);
2934};
2935
David Brazdilfc6a86a2015-06-26 10:33:45 +00002936
2937// Abstract instruction which marks the beginning and/or end of a try block and
2938// links it to the respective exception handlers. Behaves the same as a Goto in
2939// non-exceptional control flow.
2940// Normal-flow successor is stored at index zero, exception handlers under
2941// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002942class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002943 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002944 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002945 kEntry,
2946 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002947 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002948 };
2949
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002950 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002951 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2952 SetPackedField<BoundaryKindField>(kind);
2953 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002954
2955 bool IsControlFlow() const OVERRIDE { return true; }
2956
2957 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002958 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002959
David Brazdild26a4112015-11-10 11:07:31 +00002960 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2961 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2962 }
2963
David Brazdilfc6a86a2015-06-26 10:33:45 +00002964 // Returns whether `handler` is among its exception handlers (non-zero index
2965 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002966 bool HasExceptionHandler(const HBasicBlock& handler) const {
2967 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002968 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002969 }
2970
2971 // If not present already, adds `handler` to its block's list of exception
2972 // handlers.
2973 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002974 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002975 GetBlock()->AddSuccessor(handler);
2976 }
2977 }
2978
Vladimir Markoa1de9182016-02-25 11:37:38 +00002979 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2980 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002981
David Brazdilffee3d32015-07-06 11:48:53 +01002982 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2983
David Brazdilfc6a86a2015-06-26 10:33:45 +00002984 DECLARE_INSTRUCTION(TryBoundary);
2985
2986 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002987 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2988 static constexpr size_t kFieldBoundaryKindSize =
2989 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2990 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2991 kFieldBoundaryKind + kFieldBoundaryKindSize;
2992 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2993 "Too many packed fields.");
2994 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002995
2996 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2997};
2998
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002999// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003000class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003001 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003002 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3003 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003004 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003005 : HVariableInputSizeInstruction(
3006 SideEffects::All(),
3007 dex_pc,
3008 arena,
3009 /* number_of_inputs */ 1,
3010 kArenaAllocMisc) {
3011 SetPackedFlag<kFieldCanBeMoved>(false);
3012 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003013 SetRawInputAt(0, cond);
3014 }
3015
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003016 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3017 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3018 // instead of `guard`.
3019 // We set CanTriggerGC to prevent any intermediate address to be live
3020 // at the point of the `HDeoptimize`.
3021 HDeoptimize(ArenaAllocator* arena,
3022 HInstruction* cond,
3023 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003024 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003025 uint32_t dex_pc)
3026 : HVariableInputSizeInstruction(
3027 SideEffects::CanTriggerGC(),
3028 dex_pc,
3029 arena,
3030 /* number_of_inputs */ 2,
3031 kArenaAllocMisc) {
3032 SetPackedFlag<kFieldCanBeMoved>(true);
3033 SetPackedField<DeoptimizeKindField>(kind);
3034 SetRawInputAt(0, cond);
3035 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003036 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003037
3038 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3039
3040 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3041 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3042 }
3043
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003044 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003045
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003046 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003047
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003048 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003049
3050 Primitive::Type GetType() const OVERRIDE {
3051 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3052 }
3053
3054 bool GuardsAnInput() const {
3055 return InputCount() == 2;
3056 }
3057
3058 HInstruction* GuardedInput() const {
3059 DCHECK(GuardsAnInput());
3060 return InputAt(1);
3061 }
3062
3063 void RemoveGuard() {
3064 RemoveInputAt(1);
3065 }
3066
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003067 DECLARE_INSTRUCTION(Deoptimize);
3068
3069 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003070 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3071 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3072 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003073 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003074 static constexpr size_t kNumberOfDeoptimizePackedBits =
3075 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3076 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3077 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003078 using DeoptimizeKindField =
3079 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003080
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003081 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3082};
3083
Mingyao Yang063fc772016-08-02 11:02:54 -07003084// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3085// The compiled code checks this flag value in a guard before devirtualized call and
3086// if it's true, starts to do deoptimization.
3087// It has a 4-byte slot on stack.
3088// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003089class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003090 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003091 // CHA guards are only optimized in a separate pass and it has no side effects
3092 // with regard to other passes.
3093 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3094 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003095 }
3096
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003097 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3098
3099 // We do all CHA guard elimination/motion in a single pass, after which there is no
3100 // further guard elimination/motion since a guard might have been used for justification
3101 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3102 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003103 bool CanBeMoved() const OVERRIDE { return false; }
3104
3105 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3106
3107 private:
3108 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3109};
3110
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003111// Represents the ArtMethod that was passed as a first argument to
3112// the method. It is used by instructions that depend on it, like
3113// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003114class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003116 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3117 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003118
3119 DECLARE_INSTRUCTION(CurrentMethod);
3120
3121 private:
3122 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3123};
3124
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003125// Fetches an ArtMethod from the virtual table or the interface method table
3126// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003127class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003128 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003130 kVTable,
3131 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003132 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003133 };
3134 HClassTableGet(HInstruction* cls,
3135 Primitive::Type type,
3136 TableKind kind,
3137 size_t index,
3138 uint32_t dex_pc)
3139 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003140 index_(index) {
3141 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003142 SetRawInputAt(0, cls);
3143 }
3144
3145 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003146 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003147 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003148 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003149 }
3150
Vladimir Markoa1de9182016-02-25 11:37:38 +00003151 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003152 size_t GetIndex() const { return index_; }
3153
3154 DECLARE_INSTRUCTION(ClassTableGet);
3155
3156 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003157 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3158 static constexpr size_t kFieldTableKindSize =
3159 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3160 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3161 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3162 "Too many packed fields.");
3163 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3164
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003165 // The index of the ArtMethod in the table.
3166 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003167
3168 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3169};
3170
Mark Mendellfe57faa2015-09-18 09:26:15 -04003171// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3172// have one successor for each entry in the switch table, and the final successor
3173// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003174class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003175 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003176 HPackedSwitch(int32_t start_value,
3177 uint32_t num_entries,
3178 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003179 uint32_t dex_pc = kNoDexPc)
3180 : HTemplateInstruction(SideEffects::None(), dex_pc),
3181 start_value_(start_value),
3182 num_entries_(num_entries) {
3183 SetRawInputAt(0, input);
3184 }
3185
3186 bool IsControlFlow() const OVERRIDE { return true; }
3187
3188 int32_t GetStartValue() const { return start_value_; }
3189
Vladimir Marko211c2112015-09-24 16:52:33 +01003190 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003191
3192 HBasicBlock* GetDefaultBlock() const {
3193 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003194 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003195 }
3196 DECLARE_INSTRUCTION(PackedSwitch);
3197
3198 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003199 const int32_t start_value_;
3200 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003201
3202 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3203};
3204
Roland Levillain88cb1752014-10-20 16:36:47 +01003205class HUnaryOperation : public HExpression<1> {
3206 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003207 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3208 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003209 SetRawInputAt(0, input);
3210 }
3211
3212 HInstruction* GetInput() const { return InputAt(0); }
3213 Primitive::Type GetResultType() const { return GetType(); }
3214
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003215 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003216 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003217 return true;
3218 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003219
Roland Levillain31dd3d62016-02-16 12:21:02 +00003220 // Try to statically evaluate `this` and return a HConstant
3221 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003222 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003223 HConstant* TryStaticEvaluation() const;
3224
3225 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003226 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3227 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003228 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3229 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003230
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003231 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003232
3233 private:
3234 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3235};
3236
Dave Allison20dfc792014-06-16 20:44:29 -07003237class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003238 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003239 HBinaryOperation(Primitive::Type result_type,
3240 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003241 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003242 SideEffects side_effects = SideEffects::None(),
3243 uint32_t dex_pc = kNoDexPc)
3244 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003245 SetRawInputAt(0, left);
3246 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003247 }
3248
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003249 HInstruction* GetLeft() const { return InputAt(0); }
3250 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003251 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003252
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003253 virtual bool IsCommutative() const { return false; }
3254
3255 // Put constant on the right.
3256 // Returns whether order is changed.
3257 bool OrderInputsWithConstantOnTheRight() {
3258 HInstruction* left = InputAt(0);
3259 HInstruction* right = InputAt(1);
3260 if (left->IsConstant() && !right->IsConstant()) {
3261 ReplaceInput(right, 0);
3262 ReplaceInput(left, 1);
3263 return true;
3264 }
3265 return false;
3266 }
3267
3268 // Order inputs by instruction id, but favor constant on the right side.
3269 // This helps GVN for commutative ops.
3270 void OrderInputs() {
3271 DCHECK(IsCommutative());
3272 HInstruction* left = InputAt(0);
3273 HInstruction* right = InputAt(1);
3274 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3275 return;
3276 }
3277 if (OrderInputsWithConstantOnTheRight()) {
3278 return;
3279 }
3280 // Order according to instruction id.
3281 if (left->GetId() > right->GetId()) {
3282 ReplaceInput(right, 0);
3283 ReplaceInput(left, 1);
3284 }
3285 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003286
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003287 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003288 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003289 return true;
3290 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003291
Roland Levillain31dd3d62016-02-16 12:21:02 +00003292 // Try to statically evaluate `this` and return a HConstant
3293 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003294 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003295 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003296
3297 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003298 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3299 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003300 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3301 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003302 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3304 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003305 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3306 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003307 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3308 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003309 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003310 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3311 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003312
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003313 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003314 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003315 HConstant* GetConstantRight() const;
3316
3317 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003318 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003319 HInstruction* GetLeastConstantLeft() const;
3320
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003321 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003322
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003323 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003324 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3325};
3326
Mark Mendellc4701932015-04-10 13:18:51 -04003327// The comparison bias applies for floating point operations and indicates how NaN
3328// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003329enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003330 kNoBias, // bias is not applicable (i.e. for long operation)
3331 kGtBias, // return 1 for NaN comparisons
3332 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003333 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003334};
3335
Roland Levillain31dd3d62016-02-16 12:21:02 +00003336std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3337
Dave Allison20dfc792014-06-16 20:44:29 -07003338class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003339 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003340 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003341 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3342 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3343 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003344
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003345 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003346 // `instruction`, and disregard moves in between.
3347 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003348
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003349 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003350
3351 virtual IfCondition GetCondition() const = 0;
3352
Mark Mendellc4701932015-04-10 13:18:51 -04003353 virtual IfCondition GetOppositeCondition() const = 0;
3354
Vladimir Markoa1de9182016-02-25 11:37:38 +00003355 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003356 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3357
Vladimir Markoa1de9182016-02-25 11:37:38 +00003358 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3359 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003360
Vladimir Marko372f10e2016-05-17 16:30:10 +01003361 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003362 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003363 }
3364
Roland Levillain4fa13f62015-07-06 18:11:54 +01003365 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003366 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003367 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003368 if (if_cond == kCondNE) {
3369 return true;
3370 } else if (if_cond == kCondEQ) {
3371 return false;
3372 }
3373 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003374 }
3375
3376 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003377 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003378 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003379 if (if_cond == kCondEQ) {
3380 return true;
3381 } else if (if_cond == kCondNE) {
3382 return false;
3383 }
3384 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003385 }
3386
Roland Levillain31dd3d62016-02-16 12:21:02 +00003387 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003388 // Needed if we merge a HCompare into a HCondition.
3389 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3390 static constexpr size_t kFieldComparisonBiasSize =
3391 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3392 static constexpr size_t kNumberOfConditionPackedBits =
3393 kFieldComparisonBias + kFieldComparisonBiasSize;
3394 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3395 using ComparisonBiasField =
3396 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3397
Roland Levillain31dd3d62016-02-16 12:21:02 +00003398 template <typename T>
3399 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3400
3401 template <typename T>
3402 int32_t CompareFP(T x, T y) const {
3403 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3404 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3405 // Handle the bias.
3406 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3407 }
3408
3409 // Return an integer constant containing the result of a condition evaluated at compile time.
3410 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3411 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3412 }
3413
Dave Allison20dfc792014-06-16 20:44:29 -07003414 private:
3415 DISALLOW_COPY_AND_ASSIGN(HCondition);
3416};
3417
3418// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003419class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003420 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003421 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3422 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003423
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003424 bool IsCommutative() const OVERRIDE { return true; }
3425
Vladimir Marko9e23df52015-11-10 17:14:35 +00003426 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3427 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003428 return MakeConstantCondition(true, GetDexPc());
3429 }
3430 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3431 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3432 }
3433 // In the following Evaluate methods, a HCompare instruction has
3434 // been merged into this HEqual instruction; evaluate it as
3435 // `Compare(x, y) == 0`.
3436 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3437 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3438 GetDexPc());
3439 }
3440 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3441 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3442 }
3443 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3444 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003445 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003446
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003447 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003448
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003449 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003450 return kCondEQ;
3451 }
3452
Mark Mendellc4701932015-04-10 13:18:51 -04003453 IfCondition GetOppositeCondition() const OVERRIDE {
3454 return kCondNE;
3455 }
3456
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003457 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003458 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003459
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003460 DISALLOW_COPY_AND_ASSIGN(HEqual);
3461};
3462
Vladimir Markofcb503c2016-05-18 12:48:17 +01003463class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003464 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003465 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3466 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003467
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003468 bool IsCommutative() const OVERRIDE { return true; }
3469
Vladimir Marko9e23df52015-11-10 17:14:35 +00003470 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3471 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 return MakeConstantCondition(false, GetDexPc());
3473 }
3474 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3475 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3476 }
3477 // In the following Evaluate methods, a HCompare instruction has
3478 // been merged into this HNotEqual instruction; evaluate it as
3479 // `Compare(x, y) != 0`.
3480 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3481 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3482 }
3483 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3484 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3485 }
3486 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3487 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003488 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003489
Dave Allison20dfc792014-06-16 20:44:29 -07003490 DECLARE_INSTRUCTION(NotEqual);
3491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003492 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003493 return kCondNE;
3494 }
3495
Mark Mendellc4701932015-04-10 13:18:51 -04003496 IfCondition GetOppositeCondition() const OVERRIDE {
3497 return kCondEQ;
3498 }
3499
Dave Allison20dfc792014-06-16 20:44:29 -07003500 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003501 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003502
Dave Allison20dfc792014-06-16 20:44:29 -07003503 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3504};
3505
Vladimir Markofcb503c2016-05-18 12:48:17 +01003506class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003507 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003508 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3509 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003510
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003513 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 // In the following Evaluate methods, a HCompare instruction has
3515 // been merged into this HLessThan instruction; evaluate it as
3516 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003517 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003518 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3519 }
3520 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3521 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3522 }
3523 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3524 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003525 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003526
Dave Allison20dfc792014-06-16 20:44:29 -07003527 DECLARE_INSTRUCTION(LessThan);
3528
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003529 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003530 return kCondLT;
3531 }
3532
Mark Mendellc4701932015-04-10 13:18:51 -04003533 IfCondition GetOppositeCondition() const OVERRIDE {
3534 return kCondGE;
3535 }
3536
Dave Allison20dfc792014-06-16 20:44:29 -07003537 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003538 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003539
Dave Allison20dfc792014-06-16 20:44:29 -07003540 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3541};
3542
Vladimir Markofcb503c2016-05-18 12:48:17 +01003543class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003545 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3546 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003547
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003550 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003551 // In the following Evaluate methods, a HCompare instruction has
3552 // been merged into this HLessThanOrEqual instruction; evaluate it as
3553 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003554 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3556 }
3557 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3558 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3559 }
3560 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3561 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003562 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003563
Dave Allison20dfc792014-06-16 20:44:29 -07003564 DECLARE_INSTRUCTION(LessThanOrEqual);
3565
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003566 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003567 return kCondLE;
3568 }
3569
Mark Mendellc4701932015-04-10 13:18:51 -04003570 IfCondition GetOppositeCondition() const OVERRIDE {
3571 return kCondGT;
3572 }
3573
Dave Allison20dfc792014-06-16 20:44:29 -07003574 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003575 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003576
Dave Allison20dfc792014-06-16 20:44:29 -07003577 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3578};
3579
Vladimir Markofcb503c2016-05-18 12:48:17 +01003580class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003582 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3583 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003584
Roland Levillain9867bc72015-08-05 10:21:34 +01003585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003587 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 // In the following Evaluate methods, a HCompare instruction has
3589 // been merged into this HGreaterThan instruction; evaluate it as
3590 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003591 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3593 }
3594 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3595 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3596 }
3597 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3598 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003599 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003600
Dave Allison20dfc792014-06-16 20:44:29 -07003601 DECLARE_INSTRUCTION(GreaterThan);
3602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003603 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003604 return kCondGT;
3605 }
3606
Mark Mendellc4701932015-04-10 13:18:51 -04003607 IfCondition GetOppositeCondition() const OVERRIDE {
3608 return kCondLE;
3609 }
3610
Dave Allison20dfc792014-06-16 20:44:29 -07003611 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003612 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003613
Dave Allison20dfc792014-06-16 20:44:29 -07003614 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3615};
3616
Vladimir Markofcb503c2016-05-18 12:48:17 +01003617class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003618 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003619 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3620 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003621
Roland Levillain9867bc72015-08-05 10:21:34 +01003622 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003623 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003624 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625 // In the following Evaluate methods, a HCompare instruction has
3626 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3627 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003628 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003629 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3630 }
3631 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3632 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3633 }
3634 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3635 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003636 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003637
Dave Allison20dfc792014-06-16 20:44:29 -07003638 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003640 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003641 return kCondGE;
3642 }
3643
Mark Mendellc4701932015-04-10 13:18:51 -04003644 IfCondition GetOppositeCondition() const OVERRIDE {
3645 return kCondLT;
3646 }
3647
Dave Allison20dfc792014-06-16 20:44:29 -07003648 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003649 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003650
Dave Allison20dfc792014-06-16 20:44:29 -07003651 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3652};
3653
Vladimir Markofcb503c2016-05-18 12:48:17 +01003654class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003655 public:
3656 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3657 : HCondition(first, second, dex_pc) {}
3658
3659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003660 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003661 }
3662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3664 }
3665 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3666 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3667 LOG(FATAL) << DebugName() << " is not defined for float values";
3668 UNREACHABLE();
3669 }
3670 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3671 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3672 LOG(FATAL) << DebugName() << " is not defined for double values";
3673 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003674 }
3675
3676 DECLARE_INSTRUCTION(Below);
3677
3678 IfCondition GetCondition() const OVERRIDE {
3679 return kCondB;
3680 }
3681
3682 IfCondition GetOppositeCondition() const OVERRIDE {
3683 return kCondAE;
3684 }
3685
3686 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003687 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 return MakeUnsigned(x) < MakeUnsigned(y);
3689 }
Aart Bike9f37602015-10-09 11:15:55 -07003690
3691 DISALLOW_COPY_AND_ASSIGN(HBelow);
3692};
3693
Vladimir Markofcb503c2016-05-18 12:48:17 +01003694class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003695 public:
3696 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3697 : HCondition(first, second, dex_pc) {}
3698
3699 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003700 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003701 }
3702 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003703 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3704 }
3705 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3706 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3707 LOG(FATAL) << DebugName() << " is not defined for float values";
3708 UNREACHABLE();
3709 }
3710 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3711 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3712 LOG(FATAL) << DebugName() << " is not defined for double values";
3713 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003714 }
3715
3716 DECLARE_INSTRUCTION(BelowOrEqual);
3717
3718 IfCondition GetCondition() const OVERRIDE {
3719 return kCondBE;
3720 }
3721
3722 IfCondition GetOppositeCondition() const OVERRIDE {
3723 return kCondA;
3724 }
3725
3726 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003727 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeUnsigned(x) <= MakeUnsigned(y);
3729 }
Aart Bike9f37602015-10-09 11:15:55 -07003730
3731 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3732};
3733
Vladimir Markofcb503c2016-05-18 12:48:17 +01003734class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003735 public:
3736 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3737 : HCondition(first, second, dex_pc) {}
3738
3739 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003741 }
3742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3744 }
3745 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3746 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3747 LOG(FATAL) << DebugName() << " is not defined for float values";
3748 UNREACHABLE();
3749 }
3750 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3751 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3752 LOG(FATAL) << DebugName() << " is not defined for double values";
3753 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003754 }
3755
3756 DECLARE_INSTRUCTION(Above);
3757
3758 IfCondition GetCondition() const OVERRIDE {
3759 return kCondA;
3760 }
3761
3762 IfCondition GetOppositeCondition() const OVERRIDE {
3763 return kCondBE;
3764 }
3765
3766 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003767 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeUnsigned(x) > MakeUnsigned(y);
3769 }
Aart Bike9f37602015-10-09 11:15:55 -07003770
3771 DISALLOW_COPY_AND_ASSIGN(HAbove);
3772};
3773
Vladimir Markofcb503c2016-05-18 12:48:17 +01003774class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003775 public:
3776 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3777 : HCondition(first, second, dex_pc) {}
3778
3779 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003781 }
3782 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3784 }
3785 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3786 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3787 LOG(FATAL) << DebugName() << " is not defined for float values";
3788 UNREACHABLE();
3789 }
3790 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3791 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3792 LOG(FATAL) << DebugName() << " is not defined for double values";
3793 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003794 }
3795
3796 DECLARE_INSTRUCTION(AboveOrEqual);
3797
3798 IfCondition GetCondition() const OVERRIDE {
3799 return kCondAE;
3800 }
3801
3802 IfCondition GetOppositeCondition() const OVERRIDE {
3803 return kCondB;
3804 }
3805
3806 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003807 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003808 return MakeUnsigned(x) >= MakeUnsigned(y);
3809 }
Aart Bike9f37602015-10-09 11:15:55 -07003810
3811 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3812};
Dave Allison20dfc792014-06-16 20:44:29 -07003813
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003814// Instruction to check how two inputs compare to each other.
3815// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003816class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003817 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003818 // Note that `comparison_type` is the type of comparison performed
3819 // between the comparison's inputs, not the type of the instantiated
3820 // HCompare instruction (which is always Primitive::kPrimInt).
3821 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003822 HInstruction* first,
3823 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003824 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003825 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003826 : HBinaryOperation(Primitive::kPrimInt,
3827 first,
3828 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003829 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003830 dex_pc) {
3831 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003832 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3833 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003834 }
3835
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3838
3839 template <typename T>
3840 int32_t ComputeFP(T x, T y) const {
3841 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3842 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3843 // Handle the bias.
3844 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3845 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003846
Roland Levillain9867bc72015-08-05 10:21:34 +01003847 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3849 // reach this code path when processing a freshly built HIR
3850 // graph. However HCompare integer instructions can be synthesized
3851 // by the instruction simplifier to implement IntegerCompare and
3852 // IntegerSignum intrinsics, so we have to handle this case.
3853 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003854 }
3855 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3857 }
3858 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3859 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3860 }
3861 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3862 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003863 }
3864
Vladimir Marko372f10e2016-05-17 16:30:10 +01003865 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003867 }
3868
Vladimir Markoa1de9182016-02-25 11:37:38 +00003869 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003870
Roland Levillain31dd3d62016-02-16 12:21:02 +00003871 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003872 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003873 bool IsGtBias() const {
3874 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003875 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003876 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003877
Roland Levillain1693a1f2016-03-15 14:57:31 +00003878 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3879 // Comparisons do not require a runtime call in any back end.
3880 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003881 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003882
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003883 DECLARE_INSTRUCTION(Compare);
3884
Roland Levillain31dd3d62016-02-16 12:21:02 +00003885 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003886 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3887 static constexpr size_t kFieldComparisonBiasSize =
3888 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3889 static constexpr size_t kNumberOfComparePackedBits =
3890 kFieldComparisonBias + kFieldComparisonBiasSize;
3891 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3892 using ComparisonBiasField =
3893 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3894
Roland Levillain31dd3d62016-02-16 12:21:02 +00003895 // Return an integer constant containing the result of a comparison evaluated at compile time.
3896 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3897 DCHECK(value == -1 || value == 0 || value == 1) << value;
3898 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3899 }
3900
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003901 private:
3902 DISALLOW_COPY_AND_ASSIGN(HCompare);
3903};
3904
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003905class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003906 public:
3907 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003908 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003909 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003910 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003911 bool finalizable,
3912 QuickEntrypointEnum entrypoint)
3913 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3914 type_index_(type_index),
3915 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003916 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003917 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003918 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003919 }
3920
Andreas Gampea5b09a62016-11-17 15:21:22 -08003921 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003922 const DexFile& GetDexFile() const { return dex_file_; }
3923
3924 // Calls runtime so needs an environment.
3925 bool NeedsEnvironment() const OVERRIDE { return true; }
3926
Mingyao Yang062157f2016-03-02 10:15:36 -08003927 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3928 bool CanThrow() const OVERRIDE { return true; }
3929
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003930 bool NeedsChecks() const {
3931 return entrypoint_ == kQuickAllocObjectWithChecks;
3932 }
David Brazdil6de19382016-01-08 17:37:10 +00003933
Vladimir Markoa1de9182016-02-25 11:37:38 +00003934 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003935
3936 bool CanBeNull() const OVERRIDE { return false; }
3937
3938 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3939
3940 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3941 entrypoint_ = entrypoint;
3942 }
3943
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003944 HLoadClass* GetLoadClass() const {
3945 HInstruction* input = InputAt(0);
3946 if (input->IsClinitCheck()) {
3947 input = input->InputAt(0);
3948 }
3949 DCHECK(input->IsLoadClass());
3950 return input->AsLoadClass();
3951 }
3952
David Brazdil6de19382016-01-08 17:37:10 +00003953 bool IsStringAlloc() const;
3954
3955 DECLARE_INSTRUCTION(NewInstance);
3956
3957 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003958 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003959 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3960 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3961 "Too many packed fields.");
3962
Andreas Gampea5b09a62016-11-17 15:21:22 -08003963 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003964 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003965 QuickEntrypointEnum entrypoint_;
3966
3967 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3968};
3969
Agi Csaki05f20562015-08-19 14:58:14 -07003970enum IntrinsicNeedsEnvironmentOrCache {
3971 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3972 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003973};
3974
Aart Bik5d75afe2015-12-14 11:57:01 -08003975enum IntrinsicSideEffects {
3976 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3977 kReadSideEffects, // Intrinsic may read heap memory.
3978 kWriteSideEffects, // Intrinsic may write heap memory.
3979 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3980};
3981
3982enum IntrinsicExceptions {
3983 kNoThrow, // Intrinsic does not throw any exceptions.
3984 kCanThrow // Intrinsic may throw exceptions.
3985};
3986
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003987class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003988 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003989 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003990
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003991 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003992 SetRawInputAt(index, argument);
3993 }
3994
Roland Levillain3e3d7332015-04-28 11:00:54 +01003995 // Return the number of arguments. This number can be lower than
3996 // the number of inputs returned by InputCount(), as some invoke
3997 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3998 // inputs at the end of their list of inputs.
3999 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4000
Vladimir Markoa1de9182016-02-25 11:37:38 +00004001 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004002
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004003 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4004
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004005 InvokeType GetInvokeType() const {
4006 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004007 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004008
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004009 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004010 return intrinsic_;
4011 }
4012
Aart Bik5d75afe2015-12-14 11:57:01 -08004013 void SetIntrinsic(Intrinsics intrinsic,
4014 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4015 IntrinsicSideEffects side_effects,
4016 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004017
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004018 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004019 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004020 }
4021
Aart Bikff7d89c2016-11-07 08:49:28 -08004022 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4023
Vladimir Markoa1de9182016-02-25 11:37:38 +00004024 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004025
Aart Bik71bf7b42016-11-16 10:17:46 -08004026 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004027
Vladimir Marko372f10e2016-05-17 16:30:10 +01004028 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004029 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4030 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004031
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004032 uint32_t* GetIntrinsicOptimizations() {
4033 return &intrinsic_optimizations_;
4034 }
4035
4036 const uint32_t* GetIntrinsicOptimizations() const {
4037 return &intrinsic_optimizations_;
4038 }
4039
4040 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4041
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004042 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004043 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004044
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004045 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004046
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004047 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004048 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4049 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004050 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4051 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004052 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004053 static constexpr size_t kFieldReturnTypeSize =
4054 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4055 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4056 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4057 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004058 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004059 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4060
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004061 HInvoke(ArenaAllocator* arena,
4062 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004063 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004064 Primitive::Type return_type,
4065 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004066 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004067 ArtMethod* resolved_method,
4068 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004069 : HVariableInputSizeInstruction(
4070 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4071 dex_pc,
4072 arena,
4073 number_of_arguments + number_of_other_inputs,
4074 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004075 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004076 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004077 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004078 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004079 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004080 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004081 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004082 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004083 }
4084
Roland Levillain3e3d7332015-04-28 11:00:54 +01004085 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004086 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004087 const uint32_t dex_method_index_;
4088 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004089
4090 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4091 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004092
4093 private:
4094 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4095};
4096
Vladimir Markofcb503c2016-05-18 12:48:17 +01004097class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004098 public:
4099 HInvokeUnresolved(ArenaAllocator* arena,
4100 uint32_t number_of_arguments,
4101 Primitive::Type return_type,
4102 uint32_t dex_pc,
4103 uint32_t dex_method_index,
4104 InvokeType invoke_type)
4105 : HInvoke(arena,
4106 number_of_arguments,
4107 0u /* number_of_other_inputs */,
4108 return_type,
4109 dex_pc,
4110 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004111 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004112 invoke_type) {
4113 }
4114
4115 DECLARE_INSTRUCTION(InvokeUnresolved);
4116
4117 private:
4118 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4119};
4120
Orion Hodsonac141392017-01-13 11:53:47 +00004121class HInvokePolymorphic FINAL : public HInvoke {
4122 public:
4123 HInvokePolymorphic(ArenaAllocator* arena,
4124 uint32_t number_of_arguments,
4125 Primitive::Type return_type,
4126 uint32_t dex_pc,
4127 uint32_t dex_method_index)
4128 : HInvoke(arena,
4129 number_of_arguments,
4130 0u /* number_of_other_inputs */,
4131 return_type,
4132 dex_pc,
4133 dex_method_index,
4134 nullptr,
4135 kVirtual) {}
4136
4137 DECLARE_INSTRUCTION(InvokePolymorphic);
4138
4139 private:
4140 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4141};
4142
Vladimir Markofcb503c2016-05-18 12:48:17 +01004143class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004144 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004145 // Requirements of this method call regarding the class
4146 // initialization (clinit) check of its declaring class.
4147 enum class ClinitCheckRequirement {
4148 kNone, // Class already initialized.
4149 kExplicit, // Static call having explicit clinit check as last input.
4150 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004151 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004152 };
4153
Vladimir Marko58155012015-08-19 12:49:41 +00004154 // Determines how to load the target ArtMethod*.
4155 enum class MethodLoadKind {
4156 // Use a String init ArtMethod* loaded from Thread entrypoints.
4157 kStringInit,
4158
4159 // Use the method's own ArtMethod* loaded by the register allocator.
4160 kRecursive,
4161
Vladimir Marko65979462017-05-19 17:25:12 +01004162 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4163 // Used for boot image methods referenced by boot image code.
4164 kBootImageLinkTimePcRelative,
4165
Vladimir Marko58155012015-08-19 12:49:41 +00004166 // Use ArtMethod* at a known address, embed the direct address in the code.
4167 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4168 kDirectAddress,
4169
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004170 // Load from an entry in the .bss section using a PC-relative load.
4171 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4172 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004173
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004174 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4175 // used when other kinds are unimplemented on a particular architecture.
4176 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004177 };
4178
4179 // Determines the location of the code pointer.
4180 enum class CodePtrLocation {
4181 // Recursive call, use local PC-relative call instruction.
4182 kCallSelf,
4183
Vladimir Marko58155012015-08-19 12:49:41 +00004184 // Use code pointer from the ArtMethod*.
4185 // Used when we don't know the target code. This is also the last-resort-kind used when
4186 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4187 kCallArtMethod,
4188 };
4189
4190 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004191 MethodLoadKind method_load_kind;
4192 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004193 // The method load data holds
4194 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4195 // Note that there are multiple string init methods, each having its own offset.
4196 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004197 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004198 };
4199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004200 HInvokeStaticOrDirect(ArenaAllocator* arena,
4201 uint32_t number_of_arguments,
4202 Primitive::Type return_type,
4203 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004204 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004205 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004206 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004207 InvokeType invoke_type,
4208 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004209 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004210 : HInvoke(arena,
4211 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004212 // There is potentially one extra argument for the HCurrentMethod node, and
4213 // potentially one other if the clinit check is explicit, and potentially
4214 // one other if the method is a string factory.
4215 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004216 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004217 return_type,
4218 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004219 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004220 resolved_method,
4221 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004222 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004223 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004224 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4225 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004226
Vladimir Markodc151b22015-10-15 18:02:30 +01004227 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004228 bool had_current_method_input = HasCurrentMethodInput();
4229 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4230
4231 // Using the current method is the default and once we find a better
4232 // method load kind, we should not go back to using the current method.
4233 DCHECK(had_current_method_input || !needs_current_method_input);
4234
4235 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004236 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4237 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004238 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004239 dispatch_info_ = dispatch_info;
4240 }
4241
Aart Bik6daebeb2017-04-03 14:35:41 -07004242 DispatchInfo GetDispatchInfo() const {
4243 return dispatch_info_;
4244 }
4245
Vladimir Markoc53c0792015-11-19 15:48:33 +00004246 void AddSpecialInput(HInstruction* input) {
4247 // We allow only one special input.
4248 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4249 DCHECK(InputCount() == GetSpecialInputIndex() ||
4250 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4251 InsertInputAt(GetSpecialInputIndex(), input);
4252 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004253
Vladimir Marko372f10e2016-05-17 16:30:10 +01004254 using HInstruction::GetInputRecords; // Keep the const version visible.
4255 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4256 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4257 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4258 DCHECK(!input_records.empty());
4259 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4260 HInstruction* last_input = input_records.back().GetInstruction();
4261 // Note: `last_input` may be null during arguments setup.
4262 if (last_input != nullptr) {
4263 // `last_input` is the last input of a static invoke marked as having
4264 // an explicit clinit check. It must either be:
4265 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4266 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4267 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4268 }
4269 }
4270 return input_records;
4271 }
4272
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004273 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004274 // We access the method via the dex cache so we can't do an implicit null check.
4275 // TODO: for intrinsics we can generate implicit null checks.
4276 return false;
4277 }
4278
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004279 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004280 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004281 }
4282
Vladimir Markoc53c0792015-11-19 15:48:33 +00004283 // Get the index of the special input, if any.
4284 //
David Brazdil6de19382016-01-08 17:37:10 +00004285 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4286 // method pointer; otherwise there may be one platform-specific special input,
4287 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004288 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004289 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290
Vladimir Marko58155012015-08-19 12:49:41 +00004291 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4292 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4293 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004294 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004295 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004296 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004297 bool HasPcRelativeMethodLoadKind() const {
4298 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004299 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004300 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004301 bool HasCurrentMethodInput() const {
4302 // This function can be called only after the invoke has been fully initialized by the builder.
4303 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004304 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004305 return true;
4306 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004307 DCHECK(InputCount() == GetSpecialInputIndex() ||
4308 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004309 return false;
4310 }
4311 }
Vladimir Marko58155012015-08-19 12:49:41 +00004312
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004313 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004314 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004315 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004316 }
4317
4318 uint64_t GetMethodAddress() const {
4319 DCHECK(HasMethodAddress());
4320 return dispatch_info_.method_load_data;
4321 }
4322
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004323 const DexFile& GetDexFileForPcRelativeDexCache() const;
4324
Vladimir Markoa1de9182016-02-25 11:37:38 +00004325 ClinitCheckRequirement GetClinitCheckRequirement() const {
4326 return GetPackedField<ClinitCheckRequirementField>();
4327 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004328
Roland Levillain4c0eb422015-04-24 16:43:49 +01004329 // Is this instruction a call to a static method?
4330 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004331 return GetInvokeType() == kStatic;
4332 }
4333
4334 MethodReference GetTargetMethod() const {
4335 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004336 }
4337
Vladimir Markofbb184a2015-11-13 14:47:00 +00004338 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4339 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4340 // instruction; only relevant for static calls with explicit clinit check.
4341 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004342 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004343 size_t last_input_index = inputs_.size() - 1u;
4344 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004346 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004347 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004348 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004349 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004350 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004351 }
4352
4353 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004354 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004356 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004357 }
4358
4359 // Is this a call to a static method whose declaring class has an
4360 // implicit intialization check requirement?
4361 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004362 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004363 }
4364
Vladimir Markob554b5a2015-11-06 12:57:55 +00004365 // Does this method load kind need the current method as an input?
4366 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004367 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004368 }
4369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004370 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004371
4372 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004373 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004374 static constexpr size_t kFieldClinitCheckRequirementSize =
4375 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4376 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4377 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4378 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4379 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004380 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4381 kFieldClinitCheckRequirement,
4382 kFieldClinitCheckRequirementSize>;
4383
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004384 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004385 MethodReference target_method_;
4386 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004387
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004388 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004389};
Vladimir Markof64242a2015-12-01 14:58:23 +00004390std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004391std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004392
Vladimir Markofcb503c2016-05-18 12:48:17 +01004393class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004394 public:
4395 HInvokeVirtual(ArenaAllocator* arena,
4396 uint32_t number_of_arguments,
4397 Primitive::Type return_type,
4398 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004399 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004400 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004401 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004402 : HInvoke(arena,
4403 number_of_arguments,
4404 0u,
4405 return_type,
4406 dex_pc,
4407 dex_method_index,
4408 resolved_method,
4409 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004410 vtable_index_(vtable_index) {}
4411
Aart Bik71bf7b42016-11-16 10:17:46 -08004412 bool CanBeNull() const OVERRIDE {
4413 switch (GetIntrinsic()) {
4414 case Intrinsics::kThreadCurrentThread:
4415 case Intrinsics::kStringBufferAppend:
4416 case Intrinsics::kStringBufferToString:
4417 case Intrinsics::kStringBuilderAppend:
4418 case Intrinsics::kStringBuilderToString:
4419 return false;
4420 default:
4421 return HInvoke::CanBeNull();
4422 }
4423 }
4424
Calin Juravle641547a2015-04-21 22:08:51 +01004425 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004426 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004427 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004428 }
4429
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004430 uint32_t GetVTableIndex() const { return vtable_index_; }
4431
4432 DECLARE_INSTRUCTION(InvokeVirtual);
4433
4434 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004436 const uint32_t vtable_index_;
4437
4438 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4439};
4440
Vladimir Markofcb503c2016-05-18 12:48:17 +01004441class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004442 public:
4443 HInvokeInterface(ArenaAllocator* arena,
4444 uint32_t number_of_arguments,
4445 Primitive::Type return_type,
4446 uint32_t dex_pc,
4447 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004448 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004449 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004450 : HInvoke(arena,
4451 number_of_arguments,
4452 0u,
4453 return_type,
4454 dex_pc,
4455 dex_method_index,
4456 resolved_method,
4457 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004458 imt_index_(imt_index) {}
4459
Calin Juravle641547a2015-04-21 22:08:51 +01004460 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004461 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004462 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004463 }
4464
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004465 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4466 // The assembly stub currently needs it.
4467 return true;
4468 }
4469
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004470 uint32_t GetImtIndex() const { return imt_index_; }
4471 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4472
4473 DECLARE_INSTRUCTION(InvokeInterface);
4474
4475 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004476 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004477 const uint32_t imt_index_;
4478
4479 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4480};
4481
Vladimir Markofcb503c2016-05-18 12:48:17 +01004482class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004484 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004485 : HUnaryOperation(result_type, input, dex_pc) {
4486 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4487 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004488
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004489 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004490
4491 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004492 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004493 }
4494 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004497 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4498 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4499 }
4500 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4501 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4502 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004503
Roland Levillain88cb1752014-10-20 16:36:47 +01004504 DECLARE_INSTRUCTION(Neg);
4505
4506 private:
4507 DISALLOW_COPY_AND_ASSIGN(HNeg);
4508};
4509
Vladimir Markofcb503c2016-05-18 12:48:17 +01004510class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004511 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004512 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4513 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4514 SetRawInputAt(0, cls);
4515 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004516 }
4517
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004518 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004519 bool NeedsEnvironment() const OVERRIDE { return true; }
4520
Mingyao Yang0c365e62015-03-31 15:09:29 -07004521 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4522 bool CanThrow() const OVERRIDE { return true; }
4523
Calin Juravle10e244f2015-01-26 18:54:32 +00004524 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004526 HLoadClass* GetLoadClass() const {
4527 DCHECK(InputAt(0)->IsLoadClass());
4528 return InputAt(0)->AsLoadClass();
4529 }
4530
4531 HInstruction* GetLength() const {
4532 return InputAt(1);
4533 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004534
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004535 DECLARE_INSTRUCTION(NewArray);
4536
4537 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004538 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4539};
4540
Vladimir Markofcb503c2016-05-18 12:48:17 +01004541class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004542 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 HAdd(Primitive::Type result_type,
4544 HInstruction* left,
4545 HInstruction* right,
4546 uint32_t dex_pc = kNoDexPc)
4547 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004548
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004549 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004550
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004551 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004552
4553 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 return GetBlock()->GetGraph()->GetIntConstant(
4555 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004556 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 return GetBlock()->GetGraph()->GetLongConstant(
4559 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004560 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004561 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4562 return GetBlock()->GetGraph()->GetFloatConstant(
4563 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4564 }
4565 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4566 return GetBlock()->GetGraph()->GetDoubleConstant(
4567 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4568 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004569
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004570 DECLARE_INSTRUCTION(Add);
4571
4572 private:
4573 DISALLOW_COPY_AND_ASSIGN(HAdd);
4574};
4575
Vladimir Markofcb503c2016-05-18 12:48:17 +01004576class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 HSub(Primitive::Type result_type,
4579 HInstruction* left,
4580 HInstruction* right,
4581 uint32_t dex_pc = kNoDexPc)
4582 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004583
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004584 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004585
4586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 return GetBlock()->GetGraph()->GetIntConstant(
4588 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004589 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 return GetBlock()->GetGraph()->GetLongConstant(
4592 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004593 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004594 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4595 return GetBlock()->GetGraph()->GetFloatConstant(
4596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4597 }
4598 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4599 return GetBlock()->GetGraph()->GetDoubleConstant(
4600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4601 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004602
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004603 DECLARE_INSTRUCTION(Sub);
4604
4605 private:
4606 DISALLOW_COPY_AND_ASSIGN(HSub);
4607};
4608
Vladimir Markofcb503c2016-05-18 12:48:17 +01004609class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004610 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 HMul(Primitive::Type result_type,
4612 HInstruction* left,
4613 HInstruction* right,
4614 uint32_t dex_pc = kNoDexPc)
4615 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004617 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004618
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004619 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004620
4621 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004622 return GetBlock()->GetGraph()->GetIntConstant(
4623 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004624 }
4625 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 return GetBlock()->GetGraph()->GetLongConstant(
4627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004629 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4630 return GetBlock()->GetGraph()->GetFloatConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4632 }
4633 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4634 return GetBlock()->GetGraph()->GetDoubleConstant(
4635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4636 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004637
4638 DECLARE_INSTRUCTION(Mul);
4639
4640 private:
4641 DISALLOW_COPY_AND_ASSIGN(HMul);
4642};
4643
Vladimir Markofcb503c2016-05-18 12:48:17 +01004644class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004645 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004646 HDiv(Primitive::Type result_type,
4647 HInstruction* left,
4648 HInstruction* right,
4649 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004650 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004651
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004653 T ComputeIntegral(T x, T y) const {
4654 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 // Our graph structure ensures we never have 0 for `y` during
4656 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004657 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004658 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004659 return (y == -1) ? -x : x / y;
4660 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004661
Roland Levillain31dd3d62016-02-16 12:21:02 +00004662 template <typename T>
4663 T ComputeFP(T x, T y) const {
4664 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4665 return x / y;
4666 }
4667
Roland Levillain9867bc72015-08-05 10:21:34 +01004668 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004669 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004670 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 }
4672 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004673 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4675 }
4676 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4677 return GetBlock()->GetGraph()->GetFloatConstant(
4678 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4679 }
4680 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4681 return GetBlock()->GetGraph()->GetDoubleConstant(
4682 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004683 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004684
4685 DECLARE_INSTRUCTION(Div);
4686
4687 private:
4688 DISALLOW_COPY_AND_ASSIGN(HDiv);
4689};
4690
Vladimir Markofcb503c2016-05-18 12:48:17 +01004691class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004693 HRem(Primitive::Type result_type,
4694 HInstruction* left,
4695 HInstruction* right,
4696 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004697 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004698
Roland Levillain9867bc72015-08-05 10:21:34 +01004699 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004700 T ComputeIntegral(T x, T y) const {
4701 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 // Our graph structure ensures we never have 0 for `y` during
4703 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004704 DCHECK_NE(y, 0);
4705 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4706 return (y == -1) ? 0 : x % y;
4707 }
4708
Roland Levillain31dd3d62016-02-16 12:21:02 +00004709 template <typename T>
4710 T ComputeFP(T x, T y) const {
4711 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4712 return std::fmod(x, y);
4713 }
4714
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004716 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004717 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 }
4719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004721 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004722 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004723 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4724 return GetBlock()->GetGraph()->GetFloatConstant(
4725 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4726 }
4727 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4728 return GetBlock()->GetGraph()->GetDoubleConstant(
4729 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4730 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004731
4732 DECLARE_INSTRUCTION(Rem);
4733
4734 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004735 DISALLOW_COPY_AND_ASSIGN(HRem);
4736};
4737
Vladimir Markofcb503c2016-05-18 12:48:17 +01004738class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004739 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004740 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4741 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004742 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004743 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004744 SetRawInputAt(0, value);
4745 }
4746
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004747 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4748
Calin Juravled0d48522014-11-04 16:40:20 +00004749 bool CanBeMoved() const OVERRIDE { return true; }
4750
Vladimir Marko372f10e2016-05-17 16:30:10 +01004751 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004752 return true;
4753 }
4754
4755 bool NeedsEnvironment() const OVERRIDE { return true; }
4756 bool CanThrow() const OVERRIDE { return true; }
4757
Calin Juravled0d48522014-11-04 16:40:20 +00004758 DECLARE_INSTRUCTION(DivZeroCheck);
4759
4760 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004761 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4762};
4763
Vladimir Markofcb503c2016-05-18 12:48:17 +01004764class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004765 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004766 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 HInstruction* value,
4768 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4771 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4772 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
4774
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004776 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 return value << (distance & max_shift_distance);
4778 }
4779
4780 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004781 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004785 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4789 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4790 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4791 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004792 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004793 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4794 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004795 LOG(FATAL) << DebugName() << " is not defined for float values";
4796 UNREACHABLE();
4797 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4799 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004800 LOG(FATAL) << DebugName() << " is not defined for double values";
4801 UNREACHABLE();
4802 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004803
4804 DECLARE_INSTRUCTION(Shl);
4805
4806 private:
4807 DISALLOW_COPY_AND_ASSIGN(HShl);
4808};
4809
Vladimir Markofcb503c2016-05-18 12:48:17 +01004810class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004812 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 HInstruction* value,
4814 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4817 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4818 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
4820
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004822 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004823 return value >> (distance & max_shift_distance);
4824 }
4825
4826 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004828 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004832 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4835 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4836 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4837 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004838 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004839 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4840 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004841 LOG(FATAL) << DebugName() << " is not defined for float values";
4842 UNREACHABLE();
4843 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4845 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004846 LOG(FATAL) << DebugName() << " is not defined for double values";
4847 UNREACHABLE();
4848 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004849
4850 DECLARE_INSTRUCTION(Shr);
4851
4852 private:
4853 DISALLOW_COPY_AND_ASSIGN(HShr);
4854};
4855
Vladimir Markofcb503c2016-05-18 12:48:17 +01004856class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004857 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004858 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 HInstruction* value,
4860 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4863 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4864 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004865 }
4866
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004868 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004869 typedef typename std::make_unsigned<T>::type V;
4870 V ux = static_cast<V>(value);
4871 return static_cast<T>(ux >> (distance & max_shift_distance));
4872 }
4873
4874 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004875 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004877 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004881 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4883 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4884 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4885 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004886 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004887 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4888 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004889 LOG(FATAL) << DebugName() << " is not defined for float values";
4890 UNREACHABLE();
4891 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004892 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4893 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004894 LOG(FATAL) << DebugName() << " is not defined for double values";
4895 UNREACHABLE();
4896 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004897
4898 DECLARE_INSTRUCTION(UShr);
4899
4900 private:
4901 DISALLOW_COPY_AND_ASSIGN(HUShr);
4902};
4903
Vladimir Markofcb503c2016-05-18 12:48:17 +01004904class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004905 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 HAnd(Primitive::Type result_type,
4907 HInstruction* left,
4908 HInstruction* right,
4909 uint32_t dex_pc = kNoDexPc)
4910 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004911
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004912 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004913
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004914 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004915
4916 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004917 return GetBlock()->GetGraph()->GetIntConstant(
4918 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004919 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004920 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 return GetBlock()->GetGraph()->GetLongConstant(
4922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004923 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004924 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4925 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4926 LOG(FATAL) << DebugName() << " is not defined for float values";
4927 UNREACHABLE();
4928 }
4929 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4930 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4931 LOG(FATAL) << DebugName() << " is not defined for double values";
4932 UNREACHABLE();
4933 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004934
4935 DECLARE_INSTRUCTION(And);
4936
4937 private:
4938 DISALLOW_COPY_AND_ASSIGN(HAnd);
4939};
4940
Vladimir Markofcb503c2016-05-18 12:48:17 +01004941class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004942 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 HOr(Primitive::Type result_type,
4944 HInstruction* left,
4945 HInstruction* right,
4946 uint32_t dex_pc = kNoDexPc)
4947 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004948
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004949 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004950
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004951 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004952
4953 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004954 return GetBlock()->GetGraph()->GetIntConstant(
4955 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004956 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004957 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004958 return GetBlock()->GetGraph()->GetLongConstant(
4959 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004960 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004961 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4962 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4963 LOG(FATAL) << DebugName() << " is not defined for float values";
4964 UNREACHABLE();
4965 }
4966 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4967 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4968 LOG(FATAL) << DebugName() << " is not defined for double values";
4969 UNREACHABLE();
4970 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004971
4972 DECLARE_INSTRUCTION(Or);
4973
4974 private:
4975 DISALLOW_COPY_AND_ASSIGN(HOr);
4976};
4977
Vladimir Markofcb503c2016-05-18 12:48:17 +01004978class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004979 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 HXor(Primitive::Type result_type,
4981 HInstruction* left,
4982 HInstruction* right,
4983 uint32_t dex_pc = kNoDexPc)
4984 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004985
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004986 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004987
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004988 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004989
4990 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004991 return GetBlock()->GetGraph()->GetIntConstant(
4992 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004993 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004994 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 return GetBlock()->GetGraph()->GetLongConstant(
4996 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004998 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4999 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5000 LOG(FATAL) << DebugName() << " is not defined for float values";
5001 UNREACHABLE();
5002 }
5003 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5004 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5005 LOG(FATAL) << DebugName() << " is not defined for double values";
5006 UNREACHABLE();
5007 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005008
5009 DECLARE_INSTRUCTION(Xor);
5010
5011 private:
5012 DISALLOW_COPY_AND_ASSIGN(HXor);
5013};
5014
Vladimir Markofcb503c2016-05-18 12:48:17 +01005015class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005016 public:
5017 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005018 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005019 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5020 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005021 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005022
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005024 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005025 typedef typename std::make_unsigned<T>::type V;
5026 V ux = static_cast<V>(value);
5027 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005028 return static_cast<T>(ux);
5029 } else {
5030 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005031 return static_cast<T>(ux >> (distance & max_shift_value)) |
5032 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033 }
5034 }
5035
Roland Levillain5b5b9312016-03-22 14:57:31 +00005036 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005037 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005038 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005039 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005040 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005041 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005042 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005043 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005044 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5045 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5046 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5047 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005048 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005049 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5050 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 LOG(FATAL) << DebugName() << " is not defined for float values";
5052 UNREACHABLE();
5053 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005054 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5055 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005056 LOG(FATAL) << DebugName() << " is not defined for double values";
5057 UNREACHABLE();
5058 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005059
5060 DECLARE_INSTRUCTION(Ror);
5061
5062 private:
5063 DISALLOW_COPY_AND_ASSIGN(HRor);
5064};
5065
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005066// The value of a parameter in this method. Its location depends on
5067// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005068class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005069 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005070 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005071 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005072 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005073 Primitive::Type parameter_type,
5074 bool is_this = false)
5075 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005076 dex_file_(dex_file),
5077 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005078 index_(index) {
5079 SetPackedFlag<kFlagIsThis>(is_this);
5080 SetPackedFlag<kFlagCanBeNull>(!is_this);
5081 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005082
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005083 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005084 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005085 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005086 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005087
Vladimir Markoa1de9182016-02-25 11:37:38 +00005088 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5089 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005090
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005091 DECLARE_INSTRUCTION(ParameterValue);
5092
5093 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005094 // Whether or not the parameter value corresponds to 'this' argument.
5095 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5096 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5097 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5098 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5099 "Too many packed fields.");
5100
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005101 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005102 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005103 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005104 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005105 const uint8_t index_;
5106
5107 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5108};
5109
Vladimir Markofcb503c2016-05-18 12:48:17 +01005110class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005111 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005112 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5113 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005114
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005115 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005116 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005117 return true;
5118 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005119
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005120 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005121
5122 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005123 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005124 }
5125 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005126 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005127 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005128 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5129 LOG(FATAL) << DebugName() << " is not defined for float values";
5130 UNREACHABLE();
5131 }
5132 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5133 LOG(FATAL) << DebugName() << " is not defined for double values";
5134 UNREACHABLE();
5135 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005136
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005137 DECLARE_INSTRUCTION(Not);
5138
5139 private:
5140 DISALLOW_COPY_AND_ASSIGN(HNot);
5141};
5142
Vladimir Markofcb503c2016-05-18 12:48:17 +01005143class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005144 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5146 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005147
5148 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005149 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005150 return true;
5151 }
5152
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005153 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005154 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005155 return !x;
5156 }
5157
Roland Levillain9867bc72015-08-05 10:21:34 +01005158 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005159 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005160 }
5161 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5162 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005163 UNREACHABLE();
5164 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005165 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5166 LOG(FATAL) << DebugName() << " is not defined for float values";
5167 UNREACHABLE();
5168 }
5169 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5170 LOG(FATAL) << DebugName() << " is not defined for double values";
5171 UNREACHABLE();
5172 }
David Brazdil66d126e2015-04-03 16:02:44 +01005173
5174 DECLARE_INSTRUCTION(BooleanNot);
5175
5176 private:
5177 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5178};
5179
Vladimir Markofcb503c2016-05-18 12:48:17 +01005180class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005181 public:
5182 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005183 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005184 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005185 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005186 // Invariant: We should never generate a conversion to a Boolean value.
5187 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005188 }
5189
5190 HInstruction* GetInput() const { return InputAt(0); }
5191 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5192 Primitive::Type GetResultType() const { return GetType(); }
5193
5194 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005195 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5196 return true;
5197 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005198
Mark Mendelle82549b2015-05-06 10:55:34 -04005199 // Try to statically evaluate the conversion and return a HConstant
5200 // containing the result. If the input cannot be converted, return nullptr.
5201 HConstant* TryStaticEvaluation() const;
5202
Roland Levillaindff1f282014-11-05 14:15:05 +00005203 DECLARE_INSTRUCTION(TypeConversion);
5204
5205 private:
5206 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5207};
5208
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005209static constexpr uint32_t kNoRegNumber = -1;
5210
Vladimir Markofcb503c2016-05-18 12:48:17 +01005211class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005213 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5214 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005216 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 SetRawInputAt(0, value);
5218 }
5219
Calin Juravle10e244f2015-01-26 18:54:32 +00005220 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005221 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005222 return true;
5223 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005224
Calin Juravle10e244f2015-01-26 18:54:32 +00005225 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005226
Calin Juravle10e244f2015-01-26 18:54:32 +00005227 bool CanThrow() const OVERRIDE { return true; }
5228
5229 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005230
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005231
5232 DECLARE_INSTRUCTION(NullCheck);
5233
5234 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005235 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5236};
5237
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005238// Embeds an ArtField and all the information required by the compiler. We cache
5239// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005240class FieldInfo : public ValueObject {
5241 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005242 FieldInfo(ArtField* field,
5243 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005244 Primitive::Type field_type,
5245 bool is_volatile,
5246 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005247 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005248 const DexFile& dex_file)
5249 : field_(field),
5250 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005251 field_type_(field_type),
5252 is_volatile_(is_volatile),
5253 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005254 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005255 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005256
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005257 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005259 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005260 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005261 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005262 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005263 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005264
5265 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005266 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005267 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005268 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005269 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005270 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005271 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005272 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005273};
5274
Vladimir Markofcb503c2016-05-18 12:48:17 +01005275class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005276 public:
5277 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005278 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005279 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005280 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005281 bool is_volatile,
5282 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005283 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005284 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005285 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005286 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005287 field_info_(field,
5288 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005289 field_type,
5290 is_volatile,
5291 field_idx,
5292 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005293 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005294 SetRawInputAt(0, value);
5295 }
5296
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005297 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005298
Vladimir Marko372f10e2016-05-17 16:30:10 +01005299 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5300 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005301 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005302 }
5303
Calin Juravle641547a2015-04-21 22:08:51 +01005304 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005305 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005306 }
5307
5308 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005309 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5310 }
5311
Calin Juravle52c48962014-12-16 17:02:57 +00005312 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005313 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005314 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005315 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005316
5317 DECLARE_INSTRUCTION(InstanceFieldGet);
5318
5319 private:
5320 const FieldInfo field_info_;
5321
5322 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5323};
5324
Vladimir Markofcb503c2016-05-18 12:48:17 +01005325class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005326 public:
5327 HInstanceFieldSet(HInstruction* object,
5328 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005329 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005330 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005331 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005332 bool is_volatile,
5333 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005334 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005335 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005336 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005337 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005338 field_info_(field,
5339 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005340 field_type,
5341 is_volatile,
5342 field_idx,
5343 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005344 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005345 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005346 SetRawInputAt(0, object);
5347 SetRawInputAt(1, value);
5348 }
5349
Calin Juravle641547a2015-04-21 22:08:51 +01005350 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005351 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 }
5353
Calin Juravle52c48962014-12-16 17:02:57 +00005354 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005355 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005356 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005357 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005358 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005359 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5360 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005361
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005362 DECLARE_INSTRUCTION(InstanceFieldSet);
5363
5364 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005365 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5366 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5367 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5368 "Too many packed fields.");
5369
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005370 const FieldInfo field_info_;
5371
5372 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5373};
5374
Vladimir Markofcb503c2016-05-18 12:48:17 +01005375class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005377 HArrayGet(HInstruction* array,
5378 HInstruction* index,
5379 Primitive::Type type,
5380 uint32_t dex_pc,
5381 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005382 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005383 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 SetRawInputAt(0, array);
5385 SetRawInputAt(1, index);
5386 }
5387
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005388 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005389 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005390 return true;
5391 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005392 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005393 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005394 // Currently, unless the array is the result of NewArray, the array access is always
5395 // preceded by some form of null NullCheck necessary for the bounds check, usually
5396 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5397 // dynamic BCE. There are cases when these could be removed to produce better code.
5398 // If we ever add optimizations to do so we should allow an implicit check here
5399 // (as long as the address falls in the first page).
5400 //
5401 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5402 // a = cond ? new int[1] : null;
5403 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005404 return false;
5405 }
5406
David Brazdil4833f5a2015-12-16 10:37:39 +00005407 bool IsEquivalentOf(HArrayGet* other) const {
5408 bool result = (GetDexPc() == other->GetDexPc());
5409 if (kIsDebugBuild && result) {
5410 DCHECK_EQ(GetBlock(), other->GetBlock());
5411 DCHECK_EQ(GetArray(), other->GetArray());
5412 DCHECK_EQ(GetIndex(), other->GetIndex());
5413 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005414 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005415 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005416 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5417 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005418 }
5419 }
5420 return result;
5421 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005422
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005423 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5424
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005425 HInstruction* GetArray() const { return InputAt(0); }
5426 HInstruction* GetIndex() const { return InputAt(1); }
5427
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 DECLARE_INSTRUCTION(ArrayGet);
5429
5430 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005431 // We treat a String as an array, creating the HArrayGet from String.charAt()
5432 // intrinsic in the instruction simplifier. We can always determine whether
5433 // a particular HArrayGet is actually a String.charAt() by looking at the type
5434 // of the input but that requires holding the mutator lock, so we prefer to use
5435 // a flag, so that code generators don't need to do the locking.
5436 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5437 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5438 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5439 "Too many packed fields.");
5440
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5442};
5443
Vladimir Markofcb503c2016-05-18 12:48:17 +01005444class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 public:
5446 HArraySet(HInstruction* array,
5447 HInstruction* index,
5448 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005449 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005450 uint32_t dex_pc)
5451 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5453 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5454 SetPackedFlag<kFlagValueCanBeNull>(true);
5455 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456 SetRawInputAt(0, array);
5457 SetRawInputAt(1, index);
5458 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005459 // Make a best guess now, may be refined during SSA building.
5460 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005461 }
5462
Calin Juravle77520bc2015-01-12 18:45:46 +00005463 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005464 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005465 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 }
5467
Mingyao Yang81014cb2015-06-02 03:16:27 -07005468 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005470
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005471 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005472 // TODO: Same as for ArrayGet.
5473 return false;
5474 }
5475
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005476 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005477 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005478 }
5479
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005480 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005482 }
5483
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005484 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005485 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 }
5487
Vladimir Markoa1de9182016-02-25 11:37:38 +00005488 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5489 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5490 bool StaticTypeOfArrayIsObjectArray() const {
5491 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5492 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005493
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005494 HInstruction* GetArray() const { return InputAt(0); }
5495 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005496 HInstruction* GetValue() const { return InputAt(2); }
5497
5498 Primitive::Type GetComponentType() const {
5499 // The Dex format does not type floating point index operations. Since the
5500 // `expected_component_type_` is set during building and can therefore not
5501 // be correct, we also check what is the value type. If it is a floating
5502 // point type, we must use that type.
5503 Primitive::Type value_type = GetValue()->GetType();
5504 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5505 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005507 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005508
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005509 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005511 }
5512
Aart Bik18b36ab2016-04-13 16:41:35 -07005513 void ComputeSideEffects() {
5514 Primitive::Type type = GetComponentType();
5515 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5516 SideEffectsForArchRuntimeCalls(type)));
5517 }
5518
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005519 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5520 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5521 }
5522
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005523 DECLARE_INSTRUCTION(ArraySet);
5524
5525 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005526 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5527 static constexpr size_t kFieldExpectedComponentTypeSize =
5528 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5529 static constexpr size_t kFlagNeedsTypeCheck =
5530 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5531 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005532 // Cached information for the reference_type_info_ so that codegen
5533 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005534 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5535 static constexpr size_t kNumberOfArraySetPackedBits =
5536 kFlagStaticTypeOfArrayIsObjectArray + 1;
5537 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5538 using ExpectedComponentTypeField =
5539 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540
5541 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5542};
5543
Vladimir Markofcb503c2016-05-18 12:48:17 +01005544class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005545 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005546 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005547 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005548 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005549 // Note that arrays do not change length, so the instruction does not
5550 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551 SetRawInputAt(0, array);
5552 }
5553
Calin Juravle77520bc2015-01-12 18:45:46 +00005554 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005555 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005556 return true;
5557 }
Calin Juravle641547a2015-04-21 22:08:51 +01005558 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5559 return obj == InputAt(0);
5560 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005561
Vladimir Markodce016e2016-04-28 13:10:02 +01005562 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005564 DECLARE_INSTRUCTION(ArrayLength);
5565
5566 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005567 // We treat a String as an array, creating the HArrayLength from String.length()
5568 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5569 // determine whether a particular HArrayLength is actually a String.length() by
5570 // looking at the type of the input but that requires holding the mutator lock, so
5571 // we prefer to use a flag, so that code generators don't need to do the locking.
5572 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5573 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5574 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5575 "Too many packed fields.");
5576
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5578};
5579
Vladimir Markofcb503c2016-05-18 12:48:17 +01005580class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005582 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5583 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005584 HBoundsCheck(HInstruction* index,
5585 HInstruction* length,
5586 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005587 bool string_char_at = false)
5588 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005589 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005590 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 SetRawInputAt(0, index);
5592 SetRawInputAt(1, length);
5593 }
5594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005595 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005596 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005597 return true;
5598 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005600 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005602 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005603
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005604 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005605
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005606 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005607
5608 DECLARE_INSTRUCTION(BoundsCheck);
5609
5610 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005611 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005612
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005613 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5614};
5615
Vladimir Markofcb503c2016-05-18 12:48:17 +01005616class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005617 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005618 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005619 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005621 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005622 return true;
5623 }
5624
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005625 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5626 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005627
5628 DECLARE_INSTRUCTION(SuspendCheck);
5629
5630 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005631 // Only used for code generation, in order to share the same slow path between back edges
5632 // of a same loop.
5633 SlowPathCode* slow_path_;
5634
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005635 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5636};
5637
David Srbecky0cf44932015-12-09 14:09:59 +00005638// Pseudo-instruction which provides the native debugger with mapping information.
5639// It ensures that we can generate line number and local variables at this point.
5640class HNativeDebugInfo : public HTemplateInstruction<0> {
5641 public:
5642 explicit HNativeDebugInfo(uint32_t dex_pc)
5643 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5644
5645 bool NeedsEnvironment() const OVERRIDE {
5646 return true;
5647 }
5648
5649 DECLARE_INSTRUCTION(NativeDebugInfo);
5650
5651 private:
5652 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5653};
5654
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005655/**
5656 * Instruction to load a Class object.
5657 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005660 // Determines how to load the Class.
5661 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005662 // We cannot load this class. See HSharpening::SharpenLoadClass.
5663 kInvalid = -1,
5664
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 // Use the Class* from the method's own ArtMethod*.
5666 kReferrersClass,
5667
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005668 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005669 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670 kBootImageLinkTimePcRelative,
5671
5672 // Use a known boot image Class* address, embedded in the code by the codegen.
5673 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005674 kBootImageAddress,
5675
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005676 // Load from an entry in the .bss section using a PC-relative load.
5677 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5678 kBssEntry,
5679
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005680 // Load from the root table associated with the JIT compiled method.
5681 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005683 // Load using a simple runtime call. This is the fall-back load kind when
5684 // the codegen is unable to use another appropriate kind.
5685 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005686
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005687 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005688 };
5689
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005690 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005691 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005692 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005693 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005694 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005695 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005696 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5698 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005700 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005701 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005702 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005703 // Referrers class should not need access check. We never inline unverified
5704 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 DCHECK(!is_referrers_class || !needs_access_check);
5706
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005708 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005709 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005710 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005712 }
5713
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005714 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 LoadKind GetLoadKind() const {
5717 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005718 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005719
5720 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005721
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723
Andreas Gampea5b09a62016-11-17 15:21:22 -08005724 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005726 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005728 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005729 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730 }
5731
Calin Juravle0ba218d2015-05-19 18:46:01 +01005732 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005733 // The entrypoint the code generator is going to call does not do
5734 // clinit of the class.
5735 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005736 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005737 }
5738
5739 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005740 return NeedsAccessCheck() ||
5741 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005742 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005743 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005744 }
5745
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005746 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005747 return NeedsAccessCheck() ||
5748 MustGenerateClinitCheck() ||
5749 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5750 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5751 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005752 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 GetLoadKind() == LoadKind::kBssEntry) &&
5754 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005755 }
5756
Calin Juravleacf735c2015-02-12 15:25:22 +00005757 ReferenceTypeInfo GetLoadedClassRTI() {
5758 return loaded_class_rti_;
5759 }
5760
5761 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5762 // Make sure we only set exact types (the loaded class should never be merged).
5763 DCHECK(rti.IsExact());
5764 loaded_class_rti_ = rti;
5765 }
5766
Andreas Gampea5b09a62016-11-17 15:21:22 -08005767 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 const DexFile& GetDexFile() const { return dex_file_; }
5769
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005770 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005771 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005772 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005773
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005774 static SideEffects SideEffectsForArchRuntimeCalls() {
5775 return SideEffects::CanTriggerGC();
5776 }
5777
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005778 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005779 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005780 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005781 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005782
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005783 void MarkInBootImage() {
5784 SetPackedFlag<kFlagIsInBootImage>(true);
5785 }
5786
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 void AddSpecialInput(HInstruction* special_input);
5788
5789 using HInstruction::GetInputRecords; // Keep the const version visible.
5790 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5791 return ArrayRef<HUserRecord<HInstruction*>>(
5792 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5793 }
5794
5795 Primitive::Type GetType() const OVERRIDE {
5796 return Primitive::kPrimNot;
5797 }
5798
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005799 Handle<mirror::Class> GetClass() const {
5800 return klass_;
5801 }
5802
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005803 DECLARE_INSTRUCTION(LoadClass);
5804
5805 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005807 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005808 // Whether this instruction must generate the initialization check.
5809 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005810 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005811 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5812 static constexpr size_t kFieldLoadKindSize =
5813 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5814 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005815 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5817
5818 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005819 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005820 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005821 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005822 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 }
5824
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825 void SetLoadKindInternal(LoadKind load_kind);
5826
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005827 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005828 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005829 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005831
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005832 // A type index and dex file where the class can be accessed. The dex file can be:
5833 // - The compiling method's dex file if the class is defined there too.
5834 // - The compiling method's dex file if the class is referenced there.
5835 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005836 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005837 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005838 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005839
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005840 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005841
Calin Juravleacf735c2015-02-12 15:25:22 +00005842 ReferenceTypeInfo loaded_class_rti_;
5843
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005844 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5845};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005846std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5847
5848// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005850 // The special input is used for PC-relative loads on some architectures,
5851 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005852 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005853 GetLoadKind() == LoadKind::kBootImageAddress ||
5854 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 DCHECK(special_input_.GetInstruction() == nullptr);
5856 special_input_ = HUserRecord<HInstruction*>(special_input);
5857 special_input->AddUseAt(this, 0);
5858}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005859
Vladimir Marko372f10e2016-05-17 16:30:10 +01005860class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005861 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862 // Determines how to load the String.
5863 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005864 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005865 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 kBootImageLinkTimePcRelative,
5867
5868 // Use a known boot image String* address, embedded in the code by the codegen.
5869 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 kBootImageAddress,
5871
Vladimir Markoaad75c62016-10-03 08:46:48 +00005872 // Load from an entry in the .bss section using a PC-relative load.
5873 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5874 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005876 // Load from the root table associated with the JIT compiled method.
5877 kJitTableAddress,
5878
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005879 // Load using a simple runtime call. This is the fall-back load kind when
5880 // the codegen is unable to use another appropriate kind.
5881 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005883 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884 };
5885
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005886 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005887 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888 const DexFile& dex_file,
5889 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005890 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5891 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005892 string_index_(string_index),
5893 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005894 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005895 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005896
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005897 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005899 LoadKind GetLoadKind() const {
5900 return GetPackedField<LoadKindField>();
5901 }
5902
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005903 const DexFile& GetDexFile() const {
5904 return dex_file_;
5905 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005907 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908 return string_index_;
5909 }
5910
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005911 Handle<mirror::String> GetString() const {
5912 return string_;
5913 }
5914
5915 void SetString(Handle<mirror::String> str) {
5916 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 }
5918
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005919 bool CanBeMoved() const OVERRIDE { return true; }
5920
Vladimir Marko372f10e2016-05-17 16:30:10 +01005921 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005922
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005923 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005924
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 // Will call the runtime if we need to load the string through
5926 // the dex cache and the string is not guaranteed to be there yet.
5927 bool NeedsEnvironment() const OVERRIDE {
5928 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005929 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005930 load_kind == LoadKind::kBootImageAddress ||
5931 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 return false;
5933 }
Vladimir Marko4d1be492017-01-06 14:43:11 +00005934 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005936
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005937 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005938 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005939 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005940
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005941 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005943
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005944 static SideEffects SideEffectsForArchRuntimeCalls() {
5945 return SideEffects::CanTriggerGC();
5946 }
5947
Vladimir Marko372f10e2016-05-17 16:30:10 +01005948 void AddSpecialInput(HInstruction* special_input);
5949
5950 using HInstruction::GetInputRecords; // Keep the const version visible.
5951 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5952 return ArrayRef<HUserRecord<HInstruction*>>(
5953 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 }
5955
Vladimir Marko372f10e2016-05-17 16:30:10 +01005956 Primitive::Type GetType() const OVERRIDE {
5957 return Primitive::kPrimNot;
5958 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005960 DECLARE_INSTRUCTION(LoadString);
5961
5962 private:
Vladimir Marko4d1be492017-01-06 14:43:11 +00005963 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005964 static constexpr size_t kFieldLoadKindSize =
5965 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5966 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005968 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970 void SetLoadKindInternal(LoadKind load_kind);
5971
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005972 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005974 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005975 HUserRecord<HInstruction*> special_input_;
5976
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005977 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005978 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005980 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005981
5982 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5983};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005984std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5985
5986// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005988 // The special input is used for PC-relative loads on some architectures,
5989 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005991 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005992 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005993 // HLoadString::GetInputRecords() returns an empty array at this point,
5994 // so use the GetInputRecords() from the base class to set the input record.
5995 DCHECK(special_input_.GetInstruction() == nullptr);
5996 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997 special_input->AddUseAt(this, 0);
5998}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005999
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006000/**
6001 * Performs an initialization check on its Class object input.
6002 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006003class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006005 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006006 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006007 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006008 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6009 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010 SetRawInputAt(0, constant);
6011 }
6012
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006013 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006014 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006015 return true;
6016 }
6017
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018 bool NeedsEnvironment() const OVERRIDE {
6019 // May call runtime to initialize the class.
6020 return true;
6021 }
6022
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006023 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006024
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006025 HLoadClass* GetLoadClass() const {
6026 DCHECK(InputAt(0)->IsLoadClass());
6027 return InputAt(0)->AsLoadClass();
6028 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006029
6030 DECLARE_INSTRUCTION(ClinitCheck);
6031
6032 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006033 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6034};
6035
Vladimir Markofcb503c2016-05-18 12:48:17 +01006036class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006037 public:
6038 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006039 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006040 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006041 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006042 bool is_volatile,
6043 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006044 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006045 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006046 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006047 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006048 field_info_(field,
6049 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006050 field_type,
6051 is_volatile,
6052 field_idx,
6053 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006054 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006055 SetRawInputAt(0, cls);
6056 }
6057
Calin Juravle52c48962014-12-16 17:02:57 +00006058
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006059 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006060
Vladimir Marko372f10e2016-05-17 16:30:10 +01006061 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6062 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006063 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064 }
6065
6066 size_t ComputeHashCode() const OVERRIDE {
6067 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6068 }
6069
Calin Juravle52c48962014-12-16 17:02:57 +00006070 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006071 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6072 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006073 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074
6075 DECLARE_INSTRUCTION(StaticFieldGet);
6076
6077 private:
6078 const FieldInfo field_info_;
6079
6080 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6081};
6082
Vladimir Markofcb503c2016-05-18 12:48:17 +01006083class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006084 public:
6085 HStaticFieldSet(HInstruction* cls,
6086 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006087 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006088 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006089 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006090 bool is_volatile,
6091 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006092 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006093 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006094 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006095 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006096 field_info_(field,
6097 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006098 field_type,
6099 is_volatile,
6100 field_idx,
6101 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006102 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006103 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006104 SetRawInputAt(0, cls);
6105 SetRawInputAt(1, value);
6106 }
6107
Calin Juravle52c48962014-12-16 17:02:57 +00006108 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006109 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6110 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006111 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006112
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006113 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6115 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006116
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006117 DECLARE_INSTRUCTION(StaticFieldSet);
6118
6119 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006120 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6121 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6122 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6123 "Too many packed fields.");
6124
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006125 const FieldInfo field_info_;
6126
6127 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6128};
6129
Vladimir Markofcb503c2016-05-18 12:48:17 +01006130class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006131 public:
6132 HUnresolvedInstanceFieldGet(HInstruction* obj,
6133 Primitive::Type field_type,
6134 uint32_t field_index,
6135 uint32_t dex_pc)
6136 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6137 field_index_(field_index) {
6138 SetRawInputAt(0, obj);
6139 }
6140
6141 bool NeedsEnvironment() const OVERRIDE { return true; }
6142 bool CanThrow() const OVERRIDE { return true; }
6143
6144 Primitive::Type GetFieldType() const { return GetType(); }
6145 uint32_t GetFieldIndex() const { return field_index_; }
6146
6147 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6148
6149 private:
6150 const uint32_t field_index_;
6151
6152 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6153};
6154
Vladimir Markofcb503c2016-05-18 12:48:17 +01006155class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006156 public:
6157 HUnresolvedInstanceFieldSet(HInstruction* obj,
6158 HInstruction* value,
6159 Primitive::Type field_type,
6160 uint32_t field_index,
6161 uint32_t dex_pc)
6162 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006163 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006164 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006165 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006166 SetRawInputAt(0, obj);
6167 SetRawInputAt(1, value);
6168 }
6169
6170 bool NeedsEnvironment() const OVERRIDE { return true; }
6171 bool CanThrow() const OVERRIDE { return true; }
6172
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006174 uint32_t GetFieldIndex() const { return field_index_; }
6175
6176 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6177
6178 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006179 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6180 static constexpr size_t kFieldFieldTypeSize =
6181 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6182 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6183 kFieldFieldType + kFieldFieldTypeSize;
6184 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6185 "Too many packed fields.");
6186 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6187
Calin Juravlee460d1d2015-09-29 04:52:17 +01006188 const uint32_t field_index_;
6189
6190 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6191};
6192
Vladimir Markofcb503c2016-05-18 12:48:17 +01006193class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006194 public:
6195 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6196 uint32_t field_index,
6197 uint32_t dex_pc)
6198 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6199 field_index_(field_index) {
6200 }
6201
6202 bool NeedsEnvironment() const OVERRIDE { return true; }
6203 bool CanThrow() const OVERRIDE { return true; }
6204
6205 Primitive::Type GetFieldType() const { return GetType(); }
6206 uint32_t GetFieldIndex() const { return field_index_; }
6207
6208 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6209
6210 private:
6211 const uint32_t field_index_;
6212
6213 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6214};
6215
Vladimir Markofcb503c2016-05-18 12:48:17 +01006216class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006217 public:
6218 HUnresolvedStaticFieldSet(HInstruction* value,
6219 Primitive::Type field_type,
6220 uint32_t field_index,
6221 uint32_t dex_pc)
6222 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006223 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006224 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006225 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006226 SetRawInputAt(0, value);
6227 }
6228
6229 bool NeedsEnvironment() const OVERRIDE { return true; }
6230 bool CanThrow() const OVERRIDE { return true; }
6231
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006233 uint32_t GetFieldIndex() const { return field_index_; }
6234
6235 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6236
6237 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006238 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6239 static constexpr size_t kFieldFieldTypeSize =
6240 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6241 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6242 kFieldFieldType + kFieldFieldTypeSize;
6243 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6244 "Too many packed fields.");
6245 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6246
Calin Juravlee460d1d2015-09-29 04:52:17 +01006247 const uint32_t field_index_;
6248
6249 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6250};
6251
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006252// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006253class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006254 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006255 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6256 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006257
David Brazdilbbd733e2015-08-18 17:48:17 +01006258 bool CanBeNull() const OVERRIDE { return false; }
6259
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006260 DECLARE_INSTRUCTION(LoadException);
6261
6262 private:
6263 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6264};
6265
David Brazdilcb1c0552015-08-04 16:22:25 +01006266// Implicit part of move-exception which clears thread-local exception storage.
6267// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006268class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006269 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006270 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6271 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006272
6273 DECLARE_INSTRUCTION(ClearException);
6274
6275 private:
6276 DISALLOW_COPY_AND_ASSIGN(HClearException);
6277};
6278
Vladimir Markofcb503c2016-05-18 12:48:17 +01006279class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006280 public:
6281 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006282 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006283 SetRawInputAt(0, exception);
6284 }
6285
6286 bool IsControlFlow() const OVERRIDE { return true; }
6287
6288 bool NeedsEnvironment() const OVERRIDE { return true; }
6289
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006290 bool CanThrow() const OVERRIDE { return true; }
6291
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006292
6293 DECLARE_INSTRUCTION(Throw);
6294
6295 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006296 DISALLOW_COPY_AND_ASSIGN(HThrow);
6297};
6298
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299/**
6300 * Implementation strategies for the code generator of a HInstanceOf
6301 * or `HCheckCast`.
6302 */
6303enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006304 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 kExactCheck, // Can do a single class compare.
6306 kClassHierarchyCheck, // Can just walk the super class chain.
6307 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6308 kInterfaceCheck, // No optimization yet when checking against an interface.
6309 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310 kArrayCheck, // No optimization yet when checking against a generic array.
6311 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312};
6313
Roland Levillain86503782016-02-11 19:07:30 +00006314std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6315
Vladimir Markofcb503c2016-05-18 12:48:17 +01006316class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006317 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006318 HInstanceOf(HInstruction* object,
6319 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006320 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006321 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006322 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006324 dex_pc) {
6325 SetPackedField<TypeCheckKindField>(check_kind);
6326 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006327 SetRawInputAt(0, object);
6328 SetRawInputAt(1, constant);
6329 }
6330
6331 bool CanBeMoved() const OVERRIDE { return true; }
6332
Vladimir Marko372f10e2016-05-17 16:30:10 +01006333 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006334 return true;
6335 }
6336
6337 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006338 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006339 }
6340
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006341 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6343 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6344 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6345 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006346
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006347 static bool CanCallRuntime(TypeCheckKind check_kind) {
6348 // Mips currently does runtime calls for any other checks.
6349 return check_kind != TypeCheckKind::kExactCheck;
6350 }
6351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006353 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006354 }
6355
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006356 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006357
6358 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006359 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6360 static constexpr size_t kFieldTypeCheckKindSize =
6361 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6362 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6363 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6364 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6365 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006366
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006367 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6368};
6369
Vladimir Markofcb503c2016-05-18 12:48:17 +01006370class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006371 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006372 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006373 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006374 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6375 SetPackedFlag<kFlagUpperCanBeNull>(true);
6376 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006377 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006378 SetRawInputAt(0, input);
6379 }
6380
David Brazdilf5552582015-12-27 13:36:12 +00006381 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006382 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006383 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006384 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006385
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006386 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006387 DCHECK(GetUpperCanBeNull() || !can_be_null);
6388 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006389 }
6390
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006392
Calin Juravleb1498f62015-02-16 13:13:29 +00006393 DECLARE_INSTRUCTION(BoundType);
6394
6395 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006396 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6397 // is false then CanBeNull() cannot be true).
6398 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6399 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6400 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6401 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6402
Calin Juravleb1498f62015-02-16 13:13:29 +00006403 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006404 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6405 // It is used to bound the type in cases like:
6406 // if (x instanceof ClassX) {
6407 // // uper_bound_ will be ClassX
6408 // }
David Brazdilf5552582015-12-27 13:36:12 +00006409 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006410
6411 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6412};
6413
Vladimir Markofcb503c2016-05-18 12:48:17 +01006414class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006415 public:
6416 HCheckCast(HInstruction* object,
6417 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006419 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006420 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6421 SetPackedField<TypeCheckKindField>(check_kind);
6422 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006423 SetRawInputAt(0, object);
6424 SetRawInputAt(1, constant);
6425 }
6426
6427 bool CanBeMoved() const OVERRIDE { return true; }
6428
Vladimir Marko372f10e2016-05-17 16:30:10 +01006429 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006430 return true;
6431 }
6432
6433 bool NeedsEnvironment() const OVERRIDE {
6434 // Instruction may throw a CheckCastError.
6435 return true;
6436 }
6437
6438 bool CanThrow() const OVERRIDE { return true; }
6439
Vladimir Markoa1de9182016-02-25 11:37:38 +00006440 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6441 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6442 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6443 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006444
6445 DECLARE_INSTRUCTION(CheckCast);
6446
6447 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006448 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6449 static constexpr size_t kFieldTypeCheckKindSize =
6450 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6451 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6452 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6453 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6454 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006455
6456 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006457};
6458
Andreas Gampe26de38b2016-07-27 17:53:11 -07006459/**
6460 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6461 * @details We define the combined barrier types that are actually required
6462 * by the Java Memory Model, rather than using exactly the terminology from
6463 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6464 * primitives. Note that the JSR-133 cookbook generally does not deal with
6465 * store atomicity issues, and the recipes there are not always entirely sufficient.
6466 * The current recipe is as follows:
6467 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6468 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6469 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6470 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6471 * class has final fields.
6472 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6473 * store-to-memory instructions. Only generated together with non-temporal stores.
6474 */
6475enum MemBarrierKind {
6476 kAnyStore,
6477 kLoadAny,
6478 kStoreStore,
6479 kAnyAny,
6480 kNTStoreStore,
6481 kLastBarrierKind = kNTStoreStore
6482};
6483std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6484
Vladimir Markofcb503c2016-05-18 12:48:17 +01006485class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006487 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006488 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006489 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6490 SetPackedField<BarrierKindField>(barrier_kind);
6491 }
Calin Juravle27df7582015-04-17 19:12:31 +01006492
Vladimir Markoa1de9182016-02-25 11:37:38 +00006493 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006494
6495 DECLARE_INSTRUCTION(MemoryBarrier);
6496
6497 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006498 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6499 static constexpr size_t kFieldBarrierKindSize =
6500 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6501 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6502 kFieldBarrierKind + kFieldBarrierKindSize;
6503 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6504 "Too many packed fields.");
6505 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006506
6507 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6508};
6509
Igor Murashkind01745e2017-04-05 16:40:31 -07006510// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6511// the specified object(s), with respect to any subsequent store that might "publish"
6512// (i.e. make visible) the specified object to another thread.
6513//
6514// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6515// for all final fields (that were set) at the end of the invoked constructor.
6516//
6517// The constructor fence models the freeze actions for the final fields of an object
6518// being constructed (semantically at the end of the constructor). Constructor fences
6519// have a per-object affinity; two separate objects being constructed get two separate
6520// constructor fences.
6521//
6522// (Note: that if calling a super-constructor or forwarding to another constructor,
6523// the freezes would happen at the end of *that* constructor being invoked).
6524//
6525// The memory model guarantees that when the object being constructed is "published" after
6526// constructor completion (i.e. escapes the current thread via a store), then any final field
6527// writes must be observable on other threads (once they observe that publication).
6528//
6529// Further, anything written before the freeze, and read by dereferencing through the final field,
6530// must also be visible (so final object field could itself have an object with non-final fields;
6531// yet the freeze must also extend to them).
6532//
6533// Constructor example:
6534//
6535// class HasFinal {
6536// final int field; Optimizing IR for <init>()V:
6537// HasFinal() {
6538// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6539// // freeze(this.field); HConstructorFence(this)
6540// } HReturn
6541// }
6542//
6543// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6544// already-initialized classes; in that case the allocation must act as a "default-initializer"
6545// of the object which effectively writes the class pointer "final field".
6546//
6547// For example, we can model default-initialiation as roughly the equivalent of the following:
6548//
6549// class Object {
6550// private final Class header;
6551// }
6552//
6553// Java code: Optimizing IR:
6554//
6555// T new_instance<T>() {
6556// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6557// obj.header = T.class; // header write is done by above call.
6558// // freeze(obj.header) HConstructorFence(obj)
6559// return (T)obj;
6560// }
6561//
6562// See also:
6563// * CompilerDriver::RequiresConstructorBarrier
6564// * QuasiAtomic::ThreadFenceForConstructor
6565//
6566class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6567 // A fence has variable inputs because the inputs can be removed
6568 // after prepare_for_register_allocation phase.
6569 // (TODO: In the future a fence could freeze multiple objects
6570 // after merging two fences together.)
6571 public:
6572 // `fence_object` is the reference that needs to be protected for correct publication.
6573 //
6574 // It makes sense in the following situations:
6575 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6576 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6577 //
6578 // After construction the `fence_object` becomes the 0th input.
6579 // This is not an input in a real sense, but just a convenient place to stash the information
6580 // about the associated object.
6581 HConstructorFence(HInstruction* fence_object,
6582 uint32_t dex_pc,
6583 ArenaAllocator* arena)
6584 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6585 // constraints described in the class header. We claim that these SideEffects constraints
6586 // enforce a superset of the real constraints.
6587 //
6588 // The ordering described above is conservatively modeled with SideEffects as follows:
6589 //
6590 // * To prevent reordering of the publication stores:
6591 // ----> "Reads of objects" is the initial SideEffect.
6592 // * For every primitive final field store in the constructor:
6593 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6594 // * If there are any stores to reference final fields in the constructor:
6595 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6596 // that are reachable from `fence_object` also need to be prevented for reordering
6597 // (and we do not want to do alias analysis to figure out what those stores are).
6598 //
6599 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6600 // even more conservative approach than the one described above, and prevents all of the
6601 // above reordering without analyzing any of the instructions in the constructor.
6602 //
6603 // If in a later phase we discover that there are no writes to reference final fields,
6604 // we can refine the side effect to a smaller set of type reads (see above constraints).
6605 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6606 dex_pc,
6607 arena,
6608 /* number_of_inputs */ 1,
6609 kArenaAllocConstructorFenceInputs) {
6610 DCHECK(fence_object != nullptr);
6611 SetRawInputAt(0, fence_object);
6612 }
6613
6614 // The object associated with this constructor fence.
6615 //
6616 // (Note: This will be null after the prepare_for_register_allocation phase,
6617 // as all constructor fence inputs are removed there).
6618 HInstruction* GetFenceObject() const {
6619 return InputAt(0);
6620 }
6621
6622 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6623 // - Delete `fence_use` from `this`'s use list.
6624 // - Delete `this` from `fence_use`'s inputs list.
6625 // - If the `fence_use` is dead, remove it from the graph.
6626 //
6627 // A fence is considered dead once it no longer has any uses
6628 // and all of the inputs are dead.
6629 //
6630 // This must *not* be called during/after prepare_for_register_allocation,
6631 // because that removes all the inputs to the fences but the fence is actually
6632 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006633 //
6634 // Returns how many HConstructorFence instructions were removed from graph.
6635 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006636
Igor Murashkindd018df2017-08-09 10:38:31 -07006637 // Combine all inputs of `this` and `other` instruction and remove
6638 // `other` from the graph.
6639 //
6640 // Inputs are unique after the merge.
6641 //
6642 // Requirement: `this` must not be the same as `other.
6643 void Merge(HConstructorFence* other);
6644
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006645 // Check if this constructor fence is protecting
6646 // an HNewInstance or HNewArray that is also the immediate
6647 // predecessor of `this`.
6648 //
Igor Murashkindd018df2017-08-09 10:38:31 -07006649 // If `ignore_inputs` is true, then the immediate predecessor doesn't need
6650 // to be one of the inputs of `this`.
6651 //
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006652 // Returns the associated HNewArray or HNewInstance,
6653 // or null otherwise.
Igor Murashkindd018df2017-08-09 10:38:31 -07006654 HInstruction* GetAssociatedAllocation(bool ignore_inputs = false);
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006655
Igor Murashkind01745e2017-04-05 16:40:31 -07006656 DECLARE_INSTRUCTION(ConstructorFence);
6657
6658 private:
6659 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6660};
6661
Vladimir Markofcb503c2016-05-18 12:48:17 +01006662class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006663 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006664 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006665 kEnter,
6666 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006667 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006668 };
6669
6670 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006671 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006672 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6673 dex_pc) {
6674 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006675 SetRawInputAt(0, object);
6676 }
6677
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006678 // Instruction may go into runtime, so we need an environment.
6679 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006680
6681 bool CanThrow() const OVERRIDE {
6682 // Verifier guarantees that monitor-exit cannot throw.
6683 // This is important because it allows the HGraphBuilder to remove
6684 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6685 return IsEnter();
6686 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006687
Vladimir Markoa1de9182016-02-25 11:37:38 +00006688 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6689 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006690
6691 DECLARE_INSTRUCTION(MonitorOperation);
6692
Calin Juravle52c48962014-12-16 17:02:57 +00006693 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006694 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6695 static constexpr size_t kFieldOperationKindSize =
6696 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6697 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6698 kFieldOperationKind + kFieldOperationKindSize;
6699 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6700 "Too many packed fields.");
6701 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006702
6703 private:
6704 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6705};
6706
Vladimir Markofcb503c2016-05-18 12:48:17 +01006707class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006708 public:
6709 HSelect(HInstruction* condition,
6710 HInstruction* true_value,
6711 HInstruction* false_value,
6712 uint32_t dex_pc)
6713 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6714 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6715
6716 // First input must be `true_value` or `false_value` to allow codegens to
6717 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6718 // that architectures which implement HSelect as a conditional move also
6719 // will not need to invert the condition.
6720 SetRawInputAt(0, false_value);
6721 SetRawInputAt(1, true_value);
6722 SetRawInputAt(2, condition);
6723 }
6724
6725 HInstruction* GetFalseValue() const { return InputAt(0); }
6726 HInstruction* GetTrueValue() const { return InputAt(1); }
6727 HInstruction* GetCondition() const { return InputAt(2); }
6728
6729 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006730 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6731 return true;
6732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006733
6734 bool CanBeNull() const OVERRIDE {
6735 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6736 }
6737
6738 DECLARE_INSTRUCTION(Select);
6739
6740 private:
6741 DISALLOW_COPY_AND_ASSIGN(HSelect);
6742};
6743
Vladimir Markof9f64412015-09-02 14:05:49 +01006744class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006745 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006746 MoveOperands(Location source,
6747 Location destination,
6748 Primitive::Type type,
6749 HInstruction* instruction)
6750 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006751
6752 Location GetSource() const { return source_; }
6753 Location GetDestination() const { return destination_; }
6754
6755 void SetSource(Location value) { source_ = value; }
6756 void SetDestination(Location value) { destination_ = value; }
6757
6758 // The parallel move resolver marks moves as "in-progress" by clearing the
6759 // destination (but not the source).
6760 Location MarkPending() {
6761 DCHECK(!IsPending());
6762 Location dest = destination_;
6763 destination_ = Location::NoLocation();
6764 return dest;
6765 }
6766
6767 void ClearPending(Location dest) {
6768 DCHECK(IsPending());
6769 destination_ = dest;
6770 }
6771
6772 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006773 DCHECK(source_.IsValid() || destination_.IsInvalid());
6774 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006775 }
6776
6777 // True if this blocks a move from the given location.
6778 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006779 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006780 }
6781
6782 // A move is redundant if it's been eliminated, if its source and
6783 // destination are the same, or if its destination is unneeded.
6784 bool IsRedundant() const {
6785 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6786 }
6787
6788 // We clear both operands to indicate move that's been eliminated.
6789 void Eliminate() {
6790 source_ = destination_ = Location::NoLocation();
6791 }
6792
6793 bool IsEliminated() const {
6794 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6795 return source_.IsInvalid();
6796 }
6797
Alexey Frunze4dda3372015-06-01 18:31:49 -07006798 Primitive::Type GetType() const { return type_; }
6799
Nicolas Geoffray90218252015-04-15 11:56:51 +01006800 bool Is64BitMove() const {
6801 return Primitive::Is64BitType(type_);
6802 }
6803
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006804 HInstruction* GetInstruction() const { return instruction_; }
6805
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006806 private:
6807 Location source_;
6808 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006809 // The type this move is for.
6810 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006811 // The instruction this move is assocatied with. Null when this move is
6812 // for moving an input in the expected locations of user (including a phi user).
6813 // This is only used in debug mode, to ensure we do not connect interval siblings
6814 // in the same parallel move.
6815 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006816};
6817
Roland Levillainc9285912015-12-18 10:38:42 +00006818std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6819
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006820static constexpr size_t kDefaultNumberOfMoves = 4;
6821
Vladimir Markofcb503c2016-05-18 12:48:17 +01006822class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006823 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006824 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006825 : HTemplateInstruction(SideEffects::None(), dex_pc),
6826 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6827 moves_.reserve(kDefaultNumberOfMoves);
6828 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006829
Nicolas Geoffray90218252015-04-15 11:56:51 +01006830 void AddMove(Location source,
6831 Location destination,
6832 Primitive::Type type,
6833 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006834 DCHECK(source.IsValid());
6835 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006836 if (kIsDebugBuild) {
6837 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006838 for (const MoveOperands& move : moves_) {
6839 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006840 // Special case the situation where the move is for the spill slot
6841 // of the instruction.
6842 if ((GetPrevious() == instruction)
6843 || ((GetPrevious() == nullptr)
6844 && instruction->IsPhi()
6845 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006846 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006847 << "Doing parallel moves for the same instruction.";
6848 } else {
6849 DCHECK(false) << "Doing parallel moves for the same instruction.";
6850 }
6851 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006852 }
6853 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006854 for (const MoveOperands& move : moves_) {
6855 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006856 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006857 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006858 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006859 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006860 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006861 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006862 }
6863
Vladimir Marko225b6462015-09-28 12:17:40 +01006864 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006865 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006866 }
6867
Vladimir Marko225b6462015-09-28 12:17:40 +01006868 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006870 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006871
6872 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006873 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006874
6875 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6876};
6877
Mark Mendell0616ae02015-04-17 12:49:27 -04006878} // namespace art
6879
Aart Bikf8f5a162017-02-06 15:35:29 -08006880#include "nodes_vector.h"
6881
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006882#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6883#include "nodes_shared.h"
6884#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006885#ifdef ART_ENABLE_CODEGEN_mips
6886#include "nodes_mips.h"
6887#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006888#ifdef ART_ENABLE_CODEGEN_x86
6889#include "nodes_x86.h"
6890#endif
6891
6892namespace art {
6893
Igor Murashkin6ef45672017-08-08 13:59:55 -07006894class OptimizingCompilerStats;
6895
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006896class HGraphVisitor : public ValueObject {
6897 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006898 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6899 : stats_(stats),
6900 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07006901 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006903 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006904 virtual void VisitBasicBlock(HBasicBlock* block);
6905
Roland Levillain633021e2014-10-01 14:12:25 +01006906 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006907 void VisitInsertionOrder();
6908
Roland Levillain633021e2014-10-01 14:12:25 +01006909 // Visit the graph following dominator tree reverse post-order.
6910 void VisitReversePostOrder();
6911
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006912 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006913
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006914 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006915#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006916 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6917
6918 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6919
6920#undef DECLARE_VISIT_INSTRUCTION
6921
Igor Murashkin6ef45672017-08-08 13:59:55 -07006922 protected:
6923 OptimizingCompilerStats* stats_;
6924
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006925 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006926 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006927
6928 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6929};
6930
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006931class HGraphDelegateVisitor : public HGraphVisitor {
6932 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006933 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6934 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006935 virtual ~HGraphDelegateVisitor() {}
6936
6937 // Visit functions that delegate to to super class.
6938#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006939 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006940
6941 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6942
6943#undef DECLARE_VISIT_INSTRUCTION
6944
6945 private:
6946 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6947};
6948
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006949// Iterator over the blocks that art part of the loop. Includes blocks part
6950// of an inner loop. The order in which the blocks are iterated is on their
6951// block id.
6952class HBlocksInLoopIterator : public ValueObject {
6953 public:
6954 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6955 : blocks_in_loop_(info.GetBlocks()),
6956 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6957 index_(0) {
6958 if (!blocks_in_loop_.IsBitSet(index_)) {
6959 Advance();
6960 }
6961 }
6962
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006963 bool Done() const { return index_ == blocks_.size(); }
6964 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006965 void Advance() {
6966 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006967 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006968 if (blocks_in_loop_.IsBitSet(index_)) {
6969 break;
6970 }
6971 }
6972 }
6973
6974 private:
6975 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006976 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006977 size_t index_;
6978
6979 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6980};
6981
Mingyao Yang3584bce2015-05-19 16:01:59 -07006982// Iterator over the blocks that art part of the loop. Includes blocks part
6983// of an inner loop. The order in which the blocks are iterated is reverse
6984// post order.
6985class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6986 public:
6987 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6988 : blocks_in_loop_(info.GetBlocks()),
6989 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6990 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006991 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006992 Advance();
6993 }
6994 }
6995
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006996 bool Done() const { return index_ == blocks_.size(); }
6997 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006998 void Advance() {
6999 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007000 for (size_t e = blocks_.size(); index_ < e; ++index_) {
7001 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07007002 break;
7003 }
7004 }
7005 }
7006
7007 private:
7008 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007009 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007010 size_t index_;
7011
7012 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7013};
7014
Aart Bikf3e61ee2017-04-12 17:09:20 -07007015// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007016inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007017 if (constant->IsIntConstant()) {
7018 return constant->AsIntConstant()->GetValue();
7019 } else if (constant->IsLongConstant()) {
7020 return constant->AsLongConstant()->GetValue();
7021 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007022 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007023 return 0;
7024 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007025}
7026
Aart Bikf3e61ee2017-04-12 17:09:20 -07007027// Returns true iff instruction is an integral constant (and sets value on success).
7028inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7029 if (instruction->IsIntConstant()) {
7030 *value = instruction->AsIntConstant()->GetValue();
7031 return true;
7032 } else if (instruction->IsLongConstant()) {
7033 *value = instruction->AsLongConstant()->GetValue();
7034 return true;
7035 } else if (instruction->IsNullConstant()) {
7036 *value = 0;
7037 return true;
7038 }
7039 return false;
7040}
7041
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007042#define INSTRUCTION_TYPE_CHECK(type, super) \
7043 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7044 inline const H##type* HInstruction::As##type() const { \
7045 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7046 } \
7047 inline H##type* HInstruction::As##type() { \
7048 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7049 }
7050
7051 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7052#undef INSTRUCTION_TYPE_CHECK
7053
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007054// Create space in `blocks` for adding `number_of_new_blocks` entries
7055// starting at location `at`. Blocks after `at` are moved accordingly.
7056inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7057 size_t number_of_new_blocks,
7058 size_t after) {
7059 DCHECK_LT(after, blocks->size());
7060 size_t old_size = blocks->size();
7061 size_t new_size = old_size + number_of_new_blocks;
7062 blocks->resize(new_size);
7063 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7064}
7065
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007066/*
7067 * Hunt "under the hood" of array lengths (leading to array references),
7068 * null checks (also leading to array references), and new arrays
7069 * (leading to the actual length). This makes it more likely related
7070 * instructions become actually comparable.
7071 */
7072inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7073 while (instruction->IsArrayLength() ||
7074 instruction->IsNullCheck() ||
7075 instruction->IsNewArray()) {
7076 instruction = instruction->IsNewArray()
7077 ? instruction->AsNewArray()->GetLength()
7078 : instruction->InputAt(0);
7079 }
7080 return instruction;
7081}
7082
Artem Serov21c7e6f2017-07-27 16:04:42 +01007083void RemoveEnvironmentUses(HInstruction* instruction);
7084bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7085void ResetEnvironmentInputRecords(HInstruction* instruction);
7086
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007087} // namespace art
7088
7089#endif // ART_COMPILER_OPTIMIZING_NODES_H_