blob: 869fdd4182c3dc128c95a8f52796e11232617887 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "deoptimization_kind.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010032#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080033#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000034#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "handle.h"
36#include "handle_scope.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070038#include "invoke_type.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010039#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000040#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010042#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070043#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
David Brazdil1abb4192015-02-17 18:33:36 +000048class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070050class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000055class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000061class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070062class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010064class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010065class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010068class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Mathieu Chartier736b5602015-09-02 14:54:11 -070071namespace mirror {
72class DexCache;
73} // namespace mirror
74
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075static const int kDefaultNumberOfBlocks = 8;
76static const int kDefaultNumberOfSuccessors = 2;
77static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010078static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010079static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Roland Levillain5b5b9312016-03-22 14:57:31 +000082// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
83static constexpr int32_t kMaxIntShiftDistance = 0x1f;
84// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
85static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000086
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010087static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070088static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010090static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
91
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060092static constexpr uint32_t kNoDexPc = -1;
93
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010094inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
95 // For the purposes of the compiler, the dex files must actually be the same object
96 // if we want to safely treat them as the same. This is especially important for JIT
97 // as custom class loaders can open the same underlying file (or memory) multiple
98 // times and provide different class resolution but no two class loaders should ever
99 // use the same DexFile object - doing so is an unsupported hack that can lead to
100 // all sorts of weird failures.
101 return &lhs == &rhs;
102}
103
Dave Allison20dfc792014-06-16 20:44:29 -0700104enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700105 // All types.
106 kCondEQ, // ==
107 kCondNE, // !=
108 // Signed integers and floating-point numbers.
109 kCondLT, // <
110 kCondLE, // <=
111 kCondGT, // >
112 kCondGE, // >=
113 // Unsigned integers.
114 kCondB, // <
115 kCondBE, // <=
116 kCondA, // >
117 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100118 // First and last aliases.
119 kCondFirst = kCondEQ,
120 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700121};
122
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000125 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 kAnalysisFailThrowCatchLoop,
127 kAnalysisFailAmbiguousArrayOp,
128 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000129};
130
Andreas Gampe9186ced2016-12-12 14:28:21 -0800131template <typename T>
132static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
133 return static_cast<typename std::make_unsigned<T>::type>(x);
134}
135
Vladimir Markof9f64412015-09-02 14:05:49 +0100136class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 public:
138 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
139
140 void AddInstruction(HInstruction* instruction);
141 void RemoveInstruction(HInstruction* instruction);
142
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 // Insert `instruction` before/after an existing instruction `cursor`.
144 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
145 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
146
Roland Levillain6b469232014-09-25 10:10:38 +0100147 // Return true if this list contains `instruction`.
148 bool Contains(HInstruction* instruction) const;
149
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 // Return true if `instruction1` is found before `instruction2` in
151 // this instruction list and false otherwise. Abort if none
152 // of these instructions is found.
153 bool FoundBefore(const HInstruction* instruction1,
154 const HInstruction* instruction2) const;
155
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 bool IsEmpty() const { return first_instruction_ == nullptr; }
157 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
158
159 // Update the block of all instructions to be `block`.
160 void SetBlockOfInstructions(HBasicBlock* block) const;
161
162 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000163 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 void Add(const HInstructionList& instruction_list);
165
David Brazdil2d7352b2015-04-20 14:52:42 +0100166 // Return the number of instructions in the list. This is an expensive operation.
167 size_t CountSize() const;
168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 private:
170 HInstruction* first_instruction_;
171 HInstruction* last_instruction_;
172
173 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 friend class HGraph;
175 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000177 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
180 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
181};
182
David Brazdil4833f5a2015-12-16 10:37:39 +0000183class ReferenceTypeInfo : ValueObject {
184 public:
185 typedef Handle<mirror::Class> TypeHandle;
186
Vladimir Markoa1de9182016-02-25 11:37:38 +0000187 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100190 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
191 }
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return ReferenceTypeInfo(type_handle, is_exact);
195 }
196
197 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
198
Vladimir Markof39745e2016-01-26 12:16:55 +0000199 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return handle.GetReference() != nullptr;
201 }
202
Vladimir Marko456307a2016-04-19 14:12:13 +0000203 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return IsValidHandle(type_handle_);
205 }
206
207 bool IsExact() const { return is_exact_; }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsStringClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsInterface();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsPrimitiveArray();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 if (!IsExact()) return false;
247 if (!IsArrayClass()) return false;
248 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 if (!rti.IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
257 rti.GetTypeHandle()->GetComponentType());
258 }
259
260 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
261
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000263 DCHECK(IsValid());
264 DCHECK(rti.IsValid());
265 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
272 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100308 HGraph(ArenaAllocator* arena,
309 const DexFile& dex_file,
310 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_(arena->Adapter(kArenaAllocBlockList)),
318 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
319 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 entry_block_(nullptr),
321 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100322 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 number_of_vregs_(0),
324 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000325 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400326 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000327 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700328 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800329 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000331 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100333 dex_file_(dex_file),
334 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800337 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100340 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
343 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100345 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000346 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700347 osr_(osr),
348 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100349 blocks_.reserve(kDefaultNumberOfBlocks);
350 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351
David Brazdilbadd8262016-02-02 16:28:56 +0000352 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700353 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
357
David Brazdil69ba7b72015-06-23 18:27:30 +0100358 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000359 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 HBasicBlock* GetEntryBlock() const { return entry_block_; }
362 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100363 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
366 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100370 void ComputeDominanceInformation();
371 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000372 void ClearLoopInformation();
373 void FindBackEdges(ArenaBitVector* visited);
374 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100376 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // Analyze all natural loops in this graph. Returns a code specifying that it
379 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000380 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Iterate over blocks to compute try block membership. Needs reverse post
384 // order and loop information.
385 void ComputeTryBlockInformation();
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000388 // Returns the instruction to replace the invoke expression or null if the
389 // invoke is for a void method. Note that the caller is responsible for replacing
390 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000391 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000393 // Update the loop and try membership of `block`, which was spawned from `reference`.
394 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
395 // should be the new back edge.
396 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
397 HBasicBlock* reference,
398 bool replace_if_back_edge);
399
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 // Need to add a couple of blocks to test if the loop body is entered and
401 // put deoptimization instructions, etc.
402 void TransformLoopHeaderForBCE(HBasicBlock* header);
403
Aart Bikf8f5a162017-02-06 15:35:29 -0800404 // Adds a new loop directly after the loop with the given header and exit.
405 // Returns the new preheader.
406 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
407 HBasicBlock* body,
408 HBasicBlock* exit);
409
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000410 // Removes `block` from the graph. Assumes `block` has been disconnected from
411 // other blocks and has no instructions or phis.
412 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000413
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 // Splits the edge between `block` and `successor` while preserving the
415 // indices in the predecessor/successor lists. If there are multiple edges
416 // between the blocks, the lowest indices are used.
417 // Returns the new block which is empty and has the same dex pc as `successor`.
418 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
Artem Serovc73ee372017-07-31 15:08:40 +0100421 void OrderLoopHeaderPredecessors(HBasicBlock* header);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100422 void SimplifyLoop(HBasicBlock* header);
423
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 int32_t GetNextInstructionId() {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100425 CHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000426 return current_instruction_id_++;
427 }
428
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000429 int32_t GetCurrentInstructionId() const {
430 return current_instruction_id_;
431 }
432
433 void SetCurrentInstructionId(int32_t id) {
Nicolas Geoffrayc9c31042017-06-29 14:04:16 +0100434 CHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000435 current_instruction_id_ = id;
436 }
437
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100438 uint16_t GetMaximumNumberOfOutVRegs() const {
439 return maximum_number_of_out_vregs_;
440 }
441
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000442 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
443 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100444 }
445
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100446 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
447 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
448 }
449
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000450 void UpdateTemporariesVRegSlots(size_t slots) {
451 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100452 }
453
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100455 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000456 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100457 }
458
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100459 void SetNumberOfVRegs(uint16_t number_of_vregs) {
460 number_of_vregs_ = number_of_vregs;
461 }
462
463 uint16_t GetNumberOfVRegs() const {
464 return number_of_vregs_;
465 }
466
467 void SetNumberOfInVRegs(uint16_t value) {
468 number_of_in_vregs_ = value;
469 }
470
David Brazdildee58d62016-04-07 09:54:26 +0000471 uint16_t GetNumberOfInVRegs() const {
472 return number_of_in_vregs_;
473 }
474
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100475 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100476 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100477 return number_of_vregs_ - number_of_in_vregs_;
478 }
479
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100481 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100482 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100483
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100484 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
485 DCHECK(GetReversePostOrder()[0] == entry_block_);
486 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
487 }
488
489 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
490 return ReverseRange(GetReversePostOrder());
491 }
492
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100493 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100494 return linear_order_;
495 }
496
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100497 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
498 return ReverseRange(GetLinearOrder());
499 }
500
Mark Mendell1152c922015-04-24 17:06:35 -0400501 bool HasBoundsChecks() const {
502 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800503 }
504
Mark Mendell1152c922015-04-24 17:06:35 -0400505 void SetHasBoundsChecks(bool value) {
506 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800507 }
508
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000509 bool IsDebuggable() const { return debuggable_; }
510
David Brazdil8d5b8b22015-03-24 10:51:52 +0000511 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000512 // already, it is created and inserted into the graph. This method is only for
513 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000515
516 // TODO: This is problematic for the consistency of reference type propagation
517 // because it can be created anytime after the pass and thus it will be left
518 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000520
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
522 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000523 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600524 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
525 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000526 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600527 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
528 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000529 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600530 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
531 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000532 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000533
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100534 HCurrentMethod* GetCurrentMethod();
535
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100536 const DexFile& GetDexFile() const {
537 return dex_file_;
538 }
539
540 uint32_t GetMethodIdx() const {
541 return method_idx_;
542 }
543
Igor Murashkind01745e2017-04-05 16:40:31 -0700544 // Get the method name (without the signature), e.g. "<init>"
545 const char* GetMethodName() const;
546
547 // Get the pretty method name (class + name + optionally signature).
548 std::string PrettyMethod(bool with_signature = true) const;
549
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100550 InvokeType GetInvokeType() const {
551 return invoke_type_;
552 }
553
Mark Mendellc4701932015-04-10 13:18:51 -0400554 InstructionSet GetInstructionSet() const {
555 return instruction_set_;
556 }
557
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000558 bool IsCompilingOsr() const { return osr_; }
559
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
561 return cha_single_implementation_list_;
562 }
563
564 void AddCHASingleImplementationDependency(ArtMethod* method) {
565 cha_single_implementation_list_.insert(method);
566 }
567
568 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800569 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700570 }
571
David Brazdil77a48ae2015-09-15 12:34:04 +0000572 bool HasTryCatch() const { return has_try_catch_; }
573 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100574
Aart Bikb13c65b2017-03-21 20:14:07 -0700575 bool HasSIMD() const { return has_simd_; }
576 void SetHasSIMD(bool value) { has_simd_ = value; }
577
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800578 bool HasLoops() const { return has_loops_; }
579 void SetHasLoops(bool value) { has_loops_ = value; }
580
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000581 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
582 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
583
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100584 ArtMethod* GetArtMethod() const { return art_method_; }
585 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
586
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100587 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500588 // The instruction has been inserted into the graph, either as a constant, or
589 // before cursor.
590 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
591
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000592 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
593
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800594 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
595 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
596 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
597
David Brazdil2d7352b2015-04-20 14:52:42 +0100598 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000599 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100600 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000601
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 template <class InstructionType, typename ValueType>
603 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600604 ArenaSafeMap<ValueType, InstructionType*>* cache,
605 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Try to find an existing constant of the given value.
607 InstructionType* constant = nullptr;
608 auto cached_constant = cache->find(value);
609 if (cached_constant != cache->end()) {
610 constant = cached_constant->second;
611 }
612
613 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100614 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600616 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000617 cache->Overwrite(value, constant);
618 InsertConstant(constant);
619 }
620 return constant;
621 }
622
David Brazdil8d5b8b22015-03-24 10:51:52 +0000623 void InsertConstant(HConstant* instruction);
624
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000625 // Cache a float constant into the graph. This method should only be
626 // called by the SsaBuilder when creating "equivalent" instructions.
627 void CacheFloatConstant(HFloatConstant* constant);
628
629 // See CacheFloatConstant comment.
630 void CacheDoubleConstant(HDoubleConstant* constant);
631
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000633
634 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100635 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000636
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100637 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000639
Aart Bik281c6812016-08-26 11:31:48 -0700640 // List of blocks to perform a linear order tree traversal. Unlike the reverse
641 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100642 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100643
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000644 HBasicBlock* entry_block_;
645 HBasicBlock* exit_block_;
646
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100647 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100648 uint16_t maximum_number_of_out_vregs_;
649
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100650 // The number of virtual registers in this method. Contains the parameters.
651 uint16_t number_of_vregs_;
652
653 // The number of virtual registers used by parameters of this method.
654 uint16_t number_of_in_vregs_;
655
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000656 // Number of vreg size slots that the temporaries use (used in baseline compiler).
657 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800659 // Flag whether there are bounds checks in the graph. We can skip
660 // BCE if it's false. It's only best effort to keep it up to date in
661 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400662 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are try/catch blocks in the graph. We will skip
665 // try/catch-related passes if it's false. It's only best effort to keep
666 // it up to date in the presence of code elimination so there might be
667 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000668 bool has_try_catch_;
669
Aart Bikb13c65b2017-03-21 20:14:07 -0700670 // Flag whether SIMD instructions appear in the graph. If true, the
671 // code generators may have to be more careful spilling the wider
672 // contents of SIMD registers.
673 bool has_simd_;
674
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800675 // Flag whether there are any loops in the graph. We can skip loop
676 // optimization if it's false. It's only best effort to keep it up
677 // to date in the presence of code elimination so there might be false
678 // positives.
679 bool has_loops_;
680
681 // Flag whether there are any irreducible loops in the graph. It's only
682 // best effort to keep it up to date in the presence of code elimination
683 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000684 bool has_irreducible_loops_;
685
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000686 // Indicates whether the graph should be compiled in a way that
687 // ensures full debuggability. If false, we can apply more
688 // aggressive optimizations that may limit the level of debugging.
689 const bool debuggable_;
690
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000692 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000693
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100694 // The dex file from which the method is from.
695 const DexFile& dex_file_;
696
697 // The method index in the dex file.
698 const uint32_t method_idx_;
699
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100700 // If inlined, this encodes how the callee is being invoked.
701 const InvokeType invoke_type_;
702
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100703 // Whether the graph has been transformed to SSA form. Only used
704 // in debug mode to ensure we are not using properties only valid
705 // for non-SSA form (like the number of temporaries).
706 bool in_ssa_form_;
707
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800708 // Number of CHA guards in the graph. Used to short-circuit the
709 // CHA guard optimization pass when there is no CHA guard left.
710 uint32_t number_of_cha_guards_;
711
Mathieu Chartiere401d142015-04-22 13:56:20 -0700712 const InstructionSet instruction_set_;
713
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000714 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000715 HNullConstant* cached_null_constant_;
716 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000718 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000719 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000720
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100721 HCurrentMethod* cached_current_method_;
722
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100723 // The ArtMethod this graph is for. Note that for AOT, it may be null,
724 // for example for methods whose declaring class could not be resolved
725 // (such as when the superclass could not be found).
726 ArtMethod* art_method_;
727
David Brazdil4833f5a2015-12-16 10:37:39 +0000728 // Keep the RTI of inexact Object to avoid having to pass stack handle
729 // collection pointer to passes which may create NullConstant.
730 ReferenceTypeInfo inexact_object_rti_;
731
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000732 // Whether we are compiling this graph for on stack replacement: this will
733 // make all loops seen as irreducible and emit special stack maps to mark
734 // compiled code entries which the interpreter can directly jump to.
735 const bool osr_;
736
Mingyao Yang063fc772016-08-02 11:02:54 -0700737 // List of methods that are assumed to have single implementation.
738 ArenaSet<ArtMethod*> cha_single_implementation_list_;
739
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000740 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100741 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000742 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000743 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000744 DISALLOW_COPY_AND_ASSIGN(HGraph);
745};
746
Vladimir Markof9f64412015-09-02 14:05:49 +0100747class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000748 public:
749 HLoopInformation(HBasicBlock* header, HGraph* graph)
750 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100751 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100753 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100754 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100755 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000756 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100757 back_edges_.reserve(kDefaultNumberOfBackEdges);
758 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100761 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000762
763 void Dump(std::ostream& os);
764
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 HBasicBlock* GetHeader() const {
766 return header_;
767 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000769 void SetHeader(HBasicBlock* block) {
770 header_ = block;
771 }
772
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100773 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
774 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
775 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
776
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000779 }
780
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100782 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 }
784
David Brazdil46e2a392015-03-16 17:31:52 +0000785 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 }
788
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000789 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100790 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000791 }
792
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100793 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100794
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100796 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100797 }
798
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100799 // Returns the lifetime position of the back edge that has the
800 // greatest lifetime position.
801 size_t GetLifetimeEnd() const;
802
803 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100804 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100805 }
806
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000807 // Finds blocks that are part of this loop.
808 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100809
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810 // Returns whether this loop information contains `block`.
811 // Note that this loop information *must* be populated before entering this function.
812 bool Contains(const HBasicBlock& block) const;
813
814 // Returns whether this loop information is an inner loop of `other`.
815 // Note that `other` *must* be populated before entering this function.
816 bool IsIn(const HLoopInformation& other) const;
817
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800818 // Returns true if instruction is not defined within this loop.
819 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700820
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100821 const ArenaBitVector& GetBlocks() const { return blocks_; }
822
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000824 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000825
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000826 void ClearAllBlocks() {
827 blocks_.ClearAllBits();
828 }
829
David Brazdil3f4a5222016-05-06 12:46:21 +0100830 bool HasBackEdgeNotDominatedByHeader() const;
831
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100832 bool IsPopulated() const {
833 return blocks_.GetHighestBitSet() != -1;
834 }
835
Anton Shaminf89381f2016-05-16 16:44:13 +0600836 bool DominatesAllBackEdges(HBasicBlock* block);
837
David Sehrc757dec2016-11-04 15:48:34 -0700838 bool HasExitEdge() const;
839
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000840 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 // Internal recursive implementation of `Populate`.
842 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100843 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100844
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000845 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100846 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000847 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100848 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100849 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100850 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000851
852 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
853};
854
David Brazdilec16f792015-08-19 15:04:01 +0100855// Stores try/catch information for basic blocks.
856// Note that HGraph is constructed so that catch blocks cannot simultaneously
857// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100858class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100859 public:
860 // Try block information constructor.
861 explicit TryCatchInformation(const HTryBoundary& try_entry)
862 : try_entry_(&try_entry),
863 catch_dex_file_(nullptr),
864 catch_type_index_(DexFile::kDexNoIndex16) {
865 DCHECK(try_entry_ != nullptr);
866 }
867
868 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800869 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100870 : try_entry_(nullptr),
871 catch_dex_file_(&dex_file),
872 catch_type_index_(catch_type_index) {}
873
874 bool IsTryBlock() const { return try_entry_ != nullptr; }
875
876 const HTryBoundary& GetTryEntry() const {
877 DCHECK(IsTryBlock());
878 return *try_entry_;
879 }
880
881 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
882
883 bool IsCatchAllTypeIndex() const {
884 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800885 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100886 }
887
Andreas Gampea5b09a62016-11-17 15:21:22 -0800888 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100889 DCHECK(IsCatchBlock());
890 return catch_type_index_;
891 }
892
893 const DexFile& GetCatchDexFile() const {
894 DCHECK(IsCatchBlock());
895 return *catch_dex_file_;
896 }
897
898 private:
899 // One of possibly several TryBoundary instructions entering the block's try.
900 // Only set for try blocks.
901 const HTryBoundary* try_entry_;
902
903 // Exception type information. Only set for catch blocks.
904 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800905 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100906};
907
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100909static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100910
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000911// A block in a method. Contains the list of instructions represented
912// as a double linked list. Each block knows its predecessors and
913// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100914
Vladimir Markof9f64412015-09-02 14:05:49 +0100915class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700917 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000919 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
920 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000921 loop_information_(nullptr),
922 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000923 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100924 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100925 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000927 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000928 try_catch_information_(nullptr) {
929 predecessors_.reserve(kDefaultNumberOfPredecessors);
930 successors_.reserve(kDefaultNumberOfSuccessors);
931 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
932 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000933
Vladimir Marko60584552015-09-03 13:35:12 +0000934 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100935 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000936 }
937
Vladimir Marko60584552015-09-03 13:35:12 +0000938 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100939 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940 }
941
David Brazdild26a4112015-11-10 11:07:31 +0000942 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
943 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
944
Vladimir Marko60584552015-09-03 13:35:12 +0000945 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
946 return ContainsElement(successors_, block, start_from);
947 }
948
949 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100950 return dominated_blocks_;
951 }
952
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100953 bool IsEntryBlock() const {
954 return graph_->GetEntryBlock() == this;
955 }
956
957 bool IsExitBlock() const {
958 return graph_->GetExitBlock() == this;
959 }
960
David Brazdil46e2a392015-03-16 17:31:52 +0000961 bool IsSingleGoto() const;
Mads Ager16e52892017-07-14 13:11:37 +0200962 bool IsSingleReturn() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963 bool IsSingleTryBoundary() const;
964
965 // Returns true if this block emits nothing but a jump.
966 bool IsSingleJump() const {
967 HLoopInformation* loop_info = GetLoopInformation();
968 return (IsSingleGoto() || IsSingleTryBoundary())
969 // Back edges generate a suspend check.
970 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
971 }
David Brazdil46e2a392015-03-16 17:31:52 +0000972
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 void AddBackEdge(HBasicBlock* back_edge) {
974 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100977 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000978 loop_information_->AddBackEdge(back_edge);
979 }
980
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000981 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000982 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100984 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600986 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000987
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000988 HBasicBlock* GetDominator() const { return dominator_; }
989 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000990 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
991
992 void RemoveDominatedBlock(HBasicBlock* block) {
993 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100994 }
Vladimir Marko60584552015-09-03 13:35:12 +0000995
996 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
997 ReplaceElement(dominated_blocks_, existing, new_block);
998 }
999
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001000 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001001
1002 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001003 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001004 }
1005
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001006 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1007 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001008 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001009 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001010 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1011 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001013 HInstruction* GetFirstInstructionDisregardMoves() const;
1014
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.push_back(block);
1017 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001018 }
1019
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1021 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001022 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001023 new_block->predecessors_.push_back(this);
1024 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001025 }
1026
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1028 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001030 new_block->successors_.push_back(this);
1031 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001032 }
1033
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001034 // Insert `this` between `predecessor` and `successor. This method
1035 // preserves the indicies, and will update the first edge found between
1036 // `predecessor` and `successor`.
1037 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1038 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001039 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001040 successor->predecessors_[predecessor_index] = this;
1041 predecessor->successors_[successor_index] = this;
1042 successors_.push_back(successor);
1043 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001044 }
1045
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001046 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001048 }
1049
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001051 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001052 }
1053
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001056 }
1057
1058 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 predecessors_.push_back(block);
1060 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001061 }
1062
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001063 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 DCHECK_EQ(predecessors_.size(), 2u);
1065 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001066 }
1067
David Brazdil769c9e52015-04-27 13:54:09 +01001068 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001069 DCHECK_EQ(successors_.size(), 2u);
1070 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001071 }
1072
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001074 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001075 }
1076
David Brazdilfc6a86a2015-06-26 10:33:45 +00001077 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001079 }
1080
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001082 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001083 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001084 }
1085
1086 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001087 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001088 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001089 }
1090
1091 // Returns whether the first occurrence of `predecessor` in the list of
1092 // predecessors is at index `idx`.
1093 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001094 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001095 return GetPredecessorIndexOf(predecessor) == idx;
1096 }
1097
David Brazdild7558da2015-09-22 13:04:14 +01001098 // Create a new block between this block and its predecessors. The new block
1099 // is added to the graph, all predecessor edges are relinked to it and an edge
1100 // is created to `this`. Returns the new empty block. Reverse post order or
1101 // loop and try/catch information are not updated.
1102 HBasicBlock* CreateImmediateDominator();
1103
David Brazdilfc6a86a2015-06-26 10:33:45 +00001104 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001105 // created, latter block. Note that this method will add the block to the
1106 // graph, create a Goto at the end of the former block and will create an edge
1107 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001108 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001109 HBasicBlock* SplitBefore(HInstruction* cursor);
1110
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001111 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001112 // created block. Note that this method just updates raw block information,
1113 // like predecessors, successors, dominators, and instruction list. It does not
1114 // update the graph, reverse post order, loop information, nor make sure the
1115 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001116 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1117
1118 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1119 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001120
1121 // Merge `other` at the end of `this`. Successors and dominated blocks of
1122 // `other` are changed to be successors and dominated blocks of `this`. Note
1123 // that this method does not update the graph, reverse post order, loop
1124 // information, nor make sure the blocks are consistent (for example ending
1125 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001126 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001127
1128 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1129 // of `this` are moved to `other`.
1130 // Note that this method does not update the graph, reverse post order, loop
1131 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001132 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001133 void ReplaceWith(HBasicBlock* other);
1134
Aart Bik6b69e0a2017-01-11 10:20:43 -08001135 // Merges the instructions of `other` at the end of `this`.
1136 void MergeInstructionsWith(HBasicBlock* other);
1137
David Brazdil2d7352b2015-04-20 14:52:42 +01001138 // Merge `other` at the end of `this`. This method updates loops, reverse post
1139 // order, links to predecessors, successors, dominators and deletes the block
1140 // from the graph. The two blocks must be successive, i.e. `this` the only
1141 // predecessor of `other` and vice versa.
1142 void MergeWith(HBasicBlock* other);
1143
1144 // Disconnects `this` from all its predecessors, successors and dominator,
1145 // removes it from all loops it is included in and eventually from the graph.
1146 // The block must not dominate any other block. Predecessors and successors
1147 // are safely updated.
1148 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001149
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001150 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001152 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001153 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001154 // Replace instruction `initial` with `replacement` within this block.
1155 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1156 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001158 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001159 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1160 // instruction list. With 'ensure_safety' set to true, it verifies that the
1161 // instruction is not in use and removes it from the use lists of its inputs.
1162 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1163 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001164 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165
1166 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001167 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001168 }
1169
Roland Levillain6b879dd2014-09-22 17:13:44 +01001170 bool IsLoopPreHeaderFirstPredecessor() const {
1171 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001172 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001173 }
1174
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001175 bool IsFirstPredecessorBackEdge() const {
1176 DCHECK(IsLoopHeader());
1177 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1178 }
1179
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001180 HLoopInformation* GetLoopInformation() const {
1181 return loop_information_;
1182 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001183
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001186 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001187 void SetInLoop(HLoopInformation* info) {
1188 if (IsLoopHeader()) {
1189 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001190 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001191 loop_information_ = info;
1192 } else if (loop_information_->Contains(*info->GetHeader())) {
1193 // Block is currently part of an outer loop. Make it part of this inner loop.
1194 // Note that a non loop header having a loop information means this loop information
1195 // has already been populated
1196 loop_information_ = info;
1197 } else {
1198 // Block is part of an inner loop. Do not update the loop information.
1199 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1200 // at this point, because this method is being called while populating `info`.
1201 }
1202 }
1203
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001204 // Raw update of the loop information.
1205 void SetLoopInformation(HLoopInformation* info) {
1206 loop_information_ = info;
1207 }
1208
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001209 bool IsInLoop() const { return loop_information_ != nullptr; }
1210
David Brazdilec16f792015-08-19 15:04:01 +01001211 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1212
1213 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1214 try_catch_information_ = try_catch_information;
1215 }
1216
1217 bool IsTryBlock() const {
1218 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1219 }
1220
1221 bool IsCatchBlock() const {
1222 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1223 }
David Brazdilffee3d32015-07-06 11:48:53 +01001224
1225 // Returns the try entry that this block's successors should have. They will
1226 // be in the same try, unless the block ends in a try boundary. In that case,
1227 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001228 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001229
David Brazdild7558da2015-09-22 13:04:14 +01001230 bool HasThrowingInstructions() const;
1231
David Brazdila4b8c212015-05-07 09:59:30 +01001232 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001233 bool Dominates(HBasicBlock* block) const;
1234
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001235 size_t GetLifetimeStart() const { return lifetime_start_; }
1236 size_t GetLifetimeEnd() const { return lifetime_end_; }
1237
1238 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1239 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1240
David Brazdil8d5b8b22015-03-24 10:51:52 +00001241 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001243 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001244 bool HasSinglePhi() const;
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001247 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001248 ArenaVector<HBasicBlock*> predecessors_;
1249 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001250 HInstructionList instructions_;
1251 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001252 HLoopInformation* loop_information_;
1253 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001254 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001255 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001256 // The dex program counter of the first instruction of this block.
1257 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001258 size_t lifetime_start_;
1259 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001260 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001261
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001262 friend class HGraph;
1263 friend class HInstruction;
1264
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001265 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1266};
1267
David Brazdilb2bd1c52015-03-25 11:17:37 +00001268// Iterates over the LoopInformation of all loops which contain 'block'
1269// from the innermost to the outermost.
1270class HLoopInformationOutwardIterator : public ValueObject {
1271 public:
1272 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1273 : current_(block.GetLoopInformation()) {}
1274
1275 bool Done() const { return current_ == nullptr; }
1276
1277 void Advance() {
1278 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001279 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001280 }
1281
1282 HLoopInformation* Current() const {
1283 DCHECK(!Done());
1284 return current_;
1285 }
1286
1287 private:
1288 HLoopInformation* current_;
1289
1290 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1291};
1292
Alexandre Ramesef20f712015-06-09 10:29:30 +01001293#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001294 M(Above, Condition) \
1295 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001297 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001298 M(ArrayGet, Instruction) \
1299 M(ArrayLength, Instruction) \
1300 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001301 M(Below, Condition) \
1302 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001303 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001304 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001305 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001306 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001307 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001308 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001309 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001310 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001311 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001312 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001313 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001314 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001316 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001319 M(Exit, Instruction) \
1320 M(FloatConstant, Constant) \
1321 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(GreaterThan, Condition) \
1323 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001325 M(InstanceFieldGet, Instruction) \
1326 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001327 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001329 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001330 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001331 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001332 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001333 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(LessThan, Condition) \
1335 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001337 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001338 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001339 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001340 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001341 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001343 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(Neg, UnaryOperation) \
1345 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001347 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001349 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001351 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001352 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001354 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001356 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Return, Instruction) \
1358 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001359 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001360 M(Shl, BinaryOperation) \
1361 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001362 M(StaticFieldGet, Instruction) \
1363 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001364 M(UnresolvedInstanceFieldGet, Instruction) \
1365 M(UnresolvedInstanceFieldSet, Instruction) \
1366 M(UnresolvedStaticFieldGet, Instruction) \
1367 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001368 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001369 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001370 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001371 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001372 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001373 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001374 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001375 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001376 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bik0148de42017-09-05 09:25:01 -07001377 M(VecExtractScalar, VecUnaryOperation) \
1378 M(VecReduce, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001379 M(VecCnv, VecUnaryOperation) \
1380 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001381 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001382 M(VecNot, VecUnaryOperation) \
1383 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001384 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001385 M(VecSub, VecBinaryOperation) \
1386 M(VecMul, VecBinaryOperation) \
1387 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001388 M(VecMin, VecBinaryOperation) \
1389 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001390 M(VecAnd, VecBinaryOperation) \
1391 M(VecAndNot, VecBinaryOperation) \
1392 M(VecOr, VecBinaryOperation) \
1393 M(VecXor, VecBinaryOperation) \
1394 M(VecShl, VecBinaryOperation) \
1395 M(VecShr, VecBinaryOperation) \
1396 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001397 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001398 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001399 M(VecLoad, VecMemoryOperation) \
1400 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001401
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001402/*
1403 * Instructions, shared across several (not all) architectures.
1404 */
1405#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1406#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1407#else
1408#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001409 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001410 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001411 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001412 M(IntermediateAddress, Instruction) \
1413 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001414#endif
1415
Alexandre Ramesef20f712015-06-09 10:29:30 +01001416#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
1417
1418#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1419
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001420#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001421#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001422#else
1423#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1424 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001425 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001426#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001427
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001428#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1429
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001430#ifndef ART_ENABLE_CODEGEN_x86
1431#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1432#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001433#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1434 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001435 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001436 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001437 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001438#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001439
1440#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1441
1442#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1443 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001444 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001445 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1446 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001447 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001448 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001449 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1450 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1451
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001452#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1453 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001454 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001455 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001456 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001457 M(Invoke, Instruction) \
1458 M(VecOperation, Instruction) \
1459 M(VecUnaryOperation, VecOperation) \
1460 M(VecBinaryOperation, VecOperation) \
1461 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001462
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001463#define FOR_EACH_INSTRUCTION(M) \
1464 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1465 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1466
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001467#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001468FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1469#undef FORWARD_DECLARATION
1470
Vladimir Marko372f10e2016-05-17 16:30:10 +01001471#define DECLARE_INSTRUCTION(type) \
1472 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1473 const char* DebugName() const OVERRIDE { return #type; } \
1474 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1475 return other->Is##type(); \
1476 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001477 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001478
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001479#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1480 bool Is##type() const { return As##type() != nullptr; } \
1481 const H##type* As##type() const { return this; } \
1482 H##type* As##type() { return this; }
1483
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001484template <typename T>
Vladimir Marko82b07402017-03-01 19:02:04 +00001485class HUseListNode : public ArenaObject<kArenaAllocUseListNode>,
1486 public IntrusiveForwardListNode<HUseListNode<T>> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001488 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001489 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001490 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001491 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001492 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001493 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001494
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001495 private:
David Brazdiled596192015-01-23 10:39:45 +00001496 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001497 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001498
1499 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001500 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001501
Vladimir Marko46817b82016-03-29 12:21:58 +01001502 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001503
1504 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1505};
1506
David Brazdiled596192015-01-23 10:39:45 +00001507template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001508using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001509
David Brazdil1abb4192015-02-17 18:33:36 +00001510// This class is used by HEnvironment and HInstruction classes to record the
1511// instructions they use and pointers to the corresponding HUseListNodes kept
1512// by the used instructions.
1513template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001514class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001515 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001516 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1517 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001518
Vladimir Marko46817b82016-03-29 12:21:58 +01001519 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1520 : HUserRecord(old_record.instruction_, before_use_node) {}
1521 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1522 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001523 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001524 }
1525
1526 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001527 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1528 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001529
1530 private:
1531 // Instruction used by the user.
1532 HInstruction* instruction_;
1533
Vladimir Marko46817b82016-03-29 12:21:58 +01001534 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1535 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001536};
1537
Vladimir Markoe9004912016-06-16 16:50:52 +01001538// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1539// This is used for HInstruction::GetInputs() to return a container wrapper providing
1540// HInstruction* values even though the underlying container has HUserRecord<>s.
1541struct HInputExtractor {
1542 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1543 return record.GetInstruction();
1544 }
1545 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1546 return record.GetInstruction();
1547 }
1548};
1549
1550using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1551using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1552
Aart Bik854a02b2015-07-14 16:07:00 -07001553/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001555 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 * For write/read dependences on fields/arrays, the dependence analysis uses
1557 * type disambiguation (e.g. a float field write cannot modify the value of an
1558 * integer field read) and the access type (e.g. a reference array write cannot
1559 * modify the value of a reference field read [although it may modify the
1560 * reference fetch prior to reading the field, which is represented by its own
1561 * write/read dependence]). The analysis makes conservative points-to
1562 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1563 * the same, and any reference read depends on any reference read without
1564 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001565 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 * The internal representation uses 38-bit and is described in the table below.
1567 * The first line indicates the side effect, and for field/array accesses the
1568 * second line indicates the type of the access (in the order of the
1569 * Primitive::Type enum).
1570 * The two numbered lines below indicate the bit position in the bitfield (read
1571 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001572 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001573 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1574 * +-------------+---------+---------+--------------+---------+---------+
1575 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1576 * | 3 |333333322|222222221| 1 |111111110|000000000|
1577 * | 7 |654321098|765432109| 8 |765432109|876543210|
1578 *
1579 * Note that, to ease the implementation, 'changes' bits are least significant
1580 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001581 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582class SideEffects : public ValueObject {
1583 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001584 SideEffects() : flags_(0) {}
1585
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001586 static SideEffects None() {
1587 return SideEffects(0);
1588 }
1589
1590 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 return SideEffects(kAllChangeBits | kAllDependOnBits);
1592 }
1593
1594 static SideEffects AllChanges() {
1595 return SideEffects(kAllChangeBits);
1596 }
1597
1598 static SideEffects AllDependencies() {
1599 return SideEffects(kAllDependOnBits);
1600 }
1601
1602 static SideEffects AllExceptGCDependency() {
1603 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1604 }
1605
1606 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001607 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001608 }
1609
Aart Bik34c3ba92015-07-20 14:08:59 -07001610 static SideEffects AllWrites() {
1611 return SideEffects(kAllWrites);
1612 }
1613
1614 static SideEffects AllReads() {
1615 return SideEffects(kAllReads);
1616 }
1617
1618 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1619 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001621 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622 }
1623
Aart Bik854a02b2015-07-14 16:07:00 -07001624 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001625 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626 }
1627
Aart Bik34c3ba92015-07-20 14:08:59 -07001628 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1629 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001631 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001632 }
1633
1634 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001635 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001636 }
1637
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 static SideEffects CanTriggerGC() {
1639 return SideEffects(1ULL << kCanTriggerGCBit);
1640 }
1641
1642 static SideEffects DependsOnGC() {
1643 return SideEffects(1ULL << kDependsOnGCBit);
1644 }
1645
Aart Bik854a02b2015-07-14 16:07:00 -07001646 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001647 SideEffects Union(SideEffects other) const {
1648 return SideEffects(flags_ | other.flags_);
1649 }
1650
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 SideEffects Exclusion(SideEffects other) const {
1652 return SideEffects(flags_ & ~other.flags_);
1653 }
1654
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001655 void Add(SideEffects other) {
1656 flags_ |= other.flags_;
1657 }
1658
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001659 bool Includes(SideEffects other) const {
1660 return (other.flags_ & flags_) == other.flags_;
1661 }
1662
1663 bool HasSideEffects() const {
1664 return (flags_ & kAllChangeBits);
1665 }
1666
1667 bool HasDependencies() const {
1668 return (flags_ & kAllDependOnBits);
1669 }
1670
1671 // Returns true if there are no side effects or dependencies.
1672 bool DoesNothing() const {
1673 return flags_ == 0;
1674 }
1675
Aart Bik854a02b2015-07-14 16:07:00 -07001676 // Returns true if something is written.
1677 bool DoesAnyWrite() const {
1678 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001679 }
1680
Aart Bik854a02b2015-07-14 16:07:00 -07001681 // Returns true if something is read.
1682 bool DoesAnyRead() const {
1683 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001684 }
1685
Aart Bik854a02b2015-07-14 16:07:00 -07001686 // Returns true if potentially everything is written and read
1687 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 bool DoesAllReadWrite() const {
1689 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1690 }
1691
Aart Bik854a02b2015-07-14 16:07:00 -07001692 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001693 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001694 }
1695
Roland Levillain0d5a2812015-11-13 10:07:31 +00001696 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001697 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1699 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001700 }
1701
1702 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001703 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001704 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001705 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001706 for (int s = kLastBit; s >= 0; s--) {
1707 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1708 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1709 // This is a bit for the GC side effect.
1710 if (current_bit_is_set) {
1711 flags += "GC";
1712 }
Aart Bik854a02b2015-07-14 16:07:00 -07001713 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001714 } else {
1715 // This is a bit for the array/field analysis.
1716 // The underscore character stands for the 'can trigger GC' bit.
1717 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1718 if (current_bit_is_set) {
1719 flags += kDebug[s];
1720 }
1721 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1722 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1723 flags += "|";
1724 }
1725 }
Aart Bik854a02b2015-07-14 16:07:00 -07001726 }
1727 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001728 }
1729
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001730 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001731
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001732 private:
1733 static constexpr int kFieldArrayAnalysisBits = 9;
1734
1735 static constexpr int kFieldWriteOffset = 0;
1736 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1737 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1738 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1739
1740 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1741
1742 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1743 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1744 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1745 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1746
1747 static constexpr int kLastBit = kDependsOnGCBit;
1748 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1749
1750 // Aliases.
1751
1752 static_assert(kChangeBits == kDependOnBits,
1753 "the 'change' bits should match the 'depend on' bits.");
1754
1755 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1756 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1757 static constexpr uint64_t kAllWrites =
1758 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1759 static constexpr uint64_t kAllReads =
1760 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001761
Aart Bik854a02b2015-07-14 16:07:00 -07001762 // Translates type to bit flag.
1763 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1764 CHECK_NE(type, Primitive::kPrimVoid);
1765 const uint64_t one = 1;
1766 const int shift = type; // 0-based consecutive enum
1767 DCHECK_LE(kFieldWriteOffset, shift);
1768 DCHECK_LT(shift, kArrayWriteOffset);
1769 return one << (type + offset);
1770 }
1771
1772 // Private constructor on direct flags value.
1773 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1774
1775 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001776};
1777
David Brazdiled596192015-01-23 10:39:45 +00001778// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001779class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001780 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001781 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1782 size_t number_of_vregs,
1783 ArtMethod* method,
1784 uint32_t dex_pc,
1785 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001787 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001789 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001790 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001791 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 }
1793
Mingyao Yang01b47b02017-02-03 12:09:57 -08001794 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001795 : HEnvironment(arena,
1796 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001797 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001798 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001799 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001800
Vladimir Markoec32f642017-06-02 10:51:55 +01001801 void AllocateLocations() {
1802 DCHECK(locations_.empty());
1803 locations_.resize(vregs_.size());
1804 }
1805
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001807 if (parent_ != nullptr) {
1808 parent_->SetAndCopyParentChain(allocator, parent);
1809 } else {
1810 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1811 parent_->CopyFrom(parent);
1812 if (parent->GetParent() != nullptr) {
1813 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1814 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 }
David Brazdiled596192015-01-23 10:39:45 +00001816 }
1817
Vladimir Marko71bf8092015-09-15 15:33:14 +01001818 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001819 void CopyFrom(HEnvironment* environment);
1820
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001821 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1822 // input to the loop phi instead. This is for inserting instructions that
1823 // require an environment (like HDeoptimization) in the loop pre-header.
1824 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001825
1826 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001828 }
1829
1830 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001831 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001832 }
1833
David Brazdil1abb4192015-02-17 18:33:36 +00001834 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001835
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001836 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001837
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001838 HEnvironment* GetParent() const { return parent_; }
1839
1840 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001841 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001842 }
1843
1844 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001845 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001846 }
1847
1848 uint32_t GetDexPc() const {
1849 return dex_pc_;
1850 }
1851
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001852 ArtMethod* GetMethod() const {
1853 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001854 }
1855
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001856 HInstruction* GetHolder() const {
1857 return holder_;
1858 }
1859
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001860
1861 bool IsFromInlinedInvoke() const {
1862 return GetParent() != nullptr;
1863 }
1864
David Brazdiled596192015-01-23 10:39:45 +00001865 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001866 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1867 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001868 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001869 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001870 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001871
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001872 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001873 HInstruction* const holder_;
1874
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001875 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001876
1877 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1878};
1879
Vladimir Markof9f64412015-09-02 14:05:49 +01001880class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001881 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001882 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001883 : previous_(nullptr),
1884 next_(nullptr),
1885 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001886 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001887 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001888 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001889 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001890 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001891 locations_(nullptr),
1892 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001893 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001894 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001895 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1896 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1897 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001898
Dave Allison20dfc792014-06-16 20:44:29 -07001899 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001901#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001902 enum InstructionKind {
1903 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1904 };
1905#undef DECLARE_KIND
1906
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001907 HInstruction* GetNext() const { return next_; }
1908 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001909
Calin Juravle77520bc2015-01-12 18:45:46 +00001910 HInstruction* GetNextDisregardingMoves() const;
1911 HInstruction* GetPreviousDisregardingMoves() const;
1912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001914 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001915 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001916 bool IsInBlock() const { return block_ != nullptr; }
1917 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001918 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1919 bool IsIrreducibleLoopHeaderPhi() const {
1920 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1921 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001922
Vladimir Marko372f10e2016-05-17 16:30:10 +01001923 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1924
1925 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1926 // One virtual method is enough, just const_cast<> and then re-add the const.
1927 return ArrayRef<const HUserRecord<HInstruction*>>(
1928 const_cast<HInstruction*>(this)->GetInputRecords());
1929 }
1930
Vladimir Markoe9004912016-06-16 16:50:52 +01001931 HInputsRef GetInputs() {
1932 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001933 }
1934
Vladimir Markoe9004912016-06-16 16:50:52 +01001935 HConstInputsRef GetInputs() const {
1936 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001937 }
1938
1939 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001940 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941
Aart Bik2767f4b2016-10-28 15:03:53 -07001942 bool HasInput(HInstruction* input) const {
1943 for (const HInstruction* i : GetInputs()) {
1944 if (i == input) {
1945 return true;
1946 }
1947 }
1948 return false;
1949 }
1950
Vladimir Marko372f10e2016-05-17 16:30:10 +01001951 void SetRawInputAt(size_t index, HInstruction* input) {
1952 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1953 }
1954
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001955 virtual void Accept(HGraphVisitor* visitor) = 0;
1956 virtual const char* DebugName() const = 0;
1957
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001958 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001961
1962 uint32_t GetDexPc() const { return dex_pc_; }
1963
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001964 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001965
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001966 // Can the instruction throw?
1967 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1968 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001969 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001970 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001971
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001972 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001973 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001974
Calin Juravle10e244f2015-01-26 18:54:32 +00001975 // Does not apply for all instructions, but having this at top level greatly
1976 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001977 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001978 virtual bool CanBeNull() const {
1979 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1980 return true;
1981 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001982
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001983 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001984 return false;
1985 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001986
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001987 virtual bool IsActualObject() const {
1988 return GetType() == Primitive::kPrimNot;
1989 }
1990
Calin Juravle2e768302015-07-28 14:41:11 +00001991 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001992
Calin Juravle61d544b2015-02-23 16:46:57 +00001993 ReferenceTypeInfo GetReferenceTypeInfo() const {
1994 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001995 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001996 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001997 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001999 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002000 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002001 // Note: fixup_end remains valid across push_front().
2002 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2003 HUseListNode<HInstruction*>* new_node =
2004 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2005 uses_.push_front(*new_node);
2006 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007 }
2008
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002009 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002010 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002011 // Note: env_fixup_end remains valid across push_front().
2012 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2013 HUseListNode<HEnvironment*>* new_node =
2014 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2015 env_uses_.push_front(*new_node);
2016 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002017 }
2018
David Brazdil1abb4192015-02-17 18:33:36 +00002019 void RemoveAsUserOfInput(size_t input) {
2020 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002021 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2022 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2023 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002024 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002025
Vladimir Marko372f10e2016-05-17 16:30:10 +01002026 void RemoveAsUserOfAllInputs() {
2027 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2028 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2029 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2030 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2031 }
2032 }
2033
David Brazdil1abb4192015-02-17 18:33:36 +00002034 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2035 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002036
Vladimir Marko46817b82016-03-29 12:21:58 +01002037 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2038 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2039 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002040 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002041 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002042 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002043
Aart Bikcc42be02016-10-20 16:14:16 -07002044 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002045 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002046 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002047 !CanThrow() &&
2048 !IsSuspendCheck() &&
2049 !IsControlFlow() &&
2050 !IsNativeDebugInfo() &&
2051 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002052 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002053 !IsMemoryBarrier() &&
2054 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002055 }
2056
Aart Bikcc42be02016-10-20 16:14:16 -07002057 bool IsDeadAndRemovable() const {
2058 return IsRemovable() && !HasUses();
2059 }
2060
Roland Levillain6c82d402014-10-13 16:10:27 +01002061 // Does this instruction strictly dominate `other_instruction`?
2062 // Returns false if this instruction and `other_instruction` are the same.
2063 // Aborts if this instruction and `other_instruction` are both phis.
2064 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002065
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002066 int GetId() const { return id_; }
2067 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002068
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002069 int GetSsaIndex() const { return ssa_index_; }
2070 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2071 bool HasSsaIndex() const { return ssa_index_ != -1; }
2072
2073 bool HasEnvironment() const { return environment_ != nullptr; }
2074 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002075 // Set the `environment_` field. Raw because this method does not
2076 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002077 void SetRawEnvironment(HEnvironment* environment) {
2078 DCHECK(environment_ == nullptr);
2079 DCHECK_EQ(environment->GetHolder(), this);
2080 environment_ = environment;
2081 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002082
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002083 void InsertRawEnvironment(HEnvironment* environment) {
2084 DCHECK(environment_ != nullptr);
2085 DCHECK_EQ(environment->GetHolder(), this);
2086 DCHECK(environment->GetParent() == nullptr);
2087 environment->parent_ = environment_;
2088 environment_ = environment;
2089 }
2090
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002091 void RemoveEnvironment();
2092
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002093 // Set the environment of this instruction, copying it from `environment`. While
2094 // copying, the uses lists are being updated.
2095 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002096 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002097 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002098 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002099 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002100 if (environment->GetParent() != nullptr) {
2101 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2102 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002103 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002104
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002105 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2106 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002107 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002108 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002109 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002110 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002111 if (environment->GetParent() != nullptr) {
2112 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2113 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002114 }
2115
Nicolas Geoffray39468442014-09-02 15:17:15 +01002116 // Returns the number of entries in the environment. Typically, that is the
2117 // number of dex registers in a method. It could be more in case of inlining.
2118 size_t EnvironmentSize() const;
2119
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002120 LocationSummary* GetLocations() const { return locations_; }
2121 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002123 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002124 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002125 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002126
Alexandre Rames188d4312015-04-09 18:30:21 +01002127 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2128 // uses of this instruction by `other` are *not* updated.
2129 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2130 ReplaceWith(other);
2131 other->ReplaceInput(this, use_index);
2132 }
2133
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002134 // Move `this` instruction before `cursor`
2135 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002136
Vladimir Markofb337ea2015-11-25 15:25:10 +00002137 // Move `this` before its first user and out of any loops. If there is no
2138 // out-of-loop user that dominates all other users, move the instruction
2139 // to the end of the out-of-loop common dominator of the user's blocks.
2140 //
2141 // This can be used only on non-throwing instructions with no side effects that
2142 // have at least one use but no environment uses.
2143 void MoveBeforeFirstUserAndOutOfLoops();
2144
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002145#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 bool Is##type() const; \
2147 const H##type* As##type() const; \
2148 H##type* As##type();
2149
2150 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2151#undef INSTRUCTION_TYPE_CHECK
2152
2153#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002154 bool Is##type() const { return (As##type() != nullptr); } \
2155 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002156 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002157 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002158#undef INSTRUCTION_TYPE_CHECK
2159
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002160 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002161 // TODO: this method is used by LICM and GVN with possibly different
2162 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163 virtual bool CanBeMoved() const { return false; }
2164
2165 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002166 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002167 return false;
2168 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169
2170 // Returns whether any data encoded in the two instructions is equal.
2171 // This method does not look at the inputs. Both instructions must be
2172 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002173 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002174 return false;
2175 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002176
2177 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002178 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002179 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002180 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002181
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002182 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2183 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2184 // the virtual function because the __attribute__((__pure__)) doesn't really
2185 // apply the strong requirement for virtual functions, preventing optimizations.
2186 InstructionKind GetKind() const PURE;
2187 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002188
2189 virtual size_t ComputeHashCode() const {
2190 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002191 for (const HInstruction* input : GetInputs()) {
2192 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002193 }
2194 return result;
2195 }
2196
2197 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002198 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002199 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002200
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002201 size_t GetLifetimePosition() const { return lifetime_position_; }
2202 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2203 LiveInterval* GetLiveInterval() const { return live_interval_; }
2204 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2205 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2206
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002207 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2208
2209 // Returns whether the code generation of the instruction will require to have access
2210 // to the current method. Such instructions are:
2211 // (1): Instructions that require an environment, as calling the runtime requires
2212 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002213 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2214 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002215 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002216 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002217 }
2218
Vladimir Markodc151b22015-10-15 18:02:30 +01002219 // Returns whether the code generation of the instruction will require to have access
2220 // to the dex cache of the current method's declaring class via the current method.
2221 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002222
Mark Mendellc4701932015-04-10 13:18:51 -04002223 // Does this instruction have any use in an environment before
2224 // control flow hits 'other'?
2225 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2226
2227 // Remove all references to environment uses of this instruction.
2228 // The caller must ensure that this is safe to do.
2229 void RemoveEnvironmentUsers();
2230
Vladimir Markoa1de9182016-02-25 11:37:38 +00002231 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2232 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002233
David Brazdil1abb4192015-02-17 18:33:36 +00002234 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002235 // If set, the machine code for this instruction is assumed to be generated by
2236 // its users. Used by liveness analysis to compute use positions accordingly.
2237 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2238 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2239 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2240 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2241
Vladimir Marko372f10e2016-05-17 16:30:10 +01002242 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2243 return GetInputRecords()[i];
2244 }
2245
2246 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2247 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2248 input_records[index] = input;
2249 }
David Brazdil1abb4192015-02-17 18:33:36 +00002250
Vladimir Markoa1de9182016-02-25 11:37:38 +00002251 uint32_t GetPackedFields() const {
2252 return packed_fields_;
2253 }
2254
2255 template <size_t flag>
2256 bool GetPackedFlag() const {
2257 return (packed_fields_ & (1u << flag)) != 0u;
2258 }
2259
2260 template <size_t flag>
2261 void SetPackedFlag(bool value = true) {
2262 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2263 }
2264
2265 template <typename BitFieldType>
2266 typename BitFieldType::value_type GetPackedField() const {
2267 return BitFieldType::Decode(packed_fields_);
2268 }
2269
2270 template <typename BitFieldType>
2271 void SetPackedField(typename BitFieldType::value_type value) {
2272 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2273 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2274 }
2275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002277 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2278 auto before_use_node = uses_.before_begin();
2279 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2280 HInstruction* user = use_node->GetUser();
2281 size_t input_index = use_node->GetIndex();
2282 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2283 before_use_node = use_node;
2284 }
2285 }
2286
2287 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2288 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2289 if (next != uses_.end()) {
2290 HInstruction* next_user = next->GetUser();
2291 size_t next_index = next->GetIndex();
2292 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2293 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2294 }
2295 }
2296
2297 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2298 auto before_env_use_node = env_uses_.before_begin();
2299 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2300 HEnvironment* user = env_use_node->GetUser();
2301 size_t input_index = env_use_node->GetIndex();
2302 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2303 before_env_use_node = env_use_node;
2304 }
2305 }
2306
2307 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2308 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2309 if (next != env_uses_.end()) {
2310 HEnvironment* next_user = next->GetUser();
2311 size_t next_index = next->GetIndex();
2312 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2313 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2314 }
2315 }
David Brazdil1abb4192015-02-17 18:33:36 +00002316
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002317 HInstruction* previous_;
2318 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002319 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002320 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002321
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002322 // An instruction gets an id when it is added to the graph.
2323 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002324 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002325 int id_;
2326
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002327 // When doing liveness analysis, instructions that have uses get an SSA index.
2328 int ssa_index_;
2329
Vladimir Markoa1de9182016-02-25 11:37:38 +00002330 // Packed fields.
2331 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002332
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002333 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002334 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335
2336 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002337 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002338
Nicolas Geoffray39468442014-09-02 15:17:15 +01002339 // The environment associated with this instruction. Not null if the instruction
2340 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002341 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002342
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002343 // Set by the code generator.
2344 LocationSummary* locations_;
2345
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002346 // Set by the liveness analysis.
2347 LiveInterval* live_interval_;
2348
2349 // Set by the liveness analysis, this is the position in a linear
2350 // order of blocks where this instruction's live interval start.
2351 size_t lifetime_position_;
2352
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002353 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002354
Vladimir Markoa1de9182016-02-25 11:37:38 +00002355 // The reference handle part of the reference type info.
2356 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002357 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002358 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002359
David Brazdil1abb4192015-02-17 18:33:36 +00002360 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002361 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002362 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002363 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365
2366 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2367};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002368std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002370// Iterates over the instructions, while preserving the next instruction
2371// in case the current instruction gets removed from the list by the user
2372// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002373class HInstructionIterator : public ValueObject {
2374 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002375 explicit HInstructionIterator(const HInstructionList& instructions)
2376 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002377 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002378 }
2379
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002380 bool Done() const { return instruction_ == nullptr; }
2381 HInstruction* Current() const { return instruction_; }
2382 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002384 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002385 }
2386
2387 private:
2388 HInstruction* instruction_;
2389 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002390
2391 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002392};
2393
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002394// Iterates over the instructions without saving the next instruction,
2395// therefore handling changes in the graph potentially made by the user
2396// of this iterator.
2397class HInstructionIteratorHandleChanges : public ValueObject {
2398 public:
2399 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2400 : instruction_(instructions.first_instruction_) {
2401 }
2402
2403 bool Done() const { return instruction_ == nullptr; }
2404 HInstruction* Current() const { return instruction_; }
2405 void Advance() {
2406 instruction_ = instruction_->GetNext();
2407 }
2408
2409 private:
2410 HInstruction* instruction_;
2411
2412 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2413};
2414
2415
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002416class HBackwardInstructionIterator : public ValueObject {
2417 public:
2418 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2419 : instruction_(instructions.last_instruction_) {
2420 next_ = Done() ? nullptr : instruction_->GetPrevious();
2421 }
2422
2423 bool Done() const { return instruction_ == nullptr; }
2424 HInstruction* Current() const { return instruction_; }
2425 void Advance() {
2426 instruction_ = next_;
2427 next_ = Done() ? nullptr : instruction_->GetPrevious();
2428 }
2429
2430 private:
2431 HInstruction* instruction_;
2432 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002433
2434 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002435};
2436
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002437class HVariableInputSizeInstruction : public HInstruction {
2438 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002439 using HInstruction::GetInputRecords; // Keep the const version visible.
2440 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2441 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2442 }
2443
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002444 void AddInput(HInstruction* input);
2445 void InsertInputAt(size_t index, HInstruction* input);
2446 void RemoveInputAt(size_t index);
2447
Igor Murashkind01745e2017-04-05 16:40:31 -07002448 // Removes all the inputs.
2449 // Also removes this instructions from each input's use list
2450 // (for non-environment uses only).
2451 void RemoveAllInputs();
2452
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002453 protected:
2454 HVariableInputSizeInstruction(SideEffects side_effects,
2455 uint32_t dex_pc,
2456 ArenaAllocator* arena,
2457 size_t number_of_inputs,
2458 ArenaAllocKind kind)
2459 : HInstruction(side_effects, dex_pc),
2460 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2461
2462 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2463
2464 private:
2465 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2466};
2467
Vladimir Markof9f64412015-09-02 14:05:49 +01002468template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469class HTemplateInstruction: public HInstruction {
2470 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002471 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002473 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002474
Vladimir Marko372f10e2016-05-17 16:30:10 +01002475 using HInstruction::GetInputRecords; // Keep the const version visible.
2476 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2477 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002478 }
2479
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002481 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002482
2483 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484};
2485
Vladimir Markof9f64412015-09-02 14:05:49 +01002486// HTemplateInstruction specialization for N=0.
2487template<>
2488class HTemplateInstruction<0>: public HInstruction {
2489 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002490 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 : HInstruction(side_effects, dex_pc) {}
2492
Vladimir Markof9f64412015-09-02 14:05:49 +01002493 virtual ~HTemplateInstruction() {}
2494
Vladimir Marko372f10e2016-05-17 16:30:10 +01002495 using HInstruction::GetInputRecords; // Keep the const version visible.
2496 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2497 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002498 }
2499
2500 private:
2501 friend class SsaBuilder;
2502};
2503
Dave Allison20dfc792014-06-16 20:44:29 -07002504template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002505class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002506 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002507 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002508 : HTemplateInstruction<N>(side_effects, dex_pc) {
2509 this->template SetPackedField<TypeField>(type);
2510 }
Dave Allison20dfc792014-06-16 20:44:29 -07002511 virtual ~HExpression() {}
2512
Vladimir Markoa1de9182016-02-25 11:37:38 +00002513 Primitive::Type GetType() const OVERRIDE {
2514 return TypeField::Decode(this->GetPackedFields());
2515 }
Dave Allison20dfc792014-06-16 20:44:29 -07002516
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002517 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002518 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2519 static constexpr size_t kFieldTypeSize =
2520 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2521 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2522 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2523 "Too many packed fields.");
2524 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002525};
2526
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002527// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2528// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002529class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002530 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002531 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2532 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002534 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002535
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002536 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002537
2538 private:
2539 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2540};
2541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542// Represents dex's RETURN opcodes. A HReturn is a control flow
2543// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002544class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002546 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2547 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002548 SetRawInputAt(0, value);
2549 }
2550
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002551 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002552
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002553 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554
2555 private:
2556 DISALLOW_COPY_AND_ASSIGN(HReturn);
2557};
2558
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002559class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002560 public:
2561 HPhi(ArenaAllocator* arena,
2562 uint32_t reg_number,
2563 size_t number_of_inputs,
2564 Primitive::Type type,
2565 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002566 : HVariableInputSizeInstruction(
2567 SideEffects::None(),
2568 dex_pc,
2569 arena,
2570 number_of_inputs,
2571 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002572 reg_number_(reg_number) {
2573 SetPackedField<TypeField>(ToPhiType(type));
2574 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2575 // Phis are constructed live and marked dead if conflicting or unused.
2576 // Individual steps of SsaBuilder should assume that if a phi has been
2577 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2578 SetPackedFlag<kFlagIsLive>(true);
2579 SetPackedFlag<kFlagCanBeNull>(true);
2580 }
2581
2582 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2583 static Primitive::Type ToPhiType(Primitive::Type type) {
2584 return Primitive::PrimitiveKind(type);
2585 }
2586
2587 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2588
David Brazdildee58d62016-04-07 09:54:26 +00002589 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2590 void SetType(Primitive::Type new_type) {
2591 // Make sure that only valid type changes occur. The following are allowed:
2592 // (1) int -> float/ref (primitive type propagation),
2593 // (2) long -> double (primitive type propagation).
2594 DCHECK(GetType() == new_type ||
2595 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2596 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2597 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2598 SetPackedField<TypeField>(new_type);
2599 }
2600
2601 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2602 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2603
2604 uint32_t GetRegNumber() const { return reg_number_; }
2605
2606 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2607 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2608 bool IsDead() const { return !IsLive(); }
2609 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2610
Vladimir Markoe9004912016-06-16 16:50:52 +01002611 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002612 return other != nullptr
2613 && other->IsPhi()
2614 && other->AsPhi()->GetBlock() == GetBlock()
2615 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2616 }
2617
Nicolas Geoffrayf57c1ae2017-06-28 17:40:18 +01002618 bool HasEquivalentPhi() const {
2619 if (GetPrevious() != nullptr && GetPrevious()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2620 return true;
2621 }
2622 if (GetNext() != nullptr && GetNext()->AsPhi()->GetRegNumber() == GetRegNumber()) {
2623 return true;
2624 }
2625 return false;
2626 }
2627
David Brazdildee58d62016-04-07 09:54:26 +00002628 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2629 // An equivalent phi is a phi having the same dex register and type.
2630 // It assumes that phis with the same dex register are adjacent.
2631 HPhi* GetNextEquivalentPhiWithSameType() {
2632 HInstruction* next = GetNext();
2633 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2634 if (next->GetType() == GetType()) {
2635 return next->AsPhi();
2636 }
2637 next = next->GetNext();
2638 }
2639 return nullptr;
2640 }
2641
2642 DECLARE_INSTRUCTION(Phi);
2643
David Brazdildee58d62016-04-07 09:54:26 +00002644 private:
2645 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2646 static constexpr size_t kFieldTypeSize =
2647 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2648 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2649 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2650 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2651 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2652 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2653
David Brazdildee58d62016-04-07 09:54:26 +00002654 const uint32_t reg_number_;
2655
2656 DISALLOW_COPY_AND_ASSIGN(HPhi);
2657};
2658
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002660// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002662class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002663 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002665
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002666 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002667
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002668 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669
2670 private:
2671 DISALLOW_COPY_AND_ASSIGN(HExit);
2672};
2673
2674// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002675class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002677 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002678
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002679 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002680
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002681 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002682 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002683 }
2684
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002685 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002686
2687 private:
2688 DISALLOW_COPY_AND_ASSIGN(HGoto);
2689};
2690
Roland Levillain9867bc72015-08-05 10:21:34 +01002691class HConstant : public HExpression<0> {
2692 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002693 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2694 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002695
2696 bool CanBeMoved() const OVERRIDE { return true; }
2697
Roland Levillain1a653882016-03-18 18:05:57 +00002698 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002699 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002700 // Is this constant 0 in the arithmetic sense?
2701 virtual bool IsArithmeticZero() const { return false; }
2702 // Is this constant a 0-bit pattern?
2703 virtual bool IsZeroBitPattern() const { return false; }
2704 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002705 virtual bool IsOne() const { return false; }
2706
David Brazdil77a48ae2015-09-15 12:34:04 +00002707 virtual uint64_t GetValueAsUint64() const = 0;
2708
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002709 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002710
2711 private:
2712 DISALLOW_COPY_AND_ASSIGN(HConstant);
2713};
2714
Vladimir Markofcb503c2016-05-18 12:48:17 +01002715class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002716 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002717 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002718 return true;
2719 }
2720
David Brazdil77a48ae2015-09-15 12:34:04 +00002721 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2722
Roland Levillain9867bc72015-08-05 10:21:34 +01002723 size_t ComputeHashCode() const OVERRIDE { return 0; }
2724
Roland Levillain1a653882016-03-18 18:05:57 +00002725 // The null constant representation is a 0-bit pattern.
2726 virtual bool IsZeroBitPattern() const { return true; }
2727
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 DECLARE_INSTRUCTION(NullConstant);
2729
2730 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002731 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002732
2733 friend class HGraph;
2734 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2735};
2736
2737// Constants of the type int. Those can be from Dex instructions, or
2738// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002739class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002740 public:
2741 int32_t GetValue() const { return value_; }
2742
David Brazdil9f389d42015-10-01 14:32:56 +01002743 uint64_t GetValueAsUint64() const OVERRIDE {
2744 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2745 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002746
Vladimir Marko372f10e2016-05-17 16:30:10 +01002747 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002748 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002749 return other->AsIntConstant()->value_ == value_;
2750 }
2751
2752 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2753
2754 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002755 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2756 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002757 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2758
Roland Levillain1a653882016-03-18 18:05:57 +00002759 // Integer constants are used to encode Boolean values as well,
2760 // where 1 means true and 0 means false.
2761 bool IsTrue() const { return GetValue() == 1; }
2762 bool IsFalse() const { return GetValue() == 0; }
2763
Roland Levillain9867bc72015-08-05 10:21:34 +01002764 DECLARE_INSTRUCTION(IntConstant);
2765
2766 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002767 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2768 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2769 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2770 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002771
2772 const int32_t value_;
2773
2774 friend class HGraph;
2775 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2776 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2777 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2778};
2779
Vladimir Markofcb503c2016-05-18 12:48:17 +01002780class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002781 public:
2782 int64_t GetValue() const { return value_; }
2783
David Brazdil77a48ae2015-09-15 12:34:04 +00002784 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2785
Vladimir Marko372f10e2016-05-17 16:30:10 +01002786 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002787 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002788 return other->AsLongConstant()->value_ == value_;
2789 }
2790
2791 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2792
2793 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002794 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2795 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002796 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2797
2798 DECLARE_INSTRUCTION(LongConstant);
2799
2800 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002801 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2802 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002803
2804 const int64_t value_;
2805
2806 friend class HGraph;
2807 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2808};
Dave Allison20dfc792014-06-16 20:44:29 -07002809
Vladimir Markofcb503c2016-05-18 12:48:17 +01002810class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002811 public:
2812 float GetValue() const { return value_; }
2813
2814 uint64_t GetValueAsUint64() const OVERRIDE {
2815 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2816 }
2817
Vladimir Marko372f10e2016-05-17 16:30:10 +01002818 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002819 DCHECK(other->IsFloatConstant()) << other->DebugName();
2820 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2821 }
2822
2823 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2824
2825 bool IsMinusOne() const OVERRIDE {
2826 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2827 }
Roland Levillain1a653882016-03-18 18:05:57 +00002828 bool IsArithmeticZero() const OVERRIDE {
2829 return std::fpclassify(value_) == FP_ZERO;
2830 }
2831 bool IsArithmeticPositiveZero() const {
2832 return IsArithmeticZero() && !std::signbit(value_);
2833 }
2834 bool IsArithmeticNegativeZero() const {
2835 return IsArithmeticZero() && std::signbit(value_);
2836 }
2837 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002838 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002839 }
2840 bool IsOne() const OVERRIDE {
2841 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2842 }
2843 bool IsNaN() const {
2844 return std::isnan(value_);
2845 }
2846
2847 DECLARE_INSTRUCTION(FloatConstant);
2848
2849 private:
2850 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2851 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2852 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2853 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2854
2855 const float value_;
2856
2857 // Only the SsaBuilder and HGraph can create floating-point constants.
2858 friend class SsaBuilder;
2859 friend class HGraph;
2860 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2861};
2862
Vladimir Markofcb503c2016-05-18 12:48:17 +01002863class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002864 public:
2865 double GetValue() const { return value_; }
2866
2867 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2868
Vladimir Marko372f10e2016-05-17 16:30:10 +01002869 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002870 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2871 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2872 }
2873
2874 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2875
2876 bool IsMinusOne() const OVERRIDE {
2877 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2878 }
Roland Levillain1a653882016-03-18 18:05:57 +00002879 bool IsArithmeticZero() const OVERRIDE {
2880 return std::fpclassify(value_) == FP_ZERO;
2881 }
2882 bool IsArithmeticPositiveZero() const {
2883 return IsArithmeticZero() && !std::signbit(value_);
2884 }
2885 bool IsArithmeticNegativeZero() const {
2886 return IsArithmeticZero() && std::signbit(value_);
2887 }
2888 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002889 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002890 }
2891 bool IsOne() const OVERRIDE {
2892 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2893 }
2894 bool IsNaN() const {
2895 return std::isnan(value_);
2896 }
2897
2898 DECLARE_INSTRUCTION(DoubleConstant);
2899
2900 private:
2901 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2902 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2903 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2904 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2905
2906 const double value_;
2907
2908 // Only the SsaBuilder and HGraph can create floating-point constants.
2909 friend class SsaBuilder;
2910 friend class HGraph;
2911 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2912};
2913
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002914// Conditional branch. A block ending with an HIf instruction must have
2915// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002916class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002917 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002918 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2919 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002920 SetRawInputAt(0, input);
2921 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002922
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002923 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002924
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002925 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002926 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002927 }
2928
2929 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002930 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002931 }
2932
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002933 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002934
2935 private:
2936 DISALLOW_COPY_AND_ASSIGN(HIf);
2937};
2938
David Brazdilfc6a86a2015-06-26 10:33:45 +00002939
2940// Abstract instruction which marks the beginning and/or end of a try block and
2941// links it to the respective exception handlers. Behaves the same as a Goto in
2942// non-exceptional control flow.
2943// Normal-flow successor is stored at index zero, exception handlers under
2944// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002945class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002946 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002947 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002948 kEntry,
2949 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002950 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002951 };
2952
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002953 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002954 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2955 SetPackedField<BoundaryKindField>(kind);
2956 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002957
2958 bool IsControlFlow() const OVERRIDE { return true; }
2959
2960 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002961 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002962
David Brazdild26a4112015-11-10 11:07:31 +00002963 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2964 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2965 }
2966
David Brazdilfc6a86a2015-06-26 10:33:45 +00002967 // Returns whether `handler` is among its exception handlers (non-zero index
2968 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002969 bool HasExceptionHandler(const HBasicBlock& handler) const {
2970 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002971 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972 }
2973
2974 // If not present already, adds `handler` to its block's list of exception
2975 // handlers.
2976 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002977 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002978 GetBlock()->AddSuccessor(handler);
2979 }
2980 }
2981
Vladimir Markoa1de9182016-02-25 11:37:38 +00002982 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2983 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002984
David Brazdilffee3d32015-07-06 11:48:53 +01002985 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2986
David Brazdilfc6a86a2015-06-26 10:33:45 +00002987 DECLARE_INSTRUCTION(TryBoundary);
2988
2989 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002990 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2991 static constexpr size_t kFieldBoundaryKindSize =
2992 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2993 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2994 kFieldBoundaryKind + kFieldBoundaryKindSize;
2995 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2996 "Too many packed fields.");
2997 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002998
2999 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
3000};
3001
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003002// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003003class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003004 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003005 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3006 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003007 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003008 : HVariableInputSizeInstruction(
3009 SideEffects::All(),
3010 dex_pc,
3011 arena,
3012 /* number_of_inputs */ 1,
3013 kArenaAllocMisc) {
3014 SetPackedFlag<kFieldCanBeMoved>(false);
3015 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003016 SetRawInputAt(0, cond);
3017 }
3018
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003019 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3020 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3021 // instead of `guard`.
3022 // We set CanTriggerGC to prevent any intermediate address to be live
3023 // at the point of the `HDeoptimize`.
3024 HDeoptimize(ArenaAllocator* arena,
3025 HInstruction* cond,
3026 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003027 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003028 uint32_t dex_pc)
3029 : HVariableInputSizeInstruction(
3030 SideEffects::CanTriggerGC(),
3031 dex_pc,
3032 arena,
3033 /* number_of_inputs */ 2,
3034 kArenaAllocMisc) {
3035 SetPackedFlag<kFieldCanBeMoved>(true);
3036 SetPackedField<DeoptimizeKindField>(kind);
3037 SetRawInputAt(0, cond);
3038 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003039 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003040
3041 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3042
3043 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3044 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3045 }
3046
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003047 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003048
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003049 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003050
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003051 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003052
3053 Primitive::Type GetType() const OVERRIDE {
3054 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3055 }
3056
3057 bool GuardsAnInput() const {
3058 return InputCount() == 2;
3059 }
3060
3061 HInstruction* GuardedInput() const {
3062 DCHECK(GuardsAnInput());
3063 return InputAt(1);
3064 }
3065
3066 void RemoveGuard() {
3067 RemoveInputAt(1);
3068 }
3069
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003070 DECLARE_INSTRUCTION(Deoptimize);
3071
3072 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003073 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3074 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3075 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003076 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003077 static constexpr size_t kNumberOfDeoptimizePackedBits =
3078 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3079 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3080 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003081 using DeoptimizeKindField =
3082 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003083
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003084 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3085};
3086
Mingyao Yang063fc772016-08-02 11:02:54 -07003087// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3088// The compiled code checks this flag value in a guard before devirtualized call and
3089// if it's true, starts to do deoptimization.
3090// It has a 4-byte slot on stack.
3091// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003092class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003093 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003094 // CHA guards are only optimized in a separate pass and it has no side effects
3095 // with regard to other passes.
3096 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3097 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003098 }
3099
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003100 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3101
3102 // We do all CHA guard elimination/motion in a single pass, after which there is no
3103 // further guard elimination/motion since a guard might have been used for justification
3104 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3105 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003106 bool CanBeMoved() const OVERRIDE { return false; }
3107
3108 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3109
3110 private:
3111 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3112};
3113
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003114// Represents the ArtMethod that was passed as a first argument to
3115// the method. It is used by instructions that depend on it, like
3116// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003117class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003118 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003119 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3120 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003121
3122 DECLARE_INSTRUCTION(CurrentMethod);
3123
3124 private:
3125 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3126};
3127
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003128// Fetches an ArtMethod from the virtual table or the interface method table
3129// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003130class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003131 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003132 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003133 kVTable,
3134 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003135 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003136 };
3137 HClassTableGet(HInstruction* cls,
3138 Primitive::Type type,
3139 TableKind kind,
3140 size_t index,
3141 uint32_t dex_pc)
3142 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003143 index_(index) {
3144 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003145 SetRawInputAt(0, cls);
3146 }
3147
3148 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003149 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003150 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003151 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003152 }
3153
Vladimir Markoa1de9182016-02-25 11:37:38 +00003154 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003155 size_t GetIndex() const { return index_; }
3156
3157 DECLARE_INSTRUCTION(ClassTableGet);
3158
3159 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003160 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3161 static constexpr size_t kFieldTableKindSize =
3162 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3163 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3164 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3165 "Too many packed fields.");
3166 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3167
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003168 // The index of the ArtMethod in the table.
3169 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003170
3171 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3172};
3173
Mark Mendellfe57faa2015-09-18 09:26:15 -04003174// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3175// have one successor for each entry in the switch table, and the final successor
3176// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003177class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003178 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003179 HPackedSwitch(int32_t start_value,
3180 uint32_t num_entries,
3181 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003182 uint32_t dex_pc = kNoDexPc)
3183 : HTemplateInstruction(SideEffects::None(), dex_pc),
3184 start_value_(start_value),
3185 num_entries_(num_entries) {
3186 SetRawInputAt(0, input);
3187 }
3188
3189 bool IsControlFlow() const OVERRIDE { return true; }
3190
3191 int32_t GetStartValue() const { return start_value_; }
3192
Vladimir Marko211c2112015-09-24 16:52:33 +01003193 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003194
3195 HBasicBlock* GetDefaultBlock() const {
3196 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003197 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003198 }
3199 DECLARE_INSTRUCTION(PackedSwitch);
3200
3201 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003202 const int32_t start_value_;
3203 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003204
3205 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3206};
3207
Roland Levillain88cb1752014-10-20 16:36:47 +01003208class HUnaryOperation : public HExpression<1> {
3209 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003210 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3211 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003212 SetRawInputAt(0, input);
3213 }
3214
3215 HInstruction* GetInput() const { return InputAt(0); }
3216 Primitive::Type GetResultType() const { return GetType(); }
3217
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003218 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003219 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003220 return true;
3221 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003222
Roland Levillain31dd3d62016-02-16 12:21:02 +00003223 // Try to statically evaluate `this` and return a HConstant
3224 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003225 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003226 HConstant* TryStaticEvaluation() const;
3227
3228 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003229 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3230 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003231 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3232 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003233
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003234 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003235
3236 private:
3237 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3238};
3239
Dave Allison20dfc792014-06-16 20:44:29 -07003240class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003241 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003242 HBinaryOperation(Primitive::Type result_type,
3243 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003244 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003245 SideEffects side_effects = SideEffects::None(),
3246 uint32_t dex_pc = kNoDexPc)
3247 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003248 SetRawInputAt(0, left);
3249 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003250 }
3251
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003252 HInstruction* GetLeft() const { return InputAt(0); }
3253 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003254 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003255
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003256 virtual bool IsCommutative() const { return false; }
3257
3258 // Put constant on the right.
3259 // Returns whether order is changed.
3260 bool OrderInputsWithConstantOnTheRight() {
3261 HInstruction* left = InputAt(0);
3262 HInstruction* right = InputAt(1);
3263 if (left->IsConstant() && !right->IsConstant()) {
3264 ReplaceInput(right, 0);
3265 ReplaceInput(left, 1);
3266 return true;
3267 }
3268 return false;
3269 }
3270
3271 // Order inputs by instruction id, but favor constant on the right side.
3272 // This helps GVN for commutative ops.
3273 void OrderInputs() {
3274 DCHECK(IsCommutative());
3275 HInstruction* left = InputAt(0);
3276 HInstruction* right = InputAt(1);
3277 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3278 return;
3279 }
3280 if (OrderInputsWithConstantOnTheRight()) {
3281 return;
3282 }
3283 // Order according to instruction id.
3284 if (left->GetId() > right->GetId()) {
3285 ReplaceInput(right, 0);
3286 ReplaceInput(left, 1);
3287 }
3288 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003289
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003290 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003291 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003292 return true;
3293 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003294
Roland Levillain31dd3d62016-02-16 12:21:02 +00003295 // Try to statically evaluate `this` and return a HConstant
3296 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003297 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003298 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003299
3300 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3302 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003303 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3304 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003306 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3307 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3309 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003310 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3311 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003312 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3314 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003315
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003316 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003317 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003318 HConstant* GetConstantRight() const;
3319
3320 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003321 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003322 HInstruction* GetLeastConstantLeft() const;
3323
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003324 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003325
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003326 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003327 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3328};
3329
Mark Mendellc4701932015-04-10 13:18:51 -04003330// The comparison bias applies for floating point operations and indicates how NaN
3331// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003332enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003333 kNoBias, // bias is not applicable (i.e. for long operation)
3334 kGtBias, // return 1 for NaN comparisons
3335 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003336 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003337};
3338
Roland Levillain31dd3d62016-02-16 12:21:02 +00003339std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3340
Dave Allison20dfc792014-06-16 20:44:29 -07003341class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003342 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003343 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003344 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3345 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3346 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003347
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003348 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003349 // `instruction`, and disregard moves in between.
3350 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003351
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003352 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003353
3354 virtual IfCondition GetCondition() const = 0;
3355
Mark Mendellc4701932015-04-10 13:18:51 -04003356 virtual IfCondition GetOppositeCondition() const = 0;
3357
Vladimir Markoa1de9182016-02-25 11:37:38 +00003358 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003359 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3360
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3362 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003363
Vladimir Marko372f10e2016-05-17 16:30:10 +01003364 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003365 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003366 }
3367
Roland Levillain4fa13f62015-07-06 18:11:54 +01003368 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003369 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003370 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003371 if (if_cond == kCondNE) {
3372 return true;
3373 } else if (if_cond == kCondEQ) {
3374 return false;
3375 }
3376 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003377 }
3378
3379 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003381 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003382 if (if_cond == kCondEQ) {
3383 return true;
3384 } else if (if_cond == kCondNE) {
3385 return false;
3386 }
3387 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003388 }
3389
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003391 // Needed if we merge a HCompare into a HCondition.
3392 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3393 static constexpr size_t kFieldComparisonBiasSize =
3394 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3395 static constexpr size_t kNumberOfConditionPackedBits =
3396 kFieldComparisonBias + kFieldComparisonBiasSize;
3397 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3398 using ComparisonBiasField =
3399 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3400
Roland Levillain31dd3d62016-02-16 12:21:02 +00003401 template <typename T>
3402 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3403
3404 template <typename T>
3405 int32_t CompareFP(T x, T y) const {
3406 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3407 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3408 // Handle the bias.
3409 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3410 }
3411
3412 // Return an integer constant containing the result of a condition evaluated at compile time.
3413 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3414 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3415 }
3416
Dave Allison20dfc792014-06-16 20:44:29 -07003417 private:
3418 DISALLOW_COPY_AND_ASSIGN(HCondition);
3419};
3420
3421// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003422class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003423 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003424 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3425 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003426
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003427 bool IsCommutative() const OVERRIDE { return true; }
3428
Vladimir Marko9e23df52015-11-10 17:14:35 +00003429 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3430 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 return MakeConstantCondition(true, GetDexPc());
3432 }
3433 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3434 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3435 }
3436 // In the following Evaluate methods, a HCompare instruction has
3437 // been merged into this HEqual instruction; evaluate it as
3438 // `Compare(x, y) == 0`.
3439 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3440 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3441 GetDexPc());
3442 }
3443 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3444 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3445 }
3446 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3447 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003448 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003450 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003452 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003453 return kCondEQ;
3454 }
3455
Mark Mendellc4701932015-04-10 13:18:51 -04003456 IfCondition GetOppositeCondition() const OVERRIDE {
3457 return kCondNE;
3458 }
3459
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003460 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003461 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003462
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003463 DISALLOW_COPY_AND_ASSIGN(HEqual);
3464};
3465
Vladimir Markofcb503c2016-05-18 12:48:17 +01003466class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003468 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3469 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003470
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003471 bool IsCommutative() const OVERRIDE { return true; }
3472
Vladimir Marko9e23df52015-11-10 17:14:35 +00003473 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3474 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003475 return MakeConstantCondition(false, GetDexPc());
3476 }
3477 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3478 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3479 }
3480 // In the following Evaluate methods, a HCompare instruction has
3481 // been merged into this HNotEqual instruction; evaluate it as
3482 // `Compare(x, y) != 0`.
3483 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3484 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3485 }
3486 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3487 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3488 }
3489 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3490 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003491 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003492
Dave Allison20dfc792014-06-16 20:44:29 -07003493 DECLARE_INSTRUCTION(NotEqual);
3494
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003495 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003496 return kCondNE;
3497 }
3498
Mark Mendellc4701932015-04-10 13:18:51 -04003499 IfCondition GetOppositeCondition() const OVERRIDE {
3500 return kCondEQ;
3501 }
3502
Dave Allison20dfc792014-06-16 20:44:29 -07003503 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003504 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003505
Dave Allison20dfc792014-06-16 20:44:29 -07003506 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3507};
3508
Vladimir Markofcb503c2016-05-18 12:48:17 +01003509class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003510 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003511 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3512 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003513
Roland Levillain9867bc72015-08-05 10:21:34 +01003514 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003515 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003516 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 // In the following Evaluate methods, a HCompare instruction has
3518 // been merged into this HLessThan instruction; evaluate it as
3519 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003520 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3522 }
3523 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3524 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3525 }
3526 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3527 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003528 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003529
Dave Allison20dfc792014-06-16 20:44:29 -07003530 DECLARE_INSTRUCTION(LessThan);
3531
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003532 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003533 return kCondLT;
3534 }
3535
Mark Mendellc4701932015-04-10 13:18:51 -04003536 IfCondition GetOppositeCondition() const OVERRIDE {
3537 return kCondGE;
3538 }
3539
Dave Allison20dfc792014-06-16 20:44:29 -07003540 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003541 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003542
Dave Allison20dfc792014-06-16 20:44:29 -07003543 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3544};
3545
Vladimir Markofcb503c2016-05-18 12:48:17 +01003546class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003547 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003548 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3549 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003550
Roland Levillain9867bc72015-08-05 10:21:34 +01003551 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003553 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 // In the following Evaluate methods, a HCompare instruction has
3555 // been merged into this HLessThanOrEqual instruction; evaluate it as
3556 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003557 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003558 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3559 }
3560 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3561 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3562 }
3563 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3564 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003565 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003566
Dave Allison20dfc792014-06-16 20:44:29 -07003567 DECLARE_INSTRUCTION(LessThanOrEqual);
3568
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003569 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003570 return kCondLE;
3571 }
3572
Mark Mendellc4701932015-04-10 13:18:51 -04003573 IfCondition GetOppositeCondition() const OVERRIDE {
3574 return kCondGT;
3575 }
3576
Dave Allison20dfc792014-06-16 20:44:29 -07003577 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003578 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003579
Dave Allison20dfc792014-06-16 20:44:29 -07003580 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3581};
3582
Vladimir Markofcb503c2016-05-18 12:48:17 +01003583class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003584 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003585 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3586 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003587
Roland Levillain9867bc72015-08-05 10:21:34 +01003588 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003589 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003590 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 // In the following Evaluate methods, a HCompare instruction has
3592 // been merged into this HGreaterThan instruction; evaluate it as
3593 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003595 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3596 }
3597 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3598 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3599 }
3600 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3601 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003602 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003603
Dave Allison20dfc792014-06-16 20:44:29 -07003604 DECLARE_INSTRUCTION(GreaterThan);
3605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003606 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003607 return kCondGT;
3608 }
3609
Mark Mendellc4701932015-04-10 13:18:51 -04003610 IfCondition GetOppositeCondition() const OVERRIDE {
3611 return kCondLE;
3612 }
3613
Dave Allison20dfc792014-06-16 20:44:29 -07003614 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003615 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003616
Dave Allison20dfc792014-06-16 20:44:29 -07003617 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3618};
3619
Vladimir Markofcb503c2016-05-18 12:48:17 +01003620class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003622 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3623 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003624
Roland Levillain9867bc72015-08-05 10:21:34 +01003625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003626 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003627 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 // In the following Evaluate methods, a HCompare instruction has
3629 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3630 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003631 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003632 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3633 }
3634 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3635 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3636 }
3637 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3638 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003639 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003640
Dave Allison20dfc792014-06-16 20:44:29 -07003641 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3642
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003643 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003644 return kCondGE;
3645 }
3646
Mark Mendellc4701932015-04-10 13:18:51 -04003647 IfCondition GetOppositeCondition() const OVERRIDE {
3648 return kCondLT;
3649 }
3650
Dave Allison20dfc792014-06-16 20:44:29 -07003651 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003652 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003653
Dave Allison20dfc792014-06-16 20:44:29 -07003654 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3655};
3656
Vladimir Markofcb503c2016-05-18 12:48:17 +01003657class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003658 public:
3659 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3660 : HCondition(first, second, dex_pc) {}
3661
3662 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003663 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003664 }
3665 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003666 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3667 }
3668 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3669 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3670 LOG(FATAL) << DebugName() << " is not defined for float values";
3671 UNREACHABLE();
3672 }
3673 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3674 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3675 LOG(FATAL) << DebugName() << " is not defined for double values";
3676 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003677 }
3678
3679 DECLARE_INSTRUCTION(Below);
3680
3681 IfCondition GetCondition() const OVERRIDE {
3682 return kCondB;
3683 }
3684
3685 IfCondition GetOppositeCondition() const OVERRIDE {
3686 return kCondAE;
3687 }
3688
3689 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003690 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003691 return MakeUnsigned(x) < MakeUnsigned(y);
3692 }
Aart Bike9f37602015-10-09 11:15:55 -07003693
3694 DISALLOW_COPY_AND_ASSIGN(HBelow);
3695};
3696
Vladimir Markofcb503c2016-05-18 12:48:17 +01003697class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003698 public:
3699 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3700 : HCondition(first, second, dex_pc) {}
3701
3702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003703 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003704 }
3705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3707 }
3708 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3709 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3710 LOG(FATAL) << DebugName() << " is not defined for float values";
3711 UNREACHABLE();
3712 }
3713 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3714 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3715 LOG(FATAL) << DebugName() << " is not defined for double values";
3716 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003717 }
3718
3719 DECLARE_INSTRUCTION(BelowOrEqual);
3720
3721 IfCondition GetCondition() const OVERRIDE {
3722 return kCondBE;
3723 }
3724
3725 IfCondition GetOppositeCondition() const OVERRIDE {
3726 return kCondA;
3727 }
3728
3729 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003730 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003731 return MakeUnsigned(x) <= MakeUnsigned(y);
3732 }
Aart Bike9f37602015-10-09 11:15:55 -07003733
3734 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3735};
3736
Vladimir Markofcb503c2016-05-18 12:48:17 +01003737class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003738 public:
3739 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3740 : HCondition(first, second, dex_pc) {}
3741
3742 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003744 }
3745 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003746 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3747 }
3748 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3749 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3750 LOG(FATAL) << DebugName() << " is not defined for float values";
3751 UNREACHABLE();
3752 }
3753 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3754 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3755 LOG(FATAL) << DebugName() << " is not defined for double values";
3756 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003757 }
3758
3759 DECLARE_INSTRUCTION(Above);
3760
3761 IfCondition GetCondition() const OVERRIDE {
3762 return kCondA;
3763 }
3764
3765 IfCondition GetOppositeCondition() const OVERRIDE {
3766 return kCondBE;
3767 }
3768
3769 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003770 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 return MakeUnsigned(x) > MakeUnsigned(y);
3772 }
Aart Bike9f37602015-10-09 11:15:55 -07003773
3774 DISALLOW_COPY_AND_ASSIGN(HAbove);
3775};
3776
Vladimir Markofcb503c2016-05-18 12:48:17 +01003777class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003778 public:
3779 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3780 : HCondition(first, second, dex_pc) {}
3781
3782 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003783 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003784 }
3785 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003786 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3787 }
3788 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3789 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3790 LOG(FATAL) << DebugName() << " is not defined for float values";
3791 UNREACHABLE();
3792 }
3793 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3794 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3795 LOG(FATAL) << DebugName() << " is not defined for double values";
3796 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003797 }
3798
3799 DECLARE_INSTRUCTION(AboveOrEqual);
3800
3801 IfCondition GetCondition() const OVERRIDE {
3802 return kCondAE;
3803 }
3804
3805 IfCondition GetOppositeCondition() const OVERRIDE {
3806 return kCondB;
3807 }
3808
3809 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003810 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeUnsigned(x) >= MakeUnsigned(y);
3812 }
Aart Bike9f37602015-10-09 11:15:55 -07003813
3814 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3815};
Dave Allison20dfc792014-06-16 20:44:29 -07003816
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003817// Instruction to check how two inputs compare to each other.
3818// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003819class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003820 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003821 // Note that `comparison_type` is the type of comparison performed
3822 // between the comparison's inputs, not the type of the instantiated
3823 // HCompare instruction (which is always Primitive::kPrimInt).
3824 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003825 HInstruction* first,
3826 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003827 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003828 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003829 : HBinaryOperation(Primitive::kPrimInt,
3830 first,
3831 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003832 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003833 dex_pc) {
3834 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003835 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3836 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003837 }
3838
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3841
3842 template <typename T>
3843 int32_t ComputeFP(T x, T y) const {
3844 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3845 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3846 // Handle the bias.
3847 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3848 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003849
Roland Levillain9867bc72015-08-05 10:21:34 +01003850 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3852 // reach this code path when processing a freshly built HIR
3853 // graph. However HCompare integer instructions can be synthesized
3854 // by the instruction simplifier to implement IntegerCompare and
3855 // IntegerSignum intrinsics, so we have to handle this case.
3856 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003857 }
3858 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003859 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3860 }
3861 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3862 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3863 }
3864 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3865 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003866 }
3867
Vladimir Marko372f10e2016-05-17 16:30:10 +01003868 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003869 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003870 }
3871
Vladimir Markoa1de9182016-02-25 11:37:38 +00003872 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003873
Roland Levillain31dd3d62016-02-16 12:21:02 +00003874 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003875 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003876 bool IsGtBias() const {
3877 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003878 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003880
Roland Levillain1693a1f2016-03-15 14:57:31 +00003881 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3882 // Comparisons do not require a runtime call in any back end.
3883 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003884 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003885
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003886 DECLARE_INSTRUCTION(Compare);
3887
Roland Levillain31dd3d62016-02-16 12:21:02 +00003888 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003889 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3890 static constexpr size_t kFieldComparisonBiasSize =
3891 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3892 static constexpr size_t kNumberOfComparePackedBits =
3893 kFieldComparisonBias + kFieldComparisonBiasSize;
3894 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3895 using ComparisonBiasField =
3896 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3897
Roland Levillain31dd3d62016-02-16 12:21:02 +00003898 // Return an integer constant containing the result of a comparison evaluated at compile time.
3899 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3900 DCHECK(value == -1 || value == 0 || value == 1) << value;
3901 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3902 }
3903
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003904 private:
3905 DISALLOW_COPY_AND_ASSIGN(HCompare);
3906};
3907
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003908class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003909 public:
3910 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003911 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003912 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003913 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003914 bool finalizable,
3915 QuickEntrypointEnum entrypoint)
3916 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3917 type_index_(type_index),
3918 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003919 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003920 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003921 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003922 }
3923
Andreas Gampea5b09a62016-11-17 15:21:22 -08003924 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003925 const DexFile& GetDexFile() const { return dex_file_; }
3926
3927 // Calls runtime so needs an environment.
3928 bool NeedsEnvironment() const OVERRIDE { return true; }
3929
Mingyao Yang062157f2016-03-02 10:15:36 -08003930 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3931 bool CanThrow() const OVERRIDE { return true; }
3932
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003933 bool NeedsChecks() const {
3934 return entrypoint_ == kQuickAllocObjectWithChecks;
3935 }
David Brazdil6de19382016-01-08 17:37:10 +00003936
Vladimir Markoa1de9182016-02-25 11:37:38 +00003937 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003938
3939 bool CanBeNull() const OVERRIDE { return false; }
3940
3941 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3942
3943 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3944 entrypoint_ = entrypoint;
3945 }
3946
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003947 HLoadClass* GetLoadClass() const {
3948 HInstruction* input = InputAt(0);
3949 if (input->IsClinitCheck()) {
3950 input = input->InputAt(0);
3951 }
3952 DCHECK(input->IsLoadClass());
3953 return input->AsLoadClass();
3954 }
3955
David Brazdil6de19382016-01-08 17:37:10 +00003956 bool IsStringAlloc() const;
3957
3958 DECLARE_INSTRUCTION(NewInstance);
3959
3960 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003961 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3963 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3964 "Too many packed fields.");
3965
Andreas Gampea5b09a62016-11-17 15:21:22 -08003966 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003967 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003968 QuickEntrypointEnum entrypoint_;
3969
3970 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3971};
3972
Agi Csaki05f20562015-08-19 14:58:14 -07003973enum IntrinsicNeedsEnvironmentOrCache {
3974 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3975 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003976};
3977
Aart Bik5d75afe2015-12-14 11:57:01 -08003978enum IntrinsicSideEffects {
3979 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3980 kReadSideEffects, // Intrinsic may read heap memory.
3981 kWriteSideEffects, // Intrinsic may write heap memory.
3982 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3983};
3984
3985enum IntrinsicExceptions {
3986 kNoThrow, // Intrinsic does not throw any exceptions.
3987 kCanThrow // Intrinsic may throw exceptions.
3988};
3989
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003990class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003991 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003992 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003993
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003994 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003995 SetRawInputAt(index, argument);
3996 }
3997
Roland Levillain3e3d7332015-04-28 11:00:54 +01003998 // Return the number of arguments. This number can be lower than
3999 // the number of inputs returned by InputCount(), as some invoke
4000 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
4001 // inputs at the end of their list of inputs.
4002 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
4003
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004005
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004006 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4007
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004008 InvokeType GetInvokeType() const {
4009 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004010 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004011
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004012 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004013 return intrinsic_;
4014 }
4015
Aart Bik5d75afe2015-12-14 11:57:01 -08004016 void SetIntrinsic(Intrinsics intrinsic,
4017 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4018 IntrinsicSideEffects side_effects,
4019 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004020
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004021 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004022 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004023 }
4024
Aart Bikff7d89c2016-11-07 08:49:28 -08004025 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4026
Vladimir Markoa1de9182016-02-25 11:37:38 +00004027 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004028
Aart Bik71bf7b42016-11-16 10:17:46 -08004029 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004030
Vladimir Marko372f10e2016-05-17 16:30:10 +01004031 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004032 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4033 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004034
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004035 uint32_t* GetIntrinsicOptimizations() {
4036 return &intrinsic_optimizations_;
4037 }
4038
4039 const uint32_t* GetIntrinsicOptimizations() const {
4040 return &intrinsic_optimizations_;
4041 }
4042
4043 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4044
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004045 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004046 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004047
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004048 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004049
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004050 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4052 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004053 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4054 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004055 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 static constexpr size_t kFieldReturnTypeSize =
4057 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4058 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4059 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4060 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004061 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4063
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004064 HInvoke(ArenaAllocator* arena,
4065 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004066 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004067 Primitive::Type return_type,
4068 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004069 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004070 ArtMethod* resolved_method,
4071 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004072 : HVariableInputSizeInstruction(
4073 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4074 dex_pc,
4075 arena,
4076 number_of_arguments + number_of_other_inputs,
4077 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004078 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004079 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004080 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004081 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004082 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004083 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004084 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004085 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004086 }
4087
Roland Levillain3e3d7332015-04-28 11:00:54 +01004088 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004089 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004090 const uint32_t dex_method_index_;
4091 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004092
4093 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4094 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004095
4096 private:
4097 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4098};
4099
Vladimir Markofcb503c2016-05-18 12:48:17 +01004100class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004101 public:
4102 HInvokeUnresolved(ArenaAllocator* arena,
4103 uint32_t number_of_arguments,
4104 Primitive::Type return_type,
4105 uint32_t dex_pc,
4106 uint32_t dex_method_index,
4107 InvokeType invoke_type)
4108 : HInvoke(arena,
4109 number_of_arguments,
4110 0u /* number_of_other_inputs */,
4111 return_type,
4112 dex_pc,
4113 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004114 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004115 invoke_type) {
4116 }
4117
4118 DECLARE_INSTRUCTION(InvokeUnresolved);
4119
4120 private:
4121 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4122};
4123
Orion Hodsonac141392017-01-13 11:53:47 +00004124class HInvokePolymorphic FINAL : public HInvoke {
4125 public:
4126 HInvokePolymorphic(ArenaAllocator* arena,
4127 uint32_t number_of_arguments,
4128 Primitive::Type return_type,
4129 uint32_t dex_pc,
4130 uint32_t dex_method_index)
4131 : HInvoke(arena,
4132 number_of_arguments,
4133 0u /* number_of_other_inputs */,
4134 return_type,
4135 dex_pc,
4136 dex_method_index,
4137 nullptr,
4138 kVirtual) {}
4139
4140 DECLARE_INSTRUCTION(InvokePolymorphic);
4141
4142 private:
4143 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4144};
4145
Vladimir Markofcb503c2016-05-18 12:48:17 +01004146class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004147 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004148 // Requirements of this method call regarding the class
4149 // initialization (clinit) check of its declaring class.
4150 enum class ClinitCheckRequirement {
4151 kNone, // Class already initialized.
4152 kExplicit, // Static call having explicit clinit check as last input.
4153 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004154 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004155 };
4156
Vladimir Marko58155012015-08-19 12:49:41 +00004157 // Determines how to load the target ArtMethod*.
4158 enum class MethodLoadKind {
4159 // Use a String init ArtMethod* loaded from Thread entrypoints.
4160 kStringInit,
4161
4162 // Use the method's own ArtMethod* loaded by the register allocator.
4163 kRecursive,
4164
Vladimir Marko65979462017-05-19 17:25:12 +01004165 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4166 // Used for boot image methods referenced by boot image code.
4167 kBootImageLinkTimePcRelative,
4168
Vladimir Marko58155012015-08-19 12:49:41 +00004169 // Use ArtMethod* at a known address, embed the direct address in the code.
4170 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4171 kDirectAddress,
4172
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004173 // Load from an entry in the .bss section using a PC-relative load.
4174 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
4175 kBssEntry,
Vladimir Marko58155012015-08-19 12:49:41 +00004176
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004177 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4178 // used when other kinds are unimplemented on a particular architecture.
4179 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004180 };
4181
4182 // Determines the location of the code pointer.
4183 enum class CodePtrLocation {
4184 // Recursive call, use local PC-relative call instruction.
4185 kCallSelf,
4186
Vladimir Marko58155012015-08-19 12:49:41 +00004187 // Use code pointer from the ArtMethod*.
4188 // Used when we don't know the target code. This is also the last-resort-kind used when
4189 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4190 kCallArtMethod,
4191 };
4192
4193 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004194 MethodLoadKind method_load_kind;
4195 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004196 // The method load data holds
4197 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4198 // Note that there are multiple string init methods, each having its own offset.
4199 // - the method address for kDirectAddress
Vladimir Markodc151b22015-10-15 18:02:30 +01004200 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004201 };
4202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004203 HInvokeStaticOrDirect(ArenaAllocator* arena,
4204 uint32_t number_of_arguments,
4205 Primitive::Type return_type,
4206 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004207 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004208 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004209 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004210 InvokeType invoke_type,
4211 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004212 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004213 : HInvoke(arena,
4214 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004215 // There is potentially one extra argument for the HCurrentMethod node, and
4216 // potentially one other if the clinit check is explicit, and potentially
4217 // one other if the method is a string factory.
4218 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004219 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004220 return_type,
4221 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004222 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004223 resolved_method,
4224 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004225 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004227 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4228 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004229
Vladimir Markodc151b22015-10-15 18:02:30 +01004230 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004231 bool had_current_method_input = HasCurrentMethodInput();
4232 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4233
4234 // Using the current method is the default and once we find a better
4235 // method load kind, we should not go back to using the current method.
4236 DCHECK(had_current_method_input || !needs_current_method_input);
4237
4238 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004239 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4240 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004241 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004242 dispatch_info_ = dispatch_info;
4243 }
4244
Aart Bik6daebeb2017-04-03 14:35:41 -07004245 DispatchInfo GetDispatchInfo() const {
4246 return dispatch_info_;
4247 }
4248
Vladimir Markoc53c0792015-11-19 15:48:33 +00004249 void AddSpecialInput(HInstruction* input) {
4250 // We allow only one special input.
4251 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4252 DCHECK(InputCount() == GetSpecialInputIndex() ||
4253 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4254 InsertInputAt(GetSpecialInputIndex(), input);
4255 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004256
Vladimir Marko372f10e2016-05-17 16:30:10 +01004257 using HInstruction::GetInputRecords; // Keep the const version visible.
4258 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4259 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4260 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4261 DCHECK(!input_records.empty());
4262 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4263 HInstruction* last_input = input_records.back().GetInstruction();
4264 // Note: `last_input` may be null during arguments setup.
4265 if (last_input != nullptr) {
4266 // `last_input` is the last input of a static invoke marked as having
4267 // an explicit clinit check. It must either be:
4268 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4269 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4270 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4271 }
4272 }
4273 return input_records;
4274 }
4275
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004276 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004277 // We access the method via the dex cache so we can't do an implicit null check.
4278 // TODO: for intrinsics we can generate implicit null checks.
4279 return false;
4280 }
4281
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004282 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004283 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004284 }
4285
Vladimir Markoc53c0792015-11-19 15:48:33 +00004286 // Get the index of the special input, if any.
4287 //
David Brazdil6de19382016-01-08 17:37:10 +00004288 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4289 // method pointer; otherwise there may be one platform-specific special input,
4290 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004291 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004292 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004293
Vladimir Marko58155012015-08-19 12:49:41 +00004294 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4295 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4296 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004297 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004298 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004299 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko65979462017-05-19 17:25:12 +01004300 bool HasPcRelativeMethodLoadKind() const {
4301 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004302 GetMethodLoadKind() == MethodLoadKind::kBssEntry;
Vladimir Marko65979462017-05-19 17:25:12 +01004303 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004304 bool HasCurrentMethodInput() const {
4305 // This function can be called only after the invoke has been fully initialized by the builder.
4306 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004307 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004308 return true;
4309 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004310 DCHECK(InputCount() == GetSpecialInputIndex() ||
4311 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004312 return false;
4313 }
4314 }
Vladimir Marko58155012015-08-19 12:49:41 +00004315
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004316 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004317 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004318 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004319 }
4320
4321 uint64_t GetMethodAddress() const {
4322 DCHECK(HasMethodAddress());
4323 return dispatch_info_.method_load_data;
4324 }
4325
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004326 const DexFile& GetDexFileForPcRelativeDexCache() const;
4327
Vladimir Markoa1de9182016-02-25 11:37:38 +00004328 ClinitCheckRequirement GetClinitCheckRequirement() const {
4329 return GetPackedField<ClinitCheckRequirementField>();
4330 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004331
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 // Is this instruction a call to a static method?
4333 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004334 return GetInvokeType() == kStatic;
4335 }
4336
4337 MethodReference GetTargetMethod() const {
4338 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004339 }
4340
Vladimir Markofbb184a2015-11-13 14:47:00 +00004341 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4342 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4343 // instruction; only relevant for static calls with explicit clinit check.
4344 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004346 size_t last_input_index = inputs_.size() - 1u;
4347 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004348 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004349 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004350 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004351 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004352 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004353 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004354 }
4355
4356 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004357 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004358 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004359 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004360 }
4361
4362 // Is this a call to a static method whose declaring class has an
4363 // implicit intialization check requirement?
4364 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004365 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004366 }
4367
Vladimir Markob554b5a2015-11-06 12:57:55 +00004368 // Does this method load kind need the current method as an input?
4369 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004370 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004371 }
4372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004373 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374
4375 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004376 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004377 static constexpr size_t kFieldClinitCheckRequirementSize =
4378 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4379 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4380 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4381 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4382 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004383 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4384 kFieldClinitCheckRequirement,
4385 kFieldClinitCheckRequirementSize>;
4386
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004388 MethodReference target_method_;
4389 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004390
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004391 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004392};
Vladimir Markof64242a2015-12-01 14:58:23 +00004393std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004394std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004395
Vladimir Markofcb503c2016-05-18 12:48:17 +01004396class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004397 public:
4398 HInvokeVirtual(ArenaAllocator* arena,
4399 uint32_t number_of_arguments,
4400 Primitive::Type return_type,
4401 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004402 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004403 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004404 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004405 : HInvoke(arena,
4406 number_of_arguments,
4407 0u,
4408 return_type,
4409 dex_pc,
4410 dex_method_index,
4411 resolved_method,
4412 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004413 vtable_index_(vtable_index) {}
4414
Aart Bik71bf7b42016-11-16 10:17:46 -08004415 bool CanBeNull() const OVERRIDE {
4416 switch (GetIntrinsic()) {
4417 case Intrinsics::kThreadCurrentThread:
4418 case Intrinsics::kStringBufferAppend:
4419 case Intrinsics::kStringBufferToString:
4420 case Intrinsics::kStringBuilderAppend:
4421 case Intrinsics::kStringBuilderToString:
4422 return false;
4423 default:
4424 return HInvoke::CanBeNull();
4425 }
4426 }
4427
Calin Juravle641547a2015-04-21 22:08:51 +01004428 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004429 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004430 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004431 }
4432
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004433 uint32_t GetVTableIndex() const { return vtable_index_; }
4434
4435 DECLARE_INSTRUCTION(InvokeVirtual);
4436
4437 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004439 const uint32_t vtable_index_;
4440
4441 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4442};
4443
Vladimir Markofcb503c2016-05-18 12:48:17 +01004444class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004445 public:
4446 HInvokeInterface(ArenaAllocator* arena,
4447 uint32_t number_of_arguments,
4448 Primitive::Type return_type,
4449 uint32_t dex_pc,
4450 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004451 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004452 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004453 : HInvoke(arena,
4454 number_of_arguments,
4455 0u,
4456 return_type,
4457 dex_pc,
4458 dex_method_index,
4459 resolved_method,
4460 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004461 imt_index_(imt_index) {}
4462
Calin Juravle641547a2015-04-21 22:08:51 +01004463 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004464 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004465 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004466 }
4467
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004468 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4469 // The assembly stub currently needs it.
4470 return true;
4471 }
4472
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004473 uint32_t GetImtIndex() const { return imt_index_; }
4474 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4475
4476 DECLARE_INSTRUCTION(InvokeInterface);
4477
4478 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004479 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004480 const uint32_t imt_index_;
4481
4482 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4483};
4484
Vladimir Markofcb503c2016-05-18 12:48:17 +01004485class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004487 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004488 : HUnaryOperation(result_type, input, dex_pc) {
4489 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4490 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004491
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004492 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004493
4494 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
4497 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004498 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004500 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4501 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4502 }
4503 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4504 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4505 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004506
Roland Levillain88cb1752014-10-20 16:36:47 +01004507 DECLARE_INSTRUCTION(Neg);
4508
4509 private:
4510 DISALLOW_COPY_AND_ASSIGN(HNeg);
4511};
4512
Vladimir Markofcb503c2016-05-18 12:48:17 +01004513class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004514 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004515 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4516 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4517 SetRawInputAt(0, cls);
4518 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004519 }
4520
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004522 bool NeedsEnvironment() const OVERRIDE { return true; }
4523
Mingyao Yang0c365e62015-03-31 15:09:29 -07004524 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4525 bool CanThrow() const OVERRIDE { return true; }
4526
Calin Juravle10e244f2015-01-26 18:54:32 +00004527 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004528
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004529 HLoadClass* GetLoadClass() const {
4530 DCHECK(InputAt(0)->IsLoadClass());
4531 return InputAt(0)->AsLoadClass();
4532 }
4533
4534 HInstruction* GetLength() const {
4535 return InputAt(1);
4536 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004537
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004538 DECLARE_INSTRUCTION(NewArray);
4539
4540 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004541 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4542};
4543
Vladimir Markofcb503c2016-05-18 12:48:17 +01004544class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004545 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004546 HAdd(Primitive::Type result_type,
4547 HInstruction* left,
4548 HInstruction* right,
4549 uint32_t dex_pc = kNoDexPc)
4550 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004552 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004553
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004554 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004555
4556 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 return GetBlock()->GetGraph()->GetIntConstant(
4558 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004559 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004560 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004561 return GetBlock()->GetGraph()->GetLongConstant(
4562 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004563 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004564 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4565 return GetBlock()->GetGraph()->GetFloatConstant(
4566 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4567 }
4568 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4569 return GetBlock()->GetGraph()->GetDoubleConstant(
4570 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4571 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004572
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004573 DECLARE_INSTRUCTION(Add);
4574
4575 private:
4576 DISALLOW_COPY_AND_ASSIGN(HAdd);
4577};
4578
Vladimir Markofcb503c2016-05-18 12:48:17 +01004579class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004580 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 HSub(Primitive::Type result_type,
4582 HInstruction* left,
4583 HInstruction* right,
4584 uint32_t dex_pc = kNoDexPc)
4585 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004586
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004587 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004588
4589 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 return GetBlock()->GetGraph()->GetIntConstant(
4591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004592 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004593 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004594 return GetBlock()->GetGraph()->GetLongConstant(
4595 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004596 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004597 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4598 return GetBlock()->GetGraph()->GetFloatConstant(
4599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4600 }
4601 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4602 return GetBlock()->GetGraph()->GetDoubleConstant(
4603 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4604 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004605
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004606 DECLARE_INSTRUCTION(Sub);
4607
4608 private:
4609 DISALLOW_COPY_AND_ASSIGN(HSub);
4610};
4611
Vladimir Markofcb503c2016-05-18 12:48:17 +01004612class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004613 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004614 HMul(Primitive::Type result_type,
4615 HInstruction* left,
4616 HInstruction* right,
4617 uint32_t dex_pc = kNoDexPc)
4618 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004619
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004620 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004621
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004622 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004623
4624 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 return GetBlock()->GetGraph()->GetIntConstant(
4626 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
4628 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004629 return GetBlock()->GetGraph()->GetLongConstant(
4630 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004631 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004632 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4633 return GetBlock()->GetGraph()->GetFloatConstant(
4634 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4635 }
4636 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4637 return GetBlock()->GetGraph()->GetDoubleConstant(
4638 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4639 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004640
4641 DECLARE_INSTRUCTION(Mul);
4642
4643 private:
4644 DISALLOW_COPY_AND_ASSIGN(HMul);
4645};
4646
Vladimir Markofcb503c2016-05-18 12:48:17 +01004647class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 HDiv(Primitive::Type result_type,
4650 HInstruction* left,
4651 HInstruction* right,
4652 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004653 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004654
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004656 T ComputeIntegral(T x, T y) const {
4657 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 // Our graph structure ensures we never have 0 for `y` during
4659 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004660 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004661 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004662 return (y == -1) ? -x : x / y;
4663 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004664
Roland Levillain31dd3d62016-02-16 12:21:02 +00004665 template <typename T>
4666 T ComputeFP(T x, T y) const {
4667 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4668 return x / y;
4669 }
4670
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004673 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 }
4675 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004677 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4678 }
4679 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4680 return GetBlock()->GetGraph()->GetFloatConstant(
4681 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4682 }
4683 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4684 return GetBlock()->GetGraph()->GetDoubleConstant(
4685 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004686 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004687
4688 DECLARE_INSTRUCTION(Div);
4689
4690 private:
4691 DISALLOW_COPY_AND_ASSIGN(HDiv);
4692};
4693
Vladimir Markofcb503c2016-05-18 12:48:17 +01004694class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004695 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004696 HRem(Primitive::Type result_type,
4697 HInstruction* left,
4698 HInstruction* right,
4699 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004700 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004701
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004703 T ComputeIntegral(T x, T y) const {
4704 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 // Our graph structure ensures we never have 0 for `y` during
4706 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004707 DCHECK_NE(y, 0);
4708 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4709 return (y == -1) ? 0 : x % y;
4710 }
4711
Roland Levillain31dd3d62016-02-16 12:21:02 +00004712 template <typename T>
4713 T ComputeFP(T x, T y) const {
4714 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4715 return std::fmod(x, y);
4716 }
4717
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004719 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004721 }
4722 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004723 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004724 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004725 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004726 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4727 return GetBlock()->GetGraph()->GetFloatConstant(
4728 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4729 }
4730 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4731 return GetBlock()->GetGraph()->GetDoubleConstant(
4732 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4733 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004734
4735 DECLARE_INSTRUCTION(Rem);
4736
4737 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004738 DISALLOW_COPY_AND_ASSIGN(HRem);
4739};
4740
Vladimir Markofcb503c2016-05-18 12:48:17 +01004741class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004742 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004743 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4744 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004745 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004746 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004747 SetRawInputAt(0, value);
4748 }
4749
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004750 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4751
Calin Juravled0d48522014-11-04 16:40:20 +00004752 bool CanBeMoved() const OVERRIDE { return true; }
4753
Vladimir Marko372f10e2016-05-17 16:30:10 +01004754 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004755 return true;
4756 }
4757
4758 bool NeedsEnvironment() const OVERRIDE { return true; }
4759 bool CanThrow() const OVERRIDE { return true; }
4760
Calin Juravled0d48522014-11-04 16:40:20 +00004761 DECLARE_INSTRUCTION(DivZeroCheck);
4762
4763 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004764 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4765};
4766
Vladimir Markofcb503c2016-05-18 12:48:17 +01004767class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004768 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004769 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HInstruction* value,
4771 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004772 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4774 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4775 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004776 }
4777
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004779 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 return value << (distance & max_shift_distance);
4781 }
4782
4783 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004784 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004786 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004788 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004789 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004790 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004791 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4792 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4793 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4794 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004795 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004796 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4797 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004798 LOG(FATAL) << DebugName() << " is not defined for float values";
4799 UNREACHABLE();
4800 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4802 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004803 LOG(FATAL) << DebugName() << " is not defined for double values";
4804 UNREACHABLE();
4805 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004806
4807 DECLARE_INSTRUCTION(Shl);
4808
4809 private:
4810 DISALLOW_COPY_AND_ASSIGN(HShl);
4811};
4812
Vladimir Markofcb503c2016-05-18 12:48:17 +01004813class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HInstruction* value,
4817 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004818 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4820 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4821 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004822 }
4823
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004825 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 return value >> (distance & max_shift_distance);
4827 }
4828
4829 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004832 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004833 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004834 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004835 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004836 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004837 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4838 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4839 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4840 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4843 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004844 LOG(FATAL) << DebugName() << " is not defined for float values";
4845 UNREACHABLE();
4846 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004847 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4848 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004849 LOG(FATAL) << DebugName() << " is not defined for double values";
4850 UNREACHABLE();
4851 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004852
4853 DECLARE_INSTRUCTION(Shr);
4854
4855 private:
4856 DISALLOW_COPY_AND_ASSIGN(HShr);
4857};
4858
Vladimir Markofcb503c2016-05-18 12:48:17 +01004859class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004861 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 HInstruction* value,
4863 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004864 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4866 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4867 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004868 }
4869
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004871 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 typedef typename std::make_unsigned<T>::type V;
4873 V ux = static_cast<V>(value);
4874 return static_cast<T>(ux >> (distance & max_shift_distance));
4875 }
4876
4877 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004878 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004880 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004881 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004882 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004883 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004884 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004885 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4886 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4887 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4888 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004889 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004890 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4891 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004892 LOG(FATAL) << DebugName() << " is not defined for float values";
4893 UNREACHABLE();
4894 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004895 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4896 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004897 LOG(FATAL) << DebugName() << " is not defined for double values";
4898 UNREACHABLE();
4899 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004900
4901 DECLARE_INSTRUCTION(UShr);
4902
4903 private:
4904 DISALLOW_COPY_AND_ASSIGN(HUShr);
4905};
4906
Vladimir Markofcb503c2016-05-18 12:48:17 +01004907class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004909 HAnd(Primitive::Type result_type,
4910 HInstruction* left,
4911 HInstruction* right,
4912 uint32_t dex_pc = kNoDexPc)
4913 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004914
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004915 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004916
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004917 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004918
4919 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 return GetBlock()->GetGraph()->GetIntConstant(
4921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004922 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004923 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004924 return GetBlock()->GetGraph()->GetLongConstant(
4925 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004926 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004927 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4928 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4929 LOG(FATAL) << DebugName() << " is not defined for float values";
4930 UNREACHABLE();
4931 }
4932 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4933 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4934 LOG(FATAL) << DebugName() << " is not defined for double values";
4935 UNREACHABLE();
4936 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004937
4938 DECLARE_INSTRUCTION(And);
4939
4940 private:
4941 DISALLOW_COPY_AND_ASSIGN(HAnd);
4942};
4943
Vladimir Markofcb503c2016-05-18 12:48:17 +01004944class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004945 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004946 HOr(Primitive::Type result_type,
4947 HInstruction* left,
4948 HInstruction* right,
4949 uint32_t dex_pc = kNoDexPc)
4950 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004951
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004952 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004953
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004954 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004955
4956 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004957 return GetBlock()->GetGraph()->GetIntConstant(
4958 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004959 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004960 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004961 return GetBlock()->GetGraph()->GetLongConstant(
4962 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004963 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004964 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4965 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4966 LOG(FATAL) << DebugName() << " is not defined for float values";
4967 UNREACHABLE();
4968 }
4969 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4970 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4971 LOG(FATAL) << DebugName() << " is not defined for double values";
4972 UNREACHABLE();
4973 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004974
4975 DECLARE_INSTRUCTION(Or);
4976
4977 private:
4978 DISALLOW_COPY_AND_ASSIGN(HOr);
4979};
4980
Vladimir Markofcb503c2016-05-18 12:48:17 +01004981class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004982 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 HXor(Primitive::Type result_type,
4984 HInstruction* left,
4985 HInstruction* right,
4986 uint32_t dex_pc = kNoDexPc)
4987 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004988
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004989 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004991 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004992
4993 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetIntConstant(
4995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 return GetBlock()->GetGraph()->GetLongConstant(
4999 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005000 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005001 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5002 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5003 LOG(FATAL) << DebugName() << " is not defined for float values";
5004 UNREACHABLE();
5005 }
5006 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5007 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5008 LOG(FATAL) << DebugName() << " is not defined for double values";
5009 UNREACHABLE();
5010 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005011
5012 DECLARE_INSTRUCTION(Xor);
5013
5014 private:
5015 DISALLOW_COPY_AND_ASSIGN(HXor);
5016};
5017
Vladimir Markofcb503c2016-05-18 12:48:17 +01005018class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005019 public:
5020 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005021 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005022 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5023 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005024 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005025
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005027 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 typedef typename std::make_unsigned<T>::type V;
5029 V ux = static_cast<V>(value);
5030 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005031 return static_cast<T>(ux);
5032 } else {
5033 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 return static_cast<T>(ux >> (distance & max_shift_value)) |
5035 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005036 }
5037 }
5038
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005040 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005042 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005043 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005044 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005045 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005046 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005047 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5048 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5049 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5050 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005051 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005052 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5053 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005054 LOG(FATAL) << DebugName() << " is not defined for float values";
5055 UNREACHABLE();
5056 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005057 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5058 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005059 LOG(FATAL) << DebugName() << " is not defined for double values";
5060 UNREACHABLE();
5061 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005062
5063 DECLARE_INSTRUCTION(Ror);
5064
5065 private:
5066 DISALLOW_COPY_AND_ASSIGN(HRor);
5067};
5068
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005069// The value of a parameter in this method. Its location depends on
5070// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005071class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005073 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005074 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005075 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005076 Primitive::Type parameter_type,
5077 bool is_this = false)
5078 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005079 dex_file_(dex_file),
5080 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005081 index_(index) {
5082 SetPackedFlag<kFlagIsThis>(is_this);
5083 SetPackedFlag<kFlagCanBeNull>(!is_this);
5084 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005085
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005086 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005087 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005088 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005089 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005090
Vladimir Markoa1de9182016-02-25 11:37:38 +00005091 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5092 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005093
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005094 DECLARE_INSTRUCTION(ParameterValue);
5095
5096 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005097 // Whether or not the parameter value corresponds to 'this' argument.
5098 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5099 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5100 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5101 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5102 "Too many packed fields.");
5103
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005104 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005105 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005106 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005107 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005108 const uint8_t index_;
5109
5110 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5111};
5112
Vladimir Markofcb503c2016-05-18 12:48:17 +01005113class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005115 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5116 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005117
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005118 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005119 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005120 return true;
5121 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005122
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005123 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005124
5125 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005126 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005127 }
5128 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005129 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005130 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005131 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5132 LOG(FATAL) << DebugName() << " is not defined for float values";
5133 UNREACHABLE();
5134 }
5135 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5136 LOG(FATAL) << DebugName() << " is not defined for double values";
5137 UNREACHABLE();
5138 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005139
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005140 DECLARE_INSTRUCTION(Not);
5141
5142 private:
5143 DISALLOW_COPY_AND_ASSIGN(HNot);
5144};
5145
Vladimir Markofcb503c2016-05-18 12:48:17 +01005146class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005148 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5149 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005150
5151 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005152 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005153 return true;
5154 }
5155
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005156 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005157 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005158 return !x;
5159 }
5160
Roland Levillain9867bc72015-08-05 10:21:34 +01005161 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005162 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005163 }
5164 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5165 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005166 UNREACHABLE();
5167 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005168 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5169 LOG(FATAL) << DebugName() << " is not defined for float values";
5170 UNREACHABLE();
5171 }
5172 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5173 LOG(FATAL) << DebugName() << " is not defined for double values";
5174 UNREACHABLE();
5175 }
David Brazdil66d126e2015-04-03 16:02:44 +01005176
5177 DECLARE_INSTRUCTION(BooleanNot);
5178
5179 private:
5180 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5181};
5182
Vladimir Markofcb503c2016-05-18 12:48:17 +01005183class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005184 public:
5185 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005186 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005187 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005188 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005189 // Invariant: We should never generate a conversion to a Boolean value.
5190 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005191 }
5192
5193 HInstruction* GetInput() const { return InputAt(0); }
5194 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5195 Primitive::Type GetResultType() const { return GetType(); }
5196
5197 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005198 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5199 return true;
5200 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005201
Mark Mendelle82549b2015-05-06 10:55:34 -04005202 // Try to statically evaluate the conversion and return a HConstant
5203 // containing the result. If the input cannot be converted, return nullptr.
5204 HConstant* TryStaticEvaluation() const;
5205
Roland Levillaindff1f282014-11-05 14:15:05 +00005206 DECLARE_INSTRUCTION(TypeConversion);
5207
5208 private:
5209 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5210};
5211
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005212static constexpr uint32_t kNoRegNumber = -1;
5213
Vladimir Markofcb503c2016-05-18 12:48:17 +01005214class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005216 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5217 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005218 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005219 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005220 SetRawInputAt(0, value);
5221 }
5222
Calin Juravle10e244f2015-01-26 18:54:32 +00005223 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005224 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005225 return true;
5226 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005227
Calin Juravle10e244f2015-01-26 18:54:32 +00005228 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005229
Calin Juravle10e244f2015-01-26 18:54:32 +00005230 bool CanThrow() const OVERRIDE { return true; }
5231
5232 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005233
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005234
5235 DECLARE_INSTRUCTION(NullCheck);
5236
5237 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5239};
5240
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005241// Embeds an ArtField and all the information required by the compiler. We cache
5242// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243class FieldInfo : public ValueObject {
5244 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005245 FieldInfo(ArtField* field,
5246 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005247 Primitive::Type field_type,
5248 bool is_volatile,
5249 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005250 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005251 const DexFile& dex_file)
5252 : field_(field),
5253 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005254 field_type_(field_type),
5255 is_volatile_(is_volatile),
5256 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005257 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005258 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005259
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005260 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005263 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005264 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005265 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005266 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005267
5268 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005269 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005270 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005271 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005272 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005273 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005274 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005275 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005276};
5277
Vladimir Markofcb503c2016-05-18 12:48:17 +01005278class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005279 public:
5280 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005281 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005282 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005283 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005284 bool is_volatile,
5285 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005286 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005287 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005288 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005289 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005290 field_info_(field,
5291 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005292 field_type,
5293 is_volatile,
5294 field_idx,
5295 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005296 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005297 SetRawInputAt(0, value);
5298 }
5299
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005300 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005301
Vladimir Marko372f10e2016-05-17 16:30:10 +01005302 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5303 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005304 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005305 }
5306
Calin Juravle641547a2015-04-21 22:08:51 +01005307 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005308 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005309 }
5310
5311 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005312 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5313 }
5314
Calin Juravle52c48962014-12-16 17:02:57 +00005315 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005316 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005317 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005318 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005319
5320 DECLARE_INSTRUCTION(InstanceFieldGet);
5321
5322 private:
5323 const FieldInfo field_info_;
5324
5325 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5326};
5327
Vladimir Markofcb503c2016-05-18 12:48:17 +01005328class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005329 public:
5330 HInstanceFieldSet(HInstruction* object,
5331 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005332 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005333 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005334 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005335 bool is_volatile,
5336 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005337 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005338 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005339 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005340 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005341 field_info_(field,
5342 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005343 field_type,
5344 is_volatile,
5345 field_idx,
5346 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005347 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005348 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005349 SetRawInputAt(0, object);
5350 SetRawInputAt(1, value);
5351 }
5352
Calin Juravle641547a2015-04-21 22:08:51 +01005353 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005354 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005355 }
5356
Calin Juravle52c48962014-12-16 17:02:57 +00005357 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005358 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005359 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005360 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005361 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005362 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5363 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005364
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005365 DECLARE_INSTRUCTION(InstanceFieldSet);
5366
5367 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005368 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5369 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5370 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5371 "Too many packed fields.");
5372
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005373 const FieldInfo field_info_;
5374
5375 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5376};
5377
Vladimir Markofcb503c2016-05-18 12:48:17 +01005378class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005380 HArrayGet(HInstruction* array,
5381 HInstruction* index,
5382 Primitive::Type type,
5383 uint32_t dex_pc,
5384 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005385 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005386 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005387 SetRawInputAt(0, array);
5388 SetRawInputAt(1, index);
5389 }
5390
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005391 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005392 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005393 return true;
5394 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005395 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005396 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005397 // Currently, unless the array is the result of NewArray, the array access is always
5398 // preceded by some form of null NullCheck necessary for the bounds check, usually
5399 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5400 // dynamic BCE. There are cases when these could be removed to produce better code.
5401 // If we ever add optimizations to do so we should allow an implicit check here
5402 // (as long as the address falls in the first page).
5403 //
5404 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5405 // a = cond ? new int[1] : null;
5406 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005407 return false;
5408 }
5409
David Brazdil4833f5a2015-12-16 10:37:39 +00005410 bool IsEquivalentOf(HArrayGet* other) const {
5411 bool result = (GetDexPc() == other->GetDexPc());
5412 if (kIsDebugBuild && result) {
5413 DCHECK_EQ(GetBlock(), other->GetBlock());
5414 DCHECK_EQ(GetArray(), other->GetArray());
5415 DCHECK_EQ(GetIndex(), other->GetIndex());
5416 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005417 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005418 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005419 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5420 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005421 }
5422 }
5423 return result;
5424 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005425
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005426 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5427
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005428 HInstruction* GetArray() const { return InputAt(0); }
5429 HInstruction* GetIndex() const { return InputAt(1); }
5430
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 DECLARE_INSTRUCTION(ArrayGet);
5432
5433 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005434 // We treat a String as an array, creating the HArrayGet from String.charAt()
5435 // intrinsic in the instruction simplifier. We can always determine whether
5436 // a particular HArrayGet is actually a String.charAt() by looking at the type
5437 // of the input but that requires holding the mutator lock, so we prefer to use
5438 // a flag, so that code generators don't need to do the locking.
5439 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5440 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5441 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5442 "Too many packed fields.");
5443
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5445};
5446
Vladimir Markofcb503c2016-05-18 12:48:17 +01005447class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 public:
5449 HArraySet(HInstruction* array,
5450 HInstruction* index,
5451 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005452 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005453 uint32_t dex_pc)
5454 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005455 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5456 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5457 SetPackedFlag<kFlagValueCanBeNull>(true);
5458 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005459 SetRawInputAt(0, array);
5460 SetRawInputAt(1, index);
5461 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005462 // Make a best guess now, may be refined during SSA building.
5463 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 }
5465
Calin Juravle77520bc2015-01-12 18:45:46 +00005466 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005467 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005469 }
5470
Mingyao Yang81014cb2015-06-02 03:16:27 -07005471 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005473
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005474 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005475 // TODO: Same as for ArrayGet.
5476 return false;
5477 }
5478
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005479 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005481 }
5482
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005483 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005485 }
5486
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005487 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005488 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005489 }
5490
Vladimir Markoa1de9182016-02-25 11:37:38 +00005491 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5492 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5493 bool StaticTypeOfArrayIsObjectArray() const {
5494 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5495 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005496
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005497 HInstruction* GetArray() const { return InputAt(0); }
5498 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005499 HInstruction* GetValue() const { return InputAt(2); }
5500
5501 Primitive::Type GetComponentType() const {
5502 // The Dex format does not type floating point index operations. Since the
5503 // `expected_component_type_` is set during building and can therefore not
5504 // be correct, we also check what is the value type. If it is a floating
5505 // point type, we must use that type.
5506 Primitive::Type value_type = GetValue()->GetType();
5507 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5508 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005509 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005510 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005511
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005512 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005514 }
5515
Aart Bik18b36ab2016-04-13 16:41:35 -07005516 void ComputeSideEffects() {
5517 Primitive::Type type = GetComponentType();
5518 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5519 SideEffectsForArchRuntimeCalls(type)));
5520 }
5521
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005522 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5523 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5524 }
5525
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005526 DECLARE_INSTRUCTION(ArraySet);
5527
5528 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005529 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5530 static constexpr size_t kFieldExpectedComponentTypeSize =
5531 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5532 static constexpr size_t kFlagNeedsTypeCheck =
5533 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5534 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005535 // Cached information for the reference_type_info_ so that codegen
5536 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005537 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5538 static constexpr size_t kNumberOfArraySetPackedBits =
5539 kFlagStaticTypeOfArrayIsObjectArray + 1;
5540 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5541 using ExpectedComponentTypeField =
5542 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543
5544 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5545};
5546
Vladimir Markofcb503c2016-05-18 12:48:17 +01005547class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005549 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005550 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005551 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005552 // Note that arrays do not change length, so the instruction does not
5553 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005554 SetRawInputAt(0, array);
5555 }
5556
Calin Juravle77520bc2015-01-12 18:45:46 +00005557 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005558 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005559 return true;
5560 }
Calin Juravle641547a2015-04-21 22:08:51 +01005561 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5562 return obj == InputAt(0);
5563 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005564
Vladimir Markodce016e2016-04-28 13:10:02 +01005565 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5566
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 DECLARE_INSTRUCTION(ArrayLength);
5568
5569 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005570 // We treat a String as an array, creating the HArrayLength from String.length()
5571 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5572 // determine whether a particular HArrayLength is actually a String.length() by
5573 // looking at the type of the input but that requires holding the mutator lock, so
5574 // we prefer to use a flag, so that code generators don't need to do the locking.
5575 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5576 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5577 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5578 "Too many packed fields.");
5579
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005580 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5581};
5582
Vladimir Markofcb503c2016-05-18 12:48:17 +01005583class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005585 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5586 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005587 HBoundsCheck(HInstruction* index,
5588 HInstruction* length,
5589 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005590 bool string_char_at = false)
5591 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005592 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005593 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 SetRawInputAt(0, index);
5595 SetRawInputAt(1, length);
5596 }
5597
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005598 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005599 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005600 return true;
5601 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005603 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005605 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005606
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005607 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005608
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005609 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005610
5611 DECLARE_INSTRUCTION(BoundsCheck);
5612
5613 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005614 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005615
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5617};
5618
Vladimir Markofcb503c2016-05-18 12:48:17 +01005619class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005620 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005621 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005622 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005623
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005624 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005625 return true;
5626 }
5627
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005628 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5629 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005630
5631 DECLARE_INSTRUCTION(SuspendCheck);
5632
5633 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005634 // Only used for code generation, in order to share the same slow path between back edges
5635 // of a same loop.
5636 SlowPathCode* slow_path_;
5637
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005638 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5639};
5640
David Srbecky0cf44932015-12-09 14:09:59 +00005641// Pseudo-instruction which provides the native debugger with mapping information.
5642// It ensures that we can generate line number and local variables at this point.
5643class HNativeDebugInfo : public HTemplateInstruction<0> {
5644 public:
5645 explicit HNativeDebugInfo(uint32_t dex_pc)
5646 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5647
5648 bool NeedsEnvironment() const OVERRIDE {
5649 return true;
5650 }
5651
5652 DECLARE_INSTRUCTION(NativeDebugInfo);
5653
5654 private:
5655 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5656};
5657
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005658/**
5659 * Instruction to load a Class object.
5660 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005661class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005662 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005663 // Determines how to load the Class.
5664 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005665 // We cannot load this class. See HSharpening::SharpenLoadClass.
5666 kInvalid = -1,
5667
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005668 // Use the Class* from the method's own ArtMethod*.
5669 kReferrersClass,
5670
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005671 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005672 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673 kBootImageLinkTimePcRelative,
5674
5675 // Use a known boot image Class* address, embedded in the code by the codegen.
5676 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 kBootImageAddress,
5678
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005679 // Load from an entry in the .bss section using a PC-relative load.
5680 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5681 kBssEntry,
5682
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005683 // Load from the root table associated with the JIT compiled method.
5684 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005685
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005686 // Load using a simple runtime call. This is the fall-back load kind when
5687 // the codegen is unable to use another appropriate kind.
5688 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005689
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005690 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 };
5692
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005693 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005694 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005695 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005696 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005697 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005698 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005699 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5701 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005703 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005704 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005705 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005706 // Referrers class should not need access check. We never inline unverified
5707 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005708 DCHECK(!is_referrers_class || !needs_access_check);
5709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005710 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005711 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005713 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005714 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715 }
5716
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005717 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005718
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 LoadKind GetLoadKind() const {
5720 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005721 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005722
5723 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005726
Andreas Gampea5b09a62016-11-17 15:21:22 -08005727 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005729 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005731 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005732 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005733 }
5734
Calin Juravle0ba218d2015-05-19 18:46:01 +01005735 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005736 // The entrypoint the code generator is going to call does not do
5737 // clinit of the class.
5738 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005739 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005740 }
5741
5742 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005743 return NeedsAccessCheck() ||
5744 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005745 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005746 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005747 }
5748
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005749 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005750 return NeedsAccessCheck() ||
5751 MustGenerateClinitCheck() ||
5752 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5753 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5754 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005755 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005756 GetLoadKind() == LoadKind::kBssEntry) &&
5757 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005758 }
5759
Calin Juravleacf735c2015-02-12 15:25:22 +00005760 ReferenceTypeInfo GetLoadedClassRTI() {
5761 return loaded_class_rti_;
5762 }
5763
5764 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5765 // Make sure we only set exact types (the loaded class should never be merged).
5766 DCHECK(rti.IsExact());
5767 loaded_class_rti_ = rti;
5768 }
5769
Andreas Gampea5b09a62016-11-17 15:21:22 -08005770 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 const DexFile& GetDexFile() const { return dex_file_; }
5772
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005773 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005774 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005775 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005776
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005777 static SideEffects SideEffectsForArchRuntimeCalls() {
5778 return SideEffects::CanTriggerGC();
5779 }
5780
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005781 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005782 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005783 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005784 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005785
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005786 void MarkInBootImage() {
5787 SetPackedFlag<kFlagIsInBootImage>(true);
5788 }
5789
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 void AddSpecialInput(HInstruction* special_input);
5791
5792 using HInstruction::GetInputRecords; // Keep the const version visible.
5793 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5794 return ArrayRef<HUserRecord<HInstruction*>>(
5795 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5796 }
5797
5798 Primitive::Type GetType() const OVERRIDE {
5799 return Primitive::kPrimNot;
5800 }
5801
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005802 Handle<mirror::Class> GetClass() const {
5803 return klass_;
5804 }
5805
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005806 DECLARE_INSTRUCTION(LoadClass);
5807
5808 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005809 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005810 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005811 // Whether this instruction must generate the initialization check.
5812 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005813 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005814 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5815 static constexpr size_t kFieldLoadKindSize =
5816 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5817 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005818 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005819 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5820
5821 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005822 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005824 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005825 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826 }
5827
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005828 void SetLoadKindInternal(LoadKind load_kind);
5829
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005830 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005832 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005834
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005835 // A type index and dex file where the class can be accessed. The dex file can be:
5836 // - The compiling method's dex file if the class is defined there too.
5837 // - The compiling method's dex file if the class is referenced there.
5838 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005839 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005840 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005842
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005843 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005844
Calin Juravleacf735c2015-02-12 15:25:22 +00005845 ReferenceTypeInfo loaded_class_rti_;
5846
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005847 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5848};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5850
5851// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005852inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005853 // The special input is used for PC-relative loads on some architectures,
5854 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005856 GetLoadKind() == LoadKind::kBootImageAddress ||
5857 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005858 DCHECK(special_input_.GetInstruction() == nullptr);
5859 special_input_ = HUserRecord<HInstruction*>(special_input);
5860 special_input->AddUseAt(this, 0);
5861}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005862
Vladimir Marko372f10e2016-05-17 16:30:10 +01005863class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005864 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865 // Determines how to load the String.
5866 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005868 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005869 kBootImageLinkTimePcRelative,
5870
5871 // Use a known boot image String* address, embedded in the code by the codegen.
5872 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005873 kBootImageAddress,
5874
Vladimir Markoaad75c62016-10-03 08:46:48 +00005875 // Load from an entry in the .bss section using a PC-relative load.
5876 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5877 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005878
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 // Load from the root table associated with the JIT compiled method.
5880 kJitTableAddress,
5881
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005882 // Load using a simple runtime call. This is the fall-back load kind when
5883 // the codegen is unable to use another appropriate kind.
5884 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005886 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005887 };
5888
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005889 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005890 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 const DexFile& dex_file,
5892 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005893 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5894 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005895 string_index_(string_index),
5896 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005897 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005898 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005899
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005900 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005901
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902 LoadKind GetLoadKind() const {
5903 return GetPackedField<LoadKindField>();
5904 }
5905
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005906 const DexFile& GetDexFile() const {
5907 return dex_file_;
5908 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005910 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005911 return string_index_;
5912 }
5913
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005914 Handle<mirror::String> GetString() const {
5915 return string_;
5916 }
5917
5918 void SetString(Handle<mirror::String> str) {
5919 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005920 }
5921
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005922 bool CanBeMoved() const OVERRIDE { return true; }
5923
Vladimir Marko372f10e2016-05-17 16:30:10 +01005924 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005925
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005926 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005927
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005928 // Will call the runtime if we need to load the string through
5929 // the dex cache and the string is not guaranteed to be there yet.
5930 bool NeedsEnvironment() const OVERRIDE {
5931 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005932 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005933 load_kind == LoadKind::kBootImageAddress ||
5934 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 return false;
5936 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005937 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005938 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005939
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005940 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005941 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005943
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005944 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005945 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005946
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005947 static SideEffects SideEffectsForArchRuntimeCalls() {
5948 return SideEffects::CanTriggerGC();
5949 }
5950
Vladimir Marko372f10e2016-05-17 16:30:10 +01005951 void AddSpecialInput(HInstruction* special_input);
5952
5953 using HInstruction::GetInputRecords; // Keep the const version visible.
5954 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5955 return ArrayRef<HUserRecord<HInstruction*>>(
5956 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 }
5958
Vladimir Marko372f10e2016-05-17 16:30:10 +01005959 Primitive::Type GetType() const OVERRIDE {
5960 return Primitive::kPrimNot;
5961 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005962
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005963 DECLARE_INSTRUCTION(LoadString);
5964
5965 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005966 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 static constexpr size_t kFieldLoadKindSize =
5968 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5969 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005972
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005973 void SetLoadKindInternal(LoadKind load_kind);
5974
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005975 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005976 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005977 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005978 HUserRecord<HInstruction*> special_input_;
5979
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005980 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005981 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005983 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005984
5985 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5986};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5988
5989// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005991 // The special input is used for PC-relative loads on some architectures,
5992 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005993 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005994 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005995 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005996 // HLoadString::GetInputRecords() returns an empty array at this point,
5997 // so use the GetInputRecords() from the base class to set the input record.
5998 DCHECK(special_input_.GetInstruction() == nullptr);
5999 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006000 special_input->AddUseAt(this, 0);
6001}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006002
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003/**
6004 * Performs an initialization check on its Class object input.
6005 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006006class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006007 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006008 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006009 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006010 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006011 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6012 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006013 SetRawInputAt(0, constant);
6014 }
6015
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006016 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006017 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006018 return true;
6019 }
6020
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006021 bool NeedsEnvironment() const OVERRIDE {
6022 // May call runtime to initialize the class.
6023 return true;
6024 }
6025
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006026 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006028 HLoadClass* GetLoadClass() const {
6029 DCHECK(InputAt(0)->IsLoadClass());
6030 return InputAt(0)->AsLoadClass();
6031 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006032
6033 DECLARE_INSTRUCTION(ClinitCheck);
6034
6035 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006036 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6037};
6038
Vladimir Markofcb503c2016-05-18 12:48:17 +01006039class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006040 public:
6041 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006042 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006043 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006044 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006045 bool is_volatile,
6046 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006047 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006048 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006049 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006050 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006051 field_info_(field,
6052 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006053 field_type,
6054 is_volatile,
6055 field_idx,
6056 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006057 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 SetRawInputAt(0, cls);
6059 }
6060
Calin Juravle52c48962014-12-16 17:02:57 +00006061
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006062 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006063
Vladimir Marko372f10e2016-05-17 16:30:10 +01006064 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6065 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006066 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067 }
6068
6069 size_t ComputeHashCode() const OVERRIDE {
6070 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6071 }
6072
Calin Juravle52c48962014-12-16 17:02:57 +00006073 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6075 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006076 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006077
6078 DECLARE_INSTRUCTION(StaticFieldGet);
6079
6080 private:
6081 const FieldInfo field_info_;
6082
6083 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6084};
6085
Vladimir Markofcb503c2016-05-18 12:48:17 +01006086class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006087 public:
6088 HStaticFieldSet(HInstruction* cls,
6089 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006090 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006091 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006092 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006093 bool is_volatile,
6094 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006095 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006096 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006097 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006098 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006099 field_info_(field,
6100 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006101 field_type,
6102 is_volatile,
6103 field_idx,
6104 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006105 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006106 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006107 SetRawInputAt(0, cls);
6108 SetRawInputAt(1, value);
6109 }
6110
Calin Juravle52c48962014-12-16 17:02:57 +00006111 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006112 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6113 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006114 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006115
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006116 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6118 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006119
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006120 DECLARE_INSTRUCTION(StaticFieldSet);
6121
6122 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006123 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6124 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6125 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6126 "Too many packed fields.");
6127
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006128 const FieldInfo field_info_;
6129
6130 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6131};
6132
Vladimir Markofcb503c2016-05-18 12:48:17 +01006133class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006134 public:
6135 HUnresolvedInstanceFieldGet(HInstruction* obj,
6136 Primitive::Type field_type,
6137 uint32_t field_index,
6138 uint32_t dex_pc)
6139 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6140 field_index_(field_index) {
6141 SetRawInputAt(0, obj);
6142 }
6143
6144 bool NeedsEnvironment() const OVERRIDE { return true; }
6145 bool CanThrow() const OVERRIDE { return true; }
6146
6147 Primitive::Type GetFieldType() const { return GetType(); }
6148 uint32_t GetFieldIndex() const { return field_index_; }
6149
6150 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6151
6152 private:
6153 const uint32_t field_index_;
6154
6155 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6156};
6157
Vladimir Markofcb503c2016-05-18 12:48:17 +01006158class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006159 public:
6160 HUnresolvedInstanceFieldSet(HInstruction* obj,
6161 HInstruction* value,
6162 Primitive::Type field_type,
6163 uint32_t field_index,
6164 uint32_t dex_pc)
6165 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006166 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006168 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006169 SetRawInputAt(0, obj);
6170 SetRawInputAt(1, value);
6171 }
6172
6173 bool NeedsEnvironment() const OVERRIDE { return true; }
6174 bool CanThrow() const OVERRIDE { return true; }
6175
Vladimir Markoa1de9182016-02-25 11:37:38 +00006176 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006177 uint32_t GetFieldIndex() const { return field_index_; }
6178
6179 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6180
6181 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6183 static constexpr size_t kFieldFieldTypeSize =
6184 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6185 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6186 kFieldFieldType + kFieldFieldTypeSize;
6187 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6188 "Too many packed fields.");
6189 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6190
Calin Juravlee460d1d2015-09-29 04:52:17 +01006191 const uint32_t field_index_;
6192
6193 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6194};
6195
Vladimir Markofcb503c2016-05-18 12:48:17 +01006196class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006197 public:
6198 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6199 uint32_t field_index,
6200 uint32_t dex_pc)
6201 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6202 field_index_(field_index) {
6203 }
6204
6205 bool NeedsEnvironment() const OVERRIDE { return true; }
6206 bool CanThrow() const OVERRIDE { return true; }
6207
6208 Primitive::Type GetFieldType() const { return GetType(); }
6209 uint32_t GetFieldIndex() const { return field_index_; }
6210
6211 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6212
6213 private:
6214 const uint32_t field_index_;
6215
6216 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6217};
6218
Vladimir Markofcb503c2016-05-18 12:48:17 +01006219class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006220 public:
6221 HUnresolvedStaticFieldSet(HInstruction* value,
6222 Primitive::Type field_type,
6223 uint32_t field_index,
6224 uint32_t dex_pc)
6225 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006226 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006227 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006228 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006229 SetRawInputAt(0, value);
6230 }
6231
6232 bool NeedsEnvironment() const OVERRIDE { return true; }
6233 bool CanThrow() const OVERRIDE { return true; }
6234
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006236 uint32_t GetFieldIndex() const { return field_index_; }
6237
6238 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6239
6240 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006241 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6242 static constexpr size_t kFieldFieldTypeSize =
6243 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6244 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6245 kFieldFieldType + kFieldFieldTypeSize;
6246 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6247 "Too many packed fields.");
6248 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6249
Calin Juravlee460d1d2015-09-29 04:52:17 +01006250 const uint32_t field_index_;
6251
6252 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6253};
6254
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006255// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006256class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006257 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006258 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6259 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006260
David Brazdilbbd733e2015-08-18 17:48:17 +01006261 bool CanBeNull() const OVERRIDE { return false; }
6262
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006263 DECLARE_INSTRUCTION(LoadException);
6264
6265 private:
6266 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6267};
6268
David Brazdilcb1c0552015-08-04 16:22:25 +01006269// Implicit part of move-exception which clears thread-local exception storage.
6270// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006271class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006273 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6274 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006275
6276 DECLARE_INSTRUCTION(ClearException);
6277
6278 private:
6279 DISALLOW_COPY_AND_ASSIGN(HClearException);
6280};
6281
Vladimir Markofcb503c2016-05-18 12:48:17 +01006282class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006283 public:
6284 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006285 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006286 SetRawInputAt(0, exception);
6287 }
6288
6289 bool IsControlFlow() const OVERRIDE { return true; }
6290
6291 bool NeedsEnvironment() const OVERRIDE { return true; }
6292
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006293 bool CanThrow() const OVERRIDE { return true; }
6294
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006295
6296 DECLARE_INSTRUCTION(Throw);
6297
6298 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006299 DISALLOW_COPY_AND_ASSIGN(HThrow);
6300};
6301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302/**
6303 * Implementation strategies for the code generator of a HInstanceOf
6304 * or `HCheckCast`.
6305 */
6306enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006307 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 kExactCheck, // Can do a single class compare.
6309 kClassHierarchyCheck, // Can just walk the super class chain.
6310 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6311 kInterfaceCheck, // No optimization yet when checking against an interface.
6312 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006313 kArrayCheck, // No optimization yet when checking against a generic array.
6314 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006315};
6316
Roland Levillain86503782016-02-11 19:07:30 +00006317std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6318
Vladimir Markofcb503c2016-05-18 12:48:17 +01006319class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006320 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006321 HInstanceOf(HInstruction* object,
6322 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006325 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006327 dex_pc) {
6328 SetPackedField<TypeCheckKindField>(check_kind);
6329 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006330 SetRawInputAt(0, object);
6331 SetRawInputAt(1, constant);
6332 }
6333
6334 bool CanBeMoved() const OVERRIDE { return true; }
6335
Vladimir Marko372f10e2016-05-17 16:30:10 +01006336 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337 return true;
6338 }
6339
6340 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006342 }
6343
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006344 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006345 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6346 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6347 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6348 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006349
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006350 static bool CanCallRuntime(TypeCheckKind check_kind) {
6351 // Mips currently does runtime calls for any other checks.
6352 return check_kind != TypeCheckKind::kExactCheck;
6353 }
6354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006356 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006357 }
6358
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006359 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006360
6361 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006362 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6363 static constexpr size_t kFieldTypeCheckKindSize =
6364 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6365 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6366 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6367 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6368 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006369
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006370 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6371};
6372
Vladimir Markofcb503c2016-05-18 12:48:17 +01006373class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006374 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006375 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006376 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6378 SetPackedFlag<kFlagUpperCanBeNull>(true);
6379 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006380 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006381 SetRawInputAt(0, input);
6382 }
6383
David Brazdilf5552582015-12-27 13:36:12 +00006384 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006385 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006387 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006388
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006389 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 DCHECK(GetUpperCanBeNull() || !can_be_null);
6391 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006392 }
6393
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006395
Calin Juravleb1498f62015-02-16 13:13:29 +00006396 DECLARE_INSTRUCTION(BoundType);
6397
6398 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6400 // is false then CanBeNull() cannot be true).
6401 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6402 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6403 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6404 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6405
Calin Juravleb1498f62015-02-16 13:13:29 +00006406 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006407 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6408 // It is used to bound the type in cases like:
6409 // if (x instanceof ClassX) {
6410 // // uper_bound_ will be ClassX
6411 // }
David Brazdilf5552582015-12-27 13:36:12 +00006412 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006413
6414 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6415};
6416
Vladimir Markofcb503c2016-05-18 12:48:17 +01006417class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006418 public:
6419 HCheckCast(HInstruction* object,
6420 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006422 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006423 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6424 SetPackedField<TypeCheckKindField>(check_kind);
6425 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006426 SetRawInputAt(0, object);
6427 SetRawInputAt(1, constant);
6428 }
6429
6430 bool CanBeMoved() const OVERRIDE { return true; }
6431
Vladimir Marko372f10e2016-05-17 16:30:10 +01006432 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006433 return true;
6434 }
6435
6436 bool NeedsEnvironment() const OVERRIDE {
6437 // Instruction may throw a CheckCastError.
6438 return true;
6439 }
6440
6441 bool CanThrow() const OVERRIDE { return true; }
6442
Vladimir Markoa1de9182016-02-25 11:37:38 +00006443 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6444 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6445 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6446 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006447
6448 DECLARE_INSTRUCTION(CheckCast);
6449
6450 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006451 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6452 static constexpr size_t kFieldTypeCheckKindSize =
6453 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6454 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6455 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6456 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6457 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006458
6459 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006460};
6461
Andreas Gampe26de38b2016-07-27 17:53:11 -07006462/**
6463 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6464 * @details We define the combined barrier types that are actually required
6465 * by the Java Memory Model, rather than using exactly the terminology from
6466 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6467 * primitives. Note that the JSR-133 cookbook generally does not deal with
6468 * store atomicity issues, and the recipes there are not always entirely sufficient.
6469 * The current recipe is as follows:
6470 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6471 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6472 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6473 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6474 * class has final fields.
6475 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6476 * store-to-memory instructions. Only generated together with non-temporal stores.
6477 */
6478enum MemBarrierKind {
6479 kAnyStore,
6480 kLoadAny,
6481 kStoreStore,
6482 kAnyAny,
6483 kNTStoreStore,
6484 kLastBarrierKind = kNTStoreStore
6485};
6486std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6487
Vladimir Markofcb503c2016-05-18 12:48:17 +01006488class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006489 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006490 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006491 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006492 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6493 SetPackedField<BarrierKindField>(barrier_kind);
6494 }
Calin Juravle27df7582015-04-17 19:12:31 +01006495
Vladimir Markoa1de9182016-02-25 11:37:38 +00006496 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006497
6498 DECLARE_INSTRUCTION(MemoryBarrier);
6499
6500 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006501 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6502 static constexpr size_t kFieldBarrierKindSize =
6503 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6504 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6505 kFieldBarrierKind + kFieldBarrierKindSize;
6506 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6507 "Too many packed fields.");
6508 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006509
6510 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6511};
6512
Igor Murashkind01745e2017-04-05 16:40:31 -07006513// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6514// the specified object(s), with respect to any subsequent store that might "publish"
6515// (i.e. make visible) the specified object to another thread.
6516//
6517// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6518// for all final fields (that were set) at the end of the invoked constructor.
6519//
6520// The constructor fence models the freeze actions for the final fields of an object
6521// being constructed (semantically at the end of the constructor). Constructor fences
6522// have a per-object affinity; two separate objects being constructed get two separate
6523// constructor fences.
6524//
6525// (Note: that if calling a super-constructor or forwarding to another constructor,
6526// the freezes would happen at the end of *that* constructor being invoked).
6527//
6528// The memory model guarantees that when the object being constructed is "published" after
6529// constructor completion (i.e. escapes the current thread via a store), then any final field
6530// writes must be observable on other threads (once they observe that publication).
6531//
6532// Further, anything written before the freeze, and read by dereferencing through the final field,
6533// must also be visible (so final object field could itself have an object with non-final fields;
6534// yet the freeze must also extend to them).
6535//
6536// Constructor example:
6537//
6538// class HasFinal {
6539// final int field; Optimizing IR for <init>()V:
6540// HasFinal() {
6541// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6542// // freeze(this.field); HConstructorFence(this)
6543// } HReturn
6544// }
6545//
6546// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6547// already-initialized classes; in that case the allocation must act as a "default-initializer"
6548// of the object which effectively writes the class pointer "final field".
6549//
6550// For example, we can model default-initialiation as roughly the equivalent of the following:
6551//
6552// class Object {
6553// private final Class header;
6554// }
6555//
6556// Java code: Optimizing IR:
6557//
6558// T new_instance<T>() {
6559// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6560// obj.header = T.class; // header write is done by above call.
6561// // freeze(obj.header) HConstructorFence(obj)
6562// return (T)obj;
6563// }
6564//
6565// See also:
6566// * CompilerDriver::RequiresConstructorBarrier
6567// * QuasiAtomic::ThreadFenceForConstructor
6568//
6569class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6570 // A fence has variable inputs because the inputs can be removed
6571 // after prepare_for_register_allocation phase.
6572 // (TODO: In the future a fence could freeze multiple objects
6573 // after merging two fences together.)
6574 public:
6575 // `fence_object` is the reference that needs to be protected for correct publication.
6576 //
6577 // It makes sense in the following situations:
6578 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6579 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6580 //
6581 // After construction the `fence_object` becomes the 0th input.
6582 // This is not an input in a real sense, but just a convenient place to stash the information
6583 // about the associated object.
6584 HConstructorFence(HInstruction* fence_object,
6585 uint32_t dex_pc,
6586 ArenaAllocator* arena)
6587 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6588 // constraints described in the class header. We claim that these SideEffects constraints
6589 // enforce a superset of the real constraints.
6590 //
6591 // The ordering described above is conservatively modeled with SideEffects as follows:
6592 //
6593 // * To prevent reordering of the publication stores:
6594 // ----> "Reads of objects" is the initial SideEffect.
6595 // * For every primitive final field store in the constructor:
6596 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6597 // * If there are any stores to reference final fields in the constructor:
6598 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6599 // that are reachable from `fence_object` also need to be prevented for reordering
6600 // (and we do not want to do alias analysis to figure out what those stores are).
6601 //
6602 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6603 // even more conservative approach than the one described above, and prevents all of the
6604 // above reordering without analyzing any of the instructions in the constructor.
6605 //
6606 // If in a later phase we discover that there are no writes to reference final fields,
6607 // we can refine the side effect to a smaller set of type reads (see above constraints).
6608 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6609 dex_pc,
6610 arena,
6611 /* number_of_inputs */ 1,
6612 kArenaAllocConstructorFenceInputs) {
6613 DCHECK(fence_object != nullptr);
6614 SetRawInputAt(0, fence_object);
6615 }
6616
6617 // The object associated with this constructor fence.
6618 //
6619 // (Note: This will be null after the prepare_for_register_allocation phase,
6620 // as all constructor fence inputs are removed there).
6621 HInstruction* GetFenceObject() const {
6622 return InputAt(0);
6623 }
6624
6625 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6626 // - Delete `fence_use` from `this`'s use list.
6627 // - Delete `this` from `fence_use`'s inputs list.
6628 // - If the `fence_use` is dead, remove it from the graph.
6629 //
6630 // A fence is considered dead once it no longer has any uses
6631 // and all of the inputs are dead.
6632 //
6633 // This must *not* be called during/after prepare_for_register_allocation,
6634 // because that removes all the inputs to the fences but the fence is actually
6635 // still considered live.
Igor Murashkin6ef45672017-08-08 13:59:55 -07006636 //
6637 // Returns how many HConstructorFence instructions were removed from graph.
6638 static size_t RemoveConstructorFences(HInstruction* instruction);
Igor Murashkind01745e2017-04-05 16:40:31 -07006639
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006640 // Check if this constructor fence is protecting
6641 // an HNewInstance or HNewArray that is also the immediate
6642 // predecessor of `this`.
6643 //
6644 // Returns the associated HNewArray or HNewInstance,
6645 // or null otherwise.
6646 HInstruction* GetAssociatedAllocation();
6647
Igor Murashkind01745e2017-04-05 16:40:31 -07006648 DECLARE_INSTRUCTION(ConstructorFence);
6649
6650 private:
6651 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6652};
6653
Vladimir Markofcb503c2016-05-18 12:48:17 +01006654class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006655 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006656 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006657 kEnter,
6658 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006659 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006660 };
6661
6662 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006663 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006664 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6665 dex_pc) {
6666 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006667 SetRawInputAt(0, object);
6668 }
6669
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006670 // Instruction may go into runtime, so we need an environment.
6671 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006672
6673 bool CanThrow() const OVERRIDE {
6674 // Verifier guarantees that monitor-exit cannot throw.
6675 // This is important because it allows the HGraphBuilder to remove
6676 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6677 return IsEnter();
6678 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006679
Vladimir Markoa1de9182016-02-25 11:37:38 +00006680 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6681 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006682
6683 DECLARE_INSTRUCTION(MonitorOperation);
6684
Calin Juravle52c48962014-12-16 17:02:57 +00006685 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006686 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6687 static constexpr size_t kFieldOperationKindSize =
6688 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6689 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6690 kFieldOperationKind + kFieldOperationKindSize;
6691 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6692 "Too many packed fields.");
6693 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006694
6695 private:
6696 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6697};
6698
Vladimir Markofcb503c2016-05-18 12:48:17 +01006699class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006700 public:
6701 HSelect(HInstruction* condition,
6702 HInstruction* true_value,
6703 HInstruction* false_value,
6704 uint32_t dex_pc)
6705 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6706 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6707
6708 // First input must be `true_value` or `false_value` to allow codegens to
6709 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6710 // that architectures which implement HSelect as a conditional move also
6711 // will not need to invert the condition.
6712 SetRawInputAt(0, false_value);
6713 SetRawInputAt(1, true_value);
6714 SetRawInputAt(2, condition);
6715 }
6716
6717 HInstruction* GetFalseValue() const { return InputAt(0); }
6718 HInstruction* GetTrueValue() const { return InputAt(1); }
6719 HInstruction* GetCondition() const { return InputAt(2); }
6720
6721 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006722 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6723 return true;
6724 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006725
6726 bool CanBeNull() const OVERRIDE {
6727 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6728 }
6729
6730 DECLARE_INSTRUCTION(Select);
6731
6732 private:
6733 DISALLOW_COPY_AND_ASSIGN(HSelect);
6734};
6735
Vladimir Markof9f64412015-09-02 14:05:49 +01006736class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006737 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006738 MoveOperands(Location source,
6739 Location destination,
6740 Primitive::Type type,
6741 HInstruction* instruction)
6742 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006743
6744 Location GetSource() const { return source_; }
6745 Location GetDestination() const { return destination_; }
6746
6747 void SetSource(Location value) { source_ = value; }
6748 void SetDestination(Location value) { destination_ = value; }
6749
6750 // The parallel move resolver marks moves as "in-progress" by clearing the
6751 // destination (but not the source).
6752 Location MarkPending() {
6753 DCHECK(!IsPending());
6754 Location dest = destination_;
6755 destination_ = Location::NoLocation();
6756 return dest;
6757 }
6758
6759 void ClearPending(Location dest) {
6760 DCHECK(IsPending());
6761 destination_ = dest;
6762 }
6763
6764 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006765 DCHECK(source_.IsValid() || destination_.IsInvalid());
6766 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006767 }
6768
6769 // True if this blocks a move from the given location.
6770 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006771 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006772 }
6773
6774 // A move is redundant if it's been eliminated, if its source and
6775 // destination are the same, or if its destination is unneeded.
6776 bool IsRedundant() const {
6777 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6778 }
6779
6780 // We clear both operands to indicate move that's been eliminated.
6781 void Eliminate() {
6782 source_ = destination_ = Location::NoLocation();
6783 }
6784
6785 bool IsEliminated() const {
6786 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6787 return source_.IsInvalid();
6788 }
6789
Alexey Frunze4dda3372015-06-01 18:31:49 -07006790 Primitive::Type GetType() const { return type_; }
6791
Nicolas Geoffray90218252015-04-15 11:56:51 +01006792 bool Is64BitMove() const {
6793 return Primitive::Is64BitType(type_);
6794 }
6795
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006796 HInstruction* GetInstruction() const { return instruction_; }
6797
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006798 private:
6799 Location source_;
6800 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006801 // The type this move is for.
6802 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006803 // The instruction this move is assocatied with. Null when this move is
6804 // for moving an input in the expected locations of user (including a phi user).
6805 // This is only used in debug mode, to ensure we do not connect interval siblings
6806 // in the same parallel move.
6807 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006808};
6809
Roland Levillainc9285912015-12-18 10:38:42 +00006810std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6811
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006812static constexpr size_t kDefaultNumberOfMoves = 4;
6813
Vladimir Markofcb503c2016-05-18 12:48:17 +01006814class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006816 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006817 : HTemplateInstruction(SideEffects::None(), dex_pc),
6818 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6819 moves_.reserve(kDefaultNumberOfMoves);
6820 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006821
Nicolas Geoffray90218252015-04-15 11:56:51 +01006822 void AddMove(Location source,
6823 Location destination,
6824 Primitive::Type type,
6825 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006826 DCHECK(source.IsValid());
6827 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006828 if (kIsDebugBuild) {
6829 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006830 for (const MoveOperands& move : moves_) {
6831 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006832 // Special case the situation where the move is for the spill slot
6833 // of the instruction.
6834 if ((GetPrevious() == instruction)
6835 || ((GetPrevious() == nullptr)
6836 && instruction->IsPhi()
6837 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006838 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006839 << "Doing parallel moves for the same instruction.";
6840 } else {
6841 DCHECK(false) << "Doing parallel moves for the same instruction.";
6842 }
6843 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006844 }
6845 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006846 for (const MoveOperands& move : moves_) {
6847 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006848 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006849 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006850 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006851 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006852 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006853 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006854 }
6855
Vladimir Marko225b6462015-09-28 12:17:40 +01006856 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006857 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006858 }
6859
Vladimir Marko225b6462015-09-28 12:17:40 +01006860 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006861
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006862 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006863
6864 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006865 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006866
6867 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6868};
6869
Mark Mendell0616ae02015-04-17 12:49:27 -04006870} // namespace art
6871
Aart Bikf8f5a162017-02-06 15:35:29 -08006872#include "nodes_vector.h"
6873
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006874#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6875#include "nodes_shared.h"
6876#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006877#ifdef ART_ENABLE_CODEGEN_mips
6878#include "nodes_mips.h"
6879#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006880#ifdef ART_ENABLE_CODEGEN_x86
6881#include "nodes_x86.h"
6882#endif
6883
6884namespace art {
6885
Igor Murashkin6ef45672017-08-08 13:59:55 -07006886class OptimizingCompilerStats;
6887
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006888class HGraphVisitor : public ValueObject {
6889 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006890 explicit HGraphVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6891 : stats_(stats),
6892 graph_(graph) {}
Dave Allison20dfc792014-06-16 20:44:29 -07006893 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006894
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006895 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006896 virtual void VisitBasicBlock(HBasicBlock* block);
6897
Roland Levillain633021e2014-10-01 14:12:25 +01006898 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006899 void VisitInsertionOrder();
6900
Roland Levillain633021e2014-10-01 14:12:25 +01006901 // Visit the graph following dominator tree reverse post-order.
6902 void VisitReversePostOrder();
6903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006904 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006905
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006906 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006907#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006908 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6909
6910 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6911
6912#undef DECLARE_VISIT_INSTRUCTION
6913
Igor Murashkin6ef45672017-08-08 13:59:55 -07006914 protected:
6915 OptimizingCompilerStats* stats_;
6916
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006917 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006918 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006919
6920 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6921};
6922
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006923class HGraphDelegateVisitor : public HGraphVisitor {
6924 public:
Igor Murashkin6ef45672017-08-08 13:59:55 -07006925 explicit HGraphDelegateVisitor(HGraph* graph, OptimizingCompilerStats* stats = nullptr)
6926 : HGraphVisitor(graph, stats) {}
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006927 virtual ~HGraphDelegateVisitor() {}
6928
6929 // Visit functions that delegate to to super class.
6930#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006931 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006932
6933 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6934
6935#undef DECLARE_VISIT_INSTRUCTION
6936
6937 private:
6938 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6939};
6940
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006941// Iterator over the blocks that art part of the loop. Includes blocks part
6942// of an inner loop. The order in which the blocks are iterated is on their
6943// block id.
6944class HBlocksInLoopIterator : public ValueObject {
6945 public:
6946 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6947 : blocks_in_loop_(info.GetBlocks()),
6948 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6949 index_(0) {
6950 if (!blocks_in_loop_.IsBitSet(index_)) {
6951 Advance();
6952 }
6953 }
6954
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006955 bool Done() const { return index_ == blocks_.size(); }
6956 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006957 void Advance() {
6958 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006959 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006960 if (blocks_in_loop_.IsBitSet(index_)) {
6961 break;
6962 }
6963 }
6964 }
6965
6966 private:
6967 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006968 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006969 size_t index_;
6970
6971 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6972};
6973
Mingyao Yang3584bce2015-05-19 16:01:59 -07006974// Iterator over the blocks that art part of the loop. Includes blocks part
6975// of an inner loop. The order in which the blocks are iterated is reverse
6976// post order.
6977class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6978 public:
6979 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6980 : blocks_in_loop_(info.GetBlocks()),
6981 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6982 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006983 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006984 Advance();
6985 }
6986 }
6987
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006988 bool Done() const { return index_ == blocks_.size(); }
6989 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006990 void Advance() {
6991 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006992 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6993 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006994 break;
6995 }
6996 }
6997 }
6998
6999 private:
7000 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007001 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007002 size_t index_;
7003
7004 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7005};
7006
Aart Bikf3e61ee2017-04-12 17:09:20 -07007007// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007008inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007009 if (constant->IsIntConstant()) {
7010 return constant->AsIntConstant()->GetValue();
7011 } else if (constant->IsLongConstant()) {
7012 return constant->AsLongConstant()->GetValue();
7013 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007014 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007015 return 0;
7016 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007017}
7018
Aart Bikf3e61ee2017-04-12 17:09:20 -07007019// Returns true iff instruction is an integral constant (and sets value on success).
7020inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7021 if (instruction->IsIntConstant()) {
7022 *value = instruction->AsIntConstant()->GetValue();
7023 return true;
7024 } else if (instruction->IsLongConstant()) {
7025 *value = instruction->AsLongConstant()->GetValue();
7026 return true;
7027 } else if (instruction->IsNullConstant()) {
7028 *value = 0;
7029 return true;
7030 }
7031 return false;
7032}
7033
Aart Bik0148de42017-09-05 09:25:01 -07007034// Returns true iff instruction is the given integral constant.
7035inline bool IsInt64Value(HInstruction* instruction, int64_t value) {
7036 int64_t val = 0;
7037 return IsInt64AndGet(instruction, &val) && val == value;
7038}
7039
7040// Returns true iff instruction is a zero bit pattern.
7041inline bool IsZeroBitPattern(HInstruction* instruction) {
7042 return instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern();
7043}
7044
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007045#define INSTRUCTION_TYPE_CHECK(type, super) \
7046 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7047 inline const H##type* HInstruction::As##type() const { \
7048 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7049 } \
7050 inline H##type* HInstruction::As##type() { \
7051 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7052 }
7053
7054 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7055#undef INSTRUCTION_TYPE_CHECK
7056
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007057// Create space in `blocks` for adding `number_of_new_blocks` entries
7058// starting at location `at`. Blocks after `at` are moved accordingly.
7059inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7060 size_t number_of_new_blocks,
7061 size_t after) {
7062 DCHECK_LT(after, blocks->size());
7063 size_t old_size = blocks->size();
7064 size_t new_size = old_size + number_of_new_blocks;
7065 blocks->resize(new_size);
7066 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7067}
7068
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007069/*
7070 * Hunt "under the hood" of array lengths (leading to array references),
7071 * null checks (also leading to array references), and new arrays
7072 * (leading to the actual length). This makes it more likely related
7073 * instructions become actually comparable.
7074 */
7075inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7076 while (instruction->IsArrayLength() ||
7077 instruction->IsNullCheck() ||
7078 instruction->IsNewArray()) {
7079 instruction = instruction->IsNewArray()
7080 ? instruction->AsNewArray()->GetLength()
7081 : instruction->InputAt(0);
7082 }
7083 return instruction;
7084}
7085
Artem Serov21c7e6f2017-07-27 16:04:42 +01007086void RemoveEnvironmentUses(HInstruction* instruction);
7087bool HasEnvironmentUsedByOthers(HInstruction* instruction);
7088void ResetEnvironmentInputRecords(HInstruction* instruction);
7089
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007090} // namespace art
7091
7092#endif // ART_COMPILER_OPTIMIZING_NODES_H_