blob: 74bb2ab3c44ecbd660a5acdbd1781e0b2fb90918 [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +010033#include "deoptimization_kind.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000034#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000035#include "handle.h"
36#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000037#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010038#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010039#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000040#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000041#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010042#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070043#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010044#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000045
46namespace art {
47
David Brazdil1abb4192015-02-17 18:33:36 +000048class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000049class HBasicBlock;
Igor Murashkind01745e2017-04-05 16:40:31 -070050class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000055class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000056class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000057class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000058class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000059class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000060class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000061class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070062class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010064class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010065class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000067class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010068class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000069class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000070
Mathieu Chartier736b5602015-09-02 14:54:11 -070071namespace mirror {
72class DexCache;
73} // namespace mirror
74
Nicolas Geoffray818f2102014-02-18 16:43:35 +000075static const int kDefaultNumberOfBlocks = 8;
76static const int kDefaultNumberOfSuccessors = 2;
77static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010078static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010079static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000080static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000081
Roland Levillain5b5b9312016-03-22 14:57:31 +000082// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
83static constexpr int32_t kMaxIntShiftDistance = 0x1f;
84// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
85static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000086
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010087static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070088static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010089
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010090static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
91
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060092static constexpr uint32_t kNoDexPc = -1;
93
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010094inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
95 // For the purposes of the compiler, the dex files must actually be the same object
96 // if we want to safely treat them as the same. This is especially important for JIT
97 // as custom class loaders can open the same underlying file (or memory) multiple
98 // times and provide different class resolution but no two class loaders should ever
99 // use the same DexFile object - doing so is an unsupported hack that can lead to
100 // all sorts of weird failures.
101 return &lhs == &rhs;
102}
103
Dave Allison20dfc792014-06-16 20:44:29 -0700104enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700105 // All types.
106 kCondEQ, // ==
107 kCondNE, // !=
108 // Signed integers and floating-point numbers.
109 kCondLT, // <
110 kCondLE, // <=
111 kCondGT, // >
112 kCondGE, // >=
113 // Unsigned integers.
114 kCondB, // <
115 kCondBE, // <=
116 kCondA, // >
117 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100118 // First and last aliases.
119 kCondFirst = kCondEQ,
120 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700121};
122
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000124 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000125 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 kAnalysisFailThrowCatchLoop,
127 kAnalysisFailAmbiguousArrayOp,
128 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000129};
130
Andreas Gampe9186ced2016-12-12 14:28:21 -0800131template <typename T>
132static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
133 return static_cast<typename std::make_unsigned<T>::type>(x);
134}
135
Vladimir Markof9f64412015-09-02 14:05:49 +0100136class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100137 public:
138 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
139
140 void AddInstruction(HInstruction* instruction);
141 void RemoveInstruction(HInstruction* instruction);
142
David Brazdilc3d743f2015-04-22 13:40:50 +0100143 // Insert `instruction` before/after an existing instruction `cursor`.
144 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
145 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
146
Roland Levillain6b469232014-09-25 10:10:38 +0100147 // Return true if this list contains `instruction`.
148 bool Contains(HInstruction* instruction) const;
149
Roland Levillainccc07a92014-09-16 14:48:16 +0100150 // Return true if `instruction1` is found before `instruction2` in
151 // this instruction list and false otherwise. Abort if none
152 // of these instructions is found.
153 bool FoundBefore(const HInstruction* instruction1,
154 const HInstruction* instruction2) const;
155
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000156 bool IsEmpty() const { return first_instruction_ == nullptr; }
157 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
158
159 // Update the block of all instructions to be `block`.
160 void SetBlockOfInstructions(HBasicBlock* block) const;
161
162 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000163 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000164 void Add(const HInstructionList& instruction_list);
165
David Brazdil2d7352b2015-04-20 14:52:42 +0100166 // Return the number of instructions in the list. This is an expensive operation.
167 size_t CountSize() const;
168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100169 private:
170 HInstruction* first_instruction_;
171 HInstruction* last_instruction_;
172
173 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000174 friend class HGraph;
175 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000177 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100178 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100179
180 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
181};
182
David Brazdil4833f5a2015-12-16 10:37:39 +0000183class ReferenceTypeInfo : ValueObject {
184 public:
185 typedef Handle<mirror::Class> TypeHandle;
186
Vladimir Markoa1de9182016-02-25 11:37:38 +0000187 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
188
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100190 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
191 }
192
Vladimir Markoa1de9182016-02-25 11:37:38 +0000193 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000194 return ReferenceTypeInfo(type_handle, is_exact);
195 }
196
197 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
198
Vladimir Markof39745e2016-01-26 12:16:55 +0000199 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return handle.GetReference() != nullptr;
201 }
202
Vladimir Marko456307a2016-04-19 14:12:13 +0000203 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000204 return IsValidHandle(type_handle_);
205 }
206
207 bool IsExact() const { return is_exact_; }
208
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000210 DCHECK(IsValid());
211 return GetTypeHandle()->IsObjectClass();
212 }
213
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700214 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000215 DCHECK(IsValid());
216 return GetTypeHandle()->IsStringClass();
217 }
218
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700219 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000220 DCHECK(IsValid());
221 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
222 }
223
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700224 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000225 DCHECK(IsValid());
226 return GetTypeHandle()->IsInterface();
227 }
228
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700229 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000230 DCHECK(IsValid());
231 return GetTypeHandle()->IsArrayClass();
232 }
233
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700234 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000235 DCHECK(IsValid());
236 return GetTypeHandle()->IsPrimitiveArray();
237 }
238
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700239 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000240 DCHECK(IsValid());
241 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
242 }
243
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700244 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000245 DCHECK(IsValid());
246 if (!IsExact()) return false;
247 if (!IsArrayClass()) return false;
248 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
249 }
250
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700251 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000252 DCHECK(IsValid());
253 if (!IsExact()) return false;
254 if (!IsArrayClass()) return false;
255 if (!rti.IsArrayClass()) return false;
256 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
257 rti.GetTypeHandle()->GetComponentType());
258 }
259
260 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
261
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700262 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000263 DCHECK(IsValid());
264 DCHECK(rti.IsValid());
265 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
266 }
267
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700268 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000269 DCHECK(IsValid());
270 DCHECK(rti.IsValid());
271 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
272 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
273 }
274
275 // Returns true if the type information provide the same amount of details.
276 // Note that it does not mean that the instructions have the same actual type
277 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700278 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000279 if (!IsValid() && !rti.IsValid()) {
280 // Invalid types are equal.
281 return true;
282 }
283 if (!IsValid() || !rti.IsValid()) {
284 // One is valid, the other not.
285 return false;
286 }
287 return IsExact() == rti.IsExact()
288 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
289 }
290
291 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000292 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
293 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
294 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000295
296 // The class of the object.
297 TypeHandle type_handle_;
298 // Whether or not the type is exact or a superclass of the actual type.
299 // Whether or not we have any information about this type.
300 bool is_exact_;
301};
302
303std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
304
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000305// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100306class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000307 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100308 HGraph(ArenaAllocator* arena,
309 const DexFile& dex_file,
310 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100312 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000314 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000316 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 blocks_(arena->Adapter(kArenaAllocBlockList)),
318 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
319 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700320 entry_block_(nullptr),
321 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100322 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100323 number_of_vregs_(0),
324 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000325 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400326 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000327 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700328 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800329 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000330 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000331 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000332 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100333 dex_file_(dex_file),
334 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100335 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100336 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800337 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000339 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100340 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
343 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000344 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100345 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000346 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700347 osr_(osr),
348 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100349 blocks_.reserve(kDefaultNumberOfBlocks);
350 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000351
David Brazdilbadd8262016-02-02 16:28:56 +0000352 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700353 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100356 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
357
David Brazdil69ba7b72015-06-23 18:27:30 +0100358 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000359 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 HBasicBlock* GetEntryBlock() const { return entry_block_; }
362 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100363 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
366 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000367
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000368 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100370 void ComputeDominanceInformation();
371 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000372 void ClearLoopInformation();
373 void FindBackEdges(ArenaBitVector* visited);
374 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100375 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100376 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000377
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // Analyze all natural loops in this graph. Returns a code specifying that it
379 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000380 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000381 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100382
David Brazdilffee3d32015-07-06 11:48:53 +0100383 // Iterate over blocks to compute try block membership. Needs reverse post
384 // order and loop information.
385 void ComputeTryBlockInformation();
386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000387 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000388 // Returns the instruction to replace the invoke expression or null if the
389 // invoke is for a void method. Note that the caller is responsible for replacing
390 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000391 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000392
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000393 // Update the loop and try membership of `block`, which was spawned from `reference`.
394 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
395 // should be the new back edge.
396 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
397 HBasicBlock* reference,
398 bool replace_if_back_edge);
399
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 // Need to add a couple of blocks to test if the loop body is entered and
401 // put deoptimization instructions, etc.
402 void TransformLoopHeaderForBCE(HBasicBlock* header);
403
Aart Bikf8f5a162017-02-06 15:35:29 -0800404 // Adds a new loop directly after the loop with the given header and exit.
405 // Returns the new preheader.
406 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
407 HBasicBlock* body,
408 HBasicBlock* exit);
409
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000410 // Removes `block` from the graph. Assumes `block` has been disconnected from
411 // other blocks and has no instructions or phis.
412 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000413
David Brazdilfc6a86a2015-06-26 10:33:45 +0000414 // Splits the edge between `block` and `successor` while preserving the
415 // indices in the predecessor/successor lists. If there are multiple edges
416 // between the blocks, the lowest indices are used.
417 // Returns the new block which is empty and has the same dex pc as `successor`.
418 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
419
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100420 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
421 void SimplifyLoop(HBasicBlock* header);
422
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000423 int32_t GetNextInstructionId() {
424 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000425 return current_instruction_id_++;
426 }
427
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000428 int32_t GetCurrentInstructionId() const {
429 return current_instruction_id_;
430 }
431
432 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000433 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 current_instruction_id_ = id;
435 }
436
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100437 uint16_t GetMaximumNumberOfOutVRegs() const {
438 return maximum_number_of_out_vregs_;
439 }
440
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000441 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
442 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100443 }
444
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
446 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
447 }
448
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000449 void UpdateTemporariesVRegSlots(size_t slots) {
450 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100451 }
452
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000453 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100454 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000455 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100456 }
457
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100458 void SetNumberOfVRegs(uint16_t number_of_vregs) {
459 number_of_vregs_ = number_of_vregs;
460 }
461
462 uint16_t GetNumberOfVRegs() const {
463 return number_of_vregs_;
464 }
465
466 void SetNumberOfInVRegs(uint16_t value) {
467 number_of_in_vregs_ = value;
468 }
469
David Brazdildee58d62016-04-07 09:54:26 +0000470 uint16_t GetNumberOfInVRegs() const {
471 return number_of_in_vregs_;
472 }
473
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100474 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100475 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100476 return number_of_vregs_ - number_of_in_vregs_;
477 }
478
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100479 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100480 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100481 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100482
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100483 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
484 DCHECK(GetReversePostOrder()[0] == entry_block_);
485 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
486 }
487
488 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
489 return ReverseRange(GetReversePostOrder());
490 }
491
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100492 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100493 return linear_order_;
494 }
495
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100496 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
497 return ReverseRange(GetLinearOrder());
498 }
499
Mark Mendell1152c922015-04-24 17:06:35 -0400500 bool HasBoundsChecks() const {
501 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800502 }
503
Mark Mendell1152c922015-04-24 17:06:35 -0400504 void SetHasBoundsChecks(bool value) {
505 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800506 }
507
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000508 bool IsDebuggable() const { return debuggable_; }
509
David Brazdil8d5b8b22015-03-24 10:51:52 +0000510 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000511 // already, it is created and inserted into the graph. This method is only for
512 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000514
515 // TODO: This is problematic for the consistency of reference type propagation
516 // because it can be created anytime after the pass and thus it will be left
517 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000519
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
530 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000532
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100533 HCurrentMethod* GetCurrentMethod();
534
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100535 const DexFile& GetDexFile() const {
536 return dex_file_;
537 }
538
539 uint32_t GetMethodIdx() const {
540 return method_idx_;
541 }
542
Igor Murashkind01745e2017-04-05 16:40:31 -0700543 // Get the method name (without the signature), e.g. "<init>"
544 const char* GetMethodName() const;
545
546 // Get the pretty method name (class + name + optionally signature).
547 std::string PrettyMethod(bool with_signature = true) const;
548
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100549 InvokeType GetInvokeType() const {
550 return invoke_type_;
551 }
552
Mark Mendellc4701932015-04-10 13:18:51 -0400553 InstructionSet GetInstructionSet() const {
554 return instruction_set_;
555 }
556
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000557 bool IsCompilingOsr() const { return osr_; }
558
Mingyao Yang063fc772016-08-02 11:02:54 -0700559 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
560 return cha_single_implementation_list_;
561 }
562
563 void AddCHASingleImplementationDependency(ArtMethod* method) {
564 cha_single_implementation_list_.insert(method);
565 }
566
567 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800568 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700569 }
570
David Brazdil77a48ae2015-09-15 12:34:04 +0000571 bool HasTryCatch() const { return has_try_catch_; }
572 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100573
Aart Bikb13c65b2017-03-21 20:14:07 -0700574 bool HasSIMD() const { return has_simd_; }
575 void SetHasSIMD(bool value) { has_simd_ = value; }
576
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800577 bool HasLoops() const { return has_loops_; }
578 void SetHasLoops(bool value) { has_loops_ = value; }
579
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000580 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
581 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
582
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100583 ArtMethod* GetArtMethod() const { return art_method_; }
584 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
585
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100586 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500587 // The instruction has been inserted into the graph, either as a constant, or
588 // before cursor.
589 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
590
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000591 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
592
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800593 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
594 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
595 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
596
David Brazdil2d7352b2015-04-20 14:52:42 +0100597 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000598 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100599 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000600
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000601 template <class InstructionType, typename ValueType>
602 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600603 ArenaSafeMap<ValueType, InstructionType*>* cache,
604 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 // Try to find an existing constant of the given value.
606 InstructionType* constant = nullptr;
607 auto cached_constant = cache->find(value);
608 if (cached_constant != cache->end()) {
609 constant = cached_constant->second;
610 }
611
612 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100613 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000614 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600615 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000616 cache->Overwrite(value, constant);
617 InsertConstant(constant);
618 }
619 return constant;
620 }
621
David Brazdil8d5b8b22015-03-24 10:51:52 +0000622 void InsertConstant(HConstant* instruction);
623
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000624 // Cache a float constant into the graph. This method should only be
625 // called by the SsaBuilder when creating "equivalent" instructions.
626 void CacheFloatConstant(HFloatConstant* constant);
627
628 // See CacheFloatConstant comment.
629 void CacheDoubleConstant(HDoubleConstant* constant);
630
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000631 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000632
633 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100634 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000635
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100636 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000638
Aart Bik281c6812016-08-26 11:31:48 -0700639 // List of blocks to perform a linear order tree traversal. Unlike the reverse
640 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100641 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100642
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000643 HBasicBlock* entry_block_;
644 HBasicBlock* exit_block_;
645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100646 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100647 uint16_t maximum_number_of_out_vregs_;
648
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100649 // The number of virtual registers in this method. Contains the parameters.
650 uint16_t number_of_vregs_;
651
652 // The number of virtual registers used by parameters of this method.
653 uint16_t number_of_in_vregs_;
654
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000655 // Number of vreg size slots that the temporaries use (used in baseline compiler).
656 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100657
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800658 // Flag whether there are bounds checks in the graph. We can skip
659 // BCE if it's false. It's only best effort to keep it up to date in
660 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400661 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800662
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800663 // Flag whether there are try/catch blocks in the graph. We will skip
664 // try/catch-related passes if it's false. It's only best effort to keep
665 // it up to date in the presence of code elimination so there might be
666 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000667 bool has_try_catch_;
668
Aart Bikb13c65b2017-03-21 20:14:07 -0700669 // Flag whether SIMD instructions appear in the graph. If true, the
670 // code generators may have to be more careful spilling the wider
671 // contents of SIMD registers.
672 bool has_simd_;
673
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800674 // Flag whether there are any loops in the graph. We can skip loop
675 // optimization if it's false. It's only best effort to keep it up
676 // to date in the presence of code elimination so there might be false
677 // positives.
678 bool has_loops_;
679
680 // Flag whether there are any irreducible loops in the graph. It's only
681 // best effort to keep it up to date in the presence of code elimination
682 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000683 bool has_irreducible_loops_;
684
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000685 // Indicates whether the graph should be compiled in a way that
686 // ensures full debuggability. If false, we can apply more
687 // aggressive optimizations that may limit the level of debugging.
688 const bool debuggable_;
689
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000690 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000691 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000692
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100693 // The dex file from which the method is from.
694 const DexFile& dex_file_;
695
696 // The method index in the dex file.
697 const uint32_t method_idx_;
698
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100699 // If inlined, this encodes how the callee is being invoked.
700 const InvokeType invoke_type_;
701
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100702 // Whether the graph has been transformed to SSA form. Only used
703 // in debug mode to ensure we are not using properties only valid
704 // for non-SSA form (like the number of temporaries).
705 bool in_ssa_form_;
706
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800707 // Number of CHA guards in the graph. Used to short-circuit the
708 // CHA guard optimization pass when there is no CHA guard left.
709 uint32_t number_of_cha_guards_;
710
Mathieu Chartiere401d142015-04-22 13:56:20 -0700711 const InstructionSet instruction_set_;
712
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000713 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000714 HNullConstant* cached_null_constant_;
715 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000716 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000717 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000718 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000719
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100720 HCurrentMethod* cached_current_method_;
721
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100722 // The ArtMethod this graph is for. Note that for AOT, it may be null,
723 // for example for methods whose declaring class could not be resolved
724 // (such as when the superclass could not be found).
725 ArtMethod* art_method_;
726
David Brazdil4833f5a2015-12-16 10:37:39 +0000727 // Keep the RTI of inexact Object to avoid having to pass stack handle
728 // collection pointer to passes which may create NullConstant.
729 ReferenceTypeInfo inexact_object_rti_;
730
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000731 // Whether we are compiling this graph for on stack replacement: this will
732 // make all loops seen as irreducible and emit special stack maps to mark
733 // compiled code entries which the interpreter can directly jump to.
734 const bool osr_;
735
Mingyao Yang063fc772016-08-02 11:02:54 -0700736 // List of methods that are assumed to have single implementation.
737 ArenaSet<ArtMethod*> cha_single_implementation_list_;
738
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000739 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100740 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000741 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000742 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000743 DISALLOW_COPY_AND_ASSIGN(HGraph);
744};
745
Vladimir Markof9f64412015-09-02 14:05:49 +0100746class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000747 public:
748 HLoopInformation(HBasicBlock* header, HGraph* graph)
749 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100750 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000751 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100753 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100754 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000755 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 back_edges_.reserve(kDefaultNumberOfBackEdges);
757 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100758
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000759 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100760 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000761
762 void Dump(std::ostream& os);
763
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100764 HBasicBlock* GetHeader() const {
765 return header_;
766 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000768 void SetHeader(HBasicBlock* block) {
769 header_ = block;
770 }
771
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100772 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
773 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
774 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
775
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000776 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000778 }
779
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100780 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100782 }
783
David Brazdil46e2a392015-03-16 17:31:52 +0000784 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 }
787
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000788 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100789 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000790 }
791
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100792 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100793
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100795 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100796 }
797
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100798 // Returns the lifetime position of the back edge that has the
799 // greatest lifetime position.
800 size_t GetLifetimeEnd() const;
801
802 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100803 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100804 }
805
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000806 // Finds blocks that are part of this loop.
807 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100808
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100809 // Returns whether this loop information contains `block`.
810 // Note that this loop information *must* be populated before entering this function.
811 bool Contains(const HBasicBlock& block) const;
812
813 // Returns whether this loop information is an inner loop of `other`.
814 // Note that `other` *must* be populated before entering this function.
815 bool IsIn(const HLoopInformation& other) const;
816
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800817 // Returns true if instruction is not defined within this loop.
818 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700819
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100820 const ArenaBitVector& GetBlocks() const { return blocks_; }
821
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000822 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000823 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000824
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000825 void ClearAllBlocks() {
826 blocks_.ClearAllBits();
827 }
828
David Brazdil3f4a5222016-05-06 12:46:21 +0100829 bool HasBackEdgeNotDominatedByHeader() const;
830
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100831 bool IsPopulated() const {
832 return blocks_.GetHighestBitSet() != -1;
833 }
834
Anton Shaminf89381f2016-05-16 16:44:13 +0600835 bool DominatesAllBackEdges(HBasicBlock* block);
836
David Sehrc757dec2016-11-04 15:48:34 -0700837 bool HasExitEdge() const;
838
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000839 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 // Internal recursive implementation of `Populate`.
841 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100842 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100843
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000844 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100845 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000846 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100847 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100848 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100849 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000850
851 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
852};
853
David Brazdilec16f792015-08-19 15:04:01 +0100854// Stores try/catch information for basic blocks.
855// Note that HGraph is constructed so that catch blocks cannot simultaneously
856// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100857class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100858 public:
859 // Try block information constructor.
860 explicit TryCatchInformation(const HTryBoundary& try_entry)
861 : try_entry_(&try_entry),
862 catch_dex_file_(nullptr),
863 catch_type_index_(DexFile::kDexNoIndex16) {
864 DCHECK(try_entry_ != nullptr);
865 }
866
867 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800868 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100869 : try_entry_(nullptr),
870 catch_dex_file_(&dex_file),
871 catch_type_index_(catch_type_index) {}
872
873 bool IsTryBlock() const { return try_entry_ != nullptr; }
874
875 const HTryBoundary& GetTryEntry() const {
876 DCHECK(IsTryBlock());
877 return *try_entry_;
878 }
879
880 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
881
882 bool IsCatchAllTypeIndex() const {
883 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800884 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100885 }
886
Andreas Gampea5b09a62016-11-17 15:21:22 -0800887 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100888 DCHECK(IsCatchBlock());
889 return catch_type_index_;
890 }
891
892 const DexFile& GetCatchDexFile() const {
893 DCHECK(IsCatchBlock());
894 return *catch_dex_file_;
895 }
896
897 private:
898 // One of possibly several TryBoundary instructions entering the block's try.
899 // Only set for try blocks.
900 const HTryBoundary* try_entry_;
901
902 // Exception type information. Only set for catch blocks.
903 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800904 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100905};
906
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100907static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100908static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100909
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910// A block in a method. Contains the list of instructions represented
911// as a double linked list. Each block knows its predecessors and
912// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100913
Vladimir Markof9f64412015-09-02 14:05:49 +0100914class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700916 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000917 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000918 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
919 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000920 loop_information_(nullptr),
921 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000922 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100923 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100924 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100925 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000926 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000927 try_catch_information_(nullptr) {
928 predecessors_.reserve(kDefaultNumberOfPredecessors);
929 successors_.reserve(kDefaultNumberOfSuccessors);
930 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
931 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932
Vladimir Marko60584552015-09-03 13:35:12 +0000933 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000935 }
936
Vladimir Marko60584552015-09-03 13:35:12 +0000937 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100938 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000939 }
940
David Brazdild26a4112015-11-10 11:07:31 +0000941 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
942 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
943
Vladimir Marko60584552015-09-03 13:35:12 +0000944 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
945 return ContainsElement(successors_, block, start_from);
946 }
947
948 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100949 return dominated_blocks_;
950 }
951
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100952 bool IsEntryBlock() const {
953 return graph_->GetEntryBlock() == this;
954 }
955
956 bool IsExitBlock() const {
957 return graph_->GetExitBlock() == this;
958 }
959
David Brazdil46e2a392015-03-16 17:31:52 +0000960 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000961 bool IsSingleTryBoundary() const;
962
963 // Returns true if this block emits nothing but a jump.
964 bool IsSingleJump() const {
965 HLoopInformation* loop_info = GetLoopInformation();
966 return (IsSingleGoto() || IsSingleTryBoundary())
967 // Back edges generate a suspend check.
968 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
969 }
David Brazdil46e2a392015-03-16 17:31:52 +0000970
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000971 void AddBackEdge(HBasicBlock* back_edge) {
972 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000973 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000974 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100975 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000976 loop_information_->AddBackEdge(back_edge);
977 }
978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000980 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000981
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100982 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000983 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600984 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000985
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000986 HBasicBlock* GetDominator() const { return dominator_; }
987 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000988 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
989
990 void RemoveDominatedBlock(HBasicBlock* block) {
991 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100992 }
Vladimir Marko60584552015-09-03 13:35:12 +0000993
994 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
995 ReplaceElement(dominated_blocks_, existing, new_block);
996 }
997
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100998 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000999
1000 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001001 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001002 }
1003
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001004 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1005 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001006 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001007 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001008 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1009 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001010
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001011 HInstruction* GetFirstInstructionDisregardMoves() const;
1012
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001013 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001014 successors_.push_back(block);
1015 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001016 }
1017
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001018 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1019 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001020 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001021 new_block->predecessors_.push_back(this);
1022 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001023 }
1024
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001025 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1026 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001027 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001028 new_block->successors_.push_back(this);
1029 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001030 }
1031
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001032 // Insert `this` between `predecessor` and `successor. This method
1033 // preserves the indicies, and will update the first edge found between
1034 // `predecessor` and `successor`.
1035 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1036 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001037 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001038 successor->predecessors_[predecessor_index] = this;
1039 predecessor->successors_[successor_index] = this;
1040 successors_.push_back(successor);
1041 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001042 }
1043
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001045 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001046 }
1047
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001048 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001049 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001050 }
1051
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001053 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001054 }
1055
1056 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001057 predecessors_.push_back(block);
1058 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001059 }
1060
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001061 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001062 DCHECK_EQ(predecessors_.size(), 2u);
1063 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001064 }
1065
David Brazdil769c9e52015-04-27 13:54:09 +01001066 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 DCHECK_EQ(successors_.size(), 2u);
1068 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001069 }
1070
David Brazdilfc6a86a2015-06-26 10:33:45 +00001071 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001072 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001073 }
1074
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001076 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001077 }
1078
David Brazdilfc6a86a2015-06-26 10:33:45 +00001079 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001080 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001081 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001082 }
1083
1084 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001085 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001086 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001087 }
1088
1089 // Returns whether the first occurrence of `predecessor` in the list of
1090 // predecessors is at index `idx`.
1091 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001092 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 return GetPredecessorIndexOf(predecessor) == idx;
1094 }
1095
David Brazdild7558da2015-09-22 13:04:14 +01001096 // Create a new block between this block and its predecessors. The new block
1097 // is added to the graph, all predecessor edges are relinked to it and an edge
1098 // is created to `this`. Returns the new empty block. Reverse post order or
1099 // loop and try/catch information are not updated.
1100 HBasicBlock* CreateImmediateDominator();
1101
David Brazdilfc6a86a2015-06-26 10:33:45 +00001102 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001103 // created, latter block. Note that this method will add the block to the
1104 // graph, create a Goto at the end of the former block and will create an edge
1105 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001106 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001107 HBasicBlock* SplitBefore(HInstruction* cursor);
1108
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001109 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001110 // created block. Note that this method just updates raw block information,
1111 // like predecessors, successors, dominators, and instruction list. It does not
1112 // update the graph, reverse post order, loop information, nor make sure the
1113 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001114 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1115
1116 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1117 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118
1119 // Merge `other` at the end of `this`. Successors and dominated blocks of
1120 // `other` are changed to be successors and dominated blocks of `this`. Note
1121 // that this method does not update the graph, reverse post order, loop
1122 // information, nor make sure the blocks are consistent (for example ending
1123 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001124 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001125
1126 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1127 // of `this` are moved to `other`.
1128 // Note that this method does not update the graph, reverse post order, loop
1129 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001130 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001131 void ReplaceWith(HBasicBlock* other);
1132
Aart Bik6b69e0a2017-01-11 10:20:43 -08001133 // Merges the instructions of `other` at the end of `this`.
1134 void MergeInstructionsWith(HBasicBlock* other);
1135
David Brazdil2d7352b2015-04-20 14:52:42 +01001136 // Merge `other` at the end of `this`. This method updates loops, reverse post
1137 // order, links to predecessors, successors, dominators and deletes the block
1138 // from the graph. The two blocks must be successive, i.e. `this` the only
1139 // predecessor of `other` and vice versa.
1140 void MergeWith(HBasicBlock* other);
1141
1142 // Disconnects `this` from all its predecessors, successors and dominator,
1143 // removes it from all loops it is included in and eventually from the graph.
1144 // The block must not dominate any other block. Predecessors and successors
1145 // are safely updated.
1146 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001147
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001149 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001150 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001151 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001152 // Replace instruction `initial` with `replacement` within this block.
1153 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1154 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001156 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001157 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1158 // instruction list. With 'ensure_safety' set to true, it verifies that the
1159 // instruction is not in use and removes it from the use lists of its inputs.
1160 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1161 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001162 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001163
1164 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001165 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001166 }
1167
Roland Levillain6b879dd2014-09-22 17:13:44 +01001168 bool IsLoopPreHeaderFirstPredecessor() const {
1169 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001170 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001171 }
1172
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001173 bool IsFirstPredecessorBackEdge() const {
1174 DCHECK(IsLoopHeader());
1175 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1176 }
1177
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001178 HLoopInformation* GetLoopInformation() const {
1179 return loop_information_;
1180 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001181
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001182 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001183 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001184 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001185 void SetInLoop(HLoopInformation* info) {
1186 if (IsLoopHeader()) {
1187 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001188 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001189 loop_information_ = info;
1190 } else if (loop_information_->Contains(*info->GetHeader())) {
1191 // Block is currently part of an outer loop. Make it part of this inner loop.
1192 // Note that a non loop header having a loop information means this loop information
1193 // has already been populated
1194 loop_information_ = info;
1195 } else {
1196 // Block is part of an inner loop. Do not update the loop information.
1197 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1198 // at this point, because this method is being called while populating `info`.
1199 }
1200 }
1201
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001202 // Raw update of the loop information.
1203 void SetLoopInformation(HLoopInformation* info) {
1204 loop_information_ = info;
1205 }
1206
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001207 bool IsInLoop() const { return loop_information_ != nullptr; }
1208
David Brazdilec16f792015-08-19 15:04:01 +01001209 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1210
1211 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1212 try_catch_information_ = try_catch_information;
1213 }
1214
1215 bool IsTryBlock() const {
1216 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1217 }
1218
1219 bool IsCatchBlock() const {
1220 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1221 }
David Brazdilffee3d32015-07-06 11:48:53 +01001222
1223 // Returns the try entry that this block's successors should have. They will
1224 // be in the same try, unless the block ends in a try boundary. In that case,
1225 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001226 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001227
David Brazdild7558da2015-09-22 13:04:14 +01001228 bool HasThrowingInstructions() const;
1229
David Brazdila4b8c212015-05-07 09:59:30 +01001230 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001231 bool Dominates(HBasicBlock* block) const;
1232
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001233 size_t GetLifetimeStart() const { return lifetime_start_; }
1234 size_t GetLifetimeEnd() const { return lifetime_end_; }
1235
1236 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1237 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1238
David Brazdil8d5b8b22015-03-24 10:51:52 +00001239 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001240 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001241 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001242 bool HasSinglePhi() const;
1243
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001244 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001245 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001246 ArenaVector<HBasicBlock*> predecessors_;
1247 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001248 HInstructionList instructions_;
1249 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001250 HLoopInformation* loop_information_;
1251 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001252 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001253 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001254 // The dex program counter of the first instruction of this block.
1255 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001256 size_t lifetime_start_;
1257 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001258 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001260 friend class HGraph;
1261 friend class HInstruction;
1262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001263 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1264};
1265
David Brazdilb2bd1c52015-03-25 11:17:37 +00001266// Iterates over the LoopInformation of all loops which contain 'block'
1267// from the innermost to the outermost.
1268class HLoopInformationOutwardIterator : public ValueObject {
1269 public:
1270 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1271 : current_(block.GetLoopInformation()) {}
1272
1273 bool Done() const { return current_ == nullptr; }
1274
1275 void Advance() {
1276 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001277 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001278 }
1279
1280 HLoopInformation* Current() const {
1281 DCHECK(!Done());
1282 return current_;
1283 }
1284
1285 private:
1286 HLoopInformation* current_;
1287
1288 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1289};
1290
Alexandre Ramesef20f712015-06-09 10:29:30 +01001291#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001292 M(Above, Condition) \
1293 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001294 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001295 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001296 M(ArrayGet, Instruction) \
1297 M(ArrayLength, Instruction) \
1298 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001299 M(Below, Condition) \
1300 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001301 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001302 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001303 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001304 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001305 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001306 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001307 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001309 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001310 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001311 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001312 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001314 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001317 M(Exit, Instruction) \
1318 M(FloatConstant, Constant) \
1319 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(GreaterThan, Condition) \
1321 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001323 M(InstanceFieldGet, Instruction) \
1324 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001325 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001327 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001328 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001329 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001330 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001331 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(LessThan, Condition) \
1333 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001334 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001335 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001336 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001338 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001339 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001340 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001341 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(Neg, UnaryOperation) \
1343 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001345 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001347 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001348 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001349 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001350 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001352 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001354 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001355 M(Return, Instruction) \
1356 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001357 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001358 M(Shl, BinaryOperation) \
1359 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001360 M(StaticFieldGet, Instruction) \
1361 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001362 M(UnresolvedInstanceFieldGet, Instruction) \
1363 M(UnresolvedInstanceFieldSet, Instruction) \
1364 M(UnresolvedStaticFieldGet, Instruction) \
1365 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001366 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001368 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001369 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001370 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001371 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001372 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001373 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001374 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001375 M(VecSumReduce, VecUnaryOperation) \
1376 M(VecCnv, VecUnaryOperation) \
1377 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001378 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001379 M(VecNot, VecUnaryOperation) \
1380 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001381 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001382 M(VecSub, VecBinaryOperation) \
1383 M(VecMul, VecBinaryOperation) \
1384 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001385 M(VecMin, VecBinaryOperation) \
1386 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001387 M(VecAnd, VecBinaryOperation) \
1388 M(VecAndNot, VecBinaryOperation) \
1389 M(VecOr, VecBinaryOperation) \
1390 M(VecXor, VecBinaryOperation) \
1391 M(VecShl, VecBinaryOperation) \
1392 M(VecShr, VecBinaryOperation) \
1393 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001394 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001395 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001396 M(VecLoad, VecMemoryOperation) \
1397 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001398
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001399/*
1400 * Instructions, shared across several (not all) architectures.
1401 */
1402#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1403#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1404#else
1405#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001406 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001407 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001408 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001409 M(IntermediateAddress, Instruction) \
1410 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001411#endif
1412
Vladimir Markob4536b72015-11-24 13:45:23 +00001413#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001414#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001415#else
1416#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1417 M(ArmDexCacheArraysBase, Instruction)
1418#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001419
1420#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1421
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001422#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001423#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001424#else
1425#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1426 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001427 M(MipsDexCacheArraysBase, Instruction) \
1428 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001429#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001430
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001431#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1432
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001433#ifndef ART_ENABLE_CODEGEN_x86
1434#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1435#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001436#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1437 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001438 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001439 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001440 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001441#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001442
1443#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1444
1445#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1446 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001447 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001448 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1449 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001450 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001451 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001452 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1453 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1454
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001455#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1456 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001457 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001458 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001459 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001460 M(Invoke, Instruction) \
1461 M(VecOperation, Instruction) \
1462 M(VecUnaryOperation, VecOperation) \
1463 M(VecBinaryOperation, VecOperation) \
1464 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001465
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001466#define FOR_EACH_INSTRUCTION(M) \
1467 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1468 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1469
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001470#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001471FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1472#undef FORWARD_DECLARATION
1473
Vladimir Marko372f10e2016-05-17 16:30:10 +01001474#define DECLARE_INSTRUCTION(type) \
1475 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1476 const char* DebugName() const OVERRIDE { return #type; } \
1477 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1478 return other->Is##type(); \
1479 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001480 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001481
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001482#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1483 bool Is##type() const { return As##type() != nullptr; } \
1484 const H##type* As##type() const { return this; } \
1485 H##type* As##type() { return this; }
1486
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001487template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001488class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001489 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001490 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001491 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001492 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001493 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001494 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001495 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001496
Vladimir Marko46817b82016-03-29 12:21:58 +01001497 // Hook for the IntrusiveForwardList<>.
1498 // TODO: Hide this better.
1499 IntrusiveForwardListHook hook;
1500
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001501 private:
David Brazdiled596192015-01-23 10:39:45 +00001502 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001503 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001504
1505 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001506 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001507
Vladimir Marko46817b82016-03-29 12:21:58 +01001508 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001509
1510 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1511};
1512
David Brazdiled596192015-01-23 10:39:45 +00001513template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001514using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001515
David Brazdil1abb4192015-02-17 18:33:36 +00001516// This class is used by HEnvironment and HInstruction classes to record the
1517// instructions they use and pointers to the corresponding HUseListNodes kept
1518// by the used instructions.
1519template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001520class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001521 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001522 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1523 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001524
Vladimir Marko46817b82016-03-29 12:21:58 +01001525 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1526 : HUserRecord(old_record.instruction_, before_use_node) {}
1527 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1528 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001529 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001530 }
1531
1532 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001533 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1534 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001535
1536 private:
1537 // Instruction used by the user.
1538 HInstruction* instruction_;
1539
Vladimir Marko46817b82016-03-29 12:21:58 +01001540 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1541 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001542};
1543
Vladimir Markoe9004912016-06-16 16:50:52 +01001544// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1545// This is used for HInstruction::GetInputs() to return a container wrapper providing
1546// HInstruction* values even though the underlying container has HUserRecord<>s.
1547struct HInputExtractor {
1548 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1549 return record.GetInstruction();
1550 }
1551 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1552 return record.GetInstruction();
1553 }
1554};
1555
1556using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1557using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1558
Aart Bik854a02b2015-07-14 16:07:00 -07001559/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001560 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001561 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 * For write/read dependences on fields/arrays, the dependence analysis uses
1563 * type disambiguation (e.g. a float field write cannot modify the value of an
1564 * integer field read) and the access type (e.g. a reference array write cannot
1565 * modify the value of a reference field read [although it may modify the
1566 * reference fetch prior to reading the field, which is represented by its own
1567 * write/read dependence]). The analysis makes conservative points-to
1568 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1569 * the same, and any reference read depends on any reference read without
1570 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001571 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001572 * The internal representation uses 38-bit and is described in the table below.
1573 * The first line indicates the side effect, and for field/array accesses the
1574 * second line indicates the type of the access (in the order of the
1575 * Primitive::Type enum).
1576 * The two numbered lines below indicate the bit position in the bitfield (read
1577 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001578 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1580 * +-------------+---------+---------+--------------+---------+---------+
1581 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1582 * | 3 |333333322|222222221| 1 |111111110|000000000|
1583 * | 7 |654321098|765432109| 8 |765432109|876543210|
1584 *
1585 * Note that, to ease the implementation, 'changes' bits are least significant
1586 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001587 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001588class SideEffects : public ValueObject {
1589 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001590 SideEffects() : flags_(0) {}
1591
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592 static SideEffects None() {
1593 return SideEffects(0);
1594 }
1595
1596 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001597 return SideEffects(kAllChangeBits | kAllDependOnBits);
1598 }
1599
1600 static SideEffects AllChanges() {
1601 return SideEffects(kAllChangeBits);
1602 }
1603
1604 static SideEffects AllDependencies() {
1605 return SideEffects(kAllDependOnBits);
1606 }
1607
1608 static SideEffects AllExceptGCDependency() {
1609 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1610 }
1611
1612 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001613 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 }
1615
Aart Bik34c3ba92015-07-20 14:08:59 -07001616 static SideEffects AllWrites() {
1617 return SideEffects(kAllWrites);
1618 }
1619
1620 static SideEffects AllReads() {
1621 return SideEffects(kAllReads);
1622 }
1623
1624 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1625 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001627 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001628 }
1629
Aart Bik854a02b2015-07-14 16:07:00 -07001630 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001631 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001632 }
1633
Aart Bik34c3ba92015-07-20 14:08:59 -07001634 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1635 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001636 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001637 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001638 }
1639
1640 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001641 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001642 }
1643
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 static SideEffects CanTriggerGC() {
1645 return SideEffects(1ULL << kCanTriggerGCBit);
1646 }
1647
1648 static SideEffects DependsOnGC() {
1649 return SideEffects(1ULL << kDependsOnGCBit);
1650 }
1651
Aart Bik854a02b2015-07-14 16:07:00 -07001652 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001653 SideEffects Union(SideEffects other) const {
1654 return SideEffects(flags_ | other.flags_);
1655 }
1656
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001657 SideEffects Exclusion(SideEffects other) const {
1658 return SideEffects(flags_ & ~other.flags_);
1659 }
1660
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001661 void Add(SideEffects other) {
1662 flags_ |= other.flags_;
1663 }
1664
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001665 bool Includes(SideEffects other) const {
1666 return (other.flags_ & flags_) == other.flags_;
1667 }
1668
1669 bool HasSideEffects() const {
1670 return (flags_ & kAllChangeBits);
1671 }
1672
1673 bool HasDependencies() const {
1674 return (flags_ & kAllDependOnBits);
1675 }
1676
1677 // Returns true if there are no side effects or dependencies.
1678 bool DoesNothing() const {
1679 return flags_ == 0;
1680 }
1681
Aart Bik854a02b2015-07-14 16:07:00 -07001682 // Returns true if something is written.
1683 bool DoesAnyWrite() const {
1684 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001685 }
1686
Aart Bik854a02b2015-07-14 16:07:00 -07001687 // Returns true if something is read.
1688 bool DoesAnyRead() const {
1689 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001690 }
1691
Aart Bik854a02b2015-07-14 16:07:00 -07001692 // Returns true if potentially everything is written and read
1693 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 bool DoesAllReadWrite() const {
1695 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1696 }
1697
Aart Bik854a02b2015-07-14 16:07:00 -07001698 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001700 }
1701
Roland Levillain0d5a2812015-11-13 10:07:31 +00001702 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001703 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001704 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1705 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001706 }
1707
1708 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001710 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001711 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001712 for (int s = kLastBit; s >= 0; s--) {
1713 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1714 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1715 // This is a bit for the GC side effect.
1716 if (current_bit_is_set) {
1717 flags += "GC";
1718 }
Aart Bik854a02b2015-07-14 16:07:00 -07001719 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 } else {
1721 // This is a bit for the array/field analysis.
1722 // The underscore character stands for the 'can trigger GC' bit.
1723 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1724 if (current_bit_is_set) {
1725 flags += kDebug[s];
1726 }
1727 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1728 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1729 flags += "|";
1730 }
1731 }
Aart Bik854a02b2015-07-14 16:07:00 -07001732 }
1733 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001734 }
1735
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001736 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001737
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001738 private:
1739 static constexpr int kFieldArrayAnalysisBits = 9;
1740
1741 static constexpr int kFieldWriteOffset = 0;
1742 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1743 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1744 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1745
1746 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1747
1748 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1749 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1750 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1751 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1752
1753 static constexpr int kLastBit = kDependsOnGCBit;
1754 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1755
1756 // Aliases.
1757
1758 static_assert(kChangeBits == kDependOnBits,
1759 "the 'change' bits should match the 'depend on' bits.");
1760
1761 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1762 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1763 static constexpr uint64_t kAllWrites =
1764 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1765 static constexpr uint64_t kAllReads =
1766 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001767
Aart Bik854a02b2015-07-14 16:07:00 -07001768 // Translates type to bit flag.
1769 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1770 CHECK_NE(type, Primitive::kPrimVoid);
1771 const uint64_t one = 1;
1772 const int shift = type; // 0-based consecutive enum
1773 DCHECK_LE(kFieldWriteOffset, shift);
1774 DCHECK_LT(shift, kArrayWriteOffset);
1775 return one << (type + offset);
1776 }
1777
1778 // Private constructor on direct flags value.
1779 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1780
1781 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001782};
1783
David Brazdiled596192015-01-23 10:39:45 +00001784// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001785class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001786 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001787 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1788 size_t number_of_vregs,
1789 ArtMethod* method,
1790 uint32_t dex_pc,
1791 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001792 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
Vladimir Markoec32f642017-06-02 10:51:55 +01001793 locations_(arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001794 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001795 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001796 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001797 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001798 }
1799
Mingyao Yang01b47b02017-02-03 12:09:57 -08001800 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001801 : HEnvironment(arena,
1802 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001803 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001804 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001805 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001806
Vladimir Markoec32f642017-06-02 10:51:55 +01001807 void AllocateLocations() {
1808 DCHECK(locations_.empty());
1809 locations_.resize(vregs_.size());
1810 }
1811
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001812 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001813 if (parent_ != nullptr) {
1814 parent_->SetAndCopyParentChain(allocator, parent);
1815 } else {
1816 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1817 parent_->CopyFrom(parent);
1818 if (parent->GetParent() != nullptr) {
1819 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1820 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001821 }
David Brazdiled596192015-01-23 10:39:45 +00001822 }
1823
Vladimir Marko71bf8092015-09-15 15:33:14 +01001824 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001825 void CopyFrom(HEnvironment* environment);
1826
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001827 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1828 // input to the loop phi instead. This is for inserting instructions that
1829 // require an environment (like HDeoptimization) in the loop pre-header.
1830 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001831
1832 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001834 }
1835
1836 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001837 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001838 }
1839
David Brazdil1abb4192015-02-17 18:33:36 +00001840 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001841
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001842 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001843
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001844 HEnvironment* GetParent() const { return parent_; }
1845
1846 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001847 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001848 }
1849
1850 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001851 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001852 }
1853
1854 uint32_t GetDexPc() const {
1855 return dex_pc_;
1856 }
1857
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001858 ArtMethod* GetMethod() const {
1859 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 }
1861
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001862 HInstruction* GetHolder() const {
1863 return holder_;
1864 }
1865
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001866
1867 bool IsFromInlinedInvoke() const {
1868 return GetParent() != nullptr;
1869 }
1870
David Brazdiled596192015-01-23 10:39:45 +00001871 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001872 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1873 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001874 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001875 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001876 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001877
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001878 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001879 HInstruction* const holder_;
1880
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001881 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001882
1883 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1884};
1885
Vladimir Markof9f64412015-09-02 14:05:49 +01001886class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001887 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001888 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001889 : previous_(nullptr),
1890 next_(nullptr),
1891 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001892 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001893 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001894 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001895 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001896 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001897 locations_(nullptr),
1898 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001899 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001900 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001901 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1902 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1903 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001904
Dave Allison20dfc792014-06-16 20:44:29 -07001905 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001907#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001908 enum InstructionKind {
1909 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1910 };
1911#undef DECLARE_KIND
1912
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001913 HInstruction* GetNext() const { return next_; }
1914 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001915
Calin Juravle77520bc2015-01-12 18:45:46 +00001916 HInstruction* GetNextDisregardingMoves() const;
1917 HInstruction* GetPreviousDisregardingMoves() const;
1918
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001919 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001920 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001921 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001922 bool IsInBlock() const { return block_ != nullptr; }
1923 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001924 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1925 bool IsIrreducibleLoopHeaderPhi() const {
1926 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1927 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001928
Vladimir Marko372f10e2016-05-17 16:30:10 +01001929 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1930
1931 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1932 // One virtual method is enough, just const_cast<> and then re-add the const.
1933 return ArrayRef<const HUserRecord<HInstruction*>>(
1934 const_cast<HInstruction*>(this)->GetInputRecords());
1935 }
1936
Vladimir Markoe9004912016-06-16 16:50:52 +01001937 HInputsRef GetInputs() {
1938 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001939 }
1940
Vladimir Markoe9004912016-06-16 16:50:52 +01001941 HConstInputsRef GetInputs() const {
1942 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001943 }
1944
1945 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001946 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001947
Aart Bik2767f4b2016-10-28 15:03:53 -07001948 bool HasInput(HInstruction* input) const {
1949 for (const HInstruction* i : GetInputs()) {
1950 if (i == input) {
1951 return true;
1952 }
1953 }
1954 return false;
1955 }
1956
Vladimir Marko372f10e2016-05-17 16:30:10 +01001957 void SetRawInputAt(size_t index, HInstruction* input) {
1958 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1959 }
1960
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001961 virtual void Accept(HGraphVisitor* visitor) = 0;
1962 virtual const char* DebugName() const = 0;
1963
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1965
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001966 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001967
1968 uint32_t GetDexPc() const { return dex_pc_; }
1969
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001970 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001971
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001972 // Can the instruction throw?
1973 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1974 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001975 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001976 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001977
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001978 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001979 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001980
Calin Juravle10e244f2015-01-26 18:54:32 +00001981 // Does not apply for all instructions, but having this at top level greatly
1982 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001983 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001984 virtual bool CanBeNull() const {
1985 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1986 return true;
1987 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001989 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001990 return false;
1991 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001992
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001993 virtual bool IsActualObject() const {
1994 return GetType() == Primitive::kPrimNot;
1995 }
1996
Calin Juravle2e768302015-07-28 14:41:11 +00001997 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001998
Calin Juravle61d544b2015-02-23 16:46:57 +00001999 ReferenceTypeInfo GetReferenceTypeInfo() const {
2000 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00002001 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00002002 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00002003 }
Calin Juravleacf735c2015-02-12 15:25:22 +00002004
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002006 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002007 // Note: fixup_end remains valid across push_front().
2008 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2009 HUseListNode<HInstruction*>* new_node =
2010 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2011 uses_.push_front(*new_node);
2012 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002013 }
2014
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002016 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002017 // Note: env_fixup_end remains valid across push_front().
2018 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2019 HUseListNode<HEnvironment*>* new_node =
2020 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2021 env_uses_.push_front(*new_node);
2022 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002023 }
2024
David Brazdil1abb4192015-02-17 18:33:36 +00002025 void RemoveAsUserOfInput(size_t input) {
2026 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002027 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2028 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2029 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002030 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002031
Vladimir Marko372f10e2016-05-17 16:30:10 +01002032 void RemoveAsUserOfAllInputs() {
2033 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2034 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2035 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2036 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2037 }
2038 }
2039
David Brazdil1abb4192015-02-17 18:33:36 +00002040 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2041 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042
Vladimir Marko46817b82016-03-29 12:21:58 +01002043 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2044 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2045 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002046 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002047 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002048 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002049
Aart Bikcc42be02016-10-20 16:14:16 -07002050 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002051 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002052 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002053 !CanThrow() &&
2054 !IsSuspendCheck() &&
2055 !IsControlFlow() &&
2056 !IsNativeDebugInfo() &&
2057 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002058 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002059 !IsMemoryBarrier() &&
2060 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002061 }
2062
Aart Bikcc42be02016-10-20 16:14:16 -07002063 bool IsDeadAndRemovable() const {
2064 return IsRemovable() && !HasUses();
2065 }
2066
Roland Levillain6c82d402014-10-13 16:10:27 +01002067 // Does this instruction strictly dominate `other_instruction`?
2068 // Returns false if this instruction and `other_instruction` are the same.
2069 // Aborts if this instruction and `other_instruction` are both phis.
2070 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002071
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002072 int GetId() const { return id_; }
2073 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002074
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002075 int GetSsaIndex() const { return ssa_index_; }
2076 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2077 bool HasSsaIndex() const { return ssa_index_ != -1; }
2078
2079 bool HasEnvironment() const { return environment_ != nullptr; }
2080 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002081 // Set the `environment_` field. Raw because this method does not
2082 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002083 void SetRawEnvironment(HEnvironment* environment) {
2084 DCHECK(environment_ == nullptr);
2085 DCHECK_EQ(environment->GetHolder(), this);
2086 environment_ = environment;
2087 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002088
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002089 void InsertRawEnvironment(HEnvironment* environment) {
2090 DCHECK(environment_ != nullptr);
2091 DCHECK_EQ(environment->GetHolder(), this);
2092 DCHECK(environment->GetParent() == nullptr);
2093 environment->parent_ = environment_;
2094 environment_ = environment;
2095 }
2096
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002097 void RemoveEnvironment();
2098
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002099 // Set the environment of this instruction, copying it from `environment`. While
2100 // copying, the uses lists are being updated.
2101 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002102 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002103 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002104 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002105 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002106 if (environment->GetParent() != nullptr) {
2107 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2108 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002109 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002110
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002111 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2112 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002113 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002114 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002115 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002116 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002117 if (environment->GetParent() != nullptr) {
2118 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2119 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002120 }
2121
Nicolas Geoffray39468442014-09-02 15:17:15 +01002122 // Returns the number of entries in the environment. Typically, that is the
2123 // number of dex registers in a method. It could be more in case of inlining.
2124 size_t EnvironmentSize() const;
2125
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002126 LocationSummary* GetLocations() const { return locations_; }
2127 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002129 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002130 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002131 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002132
Alexandre Rames188d4312015-04-09 18:30:21 +01002133 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2134 // uses of this instruction by `other` are *not* updated.
2135 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2136 ReplaceWith(other);
2137 other->ReplaceInput(this, use_index);
2138 }
2139
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002140 // Move `this` instruction before `cursor`
2141 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002142
Vladimir Markofb337ea2015-11-25 15:25:10 +00002143 // Move `this` before its first user and out of any loops. If there is no
2144 // out-of-loop user that dominates all other users, move the instruction
2145 // to the end of the out-of-loop common dominator of the user's blocks.
2146 //
2147 // This can be used only on non-throwing instructions with no side effects that
2148 // have at least one use but no environment uses.
2149 void MoveBeforeFirstUserAndOutOfLoops();
2150
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002151#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002152 bool Is##type() const; \
2153 const H##type* As##type() const; \
2154 H##type* As##type();
2155
2156 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2157#undef INSTRUCTION_TYPE_CHECK
2158
2159#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002160 bool Is##type() const { return (As##type() != nullptr); } \
2161 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002162 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002163 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002164#undef INSTRUCTION_TYPE_CHECK
2165
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002167 // TODO: this method is used by LICM and GVN with possibly different
2168 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169 virtual bool CanBeMoved() const { return false; }
2170
2171 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002172 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002173 return false;
2174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002175
2176 // Returns whether any data encoded in the two instructions is equal.
2177 // This method does not look at the inputs. Both instructions must be
2178 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002179 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002180 return false;
2181 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182
2183 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002184 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002185 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002186 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002187
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002188 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2189 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2190 // the virtual function because the __attribute__((__pure__)) doesn't really
2191 // apply the strong requirement for virtual functions, preventing optimizations.
2192 InstructionKind GetKind() const PURE;
2193 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002194
2195 virtual size_t ComputeHashCode() const {
2196 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002197 for (const HInstruction* input : GetInputs()) {
2198 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002199 }
2200 return result;
2201 }
2202
2203 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002204 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002205 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002206
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002207 size_t GetLifetimePosition() const { return lifetime_position_; }
2208 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2209 LiveInterval* GetLiveInterval() const { return live_interval_; }
2210 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2211 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2212
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002213 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2214
2215 // Returns whether the code generation of the instruction will require to have access
2216 // to the current method. Such instructions are:
2217 // (1): Instructions that require an environment, as calling the runtime requires
2218 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002219 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2220 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002221 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002222 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002223 }
2224
Vladimir Markodc151b22015-10-15 18:02:30 +01002225 // Returns whether the code generation of the instruction will require to have access
2226 // to the dex cache of the current method's declaring class via the current method.
2227 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002228
Mark Mendellc4701932015-04-10 13:18:51 -04002229 // Does this instruction have any use in an environment before
2230 // control flow hits 'other'?
2231 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2232
2233 // Remove all references to environment uses of this instruction.
2234 // The caller must ensure that this is safe to do.
2235 void RemoveEnvironmentUsers();
2236
Vladimir Markoa1de9182016-02-25 11:37:38 +00002237 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2238 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002239
David Brazdil1abb4192015-02-17 18:33:36 +00002240 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002241 // If set, the machine code for this instruction is assumed to be generated by
2242 // its users. Used by liveness analysis to compute use positions accordingly.
2243 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2244 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2245 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2246 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2247
Vladimir Marko372f10e2016-05-17 16:30:10 +01002248 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2249 return GetInputRecords()[i];
2250 }
2251
2252 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2253 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2254 input_records[index] = input;
2255 }
David Brazdil1abb4192015-02-17 18:33:36 +00002256
Vladimir Markoa1de9182016-02-25 11:37:38 +00002257 uint32_t GetPackedFields() const {
2258 return packed_fields_;
2259 }
2260
2261 template <size_t flag>
2262 bool GetPackedFlag() const {
2263 return (packed_fields_ & (1u << flag)) != 0u;
2264 }
2265
2266 template <size_t flag>
2267 void SetPackedFlag(bool value = true) {
2268 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2269 }
2270
2271 template <typename BitFieldType>
2272 typename BitFieldType::value_type GetPackedField() const {
2273 return BitFieldType::Decode(packed_fields_);
2274 }
2275
2276 template <typename BitFieldType>
2277 void SetPackedField(typename BitFieldType::value_type value) {
2278 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2279 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2280 }
2281
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002282 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002283 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2284 auto before_use_node = uses_.before_begin();
2285 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2286 HInstruction* user = use_node->GetUser();
2287 size_t input_index = use_node->GetIndex();
2288 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2289 before_use_node = use_node;
2290 }
2291 }
2292
2293 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2294 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2295 if (next != uses_.end()) {
2296 HInstruction* next_user = next->GetUser();
2297 size_t next_index = next->GetIndex();
2298 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2299 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2300 }
2301 }
2302
2303 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2304 auto before_env_use_node = env_uses_.before_begin();
2305 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2306 HEnvironment* user = env_use_node->GetUser();
2307 size_t input_index = env_use_node->GetIndex();
2308 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2309 before_env_use_node = env_use_node;
2310 }
2311 }
2312
2313 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2314 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2315 if (next != env_uses_.end()) {
2316 HEnvironment* next_user = next->GetUser();
2317 size_t next_index = next->GetIndex();
2318 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2319 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2320 }
2321 }
David Brazdil1abb4192015-02-17 18:33:36 +00002322
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323 HInstruction* previous_;
2324 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002325 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002326 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002328 // An instruction gets an id when it is added to the graph.
2329 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002330 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002331 int id_;
2332
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002333 // When doing liveness analysis, instructions that have uses get an SSA index.
2334 int ssa_index_;
2335
Vladimir Markoa1de9182016-02-25 11:37:38 +00002336 // Packed fields.
2337 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002338
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002339 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002340 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002341
2342 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002343 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002344
Nicolas Geoffray39468442014-09-02 15:17:15 +01002345 // The environment associated with this instruction. Not null if the instruction
2346 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002347 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002348
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002349 // Set by the code generator.
2350 LocationSummary* locations_;
2351
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002352 // Set by the liveness analysis.
2353 LiveInterval* live_interval_;
2354
2355 // Set by the liveness analysis, this is the position in a linear
2356 // order of blocks where this instruction's live interval start.
2357 size_t lifetime_position_;
2358
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002359 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002360
Vladimir Markoa1de9182016-02-25 11:37:38 +00002361 // The reference handle part of the reference type info.
2362 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002363 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002364 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002365
David Brazdil1abb4192015-02-17 18:33:36 +00002366 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002368 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002369 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002370 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002371
2372 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2373};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002374std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002375
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002376// Iterates over the instructions, while preserving the next instruction
2377// in case the current instruction gets removed from the list by the user
2378// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002379class HInstructionIterator : public ValueObject {
2380 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002381 explicit HInstructionIterator(const HInstructionList& instructions)
2382 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002383 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384 }
2385
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002386 bool Done() const { return instruction_ == nullptr; }
2387 HInstruction* Current() const { return instruction_; }
2388 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002389 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002390 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391 }
2392
2393 private:
2394 HInstruction* instruction_;
2395 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002396
2397 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002398};
2399
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002400// Iterates over the instructions without saving the next instruction,
2401// therefore handling changes in the graph potentially made by the user
2402// of this iterator.
2403class HInstructionIteratorHandleChanges : public ValueObject {
2404 public:
2405 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2406 : instruction_(instructions.first_instruction_) {
2407 }
2408
2409 bool Done() const { return instruction_ == nullptr; }
2410 HInstruction* Current() const { return instruction_; }
2411 void Advance() {
2412 instruction_ = instruction_->GetNext();
2413 }
2414
2415 private:
2416 HInstruction* instruction_;
2417
2418 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2419};
2420
2421
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002422class HBackwardInstructionIterator : public ValueObject {
2423 public:
2424 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2425 : instruction_(instructions.last_instruction_) {
2426 next_ = Done() ? nullptr : instruction_->GetPrevious();
2427 }
2428
2429 bool Done() const { return instruction_ == nullptr; }
2430 HInstruction* Current() const { return instruction_; }
2431 void Advance() {
2432 instruction_ = next_;
2433 next_ = Done() ? nullptr : instruction_->GetPrevious();
2434 }
2435
2436 private:
2437 HInstruction* instruction_;
2438 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002439
2440 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002441};
2442
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002443class HVariableInputSizeInstruction : public HInstruction {
2444 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002445 using HInstruction::GetInputRecords; // Keep the const version visible.
2446 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2447 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2448 }
2449
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002450 void AddInput(HInstruction* input);
2451 void InsertInputAt(size_t index, HInstruction* input);
2452 void RemoveInputAt(size_t index);
2453
Igor Murashkind01745e2017-04-05 16:40:31 -07002454 // Removes all the inputs.
2455 // Also removes this instructions from each input's use list
2456 // (for non-environment uses only).
2457 void RemoveAllInputs();
2458
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002459 protected:
2460 HVariableInputSizeInstruction(SideEffects side_effects,
2461 uint32_t dex_pc,
2462 ArenaAllocator* arena,
2463 size_t number_of_inputs,
2464 ArenaAllocKind kind)
2465 : HInstruction(side_effects, dex_pc),
2466 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2467
2468 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2469
2470 private:
2471 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2472};
2473
Vladimir Markof9f64412015-09-02 14:05:49 +01002474template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475class HTemplateInstruction: public HInstruction {
2476 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002477 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002478 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002479 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480
Vladimir Marko372f10e2016-05-17 16:30:10 +01002481 using HInstruction::GetInputRecords; // Keep the const version visible.
2482 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2483 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002484 }
2485
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002486 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002487 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002488
2489 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490};
2491
Vladimir Markof9f64412015-09-02 14:05:49 +01002492// HTemplateInstruction specialization for N=0.
2493template<>
2494class HTemplateInstruction<0>: public HInstruction {
2495 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002496 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002497 : HInstruction(side_effects, dex_pc) {}
2498
Vladimir Markof9f64412015-09-02 14:05:49 +01002499 virtual ~HTemplateInstruction() {}
2500
Vladimir Marko372f10e2016-05-17 16:30:10 +01002501 using HInstruction::GetInputRecords; // Keep the const version visible.
2502 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2503 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002504 }
2505
2506 private:
2507 friend class SsaBuilder;
2508};
2509
Dave Allison20dfc792014-06-16 20:44:29 -07002510template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002511class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002512 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002513 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002514 : HTemplateInstruction<N>(side_effects, dex_pc) {
2515 this->template SetPackedField<TypeField>(type);
2516 }
Dave Allison20dfc792014-06-16 20:44:29 -07002517 virtual ~HExpression() {}
2518
Vladimir Markoa1de9182016-02-25 11:37:38 +00002519 Primitive::Type GetType() const OVERRIDE {
2520 return TypeField::Decode(this->GetPackedFields());
2521 }
Dave Allison20dfc792014-06-16 20:44:29 -07002522
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002523 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002524 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2525 static constexpr size_t kFieldTypeSize =
2526 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2527 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2528 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2529 "Too many packed fields.");
2530 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002531};
2532
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002533// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2534// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002535class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002537 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2538 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002539
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002540 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002541
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002542 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002543
2544 private:
2545 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2546};
2547
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002548// Represents dex's RETURN opcodes. A HReturn is a control flow
2549// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002550class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002551 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002552 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2553 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554 SetRawInputAt(0, value);
2555 }
2556
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002557 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002558
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002559 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002560
2561 private:
2562 DISALLOW_COPY_AND_ASSIGN(HReturn);
2563};
2564
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002565class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002566 public:
2567 HPhi(ArenaAllocator* arena,
2568 uint32_t reg_number,
2569 size_t number_of_inputs,
2570 Primitive::Type type,
2571 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002572 : HVariableInputSizeInstruction(
2573 SideEffects::None(),
2574 dex_pc,
2575 arena,
2576 number_of_inputs,
2577 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002578 reg_number_(reg_number) {
2579 SetPackedField<TypeField>(ToPhiType(type));
2580 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2581 // Phis are constructed live and marked dead if conflicting or unused.
2582 // Individual steps of SsaBuilder should assume that if a phi has been
2583 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2584 SetPackedFlag<kFlagIsLive>(true);
2585 SetPackedFlag<kFlagCanBeNull>(true);
2586 }
2587
2588 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2589 static Primitive::Type ToPhiType(Primitive::Type type) {
2590 return Primitive::PrimitiveKind(type);
2591 }
2592
2593 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2594
David Brazdildee58d62016-04-07 09:54:26 +00002595 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2596 void SetType(Primitive::Type new_type) {
2597 // Make sure that only valid type changes occur. The following are allowed:
2598 // (1) int -> float/ref (primitive type propagation),
2599 // (2) long -> double (primitive type propagation).
2600 DCHECK(GetType() == new_type ||
2601 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2602 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2603 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2604 SetPackedField<TypeField>(new_type);
2605 }
2606
2607 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2608 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2609
2610 uint32_t GetRegNumber() const { return reg_number_; }
2611
2612 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2613 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2614 bool IsDead() const { return !IsLive(); }
2615 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2616
Vladimir Markoe9004912016-06-16 16:50:52 +01002617 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002618 return other != nullptr
2619 && other->IsPhi()
2620 && other->AsPhi()->GetBlock() == GetBlock()
2621 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2622 }
2623
2624 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2625 // An equivalent phi is a phi having the same dex register and type.
2626 // It assumes that phis with the same dex register are adjacent.
2627 HPhi* GetNextEquivalentPhiWithSameType() {
2628 HInstruction* next = GetNext();
2629 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2630 if (next->GetType() == GetType()) {
2631 return next->AsPhi();
2632 }
2633 next = next->GetNext();
2634 }
2635 return nullptr;
2636 }
2637
2638 DECLARE_INSTRUCTION(Phi);
2639
David Brazdildee58d62016-04-07 09:54:26 +00002640 private:
2641 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2642 static constexpr size_t kFieldTypeSize =
2643 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2644 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2645 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2646 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2647 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2648 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2649
David Brazdildee58d62016-04-07 09:54:26 +00002650 const uint32_t reg_number_;
2651
2652 DISALLOW_COPY_AND_ASSIGN(HPhi);
2653};
2654
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002655// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002656// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002658class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002661
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002662 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002663
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002664 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002665
2666 private:
2667 DISALLOW_COPY_AND_ASSIGN(HExit);
2668};
2669
2670// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002671class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002673 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002674
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002675 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002676
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002677 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002678 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002679 }
2680
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002681 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002682
2683 private:
2684 DISALLOW_COPY_AND_ASSIGN(HGoto);
2685};
2686
Roland Levillain9867bc72015-08-05 10:21:34 +01002687class HConstant : public HExpression<0> {
2688 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002689 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2690 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002691
2692 bool CanBeMoved() const OVERRIDE { return true; }
2693
Roland Levillain1a653882016-03-18 18:05:57 +00002694 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002696 // Is this constant 0 in the arithmetic sense?
2697 virtual bool IsArithmeticZero() const { return false; }
2698 // Is this constant a 0-bit pattern?
2699 virtual bool IsZeroBitPattern() const { return false; }
2700 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002701 virtual bool IsOne() const { return false; }
2702
David Brazdil77a48ae2015-09-15 12:34:04 +00002703 virtual uint64_t GetValueAsUint64() const = 0;
2704
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002705 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002706
2707 private:
2708 DISALLOW_COPY_AND_ASSIGN(HConstant);
2709};
2710
Vladimir Markofcb503c2016-05-18 12:48:17 +01002711class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002713 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002714 return true;
2715 }
2716
David Brazdil77a48ae2015-09-15 12:34:04 +00002717 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2718
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 size_t ComputeHashCode() const OVERRIDE { return 0; }
2720
Roland Levillain1a653882016-03-18 18:05:57 +00002721 // The null constant representation is a 0-bit pattern.
2722 virtual bool IsZeroBitPattern() const { return true; }
2723
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 DECLARE_INSTRUCTION(NullConstant);
2725
2726 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002727 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002728
2729 friend class HGraph;
2730 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2731};
2732
2733// Constants of the type int. Those can be from Dex instructions, or
2734// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002735class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002736 public:
2737 int32_t GetValue() const { return value_; }
2738
David Brazdil9f389d42015-10-01 14:32:56 +01002739 uint64_t GetValueAsUint64() const OVERRIDE {
2740 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2741 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002742
Vladimir Marko372f10e2016-05-17 16:30:10 +01002743 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002744 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002745 return other->AsIntConstant()->value_ == value_;
2746 }
2747
2748 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2749
2750 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002751 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2752 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2754
Roland Levillain1a653882016-03-18 18:05:57 +00002755 // Integer constants are used to encode Boolean values as well,
2756 // where 1 means true and 0 means false.
2757 bool IsTrue() const { return GetValue() == 1; }
2758 bool IsFalse() const { return GetValue() == 0; }
2759
Roland Levillain9867bc72015-08-05 10:21:34 +01002760 DECLARE_INSTRUCTION(IntConstant);
2761
2762 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002763 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2764 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2765 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2766 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002767
2768 const int32_t value_;
2769
2770 friend class HGraph;
2771 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2772 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2773 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2774};
2775
Vladimir Markofcb503c2016-05-18 12:48:17 +01002776class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002777 public:
2778 int64_t GetValue() const { return value_; }
2779
David Brazdil77a48ae2015-09-15 12:34:04 +00002780 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2781
Vladimir Marko372f10e2016-05-17 16:30:10 +01002782 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002783 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002784 return other->AsLongConstant()->value_ == value_;
2785 }
2786
2787 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2788
2789 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002790 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2791 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002792 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2793
2794 DECLARE_INSTRUCTION(LongConstant);
2795
2796 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002797 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2798 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002799
2800 const int64_t value_;
2801
2802 friend class HGraph;
2803 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2804};
Dave Allison20dfc792014-06-16 20:44:29 -07002805
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002807 public:
2808 float GetValue() const { return value_; }
2809
2810 uint64_t GetValueAsUint64() const OVERRIDE {
2811 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2812 }
2813
Vladimir Marko372f10e2016-05-17 16:30:10 +01002814 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002815 DCHECK(other->IsFloatConstant()) << other->DebugName();
2816 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2817 }
2818
2819 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2820
2821 bool IsMinusOne() const OVERRIDE {
2822 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2823 }
Roland Levillain1a653882016-03-18 18:05:57 +00002824 bool IsArithmeticZero() const OVERRIDE {
2825 return std::fpclassify(value_) == FP_ZERO;
2826 }
2827 bool IsArithmeticPositiveZero() const {
2828 return IsArithmeticZero() && !std::signbit(value_);
2829 }
2830 bool IsArithmeticNegativeZero() const {
2831 return IsArithmeticZero() && std::signbit(value_);
2832 }
2833 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002834 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002835 }
2836 bool IsOne() const OVERRIDE {
2837 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2838 }
2839 bool IsNaN() const {
2840 return std::isnan(value_);
2841 }
2842
2843 DECLARE_INSTRUCTION(FloatConstant);
2844
2845 private:
2846 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2847 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2848 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2849 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2850
2851 const float value_;
2852
2853 // Only the SsaBuilder and HGraph can create floating-point constants.
2854 friend class SsaBuilder;
2855 friend class HGraph;
2856 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2857};
2858
Vladimir Markofcb503c2016-05-18 12:48:17 +01002859class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002860 public:
2861 double GetValue() const { return value_; }
2862
2863 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2864
Vladimir Marko372f10e2016-05-17 16:30:10 +01002865 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002866 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2867 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2868 }
2869
2870 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2871
2872 bool IsMinusOne() const OVERRIDE {
2873 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2874 }
Roland Levillain1a653882016-03-18 18:05:57 +00002875 bool IsArithmeticZero() const OVERRIDE {
2876 return std::fpclassify(value_) == FP_ZERO;
2877 }
2878 bool IsArithmeticPositiveZero() const {
2879 return IsArithmeticZero() && !std::signbit(value_);
2880 }
2881 bool IsArithmeticNegativeZero() const {
2882 return IsArithmeticZero() && std::signbit(value_);
2883 }
2884 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002885 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002886 }
2887 bool IsOne() const OVERRIDE {
2888 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2889 }
2890 bool IsNaN() const {
2891 return std::isnan(value_);
2892 }
2893
2894 DECLARE_INSTRUCTION(DoubleConstant);
2895
2896 private:
2897 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2898 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2899 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2900 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2901
2902 const double value_;
2903
2904 // Only the SsaBuilder and HGraph can create floating-point constants.
2905 friend class SsaBuilder;
2906 friend class HGraph;
2907 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2908};
2909
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002910// Conditional branch. A block ending with an HIf instruction must have
2911// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002912class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002913 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002914 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2915 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002916 SetRawInputAt(0, input);
2917 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002918
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002919 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002920
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002921 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002922 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002923 }
2924
2925 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002926 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002927 }
2928
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002929 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002930
2931 private:
2932 DISALLOW_COPY_AND_ASSIGN(HIf);
2933};
2934
David Brazdilfc6a86a2015-06-26 10:33:45 +00002935
2936// Abstract instruction which marks the beginning and/or end of a try block and
2937// links it to the respective exception handlers. Behaves the same as a Goto in
2938// non-exceptional control flow.
2939// Normal-flow successor is stored at index zero, exception handlers under
2940// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002941class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002942 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002943 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002944 kEntry,
2945 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002946 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002947 };
2948
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002949 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002950 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2951 SetPackedField<BoundaryKindField>(kind);
2952 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002953
2954 bool IsControlFlow() const OVERRIDE { return true; }
2955
2956 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002957 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002958
David Brazdild26a4112015-11-10 11:07:31 +00002959 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2960 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2961 }
2962
David Brazdilfc6a86a2015-06-26 10:33:45 +00002963 // Returns whether `handler` is among its exception handlers (non-zero index
2964 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002965 bool HasExceptionHandler(const HBasicBlock& handler) const {
2966 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002967 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002968 }
2969
2970 // If not present already, adds `handler` to its block's list of exception
2971 // handlers.
2972 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002973 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002974 GetBlock()->AddSuccessor(handler);
2975 }
2976 }
2977
Vladimir Markoa1de9182016-02-25 11:37:38 +00002978 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2979 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002980
David Brazdilffee3d32015-07-06 11:48:53 +01002981 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2982
David Brazdilfc6a86a2015-06-26 10:33:45 +00002983 DECLARE_INSTRUCTION(TryBoundary);
2984
2985 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002986 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2987 static constexpr size_t kFieldBoundaryKindSize =
2988 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2989 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2990 kFieldBoundaryKind + kFieldBoundaryKindSize;
2991 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2992 "Too many packed fields.");
2993 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002994
2995 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2996};
2997
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002998// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002999class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003000 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003001 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3002 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003003 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003004 : HVariableInputSizeInstruction(
3005 SideEffects::All(),
3006 dex_pc,
3007 arena,
3008 /* number_of_inputs */ 1,
3009 kArenaAllocMisc) {
3010 SetPackedFlag<kFieldCanBeMoved>(false);
3011 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003012 SetRawInputAt(0, cond);
3013 }
3014
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003015 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3016 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3017 // instead of `guard`.
3018 // We set CanTriggerGC to prevent any intermediate address to be live
3019 // at the point of the `HDeoptimize`.
3020 HDeoptimize(ArenaAllocator* arena,
3021 HInstruction* cond,
3022 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003023 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003024 uint32_t dex_pc)
3025 : HVariableInputSizeInstruction(
3026 SideEffects::CanTriggerGC(),
3027 dex_pc,
3028 arena,
3029 /* number_of_inputs */ 2,
3030 kArenaAllocMisc) {
3031 SetPackedFlag<kFieldCanBeMoved>(true);
3032 SetPackedField<DeoptimizeKindField>(kind);
3033 SetRawInputAt(0, cond);
3034 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003035 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003036
3037 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3038
3039 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3040 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3041 }
3042
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003043 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003044
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003045 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003046
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003047 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003048
3049 Primitive::Type GetType() const OVERRIDE {
3050 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3051 }
3052
3053 bool GuardsAnInput() const {
3054 return InputCount() == 2;
3055 }
3056
3057 HInstruction* GuardedInput() const {
3058 DCHECK(GuardsAnInput());
3059 return InputAt(1);
3060 }
3061
3062 void RemoveGuard() {
3063 RemoveInputAt(1);
3064 }
3065
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003066 DECLARE_INSTRUCTION(Deoptimize);
3067
3068 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003069 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3070 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3071 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003072 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003073 static constexpr size_t kNumberOfDeoptimizePackedBits =
3074 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3075 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3076 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003077 using DeoptimizeKindField =
3078 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003079
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003080 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3081};
3082
Mingyao Yang063fc772016-08-02 11:02:54 -07003083// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3084// The compiled code checks this flag value in a guard before devirtualized call and
3085// if it's true, starts to do deoptimization.
3086// It has a 4-byte slot on stack.
3087// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003088class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003089 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003090 // CHA guards are only optimized in a separate pass and it has no side effects
3091 // with regard to other passes.
3092 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3093 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003094 }
3095
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003096 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3097
3098 // We do all CHA guard elimination/motion in a single pass, after which there is no
3099 // further guard elimination/motion since a guard might have been used for justification
3100 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3101 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003102 bool CanBeMoved() const OVERRIDE { return false; }
3103
3104 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3108};
3109
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003110// Represents the ArtMethod that was passed as a first argument to
3111// the method. It is used by instructions that depend on it, like
3112// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003113class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3116 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003117
3118 DECLARE_INSTRUCTION(CurrentMethod);
3119
3120 private:
3121 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3122};
3123
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003124// Fetches an ArtMethod from the virtual table or the interface method table
3125// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003126class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003129 kVTable,
3130 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003131 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 };
3133 HClassTableGet(HInstruction* cls,
3134 Primitive::Type type,
3135 TableKind kind,
3136 size_t index,
3137 uint32_t dex_pc)
3138 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003139 index_(index) {
3140 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003141 SetRawInputAt(0, cls);
3142 }
3143
3144 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003145 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003146 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003147 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003148 }
3149
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151 size_t GetIndex() const { return index_; }
3152
3153 DECLARE_INSTRUCTION(ClassTableGet);
3154
3155 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003156 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3157 static constexpr size_t kFieldTableKindSize =
3158 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3159 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3160 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3161 "Too many packed fields.");
3162 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3163
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003164 // The index of the ArtMethod in the table.
3165 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003166
3167 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3168};
3169
Mark Mendellfe57faa2015-09-18 09:26:15 -04003170// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3171// have one successor for each entry in the switch table, and the final successor
3172// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003173class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003174 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003175 HPackedSwitch(int32_t start_value,
3176 uint32_t num_entries,
3177 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003178 uint32_t dex_pc = kNoDexPc)
3179 : HTemplateInstruction(SideEffects::None(), dex_pc),
3180 start_value_(start_value),
3181 num_entries_(num_entries) {
3182 SetRawInputAt(0, input);
3183 }
3184
3185 bool IsControlFlow() const OVERRIDE { return true; }
3186
3187 int32_t GetStartValue() const { return start_value_; }
3188
Vladimir Marko211c2112015-09-24 16:52:33 +01003189 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003190
3191 HBasicBlock* GetDefaultBlock() const {
3192 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003193 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003194 }
3195 DECLARE_INSTRUCTION(PackedSwitch);
3196
3197 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003198 const int32_t start_value_;
3199 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003200
3201 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3202};
3203
Roland Levillain88cb1752014-10-20 16:36:47 +01003204class HUnaryOperation : public HExpression<1> {
3205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003206 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3207 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003208 SetRawInputAt(0, input);
3209 }
3210
3211 HInstruction* GetInput() const { return InputAt(0); }
3212 Primitive::Type GetResultType() const { return GetType(); }
3213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003215 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003216 return true;
3217 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003218
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 // Try to statically evaluate `this` and return a HConstant
3220 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003221 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003222 HConstant* TryStaticEvaluation() const;
3223
3224 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003225 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3226 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003227 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3228 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003229
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003230 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003231
3232 private:
3233 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3234};
3235
Dave Allison20dfc792014-06-16 20:44:29 -07003236class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003237 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003238 HBinaryOperation(Primitive::Type result_type,
3239 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003240 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003241 SideEffects side_effects = SideEffects::None(),
3242 uint32_t dex_pc = kNoDexPc)
3243 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003244 SetRawInputAt(0, left);
3245 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003246 }
3247
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003248 HInstruction* GetLeft() const { return InputAt(0); }
3249 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003250 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003251
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003252 virtual bool IsCommutative() const { return false; }
3253
3254 // Put constant on the right.
3255 // Returns whether order is changed.
3256 bool OrderInputsWithConstantOnTheRight() {
3257 HInstruction* left = InputAt(0);
3258 HInstruction* right = InputAt(1);
3259 if (left->IsConstant() && !right->IsConstant()) {
3260 ReplaceInput(right, 0);
3261 ReplaceInput(left, 1);
3262 return true;
3263 }
3264 return false;
3265 }
3266
3267 // Order inputs by instruction id, but favor constant on the right side.
3268 // This helps GVN for commutative ops.
3269 void OrderInputs() {
3270 DCHECK(IsCommutative());
3271 HInstruction* left = InputAt(0);
3272 HInstruction* right = InputAt(1);
3273 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3274 return;
3275 }
3276 if (OrderInputsWithConstantOnTheRight()) {
3277 return;
3278 }
3279 // Order according to instruction id.
3280 if (left->GetId() > right->GetId()) {
3281 ReplaceInput(right, 0);
3282 ReplaceInput(left, 1);
3283 }
3284 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003286 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003287 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003288 return true;
3289 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003290
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 // Try to statically evaluate `this` and return a HConstant
3292 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003293 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003294 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003295
3296 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3298 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003299 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3300 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003302 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3303 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003304 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3305 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003306 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3307 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3310 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003311
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003312 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003313 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003314 HConstant* GetConstantRight() const;
3315
3316 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003317 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003318 HInstruction* GetLeastConstantLeft() const;
3319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003320 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003321
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003322 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003323 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3324};
3325
Mark Mendellc4701932015-04-10 13:18:51 -04003326// The comparison bias applies for floating point operations and indicates how NaN
3327// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003328enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003329 kNoBias, // bias is not applicable (i.e. for long operation)
3330 kGtBias, // return 1 for NaN comparisons
3331 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003333};
3334
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003339 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3341 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3342 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003343
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003344 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003345 // `instruction`, and disregard moves in between.
3346 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003348 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003349
3350 virtual IfCondition GetCondition() const = 0;
3351
Mark Mendellc4701932015-04-10 13:18:51 -04003352 virtual IfCondition GetOppositeCondition() const = 0;
3353
Vladimir Markoa1de9182016-02-25 11:37:38 +00003354 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003355 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3356
Vladimir Markoa1de9182016-02-25 11:37:38 +00003357 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3358 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003359
Vladimir Marko372f10e2016-05-17 16:30:10 +01003360 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003362 }
3363
Roland Levillain4fa13f62015-07-06 18:11:54 +01003364 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003365 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003366 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003367 if (if_cond == kCondNE) {
3368 return true;
3369 } else if (if_cond == kCondEQ) {
3370 return false;
3371 }
3372 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003373 }
3374
3375 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003376 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003377 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003378 if (if_cond == kCondEQ) {
3379 return true;
3380 } else if (if_cond == kCondNE) {
3381 return false;
3382 }
3383 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003384 }
3385
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003387 // Needed if we merge a HCompare into a HCondition.
3388 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3389 static constexpr size_t kFieldComparisonBiasSize =
3390 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3391 static constexpr size_t kNumberOfConditionPackedBits =
3392 kFieldComparisonBias + kFieldComparisonBiasSize;
3393 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3394 using ComparisonBiasField =
3395 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3396
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 template <typename T>
3398 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3399
3400 template <typename T>
3401 int32_t CompareFP(T x, T y) const {
3402 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3403 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3404 // Handle the bias.
3405 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3406 }
3407
3408 // Return an integer constant containing the result of a condition evaluated at compile time.
3409 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3410 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3411 }
3412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 private:
3414 DISALLOW_COPY_AND_ASSIGN(HCondition);
3415};
3416
3417// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003418class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3421 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003422
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003423 bool IsCommutative() const OVERRIDE { return true; }
3424
Vladimir Marko9e23df52015-11-10 17:14:35 +00003425 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3426 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(true, GetDexPc());
3428 }
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3431 }
3432 // In the following Evaluate methods, a HCompare instruction has
3433 // been merged into this HEqual instruction; evaluate it as
3434 // `Compare(x, y) == 0`.
3435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3436 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3437 GetDexPc());
3438 }
3439 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3440 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3441 }
3442 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3443 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003444 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003446 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003447
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003448 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003449 return kCondEQ;
3450 }
3451
Mark Mendellc4701932015-04-10 13:18:51 -04003452 IfCondition GetOppositeCondition() const OVERRIDE {
3453 return kCondNE;
3454 }
3455
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003456 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003457 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459 DISALLOW_COPY_AND_ASSIGN(HEqual);
3460};
3461
Vladimir Markofcb503c2016-05-18 12:48:17 +01003462class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003463 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003464 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3465 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003466
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003467 bool IsCommutative() const OVERRIDE { return true; }
3468
Vladimir Marko9e23df52015-11-10 17:14:35 +00003469 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3470 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantCondition(false, GetDexPc());
3472 }
3473 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3474 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3475 }
3476 // In the following Evaluate methods, a HCompare instruction has
3477 // been merged into this HNotEqual instruction; evaluate it as
3478 // `Compare(x, y) != 0`.
3479 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3480 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3481 }
3482 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3483 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3484 }
3485 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3486 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003488
Dave Allison20dfc792014-06-16 20:44:29 -07003489 DECLARE_INSTRUCTION(NotEqual);
3490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003491 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003492 return kCondNE;
3493 }
3494
Mark Mendellc4701932015-04-10 13:18:51 -04003495 IfCondition GetOppositeCondition() const OVERRIDE {
3496 return kCondEQ;
3497 }
3498
Dave Allison20dfc792014-06-16 20:44:29 -07003499 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003500 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003501
Dave Allison20dfc792014-06-16 20:44:29 -07003502 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3503};
3504
Vladimir Markofcb503c2016-05-18 12:48:17 +01003505class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003506 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3508 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003509
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003512 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 // In the following Evaluate methods, a HCompare instruction has
3514 // been merged into this HLessThan instruction; evaluate it as
3515 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3518 }
3519 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3520 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3521 }
3522 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3523 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003524 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003525
Dave Allison20dfc792014-06-16 20:44:29 -07003526 DECLARE_INSTRUCTION(LessThan);
3527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003528 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003529 return kCondLT;
3530 }
3531
Mark Mendellc4701932015-04-10 13:18:51 -04003532 IfCondition GetOppositeCondition() const OVERRIDE {
3533 return kCondGE;
3534 }
3535
Dave Allison20dfc792014-06-16 20:44:29 -07003536 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003537 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003538
Dave Allison20dfc792014-06-16 20:44:29 -07003539 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3540};
3541
Vladimir Markofcb503c2016-05-18 12:48:17 +01003542class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3545 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003546
Roland Levillain9867bc72015-08-05 10:21:34 +01003547 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003549 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 // In the following Evaluate methods, a HCompare instruction has
3551 // been merged into this HLessThanOrEqual instruction; evaluate it as
3552 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3555 }
3556 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3557 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3558 }
3559 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3560 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003561 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003562
Dave Allison20dfc792014-06-16 20:44:29 -07003563 DECLARE_INSTRUCTION(LessThanOrEqual);
3564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003565 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003566 return kCondLE;
3567 }
3568
Mark Mendellc4701932015-04-10 13:18:51 -04003569 IfCondition GetOppositeCondition() const OVERRIDE {
3570 return kCondGT;
3571 }
3572
Dave Allison20dfc792014-06-16 20:44:29 -07003573 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003574 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003575
Dave Allison20dfc792014-06-16 20:44:29 -07003576 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3577};
3578
Vladimir Markofcb503c2016-05-18 12:48:17 +01003579class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003580 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003581 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3582 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003583
Roland Levillain9867bc72015-08-05 10:21:34 +01003584 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003586 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 // In the following Evaluate methods, a HCompare instruction has
3588 // been merged into this HGreaterThan instruction; evaluate it as
3589 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3592 }
3593 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3594 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3595 }
3596 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3597 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003598 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003599
Dave Allison20dfc792014-06-16 20:44:29 -07003600 DECLARE_INSTRUCTION(GreaterThan);
3601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003602 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003603 return kCondGT;
3604 }
3605
Mark Mendellc4701932015-04-10 13:18:51 -04003606 IfCondition GetOppositeCondition() const OVERRIDE {
3607 return kCondLE;
3608 }
3609
Dave Allison20dfc792014-06-16 20:44:29 -07003610 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003611 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003612
Dave Allison20dfc792014-06-16 20:44:29 -07003613 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3614};
3615
Vladimir Markofcb503c2016-05-18 12:48:17 +01003616class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003617 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003618 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3619 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003620
Roland Levillain9867bc72015-08-05 10:21:34 +01003621 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003622 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003623 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 // In the following Evaluate methods, a HCompare instruction has
3625 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3626 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3629 }
3630 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3631 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3632 }
3633 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3634 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003635 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003636
Dave Allison20dfc792014-06-16 20:44:29 -07003637 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003639 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003640 return kCondGE;
3641 }
3642
Mark Mendellc4701932015-04-10 13:18:51 -04003643 IfCondition GetOppositeCondition() const OVERRIDE {
3644 return kCondLT;
3645 }
3646
Dave Allison20dfc792014-06-16 20:44:29 -07003647 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003648 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003649
Dave Allison20dfc792014-06-16 20:44:29 -07003650 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3651};
3652
Vladimir Markofcb503c2016-05-18 12:48:17 +01003653class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003654 public:
3655 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3656 : HCondition(first, second, dex_pc) {}
3657
3658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003660 }
3661 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003662 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3663 }
3664 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3665 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3666 LOG(FATAL) << DebugName() << " is not defined for float values";
3667 UNREACHABLE();
3668 }
3669 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3670 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3671 LOG(FATAL) << DebugName() << " is not defined for double values";
3672 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003673 }
3674
3675 DECLARE_INSTRUCTION(Below);
3676
3677 IfCondition GetCondition() const OVERRIDE {
3678 return kCondB;
3679 }
3680
3681 IfCondition GetOppositeCondition() const OVERRIDE {
3682 return kCondAE;
3683 }
3684
3685 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003686 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003687 return MakeUnsigned(x) < MakeUnsigned(y);
3688 }
Aart Bike9f37602015-10-09 11:15:55 -07003689
3690 DISALLOW_COPY_AND_ASSIGN(HBelow);
3691};
3692
Vladimir Markofcb503c2016-05-18 12:48:17 +01003693class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003694 public:
3695 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3696 : HCondition(first, second, dex_pc) {}
3697
3698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003699 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003700 }
3701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3703 }
3704 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3705 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3706 LOG(FATAL) << DebugName() << " is not defined for float values";
3707 UNREACHABLE();
3708 }
3709 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3710 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3711 LOG(FATAL) << DebugName() << " is not defined for double values";
3712 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003713 }
3714
3715 DECLARE_INSTRUCTION(BelowOrEqual);
3716
3717 IfCondition GetCondition() const OVERRIDE {
3718 return kCondBE;
3719 }
3720
3721 IfCondition GetOppositeCondition() const OVERRIDE {
3722 return kCondA;
3723 }
3724
3725 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003726 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeUnsigned(x) <= MakeUnsigned(y);
3728 }
Aart Bike9f37602015-10-09 11:15:55 -07003729
3730 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3731};
3732
Vladimir Markofcb503c2016-05-18 12:48:17 +01003733class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003734 public:
3735 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3736 : HCondition(first, second, dex_pc) {}
3737
3738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003740 }
3741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3743 }
3744 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3745 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3746 LOG(FATAL) << DebugName() << " is not defined for float values";
3747 UNREACHABLE();
3748 }
3749 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3750 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3751 LOG(FATAL) << DebugName() << " is not defined for double values";
3752 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003753 }
3754
3755 DECLARE_INSTRUCTION(Above);
3756
3757 IfCondition GetCondition() const OVERRIDE {
3758 return kCondA;
3759 }
3760
3761 IfCondition GetOppositeCondition() const OVERRIDE {
3762 return kCondBE;
3763 }
3764
3765 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003766 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 return MakeUnsigned(x) > MakeUnsigned(y);
3768 }
Aart Bike9f37602015-10-09 11:15:55 -07003769
3770 DISALLOW_COPY_AND_ASSIGN(HAbove);
3771};
3772
Vladimir Markofcb503c2016-05-18 12:48:17 +01003773class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003774 public:
3775 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3776 : HCondition(first, second, dex_pc) {}
3777
3778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003780 }
3781 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3783 }
3784 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3785 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3786 LOG(FATAL) << DebugName() << " is not defined for float values";
3787 UNREACHABLE();
3788 }
3789 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3790 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3791 LOG(FATAL) << DebugName() << " is not defined for double values";
3792 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003793 }
3794
3795 DECLARE_INSTRUCTION(AboveOrEqual);
3796
3797 IfCondition GetCondition() const OVERRIDE {
3798 return kCondAE;
3799 }
3800
3801 IfCondition GetOppositeCondition() const OVERRIDE {
3802 return kCondB;
3803 }
3804
3805 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003806 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003807 return MakeUnsigned(x) >= MakeUnsigned(y);
3808 }
Aart Bike9f37602015-10-09 11:15:55 -07003809
3810 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3811};
Dave Allison20dfc792014-06-16 20:44:29 -07003812
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003813// Instruction to check how two inputs compare to each other.
3814// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003815class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003816 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003817 // Note that `comparison_type` is the type of comparison performed
3818 // between the comparison's inputs, not the type of the instantiated
3819 // HCompare instruction (which is always Primitive::kPrimInt).
3820 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003821 HInstruction* first,
3822 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003823 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003824 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003825 : HBinaryOperation(Primitive::kPrimInt,
3826 first,
3827 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003828 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003829 dex_pc) {
3830 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003831 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3832 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003833 }
3834
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3837
3838 template <typename T>
3839 int32_t ComputeFP(T x, T y) const {
3840 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3841 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3842 // Handle the bias.
3843 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3844 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003845
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003847 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3848 // reach this code path when processing a freshly built HIR
3849 // graph. However HCompare integer instructions can be synthesized
3850 // by the instruction simplifier to implement IntegerCompare and
3851 // IntegerSignum intrinsics, so we have to handle this case.
3852 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 }
3854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3856 }
3857 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3858 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3859 }
3860 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3861 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003862 }
3863
Vladimir Marko372f10e2016-05-17 16:30:10 +01003864 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003866 }
3867
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003869
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003871 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 bool IsGtBias() const {
3873 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003874 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003876
Roland Levillain1693a1f2016-03-15 14:57:31 +00003877 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3878 // Comparisons do not require a runtime call in any back end.
3879 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003881
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003882 DECLARE_INSTRUCTION(Compare);
3883
Roland Levillain31dd3d62016-02-16 12:21:02 +00003884 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3886 static constexpr size_t kFieldComparisonBiasSize =
3887 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3888 static constexpr size_t kNumberOfComparePackedBits =
3889 kFieldComparisonBias + kFieldComparisonBiasSize;
3890 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3891 using ComparisonBiasField =
3892 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3893
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 // Return an integer constant containing the result of a comparison evaluated at compile time.
3895 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3896 DCHECK(value == -1 || value == 0 || value == 1) << value;
3897 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3898 }
3899
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003900 private:
3901 DISALLOW_COPY_AND_ASSIGN(HCompare);
3902};
3903
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003904class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003905 public:
3906 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003907 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003908 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003909 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003910 bool finalizable,
3911 QuickEntrypointEnum entrypoint)
3912 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3913 type_index_(type_index),
3914 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003915 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003917 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003918 }
3919
Andreas Gampea5b09a62016-11-17 15:21:22 -08003920 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003921 const DexFile& GetDexFile() const { return dex_file_; }
3922
3923 // Calls runtime so needs an environment.
3924 bool NeedsEnvironment() const OVERRIDE { return true; }
3925
Mingyao Yang062157f2016-03-02 10:15:36 -08003926 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3927 bool CanThrow() const OVERRIDE { return true; }
3928
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003929 bool NeedsChecks() const {
3930 return entrypoint_ == kQuickAllocObjectWithChecks;
3931 }
David Brazdil6de19382016-01-08 17:37:10 +00003932
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003934
3935 bool CanBeNull() const OVERRIDE { return false; }
3936
3937 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3938
3939 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3940 entrypoint_ = entrypoint;
3941 }
3942
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003943 HLoadClass* GetLoadClass() const {
3944 HInstruction* input = InputAt(0);
3945 if (input->IsClinitCheck()) {
3946 input = input->InputAt(0);
3947 }
3948 DCHECK(input->IsLoadClass());
3949 return input->AsLoadClass();
3950 }
3951
David Brazdil6de19382016-01-08 17:37:10 +00003952 bool IsStringAlloc() const;
3953
3954 DECLARE_INSTRUCTION(NewInstance);
3955
3956 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003957 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003958 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3959 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3960 "Too many packed fields.");
3961
Andreas Gampea5b09a62016-11-17 15:21:22 -08003962 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003963 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003964 QuickEntrypointEnum entrypoint_;
3965
3966 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3967};
3968
Agi Csaki05f20562015-08-19 14:58:14 -07003969enum IntrinsicNeedsEnvironmentOrCache {
3970 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3971 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003972};
3973
Aart Bik5d75afe2015-12-14 11:57:01 -08003974enum IntrinsicSideEffects {
3975 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3976 kReadSideEffects, // Intrinsic may read heap memory.
3977 kWriteSideEffects, // Intrinsic may write heap memory.
3978 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3979};
3980
3981enum IntrinsicExceptions {
3982 kNoThrow, // Intrinsic does not throw any exceptions.
3983 kCanThrow // Intrinsic may throw exceptions.
3984};
3985
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003986class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003987 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003988 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003989
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003990 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003991 SetRawInputAt(index, argument);
3992 }
3993
Roland Levillain3e3d7332015-04-28 11:00:54 +01003994 // Return the number of arguments. This number can be lower than
3995 // the number of inputs returned by InputCount(), as some invoke
3996 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3997 // inputs at the end of their list of inputs.
3998 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3999
Vladimir Markoa1de9182016-02-25 11:37:38 +00004000 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004001
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004002 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4003
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004004 InvokeType GetInvokeType() const {
4005 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004006 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004007
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004008 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004009 return intrinsic_;
4010 }
4011
Aart Bik5d75afe2015-12-14 11:57:01 -08004012 void SetIntrinsic(Intrinsics intrinsic,
4013 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4014 IntrinsicSideEffects side_effects,
4015 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004016
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004017 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004018 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004019 }
4020
Aart Bikff7d89c2016-11-07 08:49:28 -08004021 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4022
Vladimir Markoa1de9182016-02-25 11:37:38 +00004023 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004024
Aart Bik71bf7b42016-11-16 10:17:46 -08004025 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004026
Vladimir Marko372f10e2016-05-17 16:30:10 +01004027 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004028 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4029 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004030
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004031 uint32_t* GetIntrinsicOptimizations() {
4032 return &intrinsic_optimizations_;
4033 }
4034
4035 const uint32_t* GetIntrinsicOptimizations() const {
4036 return &intrinsic_optimizations_;
4037 }
4038
4039 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4040
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004041 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004042 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004043
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004044 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004045
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004046 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004047 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4048 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4050 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 static constexpr size_t kFieldReturnTypeSize =
4053 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4054 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4055 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4056 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004057 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4059
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004060 HInvoke(ArenaAllocator* arena,
4061 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004062 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004063 Primitive::Type return_type,
4064 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004065 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 ArtMethod* resolved_method,
4067 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004068 : HVariableInputSizeInstruction(
4069 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4070 dex_pc,
4071 arena,
4072 number_of_arguments + number_of_other_inputs,
4073 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004074 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004075 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004076 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004077 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004078 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004079 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004080 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004082 }
4083
Roland Levillain3e3d7332015-04-28 11:00:54 +01004084 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004085 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004086 const uint32_t dex_method_index_;
4087 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004088
4089 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4090 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091
4092 private:
4093 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4094};
4095
Vladimir Markofcb503c2016-05-18 12:48:17 +01004096class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004097 public:
4098 HInvokeUnresolved(ArenaAllocator* arena,
4099 uint32_t number_of_arguments,
4100 Primitive::Type return_type,
4101 uint32_t dex_pc,
4102 uint32_t dex_method_index,
4103 InvokeType invoke_type)
4104 : HInvoke(arena,
4105 number_of_arguments,
4106 0u /* number_of_other_inputs */,
4107 return_type,
4108 dex_pc,
4109 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004110 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004111 invoke_type) {
4112 }
4113
4114 DECLARE_INSTRUCTION(InvokeUnresolved);
4115
4116 private:
4117 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4118};
4119
Orion Hodsonac141392017-01-13 11:53:47 +00004120class HInvokePolymorphic FINAL : public HInvoke {
4121 public:
4122 HInvokePolymorphic(ArenaAllocator* arena,
4123 uint32_t number_of_arguments,
4124 Primitive::Type return_type,
4125 uint32_t dex_pc,
4126 uint32_t dex_method_index)
4127 : HInvoke(arena,
4128 number_of_arguments,
4129 0u /* number_of_other_inputs */,
4130 return_type,
4131 dex_pc,
4132 dex_method_index,
4133 nullptr,
4134 kVirtual) {}
4135
4136 DECLARE_INSTRUCTION(InvokePolymorphic);
4137
4138 private:
4139 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4140};
4141
Vladimir Markofcb503c2016-05-18 12:48:17 +01004142class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004143 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004144 // Requirements of this method call regarding the class
4145 // initialization (clinit) check of its declaring class.
4146 enum class ClinitCheckRequirement {
4147 kNone, // Class already initialized.
4148 kExplicit, // Static call having explicit clinit check as last input.
4149 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004151 };
4152
Vladimir Marko58155012015-08-19 12:49:41 +00004153 // Determines how to load the target ArtMethod*.
4154 enum class MethodLoadKind {
4155 // Use a String init ArtMethod* loaded from Thread entrypoints.
4156 kStringInit,
4157
4158 // Use the method's own ArtMethod* loaded by the register allocator.
4159 kRecursive,
4160
Vladimir Marko65979462017-05-19 17:25:12 +01004161 // Use PC-relative boot image ArtMethod* address that will be known at link time.
4162 // Used for boot image methods referenced by boot image code.
4163 kBootImageLinkTimePcRelative,
4164
Vladimir Marko58155012015-08-19 12:49:41 +00004165 // Use ArtMethod* at a known address, embed the direct address in the code.
4166 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4167 kDirectAddress,
4168
Vladimir Markoa1de9182016-02-25 11:37:38 +00004169 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004170 // Used when we need to use the dex cache, for example for invoke-static that
4171 // may cause class initialization (the entry may point to a resolution method),
4172 // and we know that we can access the dex cache arrays using a PC-relative load.
4173 kDexCachePcRelative,
4174
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004175 // Make a runtime call to resolve and call the method. This is the last-resort-kind
4176 // used when other kinds are unimplemented on a particular architecture.
4177 kRuntimeCall,
Vladimir Marko58155012015-08-19 12:49:41 +00004178 };
4179
4180 // Determines the location of the code pointer.
4181 enum class CodePtrLocation {
4182 // Recursive call, use local PC-relative call instruction.
4183 kCallSelf,
4184
Vladimir Marko58155012015-08-19 12:49:41 +00004185 // Use code pointer from the ArtMethod*.
4186 // Used when we don't know the target code. This is also the last-resort-kind used when
4187 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4188 kCallArtMethod,
4189 };
4190
4191 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004192 MethodLoadKind method_load_kind;
4193 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004194 // The method load data holds
4195 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4196 // Note that there are multiple string init methods, each having its own offset.
4197 // - the method address for kDirectAddress
4198 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004199 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004200 };
4201
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004202 HInvokeStaticOrDirect(ArenaAllocator* arena,
4203 uint32_t number_of_arguments,
4204 Primitive::Type return_type,
4205 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004206 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004207 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004208 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004209 InvokeType invoke_type,
4210 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004211 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004212 : HInvoke(arena,
4213 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004214 // There is potentially one extra argument for the HCurrentMethod node, and
4215 // potentially one other if the clinit check is explicit, and potentially
4216 // one other if the method is a string factory.
4217 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004218 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004219 return_type,
4220 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004221 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004222 resolved_method,
4223 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004224 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004225 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004226 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4227 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004228
Vladimir Markodc151b22015-10-15 18:02:30 +01004229 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004230 bool had_current_method_input = HasCurrentMethodInput();
4231 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4232
4233 // Using the current method is the default and once we find a better
4234 // method load kind, we should not go back to using the current method.
4235 DCHECK(had_current_method_input || !needs_current_method_input);
4236
4237 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004238 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4239 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004240 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004241 dispatch_info_ = dispatch_info;
4242 }
4243
Aart Bik6daebeb2017-04-03 14:35:41 -07004244 DispatchInfo GetDispatchInfo() const {
4245 return dispatch_info_;
4246 }
4247
Vladimir Markoc53c0792015-11-19 15:48:33 +00004248 void AddSpecialInput(HInstruction* input) {
4249 // We allow only one special input.
4250 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4251 DCHECK(InputCount() == GetSpecialInputIndex() ||
4252 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4253 InsertInputAt(GetSpecialInputIndex(), input);
4254 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004255
Vladimir Marko372f10e2016-05-17 16:30:10 +01004256 using HInstruction::GetInputRecords; // Keep the const version visible.
4257 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4258 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4259 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4260 DCHECK(!input_records.empty());
4261 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4262 HInstruction* last_input = input_records.back().GetInstruction();
4263 // Note: `last_input` may be null during arguments setup.
4264 if (last_input != nullptr) {
4265 // `last_input` is the last input of a static invoke marked as having
4266 // an explicit clinit check. It must either be:
4267 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4268 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4269 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4270 }
4271 }
4272 return input_records;
4273 }
4274
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004275 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004276 // We access the method via the dex cache so we can't do an implicit null check.
4277 // TODO: for intrinsics we can generate implicit null checks.
4278 return false;
4279 }
4280
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004281 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004282 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004283 }
4284
Vladimir Markoc53c0792015-11-19 15:48:33 +00004285 // Get the index of the special input, if any.
4286 //
David Brazdil6de19382016-01-08 17:37:10 +00004287 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4288 // method pointer; otherwise there may be one platform-specific special input,
4289 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004291 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004292
Vladimir Marko58155012015-08-19 12:49:41 +00004293 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4294 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4295 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004296 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004297 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004298 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004299 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004300 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4301 }
Vladimir Marko65979462017-05-19 17:25:12 +01004302 bool HasPcRelativeMethodLoadKind() const {
4303 return GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative ||
4304 GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4305 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004306 bool HasCurrentMethodInput() const {
4307 // This function can be called only after the invoke has been fully initialized by the builder.
4308 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004309 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004310 return true;
4311 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004312 DCHECK(InputCount() == GetSpecialInputIndex() ||
4313 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004314 return false;
4315 }
4316 }
Vladimir Marko58155012015-08-19 12:49:41 +00004317
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004318 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004319 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004320 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004321 }
4322
4323 uint64_t GetMethodAddress() const {
4324 DCHECK(HasMethodAddress());
4325 return dispatch_info_.method_load_data;
4326 }
4327
4328 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004329 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004330 return dispatch_info_.method_load_data;
4331 }
4332
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004333 const DexFile& GetDexFileForPcRelativeDexCache() const;
4334
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 ClinitCheckRequirement GetClinitCheckRequirement() const {
4336 return GetPackedField<ClinitCheckRequirementField>();
4337 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004338
Roland Levillain4c0eb422015-04-24 16:43:49 +01004339 // Is this instruction a call to a static method?
4340 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004341 return GetInvokeType() == kStatic;
4342 }
4343
4344 MethodReference GetTargetMethod() const {
4345 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004346 }
4347
Vladimir Markofbb184a2015-11-13 14:47:00 +00004348 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4349 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4350 // instruction; only relevant for static calls with explicit clinit check.
4351 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004352 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004353 size_t last_input_index = inputs_.size() - 1u;
4354 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004356 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004357 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004358 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004359 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004360 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004361 }
4362
4363 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004364 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004365 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004366 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004367 }
4368
4369 // Is this a call to a static method whose declaring class has an
4370 // implicit intialization check requirement?
4371 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004372 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004373 }
4374
Vladimir Markob554b5a2015-11-06 12:57:55 +00004375 // Does this method load kind need the current method as an input?
4376 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004377 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kRuntimeCall;
Vladimir Markob554b5a2015-11-06 12:57:55 +00004378 }
4379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004380 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004381
4382 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004383 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004384 static constexpr size_t kFieldClinitCheckRequirementSize =
4385 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4386 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4387 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4388 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4389 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004390 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4391 kFieldClinitCheckRequirement,
4392 kFieldClinitCheckRequirementSize>;
4393
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004394 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004395 MethodReference target_method_;
4396 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004397
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004398 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004399};
Vladimir Markof64242a2015-12-01 14:58:23 +00004400std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004401std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004402
Vladimir Markofcb503c2016-05-18 12:48:17 +01004403class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004404 public:
4405 HInvokeVirtual(ArenaAllocator* arena,
4406 uint32_t number_of_arguments,
4407 Primitive::Type return_type,
4408 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004409 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004410 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004411 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004412 : HInvoke(arena,
4413 number_of_arguments,
4414 0u,
4415 return_type,
4416 dex_pc,
4417 dex_method_index,
4418 resolved_method,
4419 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004420 vtable_index_(vtable_index) {}
4421
Aart Bik71bf7b42016-11-16 10:17:46 -08004422 bool CanBeNull() const OVERRIDE {
4423 switch (GetIntrinsic()) {
4424 case Intrinsics::kThreadCurrentThread:
4425 case Intrinsics::kStringBufferAppend:
4426 case Intrinsics::kStringBufferToString:
4427 case Intrinsics::kStringBuilderAppend:
4428 case Intrinsics::kStringBuilderToString:
4429 return false;
4430 default:
4431 return HInvoke::CanBeNull();
4432 }
4433 }
4434
Calin Juravle641547a2015-04-21 22:08:51 +01004435 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004436 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004437 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004438 }
4439
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004440 uint32_t GetVTableIndex() const { return vtable_index_; }
4441
4442 DECLARE_INSTRUCTION(InvokeVirtual);
4443
4444 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004446 const uint32_t vtable_index_;
4447
4448 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4449};
4450
Vladimir Markofcb503c2016-05-18 12:48:17 +01004451class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004452 public:
4453 HInvokeInterface(ArenaAllocator* arena,
4454 uint32_t number_of_arguments,
4455 Primitive::Type return_type,
4456 uint32_t dex_pc,
4457 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004458 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004459 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004460 : HInvoke(arena,
4461 number_of_arguments,
4462 0u,
4463 return_type,
4464 dex_pc,
4465 dex_method_index,
4466 resolved_method,
4467 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004468 imt_index_(imt_index) {}
4469
Calin Juravle641547a2015-04-21 22:08:51 +01004470 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004471 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004472 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004473 }
4474
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004475 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4476 // The assembly stub currently needs it.
4477 return true;
4478 }
4479
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004480 uint32_t GetImtIndex() const { return imt_index_; }
4481 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4482
4483 DECLARE_INSTRUCTION(InvokeInterface);
4484
4485 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004486 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004487 const uint32_t imt_index_;
4488
4489 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4490};
4491
Vladimir Markofcb503c2016-05-18 12:48:17 +01004492class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004493 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004495 : HUnaryOperation(result_type, input, dex_pc) {
4496 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4497 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004498
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004499 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004500
4501 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004502 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004503 }
4504 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004505 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004506 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004507 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4508 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4509 }
4510 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4511 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4512 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004513
Roland Levillain88cb1752014-10-20 16:36:47 +01004514 DECLARE_INSTRUCTION(Neg);
4515
4516 private:
4517 DISALLOW_COPY_AND_ASSIGN(HNeg);
4518};
4519
Vladimir Markofcb503c2016-05-18 12:48:17 +01004520class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004522 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4523 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4524 SetRawInputAt(0, cls);
4525 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004526 }
4527
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004528 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004529 bool NeedsEnvironment() const OVERRIDE { return true; }
4530
Mingyao Yang0c365e62015-03-31 15:09:29 -07004531 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4532 bool CanThrow() const OVERRIDE { return true; }
4533
Calin Juravle10e244f2015-01-26 18:54:32 +00004534 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004535
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004536 HLoadClass* GetLoadClass() const {
4537 DCHECK(InputAt(0)->IsLoadClass());
4538 return InputAt(0)->AsLoadClass();
4539 }
4540
4541 HInstruction* GetLength() const {
4542 return InputAt(1);
4543 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004544
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004545 DECLARE_INSTRUCTION(NewArray);
4546
4547 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004548 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4549};
4550
Vladimir Markofcb503c2016-05-18 12:48:17 +01004551class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004552 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004553 HAdd(Primitive::Type result_type,
4554 HInstruction* left,
4555 HInstruction* right,
4556 uint32_t dex_pc = kNoDexPc)
4557 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004558
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004559 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004560
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004561 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004562
4563 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004564 return GetBlock()->GetGraph()->GetIntConstant(
4565 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004566 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004567 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004568 return GetBlock()->GetGraph()->GetLongConstant(
4569 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004570 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004571 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4572 return GetBlock()->GetGraph()->GetFloatConstant(
4573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4574 }
4575 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4576 return GetBlock()->GetGraph()->GetDoubleConstant(
4577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4578 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004579
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004580 DECLARE_INSTRUCTION(Add);
4581
4582 private:
4583 DISALLOW_COPY_AND_ASSIGN(HAdd);
4584};
4585
Vladimir Markofcb503c2016-05-18 12:48:17 +01004586class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004587 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 HSub(Primitive::Type result_type,
4589 HInstruction* left,
4590 HInstruction* right,
4591 uint32_t dex_pc = kNoDexPc)
4592 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004593
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004594 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004595
4596 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 return GetBlock()->GetGraph()->GetIntConstant(
4598 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004599 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 return GetBlock()->GetGraph()->GetLongConstant(
4602 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004603 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004604 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4605 return GetBlock()->GetGraph()->GetFloatConstant(
4606 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4607 }
4608 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4609 return GetBlock()->GetGraph()->GetDoubleConstant(
4610 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4611 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004612
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004613 DECLARE_INSTRUCTION(Sub);
4614
4615 private:
4616 DISALLOW_COPY_AND_ASSIGN(HSub);
4617};
4618
Vladimir Markofcb503c2016-05-18 12:48:17 +01004619class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 HMul(Primitive::Type result_type,
4622 HInstruction* left,
4623 HInstruction* right,
4624 uint32_t dex_pc = kNoDexPc)
4625 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004626
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004627 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004628
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004629 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004630
4631 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 return GetBlock()->GetGraph()->GetIntConstant(
4633 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 }
4635 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004636 return GetBlock()->GetGraph()->GetLongConstant(
4637 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004639 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4640 return GetBlock()->GetGraph()->GetFloatConstant(
4641 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4642 }
4643 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4644 return GetBlock()->GetGraph()->GetDoubleConstant(
4645 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4646 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004647
4648 DECLARE_INSTRUCTION(Mul);
4649
4650 private:
4651 DISALLOW_COPY_AND_ASSIGN(HMul);
4652};
4653
Vladimir Markofcb503c2016-05-18 12:48:17 +01004654class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004655 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 HDiv(Primitive::Type result_type,
4657 HInstruction* left,
4658 HInstruction* right,
4659 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004660 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004661
Roland Levillain9867bc72015-08-05 10:21:34 +01004662 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004663 T ComputeIntegral(T x, T y) const {
4664 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004665 // Our graph structure ensures we never have 0 for `y` during
4666 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004667 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004668 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004669 return (y == -1) ? -x : x / y;
4670 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004671
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 template <typename T>
4673 T ComputeFP(T x, T y) const {
4674 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4675 return x / y;
4676 }
4677
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004680 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 }
4682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004684 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4685 }
4686 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4687 return GetBlock()->GetGraph()->GetFloatConstant(
4688 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4689 }
4690 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4691 return GetBlock()->GetGraph()->GetDoubleConstant(
4692 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004693 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004694
4695 DECLARE_INSTRUCTION(Div);
4696
4697 private:
4698 DISALLOW_COPY_AND_ASSIGN(HDiv);
4699};
4700
Vladimir Markofcb503c2016-05-18 12:48:17 +01004701class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004702 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 HRem(Primitive::Type result_type,
4704 HInstruction* left,
4705 HInstruction* right,
4706 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004707 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004708
Roland Levillain9867bc72015-08-05 10:21:34 +01004709 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004710 T ComputeIntegral(T x, T y) const {
4711 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004712 // Our graph structure ensures we never have 0 for `y` during
4713 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004714 DCHECK_NE(y, 0);
4715 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4716 return (y == -1) ? 0 : x % y;
4717 }
4718
Roland Levillain31dd3d62016-02-16 12:21:02 +00004719 template <typename T>
4720 T ComputeFP(T x, T y) const {
4721 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4722 return std::fmod(x, y);
4723 }
4724
Roland Levillain9867bc72015-08-05 10:21:34 +01004725 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004726 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004728 }
4729 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004730 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004731 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004732 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004733 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4734 return GetBlock()->GetGraph()->GetFloatConstant(
4735 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4736 }
4737 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4738 return GetBlock()->GetGraph()->GetDoubleConstant(
4739 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4740 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004741
4742 DECLARE_INSTRUCTION(Rem);
4743
4744 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004745 DISALLOW_COPY_AND_ASSIGN(HRem);
4746};
4747
Vladimir Markofcb503c2016-05-18 12:48:17 +01004748class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004749 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004750 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4751 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004752 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004753 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004754 SetRawInputAt(0, value);
4755 }
4756
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004757 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4758
Calin Juravled0d48522014-11-04 16:40:20 +00004759 bool CanBeMoved() const OVERRIDE { return true; }
4760
Vladimir Marko372f10e2016-05-17 16:30:10 +01004761 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004762 return true;
4763 }
4764
4765 bool NeedsEnvironment() const OVERRIDE { return true; }
4766 bool CanThrow() const OVERRIDE { return true; }
4767
Calin Juravled0d48522014-11-04 16:40:20 +00004768 DECLARE_INSTRUCTION(DivZeroCheck);
4769
4770 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004771 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4772};
4773
Vladimir Markofcb503c2016-05-18 12:48:17 +01004774class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004775 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 HInstruction* value,
4778 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004779 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4781 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4782 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 }
4784
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004786 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 return value << (distance & max_shift_distance);
4788 }
4789
4790 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004793 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004794 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004795 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004796 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004797 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4799 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4800 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4801 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004802 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4804 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004805 LOG(FATAL) << DebugName() << " is not defined for float values";
4806 UNREACHABLE();
4807 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4809 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004810 LOG(FATAL) << DebugName() << " is not defined for double values";
4811 UNREACHABLE();
4812 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004813
4814 DECLARE_INSTRUCTION(Shl);
4815
4816 private:
4817 DISALLOW_COPY_AND_ASSIGN(HShl);
4818};
4819
Vladimir Markofcb503c2016-05-18 12:48:17 +01004820class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004821 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004822 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004823 HInstruction* value,
4824 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4827 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4828 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 }
4830
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004832 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004833 return value >> (distance & max_shift_distance);
4834 }
4835
4836 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004838 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004839 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004840 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004843 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4845 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4846 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4847 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004848 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4850 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004851 LOG(FATAL) << DebugName() << " is not defined for float values";
4852 UNREACHABLE();
4853 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004854 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4855 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004856 LOG(FATAL) << DebugName() << " is not defined for double values";
4857 UNREACHABLE();
4858 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004859
4860 DECLARE_INSTRUCTION(Shr);
4861
4862 private:
4863 DISALLOW_COPY_AND_ASSIGN(HShr);
4864};
4865
Vladimir Markofcb503c2016-05-18 12:48:17 +01004866class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004867 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004868 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004869 HInstruction* value,
4870 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004871 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4873 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4874 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004875 }
4876
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004878 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 typedef typename std::make_unsigned<T>::type V;
4880 V ux = static_cast<V>(value);
4881 return static_cast<T>(ux >> (distance & max_shift_distance));
4882 }
4883
4884 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004886 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004887 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004888 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004889 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004890 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004891 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004892 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4893 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4894 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4895 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004896 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004897 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4898 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004899 LOG(FATAL) << DebugName() << " is not defined for float values";
4900 UNREACHABLE();
4901 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004902 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4903 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004904 LOG(FATAL) << DebugName() << " is not defined for double values";
4905 UNREACHABLE();
4906 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004907
4908 DECLARE_INSTRUCTION(UShr);
4909
4910 private:
4911 DISALLOW_COPY_AND_ASSIGN(HUShr);
4912};
4913
Vladimir Markofcb503c2016-05-18 12:48:17 +01004914class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004915 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 HAnd(Primitive::Type result_type,
4917 HInstruction* left,
4918 HInstruction* right,
4919 uint32_t dex_pc = kNoDexPc)
4920 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004921
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004922 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004923
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004924 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004925
4926 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004927 return GetBlock()->GetGraph()->GetIntConstant(
4928 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004929 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004930 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004931 return GetBlock()->GetGraph()->GetLongConstant(
4932 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004933 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004934 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4935 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4936 LOG(FATAL) << DebugName() << " is not defined for float values";
4937 UNREACHABLE();
4938 }
4939 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4940 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4941 LOG(FATAL) << DebugName() << " is not defined for double values";
4942 UNREACHABLE();
4943 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004944
4945 DECLARE_INSTRUCTION(And);
4946
4947 private:
4948 DISALLOW_COPY_AND_ASSIGN(HAnd);
4949};
4950
Vladimir Markofcb503c2016-05-18 12:48:17 +01004951class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 HOr(Primitive::Type result_type,
4954 HInstruction* left,
4955 HInstruction* right,
4956 uint32_t dex_pc = kNoDexPc)
4957 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004958
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004959 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004960
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004961 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004962
4963 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004964 return GetBlock()->GetGraph()->GetIntConstant(
4965 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004966 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004967 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004968 return GetBlock()->GetGraph()->GetLongConstant(
4969 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004970 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004971 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4972 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4973 LOG(FATAL) << DebugName() << " is not defined for float values";
4974 UNREACHABLE();
4975 }
4976 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4977 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4978 LOG(FATAL) << DebugName() << " is not defined for double values";
4979 UNREACHABLE();
4980 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004981
4982 DECLARE_INSTRUCTION(Or);
4983
4984 private:
4985 DISALLOW_COPY_AND_ASSIGN(HOr);
4986};
4987
Vladimir Markofcb503c2016-05-18 12:48:17 +01004988class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004989 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 HXor(Primitive::Type result_type,
4991 HInstruction* left,
4992 HInstruction* right,
4993 uint32_t dex_pc = kNoDexPc)
4994 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004995
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004996 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004997
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004998 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004999
5000 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005001 return GetBlock()->GetGraph()->GetIntConstant(
5002 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005003 }
Roland Levillain9867bc72015-08-05 10:21:34 +01005004 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005005 return GetBlock()->GetGraph()->GetLongConstant(
5006 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005007 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005008 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5009 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5010 LOG(FATAL) << DebugName() << " is not defined for float values";
5011 UNREACHABLE();
5012 }
5013 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5014 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5015 LOG(FATAL) << DebugName() << " is not defined for double values";
5016 UNREACHABLE();
5017 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005018
5019 DECLARE_INSTRUCTION(Xor);
5020
5021 private:
5022 DISALLOW_COPY_AND_ASSIGN(HXor);
5023};
5024
Vladimir Markofcb503c2016-05-18 12:48:17 +01005025class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026 public:
5027 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005028 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5030 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005031 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005032
Roland Levillain5b5b9312016-03-22 14:57:31 +00005033 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005034 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 typedef typename std::make_unsigned<T>::type V;
5036 V ux = static_cast<V>(value);
5037 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005038 return static_cast<T>(ux);
5039 } else {
5040 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 return static_cast<T>(ux >> (distance & max_shift_value)) |
5042 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005043 }
5044 }
5045
Roland Levillain5b5b9312016-03-22 14:57:31 +00005046 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005047 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005048 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005049 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005050 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005051 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005052 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005053 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005054 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5055 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5056 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5057 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005058 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005059 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5060 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005061 LOG(FATAL) << DebugName() << " is not defined for float values";
5062 UNREACHABLE();
5063 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005064 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5065 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005066 LOG(FATAL) << DebugName() << " is not defined for double values";
5067 UNREACHABLE();
5068 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005069
5070 DECLARE_INSTRUCTION(Ror);
5071
5072 private:
5073 DISALLOW_COPY_AND_ASSIGN(HRor);
5074};
5075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005076// The value of a parameter in this method. Its location depends on
5077// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005078class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005079 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005080 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005081 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005082 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005083 Primitive::Type parameter_type,
5084 bool is_this = false)
5085 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005086 dex_file_(dex_file),
5087 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005088 index_(index) {
5089 SetPackedFlag<kFlagIsThis>(is_this);
5090 SetPackedFlag<kFlagCanBeNull>(!is_this);
5091 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005092
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005093 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005094 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005095 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005096 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005097
Vladimir Markoa1de9182016-02-25 11:37:38 +00005098 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5099 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005100
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005101 DECLARE_INSTRUCTION(ParameterValue);
5102
5103 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005104 // Whether or not the parameter value corresponds to 'this' argument.
5105 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5106 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5107 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5108 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5109 "Too many packed fields.");
5110
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005111 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005112 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005113 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005114 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005115 const uint8_t index_;
5116
5117 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5118};
5119
Vladimir Markofcb503c2016-05-18 12:48:17 +01005120class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005121 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5123 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005124
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005125 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005126 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005127 return true;
5128 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005129
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005130 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005131
5132 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005133 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005134 }
5135 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005136 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005137 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005138 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5139 LOG(FATAL) << DebugName() << " is not defined for float values";
5140 UNREACHABLE();
5141 }
5142 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5143 LOG(FATAL) << DebugName() << " is not defined for double values";
5144 UNREACHABLE();
5145 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005146
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005147 DECLARE_INSTRUCTION(Not);
5148
5149 private:
5150 DISALLOW_COPY_AND_ASSIGN(HNot);
5151};
5152
Vladimir Markofcb503c2016-05-18 12:48:17 +01005153class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005154 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005155 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5156 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005157
5158 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005159 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005160 return true;
5161 }
5162
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005163 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005164 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005165 return !x;
5166 }
5167
Roland Levillain9867bc72015-08-05 10:21:34 +01005168 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005169 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005170 }
5171 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5172 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005173 UNREACHABLE();
5174 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005175 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5176 LOG(FATAL) << DebugName() << " is not defined for float values";
5177 UNREACHABLE();
5178 }
5179 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5180 LOG(FATAL) << DebugName() << " is not defined for double values";
5181 UNREACHABLE();
5182 }
David Brazdil66d126e2015-04-03 16:02:44 +01005183
5184 DECLARE_INSTRUCTION(BooleanNot);
5185
5186 private:
5187 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5188};
5189
Vladimir Markofcb503c2016-05-18 12:48:17 +01005190class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005191 public:
5192 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005193 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005194 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005195 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005196 // Invariant: We should never generate a conversion to a Boolean value.
5197 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005198 }
5199
5200 HInstruction* GetInput() const { return InputAt(0); }
5201 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5202 Primitive::Type GetResultType() const { return GetType(); }
5203
5204 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005205 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5206 return true;
5207 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005208
Mark Mendelle82549b2015-05-06 10:55:34 -04005209 // Try to statically evaluate the conversion and return a HConstant
5210 // containing the result. If the input cannot be converted, return nullptr.
5211 HConstant* TryStaticEvaluation() const;
5212
Roland Levillaindff1f282014-11-05 14:15:05 +00005213 DECLARE_INSTRUCTION(TypeConversion);
5214
5215 private:
5216 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5217};
5218
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005219static constexpr uint32_t kNoRegNumber = -1;
5220
Vladimir Markofcb503c2016-05-18 12:48:17 +01005221class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005222 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005223 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5224 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005226 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005227 SetRawInputAt(0, value);
5228 }
5229
Calin Juravle10e244f2015-01-26 18:54:32 +00005230 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005231 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005232 return true;
5233 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005234
Calin Juravle10e244f2015-01-26 18:54:32 +00005235 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005236
Calin Juravle10e244f2015-01-26 18:54:32 +00005237 bool CanThrow() const OVERRIDE { return true; }
5238
5239 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005240
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241
5242 DECLARE_INSTRUCTION(NullCheck);
5243
5244 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005245 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5246};
5247
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005248// Embeds an ArtField and all the information required by the compiler. We cache
5249// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005250class FieldInfo : public ValueObject {
5251 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005252 FieldInfo(ArtField* field,
5253 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005254 Primitive::Type field_type,
5255 bool is_volatile,
5256 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005257 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005258 const DexFile& dex_file)
5259 : field_(field),
5260 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005261 field_type_(field_type),
5262 is_volatile_(is_volatile),
5263 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005264 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005265 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005267 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005268 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005269 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005270 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005271 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005272 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005273 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005274
5275 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005276 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005277 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005278 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005279 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005280 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005281 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005282 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005283};
5284
Vladimir Markofcb503c2016-05-18 12:48:17 +01005285class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005286 public:
5287 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005288 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005289 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005290 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005291 bool is_volatile,
5292 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005293 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005294 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005295 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005296 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005297 field_info_(field,
5298 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005299 field_type,
5300 is_volatile,
5301 field_idx,
5302 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005303 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005304 SetRawInputAt(0, value);
5305 }
5306
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005307 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005308
Vladimir Marko372f10e2016-05-17 16:30:10 +01005309 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5310 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005311 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005312 }
5313
Calin Juravle641547a2015-04-21 22:08:51 +01005314 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005315 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005316 }
5317
5318 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005319 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5320 }
5321
Calin Juravle52c48962014-12-16 17:02:57 +00005322 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005323 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005324 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005325 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005326
5327 DECLARE_INSTRUCTION(InstanceFieldGet);
5328
5329 private:
5330 const FieldInfo field_info_;
5331
5332 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5333};
5334
Vladimir Markofcb503c2016-05-18 12:48:17 +01005335class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005336 public:
5337 HInstanceFieldSet(HInstruction* object,
5338 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005339 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005341 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005342 bool is_volatile,
5343 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005344 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005345 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005346 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005347 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005348 field_info_(field,
5349 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005350 field_type,
5351 is_volatile,
5352 field_idx,
5353 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005354 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005355 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005356 SetRawInputAt(0, object);
5357 SetRawInputAt(1, value);
5358 }
5359
Calin Juravle641547a2015-04-21 22:08:51 +01005360 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005361 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005362 }
5363
Calin Juravle52c48962014-12-16 17:02:57 +00005364 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005365 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005366 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005367 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005368 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005369 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5370 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005371
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 DECLARE_INSTRUCTION(InstanceFieldSet);
5373
5374 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005375 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5376 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5377 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5378 "Too many packed fields.");
5379
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005380 const FieldInfo field_info_;
5381
5382 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5383};
5384
Vladimir Markofcb503c2016-05-18 12:48:17 +01005385class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005387 HArrayGet(HInstruction* array,
5388 HInstruction* index,
5389 Primitive::Type type,
5390 uint32_t dex_pc,
5391 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005392 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005393 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394 SetRawInputAt(0, array);
5395 SetRawInputAt(1, index);
5396 }
5397
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005398 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005399 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005400 return true;
5401 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005402 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005403 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005404 // Currently, unless the array is the result of NewArray, the array access is always
5405 // preceded by some form of null NullCheck necessary for the bounds check, usually
5406 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5407 // dynamic BCE. There are cases when these could be removed to produce better code.
5408 // If we ever add optimizations to do so we should allow an implicit check here
5409 // (as long as the address falls in the first page).
5410 //
5411 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5412 // a = cond ? new int[1] : null;
5413 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005414 return false;
5415 }
5416
David Brazdil4833f5a2015-12-16 10:37:39 +00005417 bool IsEquivalentOf(HArrayGet* other) const {
5418 bool result = (GetDexPc() == other->GetDexPc());
5419 if (kIsDebugBuild && result) {
5420 DCHECK_EQ(GetBlock(), other->GetBlock());
5421 DCHECK_EQ(GetArray(), other->GetArray());
5422 DCHECK_EQ(GetIndex(), other->GetIndex());
5423 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005424 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005425 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005426 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5427 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005428 }
5429 }
5430 return result;
5431 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005432
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005433 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5434
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005435 HInstruction* GetArray() const { return InputAt(0); }
5436 HInstruction* GetIndex() const { return InputAt(1); }
5437
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005438 DECLARE_INSTRUCTION(ArrayGet);
5439
5440 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005441 // We treat a String as an array, creating the HArrayGet from String.charAt()
5442 // intrinsic in the instruction simplifier. We can always determine whether
5443 // a particular HArrayGet is actually a String.charAt() by looking at the type
5444 // of the input but that requires holding the mutator lock, so we prefer to use
5445 // a flag, so that code generators don't need to do the locking.
5446 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5447 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5448 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5449 "Too many packed fields.");
5450
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005451 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5452};
5453
Vladimir Markofcb503c2016-05-18 12:48:17 +01005454class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005455 public:
5456 HArraySet(HInstruction* array,
5457 HInstruction* index,
5458 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005459 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005460 uint32_t dex_pc)
5461 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005462 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5463 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5464 SetPackedFlag<kFlagValueCanBeNull>(true);
5465 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466 SetRawInputAt(0, array);
5467 SetRawInputAt(1, index);
5468 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005469 // Make a best guess now, may be refined during SSA building.
5470 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 }
5472
Calin Juravle77520bc2015-01-12 18:45:46 +00005473 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005474 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005476 }
5477
Mingyao Yang81014cb2015-06-02 03:16:27 -07005478 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005479 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005480
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005481 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005482 // TODO: Same as for ArrayGet.
5483 return false;
5484 }
5485
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005486 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005487 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005488 }
5489
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005490 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005491 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005492 }
5493
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005494 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005495 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005496 }
5497
Vladimir Markoa1de9182016-02-25 11:37:38 +00005498 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5499 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5500 bool StaticTypeOfArrayIsObjectArray() const {
5501 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5502 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005503
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005504 HInstruction* GetArray() const { return InputAt(0); }
5505 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005506 HInstruction* GetValue() const { return InputAt(2); }
5507
5508 Primitive::Type GetComponentType() const {
5509 // The Dex format does not type floating point index operations. Since the
5510 // `expected_component_type_` is set during building and can therefore not
5511 // be correct, we also check what is the value type. If it is a floating
5512 // point type, we must use that type.
5513 Primitive::Type value_type = GetValue()->GetType();
5514 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5515 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005516 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005517 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005518
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005519 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005520 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005521 }
5522
Aart Bik18b36ab2016-04-13 16:41:35 -07005523 void ComputeSideEffects() {
5524 Primitive::Type type = GetComponentType();
5525 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5526 SideEffectsForArchRuntimeCalls(type)));
5527 }
5528
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005529 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5530 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5531 }
5532
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005533 DECLARE_INSTRUCTION(ArraySet);
5534
5535 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5537 static constexpr size_t kFieldExpectedComponentTypeSize =
5538 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5539 static constexpr size_t kFlagNeedsTypeCheck =
5540 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5541 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005542 // Cached information for the reference_type_info_ so that codegen
5543 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005544 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5545 static constexpr size_t kNumberOfArraySetPackedBits =
5546 kFlagStaticTypeOfArrayIsObjectArray + 1;
5547 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5548 using ExpectedComponentTypeField =
5549 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550
5551 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5552};
5553
Vladimir Markofcb503c2016-05-18 12:48:17 +01005554class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005556 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005557 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005558 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005559 // Note that arrays do not change length, so the instruction does not
5560 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 SetRawInputAt(0, array);
5562 }
5563
Calin Juravle77520bc2015-01-12 18:45:46 +00005564 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005565 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005566 return true;
5567 }
Calin Juravle641547a2015-04-21 22:08:51 +01005568 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5569 return obj == InputAt(0);
5570 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005571
Vladimir Markodce016e2016-04-28 13:10:02 +01005572 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5573
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005574 DECLARE_INSTRUCTION(ArrayLength);
5575
5576 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005577 // We treat a String as an array, creating the HArrayLength from String.length()
5578 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5579 // determine whether a particular HArrayLength is actually a String.length() by
5580 // looking at the type of the input but that requires holding the mutator lock, so
5581 // we prefer to use a flag, so that code generators don't need to do the locking.
5582 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5583 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5584 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5585 "Too many packed fields.");
5586
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005587 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5588};
5589
Vladimir Markofcb503c2016-05-18 12:48:17 +01005590class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005592 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5593 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005594 HBoundsCheck(HInstruction* index,
5595 HInstruction* length,
5596 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005597 bool string_char_at = false)
5598 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005599 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005600 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005601 SetRawInputAt(0, index);
5602 SetRawInputAt(1, length);
5603 }
5604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005605 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005606 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005607 return true;
5608 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005609
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005610 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005612 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005613
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005614 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005615
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005616 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617
5618 DECLARE_INSTRUCTION(BoundsCheck);
5619
5620 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005621 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005622
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005623 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5624};
5625
Vladimir Markofcb503c2016-05-18 12:48:17 +01005626class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005627 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005628 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005629 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005630
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005631 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005632 return true;
5633 }
5634
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005635 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5636 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005637
5638 DECLARE_INSTRUCTION(SuspendCheck);
5639
5640 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005641 // Only used for code generation, in order to share the same slow path between back edges
5642 // of a same loop.
5643 SlowPathCode* slow_path_;
5644
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005645 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5646};
5647
David Srbecky0cf44932015-12-09 14:09:59 +00005648// Pseudo-instruction which provides the native debugger with mapping information.
5649// It ensures that we can generate line number and local variables at this point.
5650class HNativeDebugInfo : public HTemplateInstruction<0> {
5651 public:
5652 explicit HNativeDebugInfo(uint32_t dex_pc)
5653 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5654
5655 bool NeedsEnvironment() const OVERRIDE {
5656 return true;
5657 }
5658
5659 DECLARE_INSTRUCTION(NativeDebugInfo);
5660
5661 private:
5662 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5663};
5664
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005665/**
5666 * Instruction to load a Class object.
5667 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005668class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005669 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670 // Determines how to load the Class.
5671 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005672 // We cannot load this class. See HSharpening::SharpenLoadClass.
5673 kInvalid = -1,
5674
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005675 // Use the Class* from the method's own ArtMethod*.
5676 kReferrersClass,
5677
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005678 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005679 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005680 kBootImageLinkTimePcRelative,
5681
5682 // Use a known boot image Class* address, embedded in the code by the codegen.
5683 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684 kBootImageAddress,
5685
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005686 // Load from an entry in the .bss section using a PC-relative load.
5687 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5688 kBssEntry,
5689
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005690 // Load from the root table associated with the JIT compiled method.
5691 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005692
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005693 // Load using a simple runtime call. This is the fall-back load kind when
5694 // the codegen is unable to use another appropriate kind.
5695 kRuntimeCall,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005697 kLast = kRuntimeCall
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005698 };
5699
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005700 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005701 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005702 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005703 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005704 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005705 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005706 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5708 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005709 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005710 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005711 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005712 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005713 // Referrers class should not need access check. We never inline unverified
5714 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005715 DCHECK(!is_referrers_class || !needs_access_check);
5716
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005717 SetPackedField<LoadKindField>(
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005718 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kRuntimeCall);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005719 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005720 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722 }
5723
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005724 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005726 LoadKind GetLoadKind() const {
5727 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005728 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005729
5730 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005731
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005732 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005733
Andreas Gampea5b09a62016-11-17 15:21:22 -08005734 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005735
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005736 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005737
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005738 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005739 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005740 }
5741
Calin Juravle0ba218d2015-05-19 18:46:01 +01005742 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005743 // The entrypoint the code generator is going to call does not do
5744 // clinit of the class.
5745 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005746 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005747 }
5748
5749 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005750 return NeedsAccessCheck() ||
5751 MustGenerateClinitCheck() ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005752 GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005753 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005754 }
5755
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005756 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005757 return NeedsAccessCheck() ||
5758 MustGenerateClinitCheck() ||
5759 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5760 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5761 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005762 ((GetLoadKind() == LoadKind::kRuntimeCall ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005763 GetLoadKind() == LoadKind::kBssEntry) &&
5764 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005765 }
5766
Calin Juravleacf735c2015-02-12 15:25:22 +00005767 ReferenceTypeInfo GetLoadedClassRTI() {
5768 return loaded_class_rti_;
5769 }
5770
5771 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5772 // Make sure we only set exact types (the loaded class should never be merged).
5773 DCHECK(rti.IsExact());
5774 loaded_class_rti_ = rti;
5775 }
5776
Andreas Gampea5b09a62016-11-17 15:21:22 -08005777 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005778 const DexFile& GetDexFile() const { return dex_file_; }
5779
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005780 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005781 return GetLoadKind() == LoadKind::kRuntimeCall;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005782 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005783
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005784 static SideEffects SideEffectsForArchRuntimeCalls() {
5785 return SideEffects::CanTriggerGC();
5786 }
5787
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005789 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005790 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005791 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005792
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005793 void MarkInBootImage() {
5794 SetPackedFlag<kFlagIsInBootImage>(true);
5795 }
5796
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797 void AddSpecialInput(HInstruction* special_input);
5798
5799 using HInstruction::GetInputRecords; // Keep the const version visible.
5800 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5801 return ArrayRef<HUserRecord<HInstruction*>>(
5802 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5803 }
5804
5805 Primitive::Type GetType() const OVERRIDE {
5806 return Primitive::kPrimNot;
5807 }
5808
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005809 Handle<mirror::Class> GetClass() const {
5810 return klass_;
5811 }
5812
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005813 DECLARE_INSTRUCTION(LoadClass);
5814
5815 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005817 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005818 // Whether this instruction must generate the initialization check.
5819 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005820 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005821 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5822 static constexpr size_t kFieldLoadKindSize =
5823 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5824 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005825 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5827
5828 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005829 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005831 load_kind == LoadKind::kBssEntry ||
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005832 load_kind == LoadKind::kRuntimeCall;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 }
5834
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 void SetLoadKindInternal(LoadKind load_kind);
5836
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005837 // The special input is the HCurrentMethod for kRuntimeCall or kReferrersClass.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005838 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005839 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005840 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005842 // A type index and dex file where the class can be accessed. The dex file can be:
5843 // - The compiling method's dex file if the class is defined there too.
5844 // - The compiling method's dex file if the class is referenced there.
5845 // - The dex file where the class is defined. When the load kind can only be
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005846 // kBssEntry or kRuntimeCall, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005847 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005848 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005850 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005851
Calin Juravleacf735c2015-02-12 15:25:22 +00005852 ReferenceTypeInfo loaded_class_rti_;
5853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005854 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5855};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005856std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5857
5858// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005860 // The special input is used for PC-relative loads on some architectures,
5861 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005862 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005863 GetLoadKind() == LoadKind::kBootImageAddress ||
5864 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865 DCHECK(special_input_.GetInstruction() == nullptr);
5866 special_input_ = HUserRecord<HInstruction*>(special_input);
5867 special_input->AddUseAt(this, 0);
5868}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005869
Vladimir Marko372f10e2016-05-17 16:30:10 +01005870class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005871 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 // Determines how to load the String.
5873 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005875 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 kBootImageLinkTimePcRelative,
5877
5878 // Use a known boot image String* address, embedded in the code by the codegen.
5879 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880 kBootImageAddress,
5881
Vladimir Markoaad75c62016-10-03 08:46:48 +00005882 // Load from an entry in the .bss section using a PC-relative load.
5883 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5884 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005886 // Load from the root table associated with the JIT compiled method.
5887 kJitTableAddress,
5888
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005889 // Load using a simple runtime call. This is the fall-back load kind when
5890 // the codegen is unable to use another appropriate kind.
5891 kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005892
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005893 kLast = kRuntimeCall,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 };
5895
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005896 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005897 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898 const DexFile& dex_file,
5899 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005900 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5901 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 string_index_(string_index),
5903 dex_file_(dex_file) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005904 SetPackedField<LoadKindField>(LoadKind::kRuntimeCall);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005905 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005906
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005907 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 LoadKind GetLoadKind() const {
5910 return GetPackedField<LoadKindField>();
5911 }
5912
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005913 const DexFile& GetDexFile() const {
5914 return dex_file_;
5915 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005917 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005918 return string_index_;
5919 }
5920
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005921 Handle<mirror::String> GetString() const {
5922 return string_;
5923 }
5924
5925 void SetString(Handle<mirror::String> str) {
5926 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 }
5928
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005929 bool CanBeMoved() const OVERRIDE { return true; }
5930
Vladimir Marko372f10e2016-05-17 16:30:10 +01005931 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005932
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005933 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005934
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 // Will call the runtime if we need to load the string through
5936 // the dex cache and the string is not guaranteed to be there yet.
5937 bool NeedsEnvironment() const OVERRIDE {
5938 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005939 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005940 load_kind == LoadKind::kBootImageAddress ||
5941 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 return false;
5943 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005944 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005945 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005946
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005948 return GetLoadKind() == LoadKind::kRuntimeCall;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005950
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005951 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005953
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005954 static SideEffects SideEffectsForArchRuntimeCalls() {
5955 return SideEffects::CanTriggerGC();
5956 }
5957
Vladimir Marko372f10e2016-05-17 16:30:10 +01005958 void AddSpecialInput(HInstruction* special_input);
5959
5960 using HInstruction::GetInputRecords; // Keep the const version visible.
5961 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5962 return ArrayRef<HUserRecord<HInstruction*>>(
5963 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005964 }
5965
Vladimir Marko372f10e2016-05-17 16:30:10 +01005966 Primitive::Type GetType() const OVERRIDE {
5967 return Primitive::kPrimNot;
5968 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005970 DECLARE_INSTRUCTION(LoadString);
5971
5972 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005973 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005974 static constexpr size_t kFieldLoadKindSize =
5975 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5976 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005977 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005978 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005979
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 void SetLoadKindInternal(LoadKind load_kind);
5981
Vladimir Marko847e6ce2017-06-02 13:55:07 +01005982 // The special input is the HCurrentMethod for kRuntimeCall.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005983 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005984 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005985 HUserRecord<HInstruction*> special_input_;
5986
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005987 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005988 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005989
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005990 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005991
5992 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5993};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005994std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5995
5996// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005998 // The special input is used for PC-relative loads on some architectures,
5999 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006000 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006001 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07006002 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006003 // HLoadString::GetInputRecords() returns an empty array at this point,
6004 // so use the GetInputRecords() from the base class to set the input record.
6005 DCHECK(special_input_.GetInstruction() == nullptr);
6006 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006007 special_input->AddUseAt(this, 0);
6008}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006009
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010/**
6011 * Performs an initialization check on its Class object input.
6012 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006013class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006015 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006016 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006017 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006018 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6019 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006020 SetRawInputAt(0, constant);
6021 }
6022
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006023 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006024 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006025 return true;
6026 }
6027
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006028 bool NeedsEnvironment() const OVERRIDE {
6029 // May call runtime to initialize the class.
6030 return true;
6031 }
6032
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006033 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006034
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006035 HLoadClass* GetLoadClass() const {
6036 DCHECK(InputAt(0)->IsLoadClass());
6037 return InputAt(0)->AsLoadClass();
6038 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006039
6040 DECLARE_INSTRUCTION(ClinitCheck);
6041
6042 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006043 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6044};
6045
Vladimir Markofcb503c2016-05-18 12:48:17 +01006046class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006047 public:
6048 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006049 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006050 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006051 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006052 bool is_volatile,
6053 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006054 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006055 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006056 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006057 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006058 field_info_(field,
6059 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006060 field_type,
6061 is_volatile,
6062 field_idx,
6063 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006064 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065 SetRawInputAt(0, cls);
6066 }
6067
Calin Juravle52c48962014-12-16 17:02:57 +00006068
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006069 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006070
Vladimir Marko372f10e2016-05-17 16:30:10 +01006071 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6072 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006073 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074 }
6075
6076 size_t ComputeHashCode() const OVERRIDE {
6077 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6078 }
6079
Calin Juravle52c48962014-12-16 17:02:57 +00006080 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6082 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006083 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006084
6085 DECLARE_INSTRUCTION(StaticFieldGet);
6086
6087 private:
6088 const FieldInfo field_info_;
6089
6090 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6091};
6092
Vladimir Markofcb503c2016-05-18 12:48:17 +01006093class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094 public:
6095 HStaticFieldSet(HInstruction* cls,
6096 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006097 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006099 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006100 bool is_volatile,
6101 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006102 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006103 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006104 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006105 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006106 field_info_(field,
6107 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006108 field_type,
6109 is_volatile,
6110 field_idx,
6111 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006112 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006113 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006114 SetRawInputAt(0, cls);
6115 SetRawInputAt(1, value);
6116 }
6117
Calin Juravle52c48962014-12-16 17:02:57 +00006118 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006119 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6120 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006121 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006122
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006123 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006124 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6125 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006126
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006127 DECLARE_INSTRUCTION(StaticFieldSet);
6128
6129 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006130 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6131 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6132 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6133 "Too many packed fields.");
6134
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006135 const FieldInfo field_info_;
6136
6137 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6138};
6139
Vladimir Markofcb503c2016-05-18 12:48:17 +01006140class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006141 public:
6142 HUnresolvedInstanceFieldGet(HInstruction* obj,
6143 Primitive::Type field_type,
6144 uint32_t field_index,
6145 uint32_t dex_pc)
6146 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6147 field_index_(field_index) {
6148 SetRawInputAt(0, obj);
6149 }
6150
6151 bool NeedsEnvironment() const OVERRIDE { return true; }
6152 bool CanThrow() const OVERRIDE { return true; }
6153
6154 Primitive::Type GetFieldType() const { return GetType(); }
6155 uint32_t GetFieldIndex() const { return field_index_; }
6156
6157 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6158
6159 private:
6160 const uint32_t field_index_;
6161
6162 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6163};
6164
Vladimir Markofcb503c2016-05-18 12:48:17 +01006165class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006166 public:
6167 HUnresolvedInstanceFieldSet(HInstruction* obj,
6168 HInstruction* value,
6169 Primitive::Type field_type,
6170 uint32_t field_index,
6171 uint32_t dex_pc)
6172 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006173 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006175 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006176 SetRawInputAt(0, obj);
6177 SetRawInputAt(1, value);
6178 }
6179
6180 bool NeedsEnvironment() const OVERRIDE { return true; }
6181 bool CanThrow() const OVERRIDE { return true; }
6182
Vladimir Markoa1de9182016-02-25 11:37:38 +00006183 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006184 uint32_t GetFieldIndex() const { return field_index_; }
6185
6186 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6187
6188 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006189 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6190 static constexpr size_t kFieldFieldTypeSize =
6191 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6192 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6193 kFieldFieldType + kFieldFieldTypeSize;
6194 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6195 "Too many packed fields.");
6196 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6197
Calin Juravlee460d1d2015-09-29 04:52:17 +01006198 const uint32_t field_index_;
6199
6200 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6201};
6202
Vladimir Markofcb503c2016-05-18 12:48:17 +01006203class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006204 public:
6205 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6206 uint32_t field_index,
6207 uint32_t dex_pc)
6208 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6209 field_index_(field_index) {
6210 }
6211
6212 bool NeedsEnvironment() const OVERRIDE { return true; }
6213 bool CanThrow() const OVERRIDE { return true; }
6214
6215 Primitive::Type GetFieldType() const { return GetType(); }
6216 uint32_t GetFieldIndex() const { return field_index_; }
6217
6218 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6219
6220 private:
6221 const uint32_t field_index_;
6222
6223 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6224};
6225
Vladimir Markofcb503c2016-05-18 12:48:17 +01006226class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006227 public:
6228 HUnresolvedStaticFieldSet(HInstruction* value,
6229 Primitive::Type field_type,
6230 uint32_t field_index,
6231 uint32_t dex_pc)
6232 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006233 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006234 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006235 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006236 SetRawInputAt(0, value);
6237 }
6238
6239 bool NeedsEnvironment() const OVERRIDE { return true; }
6240 bool CanThrow() const OVERRIDE { return true; }
6241
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006243 uint32_t GetFieldIndex() const { return field_index_; }
6244
6245 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6246
6247 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006248 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6249 static constexpr size_t kFieldFieldTypeSize =
6250 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6251 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6252 kFieldFieldType + kFieldFieldTypeSize;
6253 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6254 "Too many packed fields.");
6255 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6256
Calin Juravlee460d1d2015-09-29 04:52:17 +01006257 const uint32_t field_index_;
6258
6259 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6260};
6261
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006262// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006263class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006264 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006265 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6266 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006267
David Brazdilbbd733e2015-08-18 17:48:17 +01006268 bool CanBeNull() const OVERRIDE { return false; }
6269
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006270 DECLARE_INSTRUCTION(LoadException);
6271
6272 private:
6273 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6274};
6275
David Brazdilcb1c0552015-08-04 16:22:25 +01006276// Implicit part of move-exception which clears thread-local exception storage.
6277// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006278class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006279 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006280 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6281 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006282
6283 DECLARE_INSTRUCTION(ClearException);
6284
6285 private:
6286 DISALLOW_COPY_AND_ASSIGN(HClearException);
6287};
6288
Vladimir Markofcb503c2016-05-18 12:48:17 +01006289class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006290 public:
6291 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006292 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006293 SetRawInputAt(0, exception);
6294 }
6295
6296 bool IsControlFlow() const OVERRIDE { return true; }
6297
6298 bool NeedsEnvironment() const OVERRIDE { return true; }
6299
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006300 bool CanThrow() const OVERRIDE { return true; }
6301
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006302
6303 DECLARE_INSTRUCTION(Throw);
6304
6305 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006306 DISALLOW_COPY_AND_ASSIGN(HThrow);
6307};
6308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309/**
6310 * Implementation strategies for the code generator of a HInstanceOf
6311 * or `HCheckCast`.
6312 */
6313enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006314 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006315 kExactCheck, // Can do a single class compare.
6316 kClassHierarchyCheck, // Can just walk the super class chain.
6317 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6318 kInterfaceCheck, // No optimization yet when checking against an interface.
6319 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006320 kArrayCheck, // No optimization yet when checking against a generic array.
6321 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322};
6323
Roland Levillain86503782016-02-11 19:07:30 +00006324std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6325
Vladimir Markofcb503c2016-05-18 12:48:17 +01006326class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006327 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006328 HInstanceOf(HInstruction* object,
6329 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006331 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006332 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006334 dex_pc) {
6335 SetPackedField<TypeCheckKindField>(check_kind);
6336 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337 SetRawInputAt(0, object);
6338 SetRawInputAt(1, constant);
6339 }
6340
6341 bool CanBeMoved() const OVERRIDE { return true; }
6342
Vladimir Marko372f10e2016-05-17 16:30:10 +01006343 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006344 return true;
6345 }
6346
6347 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006348 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006349 }
6350
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006351 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006352 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6353 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6354 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6355 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006356
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006357 static bool CanCallRuntime(TypeCheckKind check_kind) {
6358 // Mips currently does runtime calls for any other checks.
6359 return check_kind != TypeCheckKind::kExactCheck;
6360 }
6361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006362 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006363 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006364 }
6365
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006366 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006367
6368 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006369 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6370 static constexpr size_t kFieldTypeCheckKindSize =
6371 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6372 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6373 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6374 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6375 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006376
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006377 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6378};
6379
Vladimir Markofcb503c2016-05-18 12:48:17 +01006380class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006381 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006382 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006383 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6385 SetPackedFlag<kFlagUpperCanBeNull>(true);
6386 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006387 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006388 SetRawInputAt(0, input);
6389 }
6390
David Brazdilf5552582015-12-27 13:36:12 +00006391 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006392 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006393 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006394 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006395
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006396 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006397 DCHECK(GetUpperCanBeNull() || !can_be_null);
6398 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006399 }
6400
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006402
Calin Juravleb1498f62015-02-16 13:13:29 +00006403 DECLARE_INSTRUCTION(BoundType);
6404
6405 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006406 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6407 // is false then CanBeNull() cannot be true).
6408 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6409 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6410 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6411 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6412
Calin Juravleb1498f62015-02-16 13:13:29 +00006413 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006414 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6415 // It is used to bound the type in cases like:
6416 // if (x instanceof ClassX) {
6417 // // uper_bound_ will be ClassX
6418 // }
David Brazdilf5552582015-12-27 13:36:12 +00006419 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006420
6421 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6422};
6423
Vladimir Markofcb503c2016-05-18 12:48:17 +01006424class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006425 public:
6426 HCheckCast(HInstruction* object,
6427 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006428 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006429 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6431 SetPackedField<TypeCheckKindField>(check_kind);
6432 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006433 SetRawInputAt(0, object);
6434 SetRawInputAt(1, constant);
6435 }
6436
6437 bool CanBeMoved() const OVERRIDE { return true; }
6438
Vladimir Marko372f10e2016-05-17 16:30:10 +01006439 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006440 return true;
6441 }
6442
6443 bool NeedsEnvironment() const OVERRIDE {
6444 // Instruction may throw a CheckCastError.
6445 return true;
6446 }
6447
6448 bool CanThrow() const OVERRIDE { return true; }
6449
Vladimir Markoa1de9182016-02-25 11:37:38 +00006450 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6451 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6452 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6453 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006454
6455 DECLARE_INSTRUCTION(CheckCast);
6456
6457 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6459 static constexpr size_t kFieldTypeCheckKindSize =
6460 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6461 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6462 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6463 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6464 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006465
6466 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006467};
6468
Andreas Gampe26de38b2016-07-27 17:53:11 -07006469/**
6470 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6471 * @details We define the combined barrier types that are actually required
6472 * by the Java Memory Model, rather than using exactly the terminology from
6473 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6474 * primitives. Note that the JSR-133 cookbook generally does not deal with
6475 * store atomicity issues, and the recipes there are not always entirely sufficient.
6476 * The current recipe is as follows:
6477 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6478 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6479 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6480 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6481 * class has final fields.
6482 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6483 * store-to-memory instructions. Only generated together with non-temporal stores.
6484 */
6485enum MemBarrierKind {
6486 kAnyStore,
6487 kLoadAny,
6488 kStoreStore,
6489 kAnyAny,
6490 kNTStoreStore,
6491 kLastBarrierKind = kNTStoreStore
6492};
6493std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6494
Vladimir Markofcb503c2016-05-18 12:48:17 +01006495class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006497 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006498 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006499 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6500 SetPackedField<BarrierKindField>(barrier_kind);
6501 }
Calin Juravle27df7582015-04-17 19:12:31 +01006502
Vladimir Markoa1de9182016-02-25 11:37:38 +00006503 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006504
6505 DECLARE_INSTRUCTION(MemoryBarrier);
6506
6507 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006508 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6509 static constexpr size_t kFieldBarrierKindSize =
6510 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6511 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6512 kFieldBarrierKind + kFieldBarrierKindSize;
6513 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6514 "Too many packed fields.");
6515 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006516
6517 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6518};
6519
Igor Murashkind01745e2017-04-05 16:40:31 -07006520// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6521// the specified object(s), with respect to any subsequent store that might "publish"
6522// (i.e. make visible) the specified object to another thread.
6523//
6524// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6525// for all final fields (that were set) at the end of the invoked constructor.
6526//
6527// The constructor fence models the freeze actions for the final fields of an object
6528// being constructed (semantically at the end of the constructor). Constructor fences
6529// have a per-object affinity; two separate objects being constructed get two separate
6530// constructor fences.
6531//
6532// (Note: that if calling a super-constructor or forwarding to another constructor,
6533// the freezes would happen at the end of *that* constructor being invoked).
6534//
6535// The memory model guarantees that when the object being constructed is "published" after
6536// constructor completion (i.e. escapes the current thread via a store), then any final field
6537// writes must be observable on other threads (once they observe that publication).
6538//
6539// Further, anything written before the freeze, and read by dereferencing through the final field,
6540// must also be visible (so final object field could itself have an object with non-final fields;
6541// yet the freeze must also extend to them).
6542//
6543// Constructor example:
6544//
6545// class HasFinal {
6546// final int field; Optimizing IR for <init>()V:
6547// HasFinal() {
6548// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6549// // freeze(this.field); HConstructorFence(this)
6550// } HReturn
6551// }
6552//
6553// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6554// already-initialized classes; in that case the allocation must act as a "default-initializer"
6555// of the object which effectively writes the class pointer "final field".
6556//
6557// For example, we can model default-initialiation as roughly the equivalent of the following:
6558//
6559// class Object {
6560// private final Class header;
6561// }
6562//
6563// Java code: Optimizing IR:
6564//
6565// T new_instance<T>() {
6566// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6567// obj.header = T.class; // header write is done by above call.
6568// // freeze(obj.header) HConstructorFence(obj)
6569// return (T)obj;
6570// }
6571//
6572// See also:
6573// * CompilerDriver::RequiresConstructorBarrier
6574// * QuasiAtomic::ThreadFenceForConstructor
6575//
6576class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6577 // A fence has variable inputs because the inputs can be removed
6578 // after prepare_for_register_allocation phase.
6579 // (TODO: In the future a fence could freeze multiple objects
6580 // after merging two fences together.)
6581 public:
6582 // `fence_object` is the reference that needs to be protected for correct publication.
6583 //
6584 // It makes sense in the following situations:
6585 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6586 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6587 //
6588 // After construction the `fence_object` becomes the 0th input.
6589 // This is not an input in a real sense, but just a convenient place to stash the information
6590 // about the associated object.
6591 HConstructorFence(HInstruction* fence_object,
6592 uint32_t dex_pc,
6593 ArenaAllocator* arena)
6594 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6595 // constraints described in the class header. We claim that these SideEffects constraints
6596 // enforce a superset of the real constraints.
6597 //
6598 // The ordering described above is conservatively modeled with SideEffects as follows:
6599 //
6600 // * To prevent reordering of the publication stores:
6601 // ----> "Reads of objects" is the initial SideEffect.
6602 // * For every primitive final field store in the constructor:
6603 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6604 // * If there are any stores to reference final fields in the constructor:
6605 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6606 // that are reachable from `fence_object` also need to be prevented for reordering
6607 // (and we do not want to do alias analysis to figure out what those stores are).
6608 //
6609 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6610 // even more conservative approach than the one described above, and prevents all of the
6611 // above reordering without analyzing any of the instructions in the constructor.
6612 //
6613 // If in a later phase we discover that there are no writes to reference final fields,
6614 // we can refine the side effect to a smaller set of type reads (see above constraints).
6615 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6616 dex_pc,
6617 arena,
6618 /* number_of_inputs */ 1,
6619 kArenaAllocConstructorFenceInputs) {
6620 DCHECK(fence_object != nullptr);
6621 SetRawInputAt(0, fence_object);
6622 }
6623
6624 // The object associated with this constructor fence.
6625 //
6626 // (Note: This will be null after the prepare_for_register_allocation phase,
6627 // as all constructor fence inputs are removed there).
6628 HInstruction* GetFenceObject() const {
6629 return InputAt(0);
6630 }
6631
6632 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6633 // - Delete `fence_use` from `this`'s use list.
6634 // - Delete `this` from `fence_use`'s inputs list.
6635 // - If the `fence_use` is dead, remove it from the graph.
6636 //
6637 // A fence is considered dead once it no longer has any uses
6638 // and all of the inputs are dead.
6639 //
6640 // This must *not* be called during/after prepare_for_register_allocation,
6641 // because that removes all the inputs to the fences but the fence is actually
6642 // still considered live.
6643 static void RemoveConstructorFences(HInstruction* instruction);
6644
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006645 // Check if this constructor fence is protecting
6646 // an HNewInstance or HNewArray that is also the immediate
6647 // predecessor of `this`.
6648 //
6649 // Returns the associated HNewArray or HNewInstance,
6650 // or null otherwise.
6651 HInstruction* GetAssociatedAllocation();
6652
Igor Murashkind01745e2017-04-05 16:40:31 -07006653 DECLARE_INSTRUCTION(ConstructorFence);
6654
6655 private:
6656 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6657};
6658
Vladimir Markofcb503c2016-05-18 12:48:17 +01006659class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006660 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006661 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006662 kEnter,
6663 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006664 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006665 };
6666
6667 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006668 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006669 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6670 dex_pc) {
6671 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006672 SetRawInputAt(0, object);
6673 }
6674
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006675 // Instruction may go into runtime, so we need an environment.
6676 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006677
6678 bool CanThrow() const OVERRIDE {
6679 // Verifier guarantees that monitor-exit cannot throw.
6680 // This is important because it allows the HGraphBuilder to remove
6681 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6682 return IsEnter();
6683 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006684
Vladimir Markoa1de9182016-02-25 11:37:38 +00006685 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6686 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006687
6688 DECLARE_INSTRUCTION(MonitorOperation);
6689
Calin Juravle52c48962014-12-16 17:02:57 +00006690 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006691 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6692 static constexpr size_t kFieldOperationKindSize =
6693 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6694 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6695 kFieldOperationKind + kFieldOperationKindSize;
6696 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6697 "Too many packed fields.");
6698 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006699
6700 private:
6701 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6702};
6703
Vladimir Markofcb503c2016-05-18 12:48:17 +01006704class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006705 public:
6706 HSelect(HInstruction* condition,
6707 HInstruction* true_value,
6708 HInstruction* false_value,
6709 uint32_t dex_pc)
6710 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6711 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6712
6713 // First input must be `true_value` or `false_value` to allow codegens to
6714 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6715 // that architectures which implement HSelect as a conditional move also
6716 // will not need to invert the condition.
6717 SetRawInputAt(0, false_value);
6718 SetRawInputAt(1, true_value);
6719 SetRawInputAt(2, condition);
6720 }
6721
6722 HInstruction* GetFalseValue() const { return InputAt(0); }
6723 HInstruction* GetTrueValue() const { return InputAt(1); }
6724 HInstruction* GetCondition() const { return InputAt(2); }
6725
6726 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006727 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6728 return true;
6729 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006730
6731 bool CanBeNull() const OVERRIDE {
6732 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6733 }
6734
6735 DECLARE_INSTRUCTION(Select);
6736
6737 private:
6738 DISALLOW_COPY_AND_ASSIGN(HSelect);
6739};
6740
Vladimir Markof9f64412015-09-02 14:05:49 +01006741class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006742 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006743 MoveOperands(Location source,
6744 Location destination,
6745 Primitive::Type type,
6746 HInstruction* instruction)
6747 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006748
6749 Location GetSource() const { return source_; }
6750 Location GetDestination() const { return destination_; }
6751
6752 void SetSource(Location value) { source_ = value; }
6753 void SetDestination(Location value) { destination_ = value; }
6754
6755 // The parallel move resolver marks moves as "in-progress" by clearing the
6756 // destination (but not the source).
6757 Location MarkPending() {
6758 DCHECK(!IsPending());
6759 Location dest = destination_;
6760 destination_ = Location::NoLocation();
6761 return dest;
6762 }
6763
6764 void ClearPending(Location dest) {
6765 DCHECK(IsPending());
6766 destination_ = dest;
6767 }
6768
6769 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006770 DCHECK(source_.IsValid() || destination_.IsInvalid());
6771 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006772 }
6773
6774 // True if this blocks a move from the given location.
6775 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006776 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006777 }
6778
6779 // A move is redundant if it's been eliminated, if its source and
6780 // destination are the same, or if its destination is unneeded.
6781 bool IsRedundant() const {
6782 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6783 }
6784
6785 // We clear both operands to indicate move that's been eliminated.
6786 void Eliminate() {
6787 source_ = destination_ = Location::NoLocation();
6788 }
6789
6790 bool IsEliminated() const {
6791 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6792 return source_.IsInvalid();
6793 }
6794
Alexey Frunze4dda3372015-06-01 18:31:49 -07006795 Primitive::Type GetType() const { return type_; }
6796
Nicolas Geoffray90218252015-04-15 11:56:51 +01006797 bool Is64BitMove() const {
6798 return Primitive::Is64BitType(type_);
6799 }
6800
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006801 HInstruction* GetInstruction() const { return instruction_; }
6802
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006803 private:
6804 Location source_;
6805 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006806 // The type this move is for.
6807 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006808 // The instruction this move is assocatied with. Null when this move is
6809 // for moving an input in the expected locations of user (including a phi user).
6810 // This is only used in debug mode, to ensure we do not connect interval siblings
6811 // in the same parallel move.
6812 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006813};
6814
Roland Levillainc9285912015-12-18 10:38:42 +00006815std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6816
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006817static constexpr size_t kDefaultNumberOfMoves = 4;
6818
Vladimir Markofcb503c2016-05-18 12:48:17 +01006819class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006821 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006822 : HTemplateInstruction(SideEffects::None(), dex_pc),
6823 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6824 moves_.reserve(kDefaultNumberOfMoves);
6825 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006826
Nicolas Geoffray90218252015-04-15 11:56:51 +01006827 void AddMove(Location source,
6828 Location destination,
6829 Primitive::Type type,
6830 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006831 DCHECK(source.IsValid());
6832 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006833 if (kIsDebugBuild) {
6834 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006835 for (const MoveOperands& move : moves_) {
6836 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006837 // Special case the situation where the move is for the spill slot
6838 // of the instruction.
6839 if ((GetPrevious() == instruction)
6840 || ((GetPrevious() == nullptr)
6841 && instruction->IsPhi()
6842 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006843 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006844 << "Doing parallel moves for the same instruction.";
6845 } else {
6846 DCHECK(false) << "Doing parallel moves for the same instruction.";
6847 }
6848 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006849 }
6850 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006851 for (const MoveOperands& move : moves_) {
6852 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006853 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006854 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006855 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006856 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006857 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006858 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006859 }
6860
Vladimir Marko225b6462015-09-28 12:17:40 +01006861 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006862 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006863 }
6864
Vladimir Marko225b6462015-09-28 12:17:40 +01006865 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006867 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006868
6869 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006870 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006871
6872 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6873};
6874
Mark Mendell0616ae02015-04-17 12:49:27 -04006875} // namespace art
6876
Aart Bikf8f5a162017-02-06 15:35:29 -08006877#include "nodes_vector.h"
6878
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006879#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6880#include "nodes_shared.h"
6881#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006882#ifdef ART_ENABLE_CODEGEN_arm
6883#include "nodes_arm.h"
6884#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006885#ifdef ART_ENABLE_CODEGEN_mips
6886#include "nodes_mips.h"
6887#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006888#ifdef ART_ENABLE_CODEGEN_x86
6889#include "nodes_x86.h"
6890#endif
6891
6892namespace art {
6893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006894class HGraphVisitor : public ValueObject {
6895 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006896 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6897 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006899 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006900 virtual void VisitBasicBlock(HBasicBlock* block);
6901
Roland Levillain633021e2014-10-01 14:12:25 +01006902 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006903 void VisitInsertionOrder();
6904
Roland Levillain633021e2014-10-01 14:12:25 +01006905 // Visit the graph following dominator tree reverse post-order.
6906 void VisitReversePostOrder();
6907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006908 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006909
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006910 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006911#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006912 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6913
6914 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6915
6916#undef DECLARE_VISIT_INSTRUCTION
6917
6918 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006919 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006920
6921 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6922};
6923
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006924class HGraphDelegateVisitor : public HGraphVisitor {
6925 public:
6926 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6927 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
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007034#define INSTRUCTION_TYPE_CHECK(type, super) \
7035 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7036 inline const H##type* HInstruction::As##type() const { \
7037 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7038 } \
7039 inline H##type* HInstruction::As##type() { \
7040 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7041 }
7042
7043 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7044#undef INSTRUCTION_TYPE_CHECK
7045
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007046// Create space in `blocks` for adding `number_of_new_blocks` entries
7047// starting at location `at`. Blocks after `at` are moved accordingly.
7048inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7049 size_t number_of_new_blocks,
7050 size_t after) {
7051 DCHECK_LT(after, blocks->size());
7052 size_t old_size = blocks->size();
7053 size_t new_size = old_size + number_of_new_blocks;
7054 blocks->resize(new_size);
7055 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7056}
7057
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007058/*
7059 * Hunt "under the hood" of array lengths (leading to array references),
7060 * null checks (also leading to array references), and new arrays
7061 * (leading to the actual length). This makes it more likely related
7062 * instructions become actually comparable.
7063 */
7064inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7065 while (instruction->IsArrayLength() ||
7066 instruction->IsNullCheck() ||
7067 instruction->IsNewArray()) {
7068 instruction = instruction->IsNewArray()
7069 ? instruction->AsNewArray()->GetLength()
7070 : instruction->InputAt(0);
7071 }
7072 return instruction;
7073}
7074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007075} // namespace art
7076
7077#endif // ART_COMPILER_OPTIMIZING_NODES_H_