blob: 72774da1d1631701c71e4faae7470f43cf2f1297 [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)),
1793 locations_(number_of_vregs, 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
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001807 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001808 if (parent_ != nullptr) {
1809 parent_->SetAndCopyParentChain(allocator, parent);
1810 } else {
1811 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1812 parent_->CopyFrom(parent);
1813 if (parent->GetParent() != nullptr) {
1814 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1815 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001816 }
David Brazdiled596192015-01-23 10:39:45 +00001817 }
1818
Vladimir Marko71bf8092015-09-15 15:33:14 +01001819 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001820 void CopyFrom(HEnvironment* environment);
1821
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001822 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1823 // input to the loop phi instead. This is for inserting instructions that
1824 // require an environment (like HDeoptimization) in the loop pre-header.
1825 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001826
1827 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001829 }
1830
1831 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001832 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001833 }
1834
David Brazdil1abb4192015-02-17 18:33:36 +00001835 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001836
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001837 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001838
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001839 HEnvironment* GetParent() const { return parent_; }
1840
1841 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001842 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001843 }
1844
1845 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001846 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001847 }
1848
1849 uint32_t GetDexPc() const {
1850 return dex_pc_;
1851 }
1852
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001853 ArtMethod* GetMethod() const {
1854 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001855 }
1856
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001857 HInstruction* GetHolder() const {
1858 return holder_;
1859 }
1860
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001861
1862 bool IsFromInlinedInvoke() const {
1863 return GetParent() != nullptr;
1864 }
1865
David Brazdiled596192015-01-23 10:39:45 +00001866 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001867 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1868 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001869 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001870 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001871 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001872
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001873 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001874 HInstruction* const holder_;
1875
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001876 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001877
1878 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1879};
1880
Vladimir Markof9f64412015-09-02 14:05:49 +01001881class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001882 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001883 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001884 : previous_(nullptr),
1885 next_(nullptr),
1886 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001887 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001888 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001889 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001890 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001891 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001892 locations_(nullptr),
1893 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001894 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001895 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001896 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1897 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1898 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899
Dave Allison20dfc792014-06-16 20:44:29 -07001900 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001902#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001903 enum InstructionKind {
1904 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1905 };
1906#undef DECLARE_KIND
1907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001908 HInstruction* GetNext() const { return next_; }
1909 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001910
Calin Juravle77520bc2015-01-12 18:45:46 +00001911 HInstruction* GetNextDisregardingMoves() const;
1912 HInstruction* GetPreviousDisregardingMoves() const;
1913
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001915 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001916 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001917 bool IsInBlock() const { return block_ != nullptr; }
1918 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001919 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1920 bool IsIrreducibleLoopHeaderPhi() const {
1921 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1922 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001923
Vladimir Marko372f10e2016-05-17 16:30:10 +01001924 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1925
1926 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1927 // One virtual method is enough, just const_cast<> and then re-add the const.
1928 return ArrayRef<const HUserRecord<HInstruction*>>(
1929 const_cast<HInstruction*>(this)->GetInputRecords());
1930 }
1931
Vladimir Markoe9004912016-06-16 16:50:52 +01001932 HInputsRef GetInputs() {
1933 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001934 }
1935
Vladimir Markoe9004912016-06-16 16:50:52 +01001936 HConstInputsRef GetInputs() const {
1937 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001938 }
1939
1940 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001941 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942
Aart Bik2767f4b2016-10-28 15:03:53 -07001943 bool HasInput(HInstruction* input) const {
1944 for (const HInstruction* i : GetInputs()) {
1945 if (i == input) {
1946 return true;
1947 }
1948 }
1949 return false;
1950 }
1951
Vladimir Marko372f10e2016-05-17 16:30:10 +01001952 void SetRawInputAt(size_t index, HInstruction* input) {
1953 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1954 }
1955
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001956 virtual void Accept(HGraphVisitor* visitor) = 0;
1957 virtual const char* DebugName() const = 0;
1958
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1960
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001962
1963 uint32_t GetDexPc() const { return dex_pc_; }
1964
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001965 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001966
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001967 // Can the instruction throw?
1968 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1969 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001970 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001971 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001972
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001973 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001974 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001975
Calin Juravle10e244f2015-01-26 18:54:32 +00001976 // Does not apply for all instructions, but having this at top level greatly
1977 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001978 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001979 virtual bool CanBeNull() const {
1980 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1981 return true;
1982 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001983
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001984 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001985 return false;
1986 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001987
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001988 virtual bool IsActualObject() const {
1989 return GetType() == Primitive::kPrimNot;
1990 }
1991
Calin Juravle2e768302015-07-28 14:41:11 +00001992 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001993
Calin Juravle61d544b2015-02-23 16:46:57 +00001994 ReferenceTypeInfo GetReferenceTypeInfo() const {
1995 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001996 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001997 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001998 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00002001 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002002 // Note: fixup_end remains valid across push_front().
2003 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2004 HUseListNode<HInstruction*>* new_node =
2005 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2006 uses_.push_front(*new_node);
2007 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002008 }
2009
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002010 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002011 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002012 // Note: env_fixup_end remains valid across push_front().
2013 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2014 HUseListNode<HEnvironment*>* new_node =
2015 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2016 env_uses_.push_front(*new_node);
2017 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002018 }
2019
David Brazdil1abb4192015-02-17 18:33:36 +00002020 void RemoveAsUserOfInput(size_t input) {
2021 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002022 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2023 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2024 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002025 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002026
Vladimir Marko372f10e2016-05-17 16:30:10 +01002027 void RemoveAsUserOfAllInputs() {
2028 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2029 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2030 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2031 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2032 }
2033 }
2034
David Brazdil1abb4192015-02-17 18:33:36 +00002035 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2036 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002037
Vladimir Marko46817b82016-03-29 12:21:58 +01002038 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2039 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2040 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002041 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002042 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002043 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002044
Aart Bikcc42be02016-10-20 16:14:16 -07002045 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002046 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002047 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002048 !CanThrow() &&
2049 !IsSuspendCheck() &&
2050 !IsControlFlow() &&
2051 !IsNativeDebugInfo() &&
2052 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002053 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002054 !IsMemoryBarrier() &&
2055 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002056 }
2057
Aart Bikcc42be02016-10-20 16:14:16 -07002058 bool IsDeadAndRemovable() const {
2059 return IsRemovable() && !HasUses();
2060 }
2061
Roland Levillain6c82d402014-10-13 16:10:27 +01002062 // Does this instruction strictly dominate `other_instruction`?
2063 // Returns false if this instruction and `other_instruction` are the same.
2064 // Aborts if this instruction and `other_instruction` are both phis.
2065 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002067 int GetId() const { return id_; }
2068 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002069
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002070 int GetSsaIndex() const { return ssa_index_; }
2071 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2072 bool HasSsaIndex() const { return ssa_index_ != -1; }
2073
2074 bool HasEnvironment() const { return environment_ != nullptr; }
2075 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002076 // Set the `environment_` field. Raw because this method does not
2077 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002078 void SetRawEnvironment(HEnvironment* environment) {
2079 DCHECK(environment_ == nullptr);
2080 DCHECK_EQ(environment->GetHolder(), this);
2081 environment_ = environment;
2082 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002083
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002084 void InsertRawEnvironment(HEnvironment* environment) {
2085 DCHECK(environment_ != nullptr);
2086 DCHECK_EQ(environment->GetHolder(), this);
2087 DCHECK(environment->GetParent() == nullptr);
2088 environment->parent_ = environment_;
2089 environment_ = environment;
2090 }
2091
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002092 void RemoveEnvironment();
2093
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002094 // Set the environment of this instruction, copying it from `environment`. While
2095 // copying, the uses lists are being updated.
2096 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002097 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002098 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002099 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002100 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002101 if (environment->GetParent() != nullptr) {
2102 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2103 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002104 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002106 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2107 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002108 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002109 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002110 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002111 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002112 if (environment->GetParent() != nullptr) {
2113 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2114 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002115 }
2116
Nicolas Geoffray39468442014-09-02 15:17:15 +01002117 // Returns the number of entries in the environment. Typically, that is the
2118 // number of dex registers in a method. It could be more in case of inlining.
2119 size_t EnvironmentSize() const;
2120
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002121 LocationSummary* GetLocations() const { return locations_; }
2122 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002123
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002124 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002125 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002126 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002127
Alexandre Rames188d4312015-04-09 18:30:21 +01002128 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2129 // uses of this instruction by `other` are *not* updated.
2130 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2131 ReplaceWith(other);
2132 other->ReplaceInput(this, use_index);
2133 }
2134
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002135 // Move `this` instruction before `cursor`
2136 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002137
Vladimir Markofb337ea2015-11-25 15:25:10 +00002138 // Move `this` before its first user and out of any loops. If there is no
2139 // out-of-loop user that dominates all other users, move the instruction
2140 // to the end of the out-of-loop common dominator of the user's blocks.
2141 //
2142 // This can be used only on non-throwing instructions with no side effects that
2143 // have at least one use but no environment uses.
2144 void MoveBeforeFirstUserAndOutOfLoops();
2145
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002146#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002147 bool Is##type() const; \
2148 const H##type* As##type() const; \
2149 H##type* As##type();
2150
2151 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2152#undef INSTRUCTION_TYPE_CHECK
2153
2154#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002155 bool Is##type() const { return (As##type() != nullptr); } \
2156 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002157 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002158 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159#undef INSTRUCTION_TYPE_CHECK
2160
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002162 // TODO: this method is used by LICM and GVN with possibly different
2163 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002164 virtual bool CanBeMoved() const { return false; }
2165
2166 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002167 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002168 return false;
2169 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002170
2171 // Returns whether any data encoded in the two instructions is equal.
2172 // This method does not look at the inputs. Both instructions must be
2173 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002174 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002175 return false;
2176 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002177
2178 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002179 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002181 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002182
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002183 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2184 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2185 // the virtual function because the __attribute__((__pure__)) doesn't really
2186 // apply the strong requirement for virtual functions, preventing optimizations.
2187 InstructionKind GetKind() const PURE;
2188 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002189
2190 virtual size_t ComputeHashCode() const {
2191 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002192 for (const HInstruction* input : GetInputs()) {
2193 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002194 }
2195 return result;
2196 }
2197
2198 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002199 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002200 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002201
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002202 size_t GetLifetimePosition() const { return lifetime_position_; }
2203 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2204 LiveInterval* GetLiveInterval() const { return live_interval_; }
2205 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2206 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2207
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002208 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2209
2210 // Returns whether the code generation of the instruction will require to have access
2211 // to the current method. Such instructions are:
2212 // (1): Instructions that require an environment, as calling the runtime requires
2213 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002214 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2215 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002216 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002217 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002218 }
2219
Vladimir Markodc151b22015-10-15 18:02:30 +01002220 // Returns whether the code generation of the instruction will require to have access
2221 // to the dex cache of the current method's declaring class via the current method.
2222 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002223
Mark Mendellc4701932015-04-10 13:18:51 -04002224 // Does this instruction have any use in an environment before
2225 // control flow hits 'other'?
2226 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2227
2228 // Remove all references to environment uses of this instruction.
2229 // The caller must ensure that this is safe to do.
2230 void RemoveEnvironmentUsers();
2231
Vladimir Markoa1de9182016-02-25 11:37:38 +00002232 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2233 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002234
David Brazdil1abb4192015-02-17 18:33:36 +00002235 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002236 // If set, the machine code for this instruction is assumed to be generated by
2237 // its users. Used by liveness analysis to compute use positions accordingly.
2238 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2239 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2240 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2241 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2242
Vladimir Marko372f10e2016-05-17 16:30:10 +01002243 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2244 return GetInputRecords()[i];
2245 }
2246
2247 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2248 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2249 input_records[index] = input;
2250 }
David Brazdil1abb4192015-02-17 18:33:36 +00002251
Vladimir Markoa1de9182016-02-25 11:37:38 +00002252 uint32_t GetPackedFields() const {
2253 return packed_fields_;
2254 }
2255
2256 template <size_t flag>
2257 bool GetPackedFlag() const {
2258 return (packed_fields_ & (1u << flag)) != 0u;
2259 }
2260
2261 template <size_t flag>
2262 void SetPackedFlag(bool value = true) {
2263 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2264 }
2265
2266 template <typename BitFieldType>
2267 typename BitFieldType::value_type GetPackedField() const {
2268 return BitFieldType::Decode(packed_fields_);
2269 }
2270
2271 template <typename BitFieldType>
2272 void SetPackedField(typename BitFieldType::value_type value) {
2273 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2274 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2275 }
2276
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002278 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2279 auto before_use_node = uses_.before_begin();
2280 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2281 HInstruction* user = use_node->GetUser();
2282 size_t input_index = use_node->GetIndex();
2283 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2284 before_use_node = use_node;
2285 }
2286 }
2287
2288 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2289 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2290 if (next != uses_.end()) {
2291 HInstruction* next_user = next->GetUser();
2292 size_t next_index = next->GetIndex();
2293 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2294 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2295 }
2296 }
2297
2298 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2299 auto before_env_use_node = env_uses_.before_begin();
2300 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2301 HEnvironment* user = env_use_node->GetUser();
2302 size_t input_index = env_use_node->GetIndex();
2303 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2304 before_env_use_node = env_use_node;
2305 }
2306 }
2307
2308 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2309 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2310 if (next != env_uses_.end()) {
2311 HEnvironment* next_user = next->GetUser();
2312 size_t next_index = next->GetIndex();
2313 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2314 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2315 }
2316 }
David Brazdil1abb4192015-02-17 18:33:36 +00002317
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318 HInstruction* previous_;
2319 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002320 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002321 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002322
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002323 // An instruction gets an id when it is added to the graph.
2324 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002325 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002326 int id_;
2327
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002328 // When doing liveness analysis, instructions that have uses get an SSA index.
2329 int ssa_index_;
2330
Vladimir Markoa1de9182016-02-25 11:37:38 +00002331 // Packed fields.
2332 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002333
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002334 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002335 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002336
2337 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002338 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002339
Nicolas Geoffray39468442014-09-02 15:17:15 +01002340 // The environment associated with this instruction. Not null if the instruction
2341 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002342 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002343
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002344 // Set by the code generator.
2345 LocationSummary* locations_;
2346
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002347 // Set by the liveness analysis.
2348 LiveInterval* live_interval_;
2349
2350 // Set by the liveness analysis, this is the position in a linear
2351 // order of blocks where this instruction's live interval start.
2352 size_t lifetime_position_;
2353
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002354 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002355
Vladimir Markoa1de9182016-02-25 11:37:38 +00002356 // The reference handle part of the reference type info.
2357 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002358 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002359 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002360
David Brazdil1abb4192015-02-17 18:33:36 +00002361 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002363 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002364 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002365 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366
2367 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2368};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002369std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002371// Iterates over the instructions, while preserving the next instruction
2372// in case the current instruction gets removed from the list by the user
2373// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002374class HInstructionIterator : public ValueObject {
2375 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002376 explicit HInstructionIterator(const HInstructionList& instructions)
2377 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002378 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002379 }
2380
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002381 bool Done() const { return instruction_ == nullptr; }
2382 HInstruction* Current() const { return instruction_; }
2383 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002385 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002386 }
2387
2388 private:
2389 HInstruction* instruction_;
2390 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002391
2392 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002393};
2394
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002395// Iterates over the instructions without saving the next instruction,
2396// therefore handling changes in the graph potentially made by the user
2397// of this iterator.
2398class HInstructionIteratorHandleChanges : public ValueObject {
2399 public:
2400 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2401 : instruction_(instructions.first_instruction_) {
2402 }
2403
2404 bool Done() const { return instruction_ == nullptr; }
2405 HInstruction* Current() const { return instruction_; }
2406 void Advance() {
2407 instruction_ = instruction_->GetNext();
2408 }
2409
2410 private:
2411 HInstruction* instruction_;
2412
2413 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2414};
2415
2416
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002417class HBackwardInstructionIterator : public ValueObject {
2418 public:
2419 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2420 : instruction_(instructions.last_instruction_) {
2421 next_ = Done() ? nullptr : instruction_->GetPrevious();
2422 }
2423
2424 bool Done() const { return instruction_ == nullptr; }
2425 HInstruction* Current() const { return instruction_; }
2426 void Advance() {
2427 instruction_ = next_;
2428 next_ = Done() ? nullptr : instruction_->GetPrevious();
2429 }
2430
2431 private:
2432 HInstruction* instruction_;
2433 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002434
2435 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002436};
2437
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002438class HVariableInputSizeInstruction : public HInstruction {
2439 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002440 using HInstruction::GetInputRecords; // Keep the const version visible.
2441 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2442 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2443 }
2444
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002445 void AddInput(HInstruction* input);
2446 void InsertInputAt(size_t index, HInstruction* input);
2447 void RemoveInputAt(size_t index);
2448
Igor Murashkind01745e2017-04-05 16:40:31 -07002449 // Removes all the inputs.
2450 // Also removes this instructions from each input's use list
2451 // (for non-environment uses only).
2452 void RemoveAllInputs();
2453
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002454 protected:
2455 HVariableInputSizeInstruction(SideEffects side_effects,
2456 uint32_t dex_pc,
2457 ArenaAllocator* arena,
2458 size_t number_of_inputs,
2459 ArenaAllocKind kind)
2460 : HInstruction(side_effects, dex_pc),
2461 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2462
2463 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2464
2465 private:
2466 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2467};
2468
Vladimir Markof9f64412015-09-02 14:05:49 +01002469template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002470class HTemplateInstruction: public HInstruction {
2471 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002472 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002474 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475
Vladimir Marko372f10e2016-05-17 16:30:10 +01002476 using HInstruction::GetInputRecords; // Keep the const version visible.
2477 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2478 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002479 }
2480
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002482 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002483
2484 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485};
2486
Vladimir Markof9f64412015-09-02 14:05:49 +01002487// HTemplateInstruction specialization for N=0.
2488template<>
2489class HTemplateInstruction<0>: public HInstruction {
2490 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002491 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002492 : HInstruction(side_effects, dex_pc) {}
2493
Vladimir Markof9f64412015-09-02 14:05:49 +01002494 virtual ~HTemplateInstruction() {}
2495
Vladimir Marko372f10e2016-05-17 16:30:10 +01002496 using HInstruction::GetInputRecords; // Keep the const version visible.
2497 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2498 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002499 }
2500
2501 private:
2502 friend class SsaBuilder;
2503};
2504
Dave Allison20dfc792014-06-16 20:44:29 -07002505template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002506class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002507 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002508 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002509 : HTemplateInstruction<N>(side_effects, dex_pc) {
2510 this->template SetPackedField<TypeField>(type);
2511 }
Dave Allison20dfc792014-06-16 20:44:29 -07002512 virtual ~HExpression() {}
2513
Vladimir Markoa1de9182016-02-25 11:37:38 +00002514 Primitive::Type GetType() const OVERRIDE {
2515 return TypeField::Decode(this->GetPackedFields());
2516 }
Dave Allison20dfc792014-06-16 20:44:29 -07002517
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002518 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002519 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2520 static constexpr size_t kFieldTypeSize =
2521 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2522 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2523 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2524 "Too many packed fields.");
2525 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002526};
2527
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002528// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2529// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002530class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002532 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2533 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002534
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002535 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002537 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002538
2539 private:
2540 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2541};
2542
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002543// Represents dex's RETURN opcodes. A HReturn is a control flow
2544// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002545class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002547 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2548 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002549 SetRawInputAt(0, value);
2550 }
2551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002552 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002553
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002554 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002555
2556 private:
2557 DISALLOW_COPY_AND_ASSIGN(HReturn);
2558};
2559
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002560class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002561 public:
2562 HPhi(ArenaAllocator* arena,
2563 uint32_t reg_number,
2564 size_t number_of_inputs,
2565 Primitive::Type type,
2566 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002567 : HVariableInputSizeInstruction(
2568 SideEffects::None(),
2569 dex_pc,
2570 arena,
2571 number_of_inputs,
2572 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002573 reg_number_(reg_number) {
2574 SetPackedField<TypeField>(ToPhiType(type));
2575 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2576 // Phis are constructed live and marked dead if conflicting or unused.
2577 // Individual steps of SsaBuilder should assume that if a phi has been
2578 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2579 SetPackedFlag<kFlagIsLive>(true);
2580 SetPackedFlag<kFlagCanBeNull>(true);
2581 }
2582
2583 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2584 static Primitive::Type ToPhiType(Primitive::Type type) {
2585 return Primitive::PrimitiveKind(type);
2586 }
2587
2588 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2589
David Brazdildee58d62016-04-07 09:54:26 +00002590 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2591 void SetType(Primitive::Type new_type) {
2592 // Make sure that only valid type changes occur. The following are allowed:
2593 // (1) int -> float/ref (primitive type propagation),
2594 // (2) long -> double (primitive type propagation).
2595 DCHECK(GetType() == new_type ||
2596 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2597 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2598 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2599 SetPackedField<TypeField>(new_type);
2600 }
2601
2602 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2603 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2604
2605 uint32_t GetRegNumber() const { return reg_number_; }
2606
2607 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2608 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2609 bool IsDead() const { return !IsLive(); }
2610 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2611
Vladimir Markoe9004912016-06-16 16:50:52 +01002612 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002613 return other != nullptr
2614 && other->IsPhi()
2615 && other->AsPhi()->GetBlock() == GetBlock()
2616 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2617 }
2618
2619 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2620 // An equivalent phi is a phi having the same dex register and type.
2621 // It assumes that phis with the same dex register are adjacent.
2622 HPhi* GetNextEquivalentPhiWithSameType() {
2623 HInstruction* next = GetNext();
2624 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2625 if (next->GetType() == GetType()) {
2626 return next->AsPhi();
2627 }
2628 next = next->GetNext();
2629 }
2630 return nullptr;
2631 }
2632
2633 DECLARE_INSTRUCTION(Phi);
2634
David Brazdildee58d62016-04-07 09:54:26 +00002635 private:
2636 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2637 static constexpr size_t kFieldTypeSize =
2638 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2639 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2640 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2641 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2642 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2643 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2644
David Brazdildee58d62016-04-07 09:54:26 +00002645 const uint32_t reg_number_;
2646
2647 DISALLOW_COPY_AND_ASSIGN(HPhi);
2648};
2649
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002651// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002653class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002655 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002656
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002657 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002659 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660
2661 private:
2662 DISALLOW_COPY_AND_ASSIGN(HExit);
2663};
2664
2665// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002666class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002668 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002669
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002670 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002671
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002672 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002673 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002674 }
2675
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002676 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002677
2678 private:
2679 DISALLOW_COPY_AND_ASSIGN(HGoto);
2680};
2681
Roland Levillain9867bc72015-08-05 10:21:34 +01002682class HConstant : public HExpression<0> {
2683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002684 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2685 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002686
2687 bool CanBeMoved() const OVERRIDE { return true; }
2688
Roland Levillain1a653882016-03-18 18:05:57 +00002689 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002691 // Is this constant 0 in the arithmetic sense?
2692 virtual bool IsArithmeticZero() const { return false; }
2693 // Is this constant a 0-bit pattern?
2694 virtual bool IsZeroBitPattern() const { return false; }
2695 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002696 virtual bool IsOne() const { return false; }
2697
David Brazdil77a48ae2015-09-15 12:34:04 +00002698 virtual uint64_t GetValueAsUint64() const = 0;
2699
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002700 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002701
2702 private:
2703 DISALLOW_COPY_AND_ASSIGN(HConstant);
2704};
2705
Vladimir Markofcb503c2016-05-18 12:48:17 +01002706class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002708 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002709 return true;
2710 }
2711
David Brazdil77a48ae2015-09-15 12:34:04 +00002712 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2713
Roland Levillain9867bc72015-08-05 10:21:34 +01002714 size_t ComputeHashCode() const OVERRIDE { return 0; }
2715
Roland Levillain1a653882016-03-18 18:05:57 +00002716 // The null constant representation is a 0-bit pattern.
2717 virtual bool IsZeroBitPattern() const { return true; }
2718
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 DECLARE_INSTRUCTION(NullConstant);
2720
2721 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002722 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002723
2724 friend class HGraph;
2725 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2726};
2727
2728// Constants of the type int. Those can be from Dex instructions, or
2729// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002730class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 public:
2732 int32_t GetValue() const { return value_; }
2733
David Brazdil9f389d42015-10-01 14:32:56 +01002734 uint64_t GetValueAsUint64() const OVERRIDE {
2735 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2736 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002737
Vladimir Marko372f10e2016-05-17 16:30:10 +01002738 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002739 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002740 return other->AsIntConstant()->value_ == value_;
2741 }
2742
2743 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2744
2745 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002746 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2747 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2749
Roland Levillain1a653882016-03-18 18:05:57 +00002750 // Integer constants are used to encode Boolean values as well,
2751 // where 1 means true and 0 means false.
2752 bool IsTrue() const { return GetValue() == 1; }
2753 bool IsFalse() const { return GetValue() == 0; }
2754
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 DECLARE_INSTRUCTION(IntConstant);
2756
2757 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002758 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2759 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2760 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2761 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002762
2763 const int32_t value_;
2764
2765 friend class HGraph;
2766 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2767 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2768 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2769};
2770
Vladimir Markofcb503c2016-05-18 12:48:17 +01002771class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002772 public:
2773 int64_t GetValue() const { return value_; }
2774
David Brazdil77a48ae2015-09-15 12:34:04 +00002775 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2776
Vladimir Marko372f10e2016-05-17 16:30:10 +01002777 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002778 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002779 return other->AsLongConstant()->value_ == value_;
2780 }
2781
2782 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2783
2784 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002785 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2786 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002787 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2788
2789 DECLARE_INSTRUCTION(LongConstant);
2790
2791 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002792 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2793 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002794
2795 const int64_t value_;
2796
2797 friend class HGraph;
2798 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2799};
Dave Allison20dfc792014-06-16 20:44:29 -07002800
Vladimir Markofcb503c2016-05-18 12:48:17 +01002801class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002802 public:
2803 float GetValue() const { return value_; }
2804
2805 uint64_t GetValueAsUint64() const OVERRIDE {
2806 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2807 }
2808
Vladimir Marko372f10e2016-05-17 16:30:10 +01002809 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002810 DCHECK(other->IsFloatConstant()) << other->DebugName();
2811 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2812 }
2813
2814 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2815
2816 bool IsMinusOne() const OVERRIDE {
2817 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2818 }
Roland Levillain1a653882016-03-18 18:05:57 +00002819 bool IsArithmeticZero() const OVERRIDE {
2820 return std::fpclassify(value_) == FP_ZERO;
2821 }
2822 bool IsArithmeticPositiveZero() const {
2823 return IsArithmeticZero() && !std::signbit(value_);
2824 }
2825 bool IsArithmeticNegativeZero() const {
2826 return IsArithmeticZero() && std::signbit(value_);
2827 }
2828 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002829 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002830 }
2831 bool IsOne() const OVERRIDE {
2832 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2833 }
2834 bool IsNaN() const {
2835 return std::isnan(value_);
2836 }
2837
2838 DECLARE_INSTRUCTION(FloatConstant);
2839
2840 private:
2841 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2842 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2843 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2844 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2845
2846 const float value_;
2847
2848 // Only the SsaBuilder and HGraph can create floating-point constants.
2849 friend class SsaBuilder;
2850 friend class HGraph;
2851 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2852};
2853
Vladimir Markofcb503c2016-05-18 12:48:17 +01002854class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002855 public:
2856 double GetValue() const { return value_; }
2857
2858 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2859
Vladimir Marko372f10e2016-05-17 16:30:10 +01002860 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002861 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2862 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2863 }
2864
2865 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2866
2867 bool IsMinusOne() const OVERRIDE {
2868 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2869 }
Roland Levillain1a653882016-03-18 18:05:57 +00002870 bool IsArithmeticZero() const OVERRIDE {
2871 return std::fpclassify(value_) == FP_ZERO;
2872 }
2873 bool IsArithmeticPositiveZero() const {
2874 return IsArithmeticZero() && !std::signbit(value_);
2875 }
2876 bool IsArithmeticNegativeZero() const {
2877 return IsArithmeticZero() && std::signbit(value_);
2878 }
2879 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002880 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002881 }
2882 bool IsOne() const OVERRIDE {
2883 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2884 }
2885 bool IsNaN() const {
2886 return std::isnan(value_);
2887 }
2888
2889 DECLARE_INSTRUCTION(DoubleConstant);
2890
2891 private:
2892 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2893 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2894 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2895 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2896
2897 const double value_;
2898
2899 // Only the SsaBuilder and HGraph can create floating-point constants.
2900 friend class SsaBuilder;
2901 friend class HGraph;
2902 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2903};
2904
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002905// Conditional branch. A block ending with an HIf instruction must have
2906// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002907class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002908 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002909 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2910 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002911 SetRawInputAt(0, input);
2912 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002913
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002914 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002915
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002916 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002917 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002918 }
2919
2920 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002921 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002922 }
2923
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002924 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002925
2926 private:
2927 DISALLOW_COPY_AND_ASSIGN(HIf);
2928};
2929
David Brazdilfc6a86a2015-06-26 10:33:45 +00002930
2931// Abstract instruction which marks the beginning and/or end of a try block and
2932// links it to the respective exception handlers. Behaves the same as a Goto in
2933// non-exceptional control flow.
2934// Normal-flow successor is stored at index zero, exception handlers under
2935// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002936class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002937 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002938 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002939 kEntry,
2940 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002941 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002942 };
2943
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002944 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002945 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2946 SetPackedField<BoundaryKindField>(kind);
2947 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002948
2949 bool IsControlFlow() const OVERRIDE { return true; }
2950
2951 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002952 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002953
David Brazdild26a4112015-11-10 11:07:31 +00002954 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2955 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2956 }
2957
David Brazdilfc6a86a2015-06-26 10:33:45 +00002958 // Returns whether `handler` is among its exception handlers (non-zero index
2959 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002960 bool HasExceptionHandler(const HBasicBlock& handler) const {
2961 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002962 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002963 }
2964
2965 // If not present already, adds `handler` to its block's list of exception
2966 // handlers.
2967 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002968 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002969 GetBlock()->AddSuccessor(handler);
2970 }
2971 }
2972
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2974 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002975
David Brazdilffee3d32015-07-06 11:48:53 +01002976 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2977
David Brazdilfc6a86a2015-06-26 10:33:45 +00002978 DECLARE_INSTRUCTION(TryBoundary);
2979
2980 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002981 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2982 static constexpr size_t kFieldBoundaryKindSize =
2983 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2984 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2985 kFieldBoundaryKind + kFieldBoundaryKindSize;
2986 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2987 "Too many packed fields.");
2988 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002989
2990 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2991};
2992
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002993// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002994class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002995 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002996 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2997 // across.
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002998 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, DeoptimizationKind kind, uint32_t dex_pc)
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002999 : HVariableInputSizeInstruction(
3000 SideEffects::All(),
3001 dex_pc,
3002 arena,
3003 /* number_of_inputs */ 1,
3004 kArenaAllocMisc) {
3005 SetPackedFlag<kFieldCanBeMoved>(false);
3006 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003007 SetRawInputAt(0, cond);
3008 }
3009
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003010 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3011 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3012 // instead of `guard`.
3013 // We set CanTriggerGC to prevent any intermediate address to be live
3014 // at the point of the `HDeoptimize`.
3015 HDeoptimize(ArenaAllocator* arena,
3016 HInstruction* cond,
3017 HInstruction* guard,
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003018 DeoptimizationKind kind,
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003019 uint32_t dex_pc)
3020 : HVariableInputSizeInstruction(
3021 SideEffects::CanTriggerGC(),
3022 dex_pc,
3023 arena,
3024 /* number_of_inputs */ 2,
3025 kArenaAllocMisc) {
3026 SetPackedFlag<kFieldCanBeMoved>(true);
3027 SetPackedField<DeoptimizeKindField>(kind);
3028 SetRawInputAt(0, cond);
3029 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003030 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003031
3032 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3033
3034 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3035 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3036 }
3037
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003038 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003039
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003040 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003041
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003042 DeoptimizationKind GetDeoptimizationKind() const { return GetPackedField<DeoptimizeKindField>(); }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003043
3044 Primitive::Type GetType() const OVERRIDE {
3045 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3046 }
3047
3048 bool GuardsAnInput() const {
3049 return InputCount() == 2;
3050 }
3051
3052 HInstruction* GuardedInput() const {
3053 DCHECK(GuardsAnInput());
3054 return InputAt(1);
3055 }
3056
3057 void RemoveGuard() {
3058 RemoveInputAt(1);
3059 }
3060
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003061 DECLARE_INSTRUCTION(Deoptimize);
3062
3063 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003064 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3065 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3066 static constexpr size_t kFieldDeoptimizeKindSize =
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003067 MinimumBitsToStore(static_cast<size_t>(DeoptimizationKind::kLast));
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003068 static constexpr size_t kNumberOfDeoptimizePackedBits =
3069 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3070 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3071 "Too many packed fields.");
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003072 using DeoptimizeKindField =
3073 BitField<DeoptimizationKind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003074
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003075 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3076};
3077
Mingyao Yang063fc772016-08-02 11:02:54 -07003078// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3079// The compiled code checks this flag value in a guard before devirtualized call and
3080// if it's true, starts to do deoptimization.
3081// It has a 4-byte slot on stack.
3082// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003083class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003084 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003085 // CHA guards are only optimized in a separate pass and it has no side effects
3086 // with regard to other passes.
3087 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3088 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003089 }
3090
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003091 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3092
3093 // We do all CHA guard elimination/motion in a single pass, after which there is no
3094 // further guard elimination/motion since a guard might have been used for justification
3095 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3096 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003097 bool CanBeMoved() const OVERRIDE { return false; }
3098
3099 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3100
3101 private:
3102 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3103};
3104
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003105// Represents the ArtMethod that was passed as a first argument to
3106// the method. It is used by instructions that depend on it, like
3107// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003108class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003109 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003110 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3111 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003112
3113 DECLARE_INSTRUCTION(CurrentMethod);
3114
3115 private:
3116 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3117};
3118
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003119// Fetches an ArtMethod from the virtual table or the interface method table
3120// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003121class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003122 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003123 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003124 kVTable,
3125 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003126 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127 };
3128 HClassTableGet(HInstruction* cls,
3129 Primitive::Type type,
3130 TableKind kind,
3131 size_t index,
3132 uint32_t dex_pc)
3133 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003134 index_(index) {
3135 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003136 SetRawInputAt(0, cls);
3137 }
3138
3139 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003140 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003141 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003142 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003143 }
3144
Vladimir Markoa1de9182016-02-25 11:37:38 +00003145 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003146 size_t GetIndex() const { return index_; }
3147
3148 DECLARE_INSTRUCTION(ClassTableGet);
3149
3150 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003151 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3152 static constexpr size_t kFieldTableKindSize =
3153 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3154 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3155 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3156 "Too many packed fields.");
3157 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3158
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003159 // The index of the ArtMethod in the table.
3160 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003161
3162 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3163};
3164
Mark Mendellfe57faa2015-09-18 09:26:15 -04003165// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3166// have one successor for each entry in the switch table, and the final successor
3167// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003168class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003169 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003170 HPackedSwitch(int32_t start_value,
3171 uint32_t num_entries,
3172 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003173 uint32_t dex_pc = kNoDexPc)
3174 : HTemplateInstruction(SideEffects::None(), dex_pc),
3175 start_value_(start_value),
3176 num_entries_(num_entries) {
3177 SetRawInputAt(0, input);
3178 }
3179
3180 bool IsControlFlow() const OVERRIDE { return true; }
3181
3182 int32_t GetStartValue() const { return start_value_; }
3183
Vladimir Marko211c2112015-09-24 16:52:33 +01003184 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003185
3186 HBasicBlock* GetDefaultBlock() const {
3187 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003188 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003189 }
3190 DECLARE_INSTRUCTION(PackedSwitch);
3191
3192 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003193 const int32_t start_value_;
3194 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003195
3196 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3197};
3198
Roland Levillain88cb1752014-10-20 16:36:47 +01003199class HUnaryOperation : public HExpression<1> {
3200 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003201 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3202 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003203 SetRawInputAt(0, input);
3204 }
3205
3206 HInstruction* GetInput() const { return InputAt(0); }
3207 Primitive::Type GetResultType() const { return GetType(); }
3208
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003209 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003210 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003211 return true;
3212 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003213
Roland Levillain31dd3d62016-02-16 12:21:02 +00003214 // Try to statically evaluate `this` and return a HConstant
3215 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003216 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003217 HConstant* TryStaticEvaluation() const;
3218
3219 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003220 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3221 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003222 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3223 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003224
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003225 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003226
3227 private:
3228 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3229};
3230
Dave Allison20dfc792014-06-16 20:44:29 -07003231class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003232 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003233 HBinaryOperation(Primitive::Type result_type,
3234 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003235 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003236 SideEffects side_effects = SideEffects::None(),
3237 uint32_t dex_pc = kNoDexPc)
3238 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003239 SetRawInputAt(0, left);
3240 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003241 }
3242
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003243 HInstruction* GetLeft() const { return InputAt(0); }
3244 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003245 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003246
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003247 virtual bool IsCommutative() const { return false; }
3248
3249 // Put constant on the right.
3250 // Returns whether order is changed.
3251 bool OrderInputsWithConstantOnTheRight() {
3252 HInstruction* left = InputAt(0);
3253 HInstruction* right = InputAt(1);
3254 if (left->IsConstant() && !right->IsConstant()) {
3255 ReplaceInput(right, 0);
3256 ReplaceInput(left, 1);
3257 return true;
3258 }
3259 return false;
3260 }
3261
3262 // Order inputs by instruction id, but favor constant on the right side.
3263 // This helps GVN for commutative ops.
3264 void OrderInputs() {
3265 DCHECK(IsCommutative());
3266 HInstruction* left = InputAt(0);
3267 HInstruction* right = InputAt(1);
3268 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3269 return;
3270 }
3271 if (OrderInputsWithConstantOnTheRight()) {
3272 return;
3273 }
3274 // Order according to instruction id.
3275 if (left->GetId() > right->GetId()) {
3276 ReplaceInput(right, 0);
3277 ReplaceInput(left, 1);
3278 }
3279 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003280
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003281 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003282 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003283 return true;
3284 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003285
Roland Levillain31dd3d62016-02-16 12:21:02 +00003286 // Try to statically evaluate `this` and return a HConstant
3287 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003288 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003289 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003290
3291 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003292 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3293 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003294 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3295 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003296 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003297 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3298 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003299 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3300 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003301 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3302 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003303 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3305 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003306
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003307 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003308 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003309 HConstant* GetConstantRight() const;
3310
3311 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003312 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003313 HInstruction* GetLeastConstantLeft() const;
3314
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003315 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003316
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003317 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003318 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3319};
3320
Mark Mendellc4701932015-04-10 13:18:51 -04003321// The comparison bias applies for floating point operations and indicates how NaN
3322// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003323enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003324 kNoBias, // bias is not applicable (i.e. for long operation)
3325 kGtBias, // return 1 for NaN comparisons
3326 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003327 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003328};
3329
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3331
Dave Allison20dfc792014-06-16 20:44:29 -07003332class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003333 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003334 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003335 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3336 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3337 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003338
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003339 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003340 // `instruction`, and disregard moves in between.
3341 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003342
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003343 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003344
3345 virtual IfCondition GetCondition() const = 0;
3346
Mark Mendellc4701932015-04-10 13:18:51 -04003347 virtual IfCondition GetOppositeCondition() const = 0;
3348
Vladimir Markoa1de9182016-02-25 11:37:38 +00003349 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003350 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3351
Vladimir Markoa1de9182016-02-25 11:37:38 +00003352 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3353 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003354
Vladimir Marko372f10e2016-05-17 16:30:10 +01003355 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003356 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003357 }
3358
Roland Levillain4fa13f62015-07-06 18:11:54 +01003359 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003360 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003361 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003362 if (if_cond == kCondNE) {
3363 return true;
3364 } else if (if_cond == kCondEQ) {
3365 return false;
3366 }
3367 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003368 }
3369
3370 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003371 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003372 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003373 if (if_cond == kCondEQ) {
3374 return true;
3375 } else if (if_cond == kCondNE) {
3376 return false;
3377 }
3378 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003379 }
3380
Roland Levillain31dd3d62016-02-16 12:21:02 +00003381 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003382 // Needed if we merge a HCompare into a HCondition.
3383 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3384 static constexpr size_t kFieldComparisonBiasSize =
3385 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3386 static constexpr size_t kNumberOfConditionPackedBits =
3387 kFieldComparisonBias + kFieldComparisonBiasSize;
3388 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3389 using ComparisonBiasField =
3390 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3391
Roland Levillain31dd3d62016-02-16 12:21:02 +00003392 template <typename T>
3393 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3394
3395 template <typename T>
3396 int32_t CompareFP(T x, T y) const {
3397 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3398 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3399 // Handle the bias.
3400 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3401 }
3402
3403 // Return an integer constant containing the result of a condition evaluated at compile time.
3404 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3405 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3406 }
3407
Dave Allison20dfc792014-06-16 20:44:29 -07003408 private:
3409 DISALLOW_COPY_AND_ASSIGN(HCondition);
3410};
3411
3412// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003413class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003414 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003415 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3416 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003417
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003418 bool IsCommutative() const OVERRIDE { return true; }
3419
Vladimir Marko9e23df52015-11-10 17:14:35 +00003420 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3421 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003422 return MakeConstantCondition(true, GetDexPc());
3423 }
3424 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3425 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3426 }
3427 // In the following Evaluate methods, a HCompare instruction has
3428 // been merged into this HEqual instruction; evaluate it as
3429 // `Compare(x, y) == 0`.
3430 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3431 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3432 GetDexPc());
3433 }
3434 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3435 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3436 }
3437 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3438 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003439 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003440
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003441 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003443 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003444 return kCondEQ;
3445 }
3446
Mark Mendellc4701932015-04-10 13:18:51 -04003447 IfCondition GetOppositeCondition() const OVERRIDE {
3448 return kCondNE;
3449 }
3450
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003451 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003452 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003453
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003454 DISALLOW_COPY_AND_ASSIGN(HEqual);
3455};
3456
Vladimir Markofcb503c2016-05-18 12:48:17 +01003457class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003458 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003459 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3460 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003461
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003462 bool IsCommutative() const OVERRIDE { return true; }
3463
Vladimir Marko9e23df52015-11-10 17:14:35 +00003464 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3465 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003466 return MakeConstantCondition(false, GetDexPc());
3467 }
3468 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3469 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3470 }
3471 // In the following Evaluate methods, a HCompare instruction has
3472 // been merged into this HNotEqual instruction; evaluate it as
3473 // `Compare(x, y) != 0`.
3474 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3475 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3476 }
3477 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3478 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3479 }
3480 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3481 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003482 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 DECLARE_INSTRUCTION(NotEqual);
3485
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003486 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003487 return kCondNE;
3488 }
3489
Mark Mendellc4701932015-04-10 13:18:51 -04003490 IfCondition GetOppositeCondition() const OVERRIDE {
3491 return kCondEQ;
3492 }
3493
Dave Allison20dfc792014-06-16 20:44:29 -07003494 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003495 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003496
Dave Allison20dfc792014-06-16 20:44:29 -07003497 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3498};
3499
Vladimir Markofcb503c2016-05-18 12:48:17 +01003500class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003501 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003502 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3503 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003504
Roland Levillain9867bc72015-08-05 10:21:34 +01003505 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003507 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 // In the following Evaluate methods, a HCompare instruction has
3509 // been merged into this HLessThan instruction; evaluate it as
3510 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003511 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3513 }
3514 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3515 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3516 }
3517 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3518 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003519 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003520
Dave Allison20dfc792014-06-16 20:44:29 -07003521 DECLARE_INSTRUCTION(LessThan);
3522
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003523 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003524 return kCondLT;
3525 }
3526
Mark Mendellc4701932015-04-10 13:18:51 -04003527 IfCondition GetOppositeCondition() const OVERRIDE {
3528 return kCondGE;
3529 }
3530
Dave Allison20dfc792014-06-16 20:44:29 -07003531 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003532 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003533
Dave Allison20dfc792014-06-16 20:44:29 -07003534 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3535};
3536
Vladimir Markofcb503c2016-05-18 12:48:17 +01003537class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003538 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003539 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3540 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003541
Roland Levillain9867bc72015-08-05 10:21:34 +01003542 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003544 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 // In the following Evaluate methods, a HCompare instruction has
3546 // been merged into this HLessThanOrEqual instruction; evaluate it as
3547 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003548 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003549 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3550 }
3551 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3552 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3553 }
3554 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3555 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003556 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003557
Dave Allison20dfc792014-06-16 20:44:29 -07003558 DECLARE_INSTRUCTION(LessThanOrEqual);
3559
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003560 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003561 return kCondLE;
3562 }
3563
Mark Mendellc4701932015-04-10 13:18:51 -04003564 IfCondition GetOppositeCondition() const OVERRIDE {
3565 return kCondGT;
3566 }
3567
Dave Allison20dfc792014-06-16 20:44:29 -07003568 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003569 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003570
Dave Allison20dfc792014-06-16 20:44:29 -07003571 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3572};
3573
Vladimir Markofcb503c2016-05-18 12:48:17 +01003574class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003575 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003576 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3577 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003578
Roland Levillain9867bc72015-08-05 10:21:34 +01003579 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003581 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003582 // In the following Evaluate methods, a HCompare instruction has
3583 // been merged into this HGreaterThan instruction; evaluate it as
3584 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003585 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003586 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3587 }
3588 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3589 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3590 }
3591 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3592 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003593 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003594
Dave Allison20dfc792014-06-16 20:44:29 -07003595 DECLARE_INSTRUCTION(GreaterThan);
3596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003597 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003598 return kCondGT;
3599 }
3600
Mark Mendellc4701932015-04-10 13:18:51 -04003601 IfCondition GetOppositeCondition() const OVERRIDE {
3602 return kCondLE;
3603 }
3604
Dave Allison20dfc792014-06-16 20:44:29 -07003605 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003606 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003607
Dave Allison20dfc792014-06-16 20:44:29 -07003608 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3609};
3610
Vladimir Markofcb503c2016-05-18 12:48:17 +01003611class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003612 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003613 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3614 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003615
Roland Levillain9867bc72015-08-05 10:21:34 +01003616 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003617 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003618 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003619 // In the following Evaluate methods, a HCompare instruction has
3620 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3621 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003622 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003623 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3624 }
3625 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3626 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3627 }
3628 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3629 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003630 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003631
Dave Allison20dfc792014-06-16 20:44:29 -07003632 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3633
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003634 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003635 return kCondGE;
3636 }
3637
Mark Mendellc4701932015-04-10 13:18:51 -04003638 IfCondition GetOppositeCondition() const OVERRIDE {
3639 return kCondLT;
3640 }
3641
Dave Allison20dfc792014-06-16 20:44:29 -07003642 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003643 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003644
Dave Allison20dfc792014-06-16 20:44:29 -07003645 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3646};
3647
Vladimir Markofcb503c2016-05-18 12:48:17 +01003648class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003649 public:
3650 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3651 : HCondition(first, second, dex_pc) {}
3652
3653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003654 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003655 }
3656 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003657 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3658 }
3659 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3660 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3661 LOG(FATAL) << DebugName() << " is not defined for float values";
3662 UNREACHABLE();
3663 }
3664 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3665 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3666 LOG(FATAL) << DebugName() << " is not defined for double values";
3667 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003668 }
3669
3670 DECLARE_INSTRUCTION(Below);
3671
3672 IfCondition GetCondition() const OVERRIDE {
3673 return kCondB;
3674 }
3675
3676 IfCondition GetOppositeCondition() const OVERRIDE {
3677 return kCondAE;
3678 }
3679
3680 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003681 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003682 return MakeUnsigned(x) < MakeUnsigned(y);
3683 }
Aart Bike9f37602015-10-09 11:15:55 -07003684
3685 DISALLOW_COPY_AND_ASSIGN(HBelow);
3686};
3687
Vladimir Markofcb503c2016-05-18 12:48:17 +01003688class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003689 public:
3690 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3691 : HCondition(first, second, dex_pc) {}
3692
3693 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003694 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003695 }
3696 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3698 }
3699 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3700 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3701 LOG(FATAL) << DebugName() << " is not defined for float values";
3702 UNREACHABLE();
3703 }
3704 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3705 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3706 LOG(FATAL) << DebugName() << " is not defined for double values";
3707 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003708 }
3709
3710 DECLARE_INSTRUCTION(BelowOrEqual);
3711
3712 IfCondition GetCondition() const OVERRIDE {
3713 return kCondBE;
3714 }
3715
3716 IfCondition GetOppositeCondition() const OVERRIDE {
3717 return kCondA;
3718 }
3719
3720 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003721 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeUnsigned(x) <= MakeUnsigned(y);
3723 }
Aart Bike9f37602015-10-09 11:15:55 -07003724
3725 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3726};
3727
Vladimir Markofcb503c2016-05-18 12:48:17 +01003728class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003729 public:
3730 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3731 : HCondition(first, second, dex_pc) {}
3732
3733 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003734 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003735 }
3736 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003737 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3738 }
3739 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3740 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3741 LOG(FATAL) << DebugName() << " is not defined for float values";
3742 UNREACHABLE();
3743 }
3744 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3745 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3746 LOG(FATAL) << DebugName() << " is not defined for double values";
3747 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003748 }
3749
3750 DECLARE_INSTRUCTION(Above);
3751
3752 IfCondition GetCondition() const OVERRIDE {
3753 return kCondA;
3754 }
3755
3756 IfCondition GetOppositeCondition() const OVERRIDE {
3757 return kCondBE;
3758 }
3759
3760 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003761 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 return MakeUnsigned(x) > MakeUnsigned(y);
3763 }
Aart Bike9f37602015-10-09 11:15:55 -07003764
3765 DISALLOW_COPY_AND_ASSIGN(HAbove);
3766};
3767
Vladimir Markofcb503c2016-05-18 12:48:17 +01003768class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003769 public:
3770 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3771 : HCondition(first, second, dex_pc) {}
3772
3773 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003774 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003775 }
3776 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003777 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3778 }
3779 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3780 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3781 LOG(FATAL) << DebugName() << " is not defined for float values";
3782 UNREACHABLE();
3783 }
3784 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3785 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3786 LOG(FATAL) << DebugName() << " is not defined for double values";
3787 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003788 }
3789
3790 DECLARE_INSTRUCTION(AboveOrEqual);
3791
3792 IfCondition GetCondition() const OVERRIDE {
3793 return kCondAE;
3794 }
3795
3796 IfCondition GetOppositeCondition() const OVERRIDE {
3797 return kCondB;
3798 }
3799
3800 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003801 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003802 return MakeUnsigned(x) >= MakeUnsigned(y);
3803 }
Aart Bike9f37602015-10-09 11:15:55 -07003804
3805 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3806};
Dave Allison20dfc792014-06-16 20:44:29 -07003807
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003808// Instruction to check how two inputs compare to each other.
3809// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003810class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003811 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003812 // Note that `comparison_type` is the type of comparison performed
3813 // between the comparison's inputs, not the type of the instantiated
3814 // HCompare instruction (which is always Primitive::kPrimInt).
3815 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003816 HInstruction* first,
3817 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003818 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003819 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003820 : HBinaryOperation(Primitive::kPrimInt,
3821 first,
3822 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003823 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003824 dex_pc) {
3825 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003826 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3827 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003828 }
3829
Roland Levillain9867bc72015-08-05 10:21:34 +01003830 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3832
3833 template <typename T>
3834 int32_t ComputeFP(T x, T y) const {
3835 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3836 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3837 // Handle the bias.
3838 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3839 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003840
Roland Levillain9867bc72015-08-05 10:21:34 +01003841 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003842 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3843 // reach this code path when processing a freshly built HIR
3844 // graph. However HCompare integer instructions can be synthesized
3845 // by the instruction simplifier to implement IntegerCompare and
3846 // IntegerSignum intrinsics, so we have to handle this case.
3847 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003848 }
3849 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3851 }
3852 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3853 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3854 }
3855 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3856 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003857 }
3858
Vladimir Marko372f10e2016-05-17 16:30:10 +01003859 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003860 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003861 }
3862
Vladimir Markoa1de9182016-02-25 11:37:38 +00003863 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003864
Roland Levillain31dd3d62016-02-16 12:21:02 +00003865 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 bool IsGtBias() const {
3868 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003869 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003871
Roland Levillain1693a1f2016-03-15 14:57:31 +00003872 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3873 // Comparisons do not require a runtime call in any back end.
3874 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003875 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003876
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003877 DECLARE_INSTRUCTION(Compare);
3878
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003880 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3881 static constexpr size_t kFieldComparisonBiasSize =
3882 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3883 static constexpr size_t kNumberOfComparePackedBits =
3884 kFieldComparisonBias + kFieldComparisonBiasSize;
3885 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3886 using ComparisonBiasField =
3887 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3888
Roland Levillain31dd3d62016-02-16 12:21:02 +00003889 // Return an integer constant containing the result of a comparison evaluated at compile time.
3890 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3891 DCHECK(value == -1 || value == 0 || value == 1) << value;
3892 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3893 }
3894
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003895 private:
3896 DISALLOW_COPY_AND_ASSIGN(HCompare);
3897};
3898
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003899class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003900 public:
3901 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003902 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003903 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003904 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003905 bool finalizable,
3906 QuickEntrypointEnum entrypoint)
3907 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3908 type_index_(type_index),
3909 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003910 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003911 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003912 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003913 }
3914
Andreas Gampea5b09a62016-11-17 15:21:22 -08003915 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003916 const DexFile& GetDexFile() const { return dex_file_; }
3917
3918 // Calls runtime so needs an environment.
3919 bool NeedsEnvironment() const OVERRIDE { return true; }
3920
Mingyao Yang062157f2016-03-02 10:15:36 -08003921 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3922 bool CanThrow() const OVERRIDE { return true; }
3923
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003924 bool NeedsChecks() const {
3925 return entrypoint_ == kQuickAllocObjectWithChecks;
3926 }
David Brazdil6de19382016-01-08 17:37:10 +00003927
Vladimir Markoa1de9182016-02-25 11:37:38 +00003928 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003929
3930 bool CanBeNull() const OVERRIDE { return false; }
3931
3932 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3933
3934 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3935 entrypoint_ = entrypoint;
3936 }
3937
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003938 HLoadClass* GetLoadClass() const {
3939 HInstruction* input = InputAt(0);
3940 if (input->IsClinitCheck()) {
3941 input = input->InputAt(0);
3942 }
3943 DCHECK(input->IsLoadClass());
3944 return input->AsLoadClass();
3945 }
3946
David Brazdil6de19382016-01-08 17:37:10 +00003947 bool IsStringAlloc() const;
3948
3949 DECLARE_INSTRUCTION(NewInstance);
3950
3951 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003952 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003953 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3954 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3955 "Too many packed fields.");
3956
Andreas Gampea5b09a62016-11-17 15:21:22 -08003957 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003958 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003959 QuickEntrypointEnum entrypoint_;
3960
3961 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3962};
3963
Agi Csaki05f20562015-08-19 14:58:14 -07003964enum IntrinsicNeedsEnvironmentOrCache {
3965 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3966 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003967};
3968
Aart Bik5d75afe2015-12-14 11:57:01 -08003969enum IntrinsicSideEffects {
3970 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3971 kReadSideEffects, // Intrinsic may read heap memory.
3972 kWriteSideEffects, // Intrinsic may write heap memory.
3973 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3974};
3975
3976enum IntrinsicExceptions {
3977 kNoThrow, // Intrinsic does not throw any exceptions.
3978 kCanThrow // Intrinsic may throw exceptions.
3979};
3980
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003981class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003982 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003983 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003984
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003985 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003986 SetRawInputAt(index, argument);
3987 }
3988
Roland Levillain3e3d7332015-04-28 11:00:54 +01003989 // Return the number of arguments. This number can be lower than
3990 // the number of inputs returned by InputCount(), as some invoke
3991 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3992 // inputs at the end of their list of inputs.
3993 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3994
Vladimir Markoa1de9182016-02-25 11:37:38 +00003995 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003996
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003997 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3998
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003999 InvokeType GetInvokeType() const {
4000 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004001 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004002
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004003 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004004 return intrinsic_;
4005 }
4006
Aart Bik5d75afe2015-12-14 11:57:01 -08004007 void SetIntrinsic(Intrinsics intrinsic,
4008 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4009 IntrinsicSideEffects side_effects,
4010 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004011
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004012 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004013 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004014 }
4015
Aart Bikff7d89c2016-11-07 08:49:28 -08004016 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4017
Vladimir Markoa1de9182016-02-25 11:37:38 +00004018 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004019
Aart Bik71bf7b42016-11-16 10:17:46 -08004020 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004021
Vladimir Marko372f10e2016-05-17 16:30:10 +01004022 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004023 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4024 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004025
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004026 uint32_t* GetIntrinsicOptimizations() {
4027 return &intrinsic_optimizations_;
4028 }
4029
4030 const uint32_t* GetIntrinsicOptimizations() const {
4031 return &intrinsic_optimizations_;
4032 }
4033
4034 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4035
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004036 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004037 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004038
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004039 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004040
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004041 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004042 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4043 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004044 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4045 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004046 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004047 static constexpr size_t kFieldReturnTypeSize =
4048 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4049 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4050 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4051 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004052 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004053 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4054
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004055 HInvoke(ArenaAllocator* arena,
4056 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004057 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004058 Primitive::Type return_type,
4059 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004060 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004061 ArtMethod* resolved_method,
4062 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004063 : HVariableInputSizeInstruction(
4064 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4065 dex_pc,
4066 arena,
4067 number_of_arguments + number_of_other_inputs,
4068 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004069 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004070 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004071 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004072 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004073 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004074 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004075 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004076 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004077 }
4078
Roland Levillain3e3d7332015-04-28 11:00:54 +01004079 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004080 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004081 const uint32_t dex_method_index_;
4082 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004083
4084 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4085 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004086
4087 private:
4088 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4089};
4090
Vladimir Markofcb503c2016-05-18 12:48:17 +01004091class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004092 public:
4093 HInvokeUnresolved(ArenaAllocator* arena,
4094 uint32_t number_of_arguments,
4095 Primitive::Type return_type,
4096 uint32_t dex_pc,
4097 uint32_t dex_method_index,
4098 InvokeType invoke_type)
4099 : HInvoke(arena,
4100 number_of_arguments,
4101 0u /* number_of_other_inputs */,
4102 return_type,
4103 dex_pc,
4104 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004105 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004106 invoke_type) {
4107 }
4108
4109 DECLARE_INSTRUCTION(InvokeUnresolved);
4110
4111 private:
4112 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4113};
4114
Orion Hodsonac141392017-01-13 11:53:47 +00004115class HInvokePolymorphic FINAL : public HInvoke {
4116 public:
4117 HInvokePolymorphic(ArenaAllocator* arena,
4118 uint32_t number_of_arguments,
4119 Primitive::Type return_type,
4120 uint32_t dex_pc,
4121 uint32_t dex_method_index)
4122 : HInvoke(arena,
4123 number_of_arguments,
4124 0u /* number_of_other_inputs */,
4125 return_type,
4126 dex_pc,
4127 dex_method_index,
4128 nullptr,
4129 kVirtual) {}
4130
4131 DECLARE_INSTRUCTION(InvokePolymorphic);
4132
4133 private:
4134 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4135};
4136
Vladimir Markofcb503c2016-05-18 12:48:17 +01004137class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004138 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004139 // Requirements of this method call regarding the class
4140 // initialization (clinit) check of its declaring class.
4141 enum class ClinitCheckRequirement {
4142 kNone, // Class already initialized.
4143 kExplicit, // Static call having explicit clinit check as last input.
4144 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004145 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004146 };
4147
Vladimir Marko58155012015-08-19 12:49:41 +00004148 // Determines how to load the target ArtMethod*.
4149 enum class MethodLoadKind {
4150 // Use a String init ArtMethod* loaded from Thread entrypoints.
4151 kStringInit,
4152
4153 // Use the method's own ArtMethod* loaded by the register allocator.
4154 kRecursive,
4155
4156 // Use ArtMethod* at a known address, embed the direct address in the code.
4157 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4158 kDirectAddress,
4159
Vladimir Markoa1de9182016-02-25 11:37:38 +00004160 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004161 // Used when we need to use the dex cache, for example for invoke-static that
4162 // may cause class initialization (the entry may point to a resolution method),
4163 // and we know that we can access the dex cache arrays using a PC-relative load.
4164 kDexCachePcRelative,
4165
4166 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4167 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4168 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4169 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4170 kDexCacheViaMethod,
4171 };
4172
4173 // Determines the location of the code pointer.
4174 enum class CodePtrLocation {
4175 // Recursive call, use local PC-relative call instruction.
4176 kCallSelf,
4177
Vladimir Marko58155012015-08-19 12:49:41 +00004178 // Use code pointer from the ArtMethod*.
4179 // Used when we don't know the target code. This is also the last-resort-kind used when
4180 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4181 kCallArtMethod,
4182 };
4183
4184 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004185 MethodLoadKind method_load_kind;
4186 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004187 // The method load data holds
4188 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4189 // Note that there are multiple string init methods, each having its own offset.
4190 // - the method address for kDirectAddress
4191 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004192 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004193 };
4194
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004195 HInvokeStaticOrDirect(ArenaAllocator* arena,
4196 uint32_t number_of_arguments,
4197 Primitive::Type return_type,
4198 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004199 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004200 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004201 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004202 InvokeType invoke_type,
4203 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004204 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004205 : HInvoke(arena,
4206 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004207 // There is potentially one extra argument for the HCurrentMethod node, and
4208 // potentially one other if the clinit check is explicit, and potentially
4209 // one other if the method is a string factory.
4210 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004211 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004212 return_type,
4213 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004214 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004215 resolved_method,
4216 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004217 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004218 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004219 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4220 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004221
Vladimir Markodc151b22015-10-15 18:02:30 +01004222 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004223 bool had_current_method_input = HasCurrentMethodInput();
4224 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4225
4226 // Using the current method is the default and once we find a better
4227 // method load kind, we should not go back to using the current method.
4228 DCHECK(had_current_method_input || !needs_current_method_input);
4229
4230 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004231 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4232 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004233 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004234 dispatch_info_ = dispatch_info;
4235 }
4236
Aart Bik6daebeb2017-04-03 14:35:41 -07004237 DispatchInfo GetDispatchInfo() const {
4238 return dispatch_info_;
4239 }
4240
Vladimir Markoc53c0792015-11-19 15:48:33 +00004241 void AddSpecialInput(HInstruction* input) {
4242 // We allow only one special input.
4243 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4244 DCHECK(InputCount() == GetSpecialInputIndex() ||
4245 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4246 InsertInputAt(GetSpecialInputIndex(), input);
4247 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004248
Vladimir Marko372f10e2016-05-17 16:30:10 +01004249 using HInstruction::GetInputRecords; // Keep the const version visible.
4250 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4251 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4252 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4253 DCHECK(!input_records.empty());
4254 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4255 HInstruction* last_input = input_records.back().GetInstruction();
4256 // Note: `last_input` may be null during arguments setup.
4257 if (last_input != nullptr) {
4258 // `last_input` is the last input of a static invoke marked as having
4259 // an explicit clinit check. It must either be:
4260 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4261 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4262 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4263 }
4264 }
4265 return input_records;
4266 }
4267
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004268 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004269 // We access the method via the dex cache so we can't do an implicit null check.
4270 // TODO: for intrinsics we can generate implicit null checks.
4271 return false;
4272 }
4273
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004274 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004275 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004276 }
4277
Vladimir Markoc53c0792015-11-19 15:48:33 +00004278 // Get the index of the special input, if any.
4279 //
David Brazdil6de19382016-01-08 17:37:10 +00004280 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4281 // method pointer; otherwise there may be one platform-specific special input,
4282 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004283 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004284 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004285
Vladimir Marko58155012015-08-19 12:49:41 +00004286 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4287 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4288 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004289 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004290 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004291 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004292 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004293 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4294 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004295 bool HasCurrentMethodInput() const {
4296 // This function can be called only after the invoke has been fully initialized by the builder.
4297 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004298 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004299 return true;
4300 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004301 DCHECK(InputCount() == GetSpecialInputIndex() ||
4302 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004303 return false;
4304 }
4305 }
Vladimir Marko58155012015-08-19 12:49:41 +00004306
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004307 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004308 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004309 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004310 }
4311
4312 uint64_t GetMethodAddress() const {
4313 DCHECK(HasMethodAddress());
4314 return dispatch_info_.method_load_data;
4315 }
4316
4317 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004318 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004319 return dispatch_info_.method_load_data;
4320 }
4321
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004322 const DexFile& GetDexFileForPcRelativeDexCache() const;
4323
Vladimir Markoa1de9182016-02-25 11:37:38 +00004324 ClinitCheckRequirement GetClinitCheckRequirement() const {
4325 return GetPackedField<ClinitCheckRequirementField>();
4326 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004327
Roland Levillain4c0eb422015-04-24 16:43:49 +01004328 // Is this instruction a call to a static method?
4329 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004330 return GetInvokeType() == kStatic;
4331 }
4332
4333 MethodReference GetTargetMethod() const {
4334 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004335 }
4336
Vladimir Markofbb184a2015-11-13 14:47:00 +00004337 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4338 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4339 // instruction; only relevant for static calls with explicit clinit check.
4340 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004341 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004342 size_t last_input_index = inputs_.size() - 1u;
4343 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004344 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004345 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004346 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004347 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004348 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004349 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004350 }
4351
4352 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004353 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004354 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004355 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004356 }
4357
4358 // Is this a call to a static method whose declaring class has an
4359 // implicit intialization check requirement?
4360 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004361 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004362 }
4363
Vladimir Markob554b5a2015-11-06 12:57:55 +00004364 // Does this method load kind need the current method as an input?
4365 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4366 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4367 }
4368
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004369 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004370
4371 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004372 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004373 static constexpr size_t kFieldClinitCheckRequirementSize =
4374 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4375 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4376 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4377 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4378 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004379 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4380 kFieldClinitCheckRequirement,
4381 kFieldClinitCheckRequirementSize>;
4382
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004383 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004384 MethodReference target_method_;
4385 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004386
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004387 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004388};
Vladimir Markof64242a2015-12-01 14:58:23 +00004389std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004390std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004391
Vladimir Markofcb503c2016-05-18 12:48:17 +01004392class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004393 public:
4394 HInvokeVirtual(ArenaAllocator* arena,
4395 uint32_t number_of_arguments,
4396 Primitive::Type return_type,
4397 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004398 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004399 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004400 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004401 : HInvoke(arena,
4402 number_of_arguments,
4403 0u,
4404 return_type,
4405 dex_pc,
4406 dex_method_index,
4407 resolved_method,
4408 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004409 vtable_index_(vtable_index) {}
4410
Aart Bik71bf7b42016-11-16 10:17:46 -08004411 bool CanBeNull() const OVERRIDE {
4412 switch (GetIntrinsic()) {
4413 case Intrinsics::kThreadCurrentThread:
4414 case Intrinsics::kStringBufferAppend:
4415 case Intrinsics::kStringBufferToString:
4416 case Intrinsics::kStringBuilderAppend:
4417 case Intrinsics::kStringBuilderToString:
4418 return false;
4419 default:
4420 return HInvoke::CanBeNull();
4421 }
4422 }
4423
Calin Juravle641547a2015-04-21 22:08:51 +01004424 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004425 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004426 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004427 }
4428
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004429 uint32_t GetVTableIndex() const { return vtable_index_; }
4430
4431 DECLARE_INSTRUCTION(InvokeVirtual);
4432
4433 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004434 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004435 const uint32_t vtable_index_;
4436
4437 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4438};
4439
Vladimir Markofcb503c2016-05-18 12:48:17 +01004440class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004441 public:
4442 HInvokeInterface(ArenaAllocator* arena,
4443 uint32_t number_of_arguments,
4444 Primitive::Type return_type,
4445 uint32_t dex_pc,
4446 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004447 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004448 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004449 : HInvoke(arena,
4450 number_of_arguments,
4451 0u,
4452 return_type,
4453 dex_pc,
4454 dex_method_index,
4455 resolved_method,
4456 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004457 imt_index_(imt_index) {}
4458
Calin Juravle641547a2015-04-21 22:08:51 +01004459 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004460 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004461 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004462 }
4463
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004464 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4465 // The assembly stub currently needs it.
4466 return true;
4467 }
4468
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004469 uint32_t GetImtIndex() const { return imt_index_; }
4470 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4471
4472 DECLARE_INSTRUCTION(InvokeInterface);
4473
4474 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004475 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004476 const uint32_t imt_index_;
4477
4478 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4479};
4480
Vladimir Markofcb503c2016-05-18 12:48:17 +01004481class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004483 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004484 : HUnaryOperation(result_type, input, dex_pc) {
4485 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4486 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004487
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004488 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004489
4490 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 }
4493 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004496 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4497 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4498 }
4499 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4500 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4501 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004502
Roland Levillain88cb1752014-10-20 16:36:47 +01004503 DECLARE_INSTRUCTION(Neg);
4504
4505 private:
4506 DISALLOW_COPY_AND_ASSIGN(HNeg);
4507};
4508
Vladimir Markofcb503c2016-05-18 12:48:17 +01004509class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004510 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004511 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4512 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4513 SetRawInputAt(0, cls);
4514 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004515 }
4516
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004517 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004518 bool NeedsEnvironment() const OVERRIDE { return true; }
4519
Mingyao Yang0c365e62015-03-31 15:09:29 -07004520 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4521 bool CanThrow() const OVERRIDE { return true; }
4522
Calin Juravle10e244f2015-01-26 18:54:32 +00004523 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004524
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004525 HLoadClass* GetLoadClass() const {
4526 DCHECK(InputAt(0)->IsLoadClass());
4527 return InputAt(0)->AsLoadClass();
4528 }
4529
4530 HInstruction* GetLength() const {
4531 return InputAt(1);
4532 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004533
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004534 DECLARE_INSTRUCTION(NewArray);
4535
4536 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004537 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4538};
4539
Vladimir Markofcb503c2016-05-18 12:48:17 +01004540class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004541 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 HAdd(Primitive::Type result_type,
4543 HInstruction* left,
4544 HInstruction* right,
4545 uint32_t dex_pc = kNoDexPc)
4546 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004548 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004549
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004550 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004551
4552 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004553 return GetBlock()->GetGraph()->GetIntConstant(
4554 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004555 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004557 return GetBlock()->GetGraph()->GetLongConstant(
4558 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004559 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004560 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4561 return GetBlock()->GetGraph()->GetFloatConstant(
4562 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4563 }
4564 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4565 return GetBlock()->GetGraph()->GetDoubleConstant(
4566 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4567 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004568
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004569 DECLARE_INSTRUCTION(Add);
4570
4571 private:
4572 DISALLOW_COPY_AND_ASSIGN(HAdd);
4573};
4574
Vladimir Markofcb503c2016-05-18 12:48:17 +01004575class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004576 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 HSub(Primitive::Type result_type,
4578 HInstruction* left,
4579 HInstruction* right,
4580 uint32_t dex_pc = kNoDexPc)
4581 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004582
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004583 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004584
4585 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004586 return GetBlock()->GetGraph()->GetIntConstant(
4587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004588 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004589 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004590 return GetBlock()->GetGraph()->GetLongConstant(
4591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004592 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004593 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4594 return GetBlock()->GetGraph()->GetFloatConstant(
4595 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4596 }
4597 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4598 return GetBlock()->GetGraph()->GetDoubleConstant(
4599 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4600 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004601
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004602 DECLARE_INSTRUCTION(Sub);
4603
4604 private:
4605 DISALLOW_COPY_AND_ASSIGN(HSub);
4606};
4607
Vladimir Markofcb503c2016-05-18 12:48:17 +01004608class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004609 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004610 HMul(Primitive::Type result_type,
4611 HInstruction* left,
4612 HInstruction* right,
4613 uint32_t dex_pc = kNoDexPc)
4614 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004615
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004616 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004617
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004618 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004619
4620 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004621 return GetBlock()->GetGraph()->GetIntConstant(
4622 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004623 }
4624 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 return GetBlock()->GetGraph()->GetLongConstant(
4626 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004628 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4629 return GetBlock()->GetGraph()->GetFloatConstant(
4630 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4631 }
4632 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4633 return GetBlock()->GetGraph()->GetDoubleConstant(
4634 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4635 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004636
4637 DECLARE_INSTRUCTION(Mul);
4638
4639 private:
4640 DISALLOW_COPY_AND_ASSIGN(HMul);
4641};
4642
Vladimir Markofcb503c2016-05-18 12:48:17 +01004643class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004644 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004645 HDiv(Primitive::Type result_type,
4646 HInstruction* left,
4647 HInstruction* right,
4648 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004649 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004650
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 T ComputeIntegral(T x, T y) const {
4653 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 // Our graph structure ensures we never have 0 for `y` during
4655 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004656 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004657 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004658 return (y == -1) ? -x : x / y;
4659 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004660
Roland Levillain31dd3d62016-02-16 12:21:02 +00004661 template <typename T>
4662 T ComputeFP(T x, T y) const {
4663 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4664 return x / y;
4665 }
4666
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004668 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004669 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 }
4671 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004673 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4674 }
4675 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4676 return GetBlock()->GetGraph()->GetFloatConstant(
4677 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4678 }
4679 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4680 return GetBlock()->GetGraph()->GetDoubleConstant(
4681 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004683
4684 DECLARE_INSTRUCTION(Div);
4685
4686 private:
4687 DISALLOW_COPY_AND_ASSIGN(HDiv);
4688};
4689
Vladimir Markofcb503c2016-05-18 12:48:17 +01004690class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004691 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004692 HRem(Primitive::Type result_type,
4693 HInstruction* left,
4694 HInstruction* right,
4695 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004696 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004697
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 T ComputeIntegral(T x, T y) const {
4700 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 // Our graph structure ensures we never have 0 for `y` during
4702 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004703 DCHECK_NE(y, 0);
4704 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4705 return (y == -1) ? 0 : x % y;
4706 }
4707
Roland Levillain31dd3d62016-02-16 12:21:02 +00004708 template <typename T>
4709 T ComputeFP(T x, T y) const {
4710 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4711 return std::fmod(x, y);
4712 }
4713
Roland Levillain9867bc72015-08-05 10:21:34 +01004714 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004715 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004716 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 }
4718 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004719 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004720 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004721 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004722 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4723 return GetBlock()->GetGraph()->GetFloatConstant(
4724 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4725 }
4726 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4727 return GetBlock()->GetGraph()->GetDoubleConstant(
4728 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4729 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004730
4731 DECLARE_INSTRUCTION(Rem);
4732
4733 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004734 DISALLOW_COPY_AND_ASSIGN(HRem);
4735};
4736
Vladimir Markofcb503c2016-05-18 12:48:17 +01004737class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004738 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004739 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4740 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004741 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004742 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004743 SetRawInputAt(0, value);
4744 }
4745
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004746 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4747
Calin Juravled0d48522014-11-04 16:40:20 +00004748 bool CanBeMoved() const OVERRIDE { return true; }
4749
Vladimir Marko372f10e2016-05-17 16:30:10 +01004750 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004751 return true;
4752 }
4753
4754 bool NeedsEnvironment() const OVERRIDE { return true; }
4755 bool CanThrow() const OVERRIDE { return true; }
4756
Calin Juravled0d48522014-11-04 16:40:20 +00004757 DECLARE_INSTRUCTION(DivZeroCheck);
4758
4759 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004760 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4761};
4762
Vladimir Markofcb503c2016-05-18 12:48:17 +01004763class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 HInstruction* value,
4767 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4770 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4771 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 }
4773
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004775 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 return value << (distance & max_shift_distance);
4777 }
4778
4779 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004780 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004784 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004786 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004787 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4788 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4789 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4790 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4793 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004794 LOG(FATAL) << DebugName() << " is not defined for float values";
4795 UNREACHABLE();
4796 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004797 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4798 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004799 LOG(FATAL) << DebugName() << " is not defined for double values";
4800 UNREACHABLE();
4801 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004802
4803 DECLARE_INSTRUCTION(Shl);
4804
4805 private:
4806 DISALLOW_COPY_AND_ASSIGN(HShl);
4807};
4808
Vladimir Markofcb503c2016-05-18 12:48:17 +01004809class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004810 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004811 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004812 HInstruction* value,
4813 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4816 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4817 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
4819
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004821 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004822 return value >> (distance & max_shift_distance);
4823 }
4824
4825 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004826 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004827 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004829 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004830 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004832 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004833 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4834 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4835 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4836 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004838 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4839 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004840 LOG(FATAL) << DebugName() << " is not defined for float values";
4841 UNREACHABLE();
4842 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004843 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4844 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004845 LOG(FATAL) << DebugName() << " is not defined for double values";
4846 UNREACHABLE();
4847 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004848
4849 DECLARE_INSTRUCTION(Shr);
4850
4851 private:
4852 DISALLOW_COPY_AND_ASSIGN(HShr);
4853};
4854
Vladimir Markofcb503c2016-05-18 12:48:17 +01004855class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004856 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004857 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004858 HInstruction* value,
4859 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004860 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4862 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4863 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004864 }
4865
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004867 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004868 typedef typename std::make_unsigned<T>::type V;
4869 V ux = static_cast<V>(value);
4870 return static_cast<T>(ux >> (distance & max_shift_distance));
4871 }
4872
4873 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004874 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004875 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004876 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004878 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004880 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004881 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4882 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4883 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4884 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004885 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004886 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4887 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004888 LOG(FATAL) << DebugName() << " is not defined for float values";
4889 UNREACHABLE();
4890 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4892 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004893 LOG(FATAL) << DebugName() << " is not defined for double values";
4894 UNREACHABLE();
4895 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004896
4897 DECLARE_INSTRUCTION(UShr);
4898
4899 private:
4900 DISALLOW_COPY_AND_ASSIGN(HUShr);
4901};
4902
Vladimir Markofcb503c2016-05-18 12:48:17 +01004903class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004904 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004905 HAnd(Primitive::Type result_type,
4906 HInstruction* left,
4907 HInstruction* right,
4908 uint32_t dex_pc = kNoDexPc)
4909 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004910
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004911 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004912
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004913 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004914
4915 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004916 return GetBlock()->GetGraph()->GetIntConstant(
4917 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004918 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004919 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004920 return GetBlock()->GetGraph()->GetLongConstant(
4921 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004922 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004923 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4924 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4925 LOG(FATAL) << DebugName() << " is not defined for float values";
4926 UNREACHABLE();
4927 }
4928 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4929 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4930 LOG(FATAL) << DebugName() << " is not defined for double values";
4931 UNREACHABLE();
4932 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004933
4934 DECLARE_INSTRUCTION(And);
4935
4936 private:
4937 DISALLOW_COPY_AND_ASSIGN(HAnd);
4938};
4939
Vladimir Markofcb503c2016-05-18 12:48:17 +01004940class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004941 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004942 HOr(Primitive::Type result_type,
4943 HInstruction* left,
4944 HInstruction* right,
4945 uint32_t dex_pc = kNoDexPc)
4946 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004947
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004948 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004949
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004950 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004951
4952 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004953 return GetBlock()->GetGraph()->GetIntConstant(
4954 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004955 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004956 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004957 return GetBlock()->GetGraph()->GetLongConstant(
4958 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004959 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004960 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4961 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4962 LOG(FATAL) << DebugName() << " is not defined for float values";
4963 UNREACHABLE();
4964 }
4965 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4966 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4967 LOG(FATAL) << DebugName() << " is not defined for double values";
4968 UNREACHABLE();
4969 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004970
4971 DECLARE_INSTRUCTION(Or);
4972
4973 private:
4974 DISALLOW_COPY_AND_ASSIGN(HOr);
4975};
4976
Vladimir Markofcb503c2016-05-18 12:48:17 +01004977class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004978 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004979 HXor(Primitive::Type result_type,
4980 HInstruction* left,
4981 HInstruction* right,
4982 uint32_t dex_pc = kNoDexPc)
4983 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004984
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004985 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004986
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004987 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004988
4989 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004990 return GetBlock()->GetGraph()->GetIntConstant(
4991 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004992 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004993 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetLongConstant(
4995 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004997 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4998 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4999 LOG(FATAL) << DebugName() << " is not defined for float values";
5000 UNREACHABLE();
5001 }
5002 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5003 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5004 LOG(FATAL) << DebugName() << " is not defined for double values";
5005 UNREACHABLE();
5006 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005007
5008 DECLARE_INSTRUCTION(Xor);
5009
5010 private:
5011 DISALLOW_COPY_AND_ASSIGN(HXor);
5012};
5013
Vladimir Markofcb503c2016-05-18 12:48:17 +01005014class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015 public:
5016 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005017 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5019 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005020 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005021
Roland Levillain5b5b9312016-03-22 14:57:31 +00005022 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005023 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005024 typedef typename std::make_unsigned<T>::type V;
5025 V ux = static_cast<V>(value);
5026 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005027 return static_cast<T>(ux);
5028 } else {
5029 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005030 return static_cast<T>(ux >> (distance & max_shift_value)) |
5031 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005032 }
5033 }
5034
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005036 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005037 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005038 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005040 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005042 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005043 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5044 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5045 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5046 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005047 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005048 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5049 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005050 LOG(FATAL) << DebugName() << " is not defined for float values";
5051 UNREACHABLE();
5052 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005053 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5054 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005055 LOG(FATAL) << DebugName() << " is not defined for double values";
5056 UNREACHABLE();
5057 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005058
5059 DECLARE_INSTRUCTION(Ror);
5060
5061 private:
5062 DISALLOW_COPY_AND_ASSIGN(HRor);
5063};
5064
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005065// The value of a parameter in this method. Its location depends on
5066// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005067class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005068 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005069 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005070 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005071 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005072 Primitive::Type parameter_type,
5073 bool is_this = false)
5074 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005075 dex_file_(dex_file),
5076 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005077 index_(index) {
5078 SetPackedFlag<kFlagIsThis>(is_this);
5079 SetPackedFlag<kFlagCanBeNull>(!is_this);
5080 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005081
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005082 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005083 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005084 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005085 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005086
Vladimir Markoa1de9182016-02-25 11:37:38 +00005087 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5088 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005089
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005090 DECLARE_INSTRUCTION(ParameterValue);
5091
5092 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005093 // Whether or not the parameter value corresponds to 'this' argument.
5094 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5095 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5096 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5097 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5098 "Too many packed fields.");
5099
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005100 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005101 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005102 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005103 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005104 const uint8_t index_;
5105
5106 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5107};
5108
Vladimir Markofcb503c2016-05-18 12:48:17 +01005109class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005110 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5112 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005113
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005114 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005115 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005116 return true;
5117 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005118
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005119 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005120
5121 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005123 }
5124 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005125 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005126 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005127 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5128 LOG(FATAL) << DebugName() << " is not defined for float values";
5129 UNREACHABLE();
5130 }
5131 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5132 LOG(FATAL) << DebugName() << " is not defined for double values";
5133 UNREACHABLE();
5134 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005135
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005136 DECLARE_INSTRUCTION(Not);
5137
5138 private:
5139 DISALLOW_COPY_AND_ASSIGN(HNot);
5140};
5141
Vladimir Markofcb503c2016-05-18 12:48:17 +01005142class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005143 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5145 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005146
5147 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005148 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005149 return true;
5150 }
5151
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005152 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005153 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005154 return !x;
5155 }
5156
Roland Levillain9867bc72015-08-05 10:21:34 +01005157 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005158 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005159 }
5160 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5161 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005162 UNREACHABLE();
5163 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005164 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5165 LOG(FATAL) << DebugName() << " is not defined for float values";
5166 UNREACHABLE();
5167 }
5168 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5169 LOG(FATAL) << DebugName() << " is not defined for double values";
5170 UNREACHABLE();
5171 }
David Brazdil66d126e2015-04-03 16:02:44 +01005172
5173 DECLARE_INSTRUCTION(BooleanNot);
5174
5175 private:
5176 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5177};
5178
Vladimir Markofcb503c2016-05-18 12:48:17 +01005179class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005180 public:
5181 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005182 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005183 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005184 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005185 // Invariant: We should never generate a conversion to a Boolean value.
5186 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005187 }
5188
5189 HInstruction* GetInput() const { return InputAt(0); }
5190 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5191 Primitive::Type GetResultType() const { return GetType(); }
5192
5193 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005194 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5195 return true;
5196 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005197
Mark Mendelle82549b2015-05-06 10:55:34 -04005198 // Try to statically evaluate the conversion and return a HConstant
5199 // containing the result. If the input cannot be converted, return nullptr.
5200 HConstant* TryStaticEvaluation() const;
5201
Roland Levillaindff1f282014-11-05 14:15:05 +00005202 DECLARE_INSTRUCTION(TypeConversion);
5203
5204 private:
5205 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5206};
5207
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005208static constexpr uint32_t kNoRegNumber = -1;
5209
Vladimir Markofcb503c2016-05-18 12:48:17 +01005210class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005212 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5213 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005214 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005215 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216 SetRawInputAt(0, value);
5217 }
5218
Calin Juravle10e244f2015-01-26 18:54:32 +00005219 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005220 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005221 return true;
5222 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005223
Calin Juravle10e244f2015-01-26 18:54:32 +00005224 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225
Calin Juravle10e244f2015-01-26 18:54:32 +00005226 bool CanThrow() const OVERRIDE { return true; }
5227
5228 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005229
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230
5231 DECLARE_INSTRUCTION(NullCheck);
5232
5233 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005234 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5235};
5236
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005237// Embeds an ArtField and all the information required by the compiler. We cache
5238// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005239class FieldInfo : public ValueObject {
5240 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005241 FieldInfo(ArtField* field,
5242 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005243 Primitive::Type field_type,
5244 bool is_volatile,
5245 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005246 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005247 const DexFile& dex_file)
5248 : field_(field),
5249 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005250 field_type_(field_type),
5251 is_volatile_(is_volatile),
5252 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005253 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005254 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005255
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005256 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005257 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005258 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005259 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005260 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005261 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005262 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005263
5264 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005265 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005267 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005268 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005269 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005270 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005271 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005272};
5273
Vladimir Markofcb503c2016-05-18 12:48:17 +01005274class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005275 public:
5276 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005277 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005278 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005279 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005280 bool is_volatile,
5281 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005282 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005283 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005284 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005285 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005286 field_info_(field,
5287 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005288 field_type,
5289 is_volatile,
5290 field_idx,
5291 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005292 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005293 SetRawInputAt(0, value);
5294 }
5295
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005296 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005297
Vladimir Marko372f10e2016-05-17 16:30:10 +01005298 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5299 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005300 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005301 }
5302
Calin Juravle641547a2015-04-21 22:08:51 +01005303 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005304 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 }
5306
5307 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005308 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5309 }
5310
Calin Juravle52c48962014-12-16 17:02:57 +00005311 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005312 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005313 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005314 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005315
5316 DECLARE_INSTRUCTION(InstanceFieldGet);
5317
5318 private:
5319 const FieldInfo field_info_;
5320
5321 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5322};
5323
Vladimir Markofcb503c2016-05-18 12:48:17 +01005324class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005325 public:
5326 HInstanceFieldSet(HInstruction* object,
5327 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005328 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005329 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005330 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005331 bool is_volatile,
5332 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005333 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005334 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005335 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005336 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005337 field_info_(field,
5338 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005339 field_type,
5340 is_volatile,
5341 field_idx,
5342 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005343 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005344 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005345 SetRawInputAt(0, object);
5346 SetRawInputAt(1, value);
5347 }
5348
Calin Juravle641547a2015-04-21 22:08:51 +01005349 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005350 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005351 }
5352
Calin Juravle52c48962014-12-16 17:02:57 +00005353 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005354 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005355 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005356 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005357 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005358 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5359 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005360
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005361 DECLARE_INSTRUCTION(InstanceFieldSet);
5362
5363 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005364 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5365 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5366 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5367 "Too many packed fields.");
5368
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005369 const FieldInfo field_info_;
5370
5371 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5372};
5373
Vladimir Markofcb503c2016-05-18 12:48:17 +01005374class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005376 HArrayGet(HInstruction* array,
5377 HInstruction* index,
5378 Primitive::Type type,
5379 uint32_t dex_pc,
5380 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005381 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005382 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383 SetRawInputAt(0, array);
5384 SetRawInputAt(1, index);
5385 }
5386
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005387 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005388 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005389 return true;
5390 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005391 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005392 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005393 // Currently, unless the array is the result of NewArray, the array access is always
5394 // preceded by some form of null NullCheck necessary for the bounds check, usually
5395 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5396 // dynamic BCE. There are cases when these could be removed to produce better code.
5397 // If we ever add optimizations to do so we should allow an implicit check here
5398 // (as long as the address falls in the first page).
5399 //
5400 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5401 // a = cond ? new int[1] : null;
5402 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005403 return false;
5404 }
5405
David Brazdil4833f5a2015-12-16 10:37:39 +00005406 bool IsEquivalentOf(HArrayGet* other) const {
5407 bool result = (GetDexPc() == other->GetDexPc());
5408 if (kIsDebugBuild && result) {
5409 DCHECK_EQ(GetBlock(), other->GetBlock());
5410 DCHECK_EQ(GetArray(), other->GetArray());
5411 DCHECK_EQ(GetIndex(), other->GetIndex());
5412 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005413 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005414 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005415 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5416 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005417 }
5418 }
5419 return result;
5420 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005421
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005422 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5423
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005424 HInstruction* GetArray() const { return InputAt(0); }
5425 HInstruction* GetIndex() const { return InputAt(1); }
5426
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005427 DECLARE_INSTRUCTION(ArrayGet);
5428
5429 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005430 // We treat a String as an array, creating the HArrayGet from String.charAt()
5431 // intrinsic in the instruction simplifier. We can always determine whether
5432 // a particular HArrayGet is actually a String.charAt() by looking at the type
5433 // of the input but that requires holding the mutator lock, so we prefer to use
5434 // a flag, so that code generators don't need to do the locking.
5435 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5436 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5437 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5438 "Too many packed fields.");
5439
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5441};
5442
Vladimir Markofcb503c2016-05-18 12:48:17 +01005443class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 public:
5445 HArraySet(HInstruction* array,
5446 HInstruction* index,
5447 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005448 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005449 uint32_t dex_pc)
5450 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005451 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5452 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5453 SetPackedFlag<kFlagValueCanBeNull>(true);
5454 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005455 SetRawInputAt(0, array);
5456 SetRawInputAt(1, index);
5457 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005458 // Make a best guess now, may be refined during SSA building.
5459 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 }
5461
Calin Juravle77520bc2015-01-12 18:45:46 +00005462 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005463 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005465 }
5466
Mingyao Yang81014cb2015-06-02 03:16:27 -07005467 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005470 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005471 // TODO: Same as for ArrayGet.
5472 return false;
5473 }
5474
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005475 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005477 }
5478
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005479 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005480 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005481 }
5482
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005483 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005485 }
5486
Vladimir Markoa1de9182016-02-25 11:37:38 +00005487 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5488 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5489 bool StaticTypeOfArrayIsObjectArray() const {
5490 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5491 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005492
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005493 HInstruction* GetArray() const { return InputAt(0); }
5494 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005495 HInstruction* GetValue() const { return InputAt(2); }
5496
5497 Primitive::Type GetComponentType() const {
5498 // The Dex format does not type floating point index operations. Since the
5499 // `expected_component_type_` is set during building and can therefore not
5500 // be correct, we also check what is the value type. If it is a floating
5501 // point type, we must use that type.
5502 Primitive::Type value_type = GetValue()->GetType();
5503 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5504 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005506 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005507
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005508 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005509 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005510 }
5511
Aart Bik18b36ab2016-04-13 16:41:35 -07005512 void ComputeSideEffects() {
5513 Primitive::Type type = GetComponentType();
5514 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5515 SideEffectsForArchRuntimeCalls(type)));
5516 }
5517
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005518 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5519 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5520 }
5521
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 DECLARE_INSTRUCTION(ArraySet);
5523
5524 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005525 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5526 static constexpr size_t kFieldExpectedComponentTypeSize =
5527 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5528 static constexpr size_t kFlagNeedsTypeCheck =
5529 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5530 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005531 // Cached information for the reference_type_info_ so that codegen
5532 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005533 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5534 static constexpr size_t kNumberOfArraySetPackedBits =
5535 kFlagStaticTypeOfArrayIsObjectArray + 1;
5536 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5537 using ExpectedComponentTypeField =
5538 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539
5540 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5541};
5542
Vladimir Markofcb503c2016-05-18 12:48:17 +01005543class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005545 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005546 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005547 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005548 // Note that arrays do not change length, so the instruction does not
5549 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005550 SetRawInputAt(0, array);
5551 }
5552
Calin Juravle77520bc2015-01-12 18:45:46 +00005553 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005554 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005555 return true;
5556 }
Calin Juravle641547a2015-04-21 22:08:51 +01005557 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5558 return obj == InputAt(0);
5559 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005560
Vladimir Markodce016e2016-04-28 13:10:02 +01005561 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005563 DECLARE_INSTRUCTION(ArrayLength);
5564
5565 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005566 // We treat a String as an array, creating the HArrayLength from String.length()
5567 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5568 // determine whether a particular HArrayLength is actually a String.length() by
5569 // looking at the type of the input but that requires holding the mutator lock, so
5570 // we prefer to use a flag, so that code generators don't need to do the locking.
5571 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5572 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5573 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5574 "Too many packed fields.");
5575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5577};
5578
Vladimir Markofcb503c2016-05-18 12:48:17 +01005579class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005580 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005581 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5582 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005583 HBoundsCheck(HInstruction* index,
5584 HInstruction* length,
5585 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005586 bool string_char_at = false)
5587 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005588 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005589 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005590 SetRawInputAt(0, index);
5591 SetRawInputAt(1, length);
5592 }
5593
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005594 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005595 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005596 return true;
5597 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005599 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005601 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005602
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005603 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005604
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005605 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005606
5607 DECLARE_INSTRUCTION(BoundsCheck);
5608
5609 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005610 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5613};
5614
Vladimir Markofcb503c2016-05-18 12:48:17 +01005615class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005616 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005617 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005618 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005619
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005620 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005621 return true;
5622 }
5623
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005624 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5625 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005626
5627 DECLARE_INSTRUCTION(SuspendCheck);
5628
5629 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005630 // Only used for code generation, in order to share the same slow path between back edges
5631 // of a same loop.
5632 SlowPathCode* slow_path_;
5633
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005634 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5635};
5636
David Srbecky0cf44932015-12-09 14:09:59 +00005637// Pseudo-instruction which provides the native debugger with mapping information.
5638// It ensures that we can generate line number and local variables at this point.
5639class HNativeDebugInfo : public HTemplateInstruction<0> {
5640 public:
5641 explicit HNativeDebugInfo(uint32_t dex_pc)
5642 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5643
5644 bool NeedsEnvironment() const OVERRIDE {
5645 return true;
5646 }
5647
5648 DECLARE_INSTRUCTION(NativeDebugInfo);
5649
5650 private:
5651 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5652};
5653
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654/**
5655 * Instruction to load a Class object.
5656 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005657class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005658 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005659 // Determines how to load the Class.
5660 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005661 // We cannot load this class. See HSharpening::SharpenLoadClass.
5662 kInvalid = -1,
5663
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005664 // Use the Class* from the method's own ArtMethod*.
5665 kReferrersClass,
5666
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 // Use PC-relative boot image Class* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005668 // Used for boot image classes referenced by boot image code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005669 kBootImageLinkTimePcRelative,
5670
5671 // Use a known boot image Class* address, embedded in the code by the codegen.
5672 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673 kBootImageAddress,
5674
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005675 // Load from an entry in the .bss section using a PC-relative load.
5676 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5677 kBssEntry,
5678
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005679 // Load from the root table associated with the JIT compiled method.
5680 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005681
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 // Load from resolved types array accessed through the class loaded from
5683 // the compiled method's own ArtMethod*. This is the default access type when
5684 // all other types are unavailable.
5685 kDexCacheViaMethod,
5686
5687 kLast = kDexCacheViaMethod
5688 };
5689
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005690 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005691 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005692 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005693 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005694 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005695 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005696 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5698 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005699 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005700 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005701 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005702 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005703 // Referrers class should not need access check. We never inline unverified
5704 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705 DCHECK(!is_referrers_class || !needs_access_check);
5706
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707 SetPackedField<LoadKindField>(
5708 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005709 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005710 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005711 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005712 }
5713
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005714 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 LoadKind GetLoadKind() const {
5717 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005718 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005719
5720 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005721
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723
Andreas Gampea5b09a62016-11-17 15:21:22 -08005724 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005726 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005728 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005729 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730 }
5731
Calin Juravle0ba218d2015-05-19 18:46:01 +01005732 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005733 // The entrypoint the code generator is going to call does not do
5734 // clinit of the class.
5735 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005736 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005737 }
5738
5739 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005740 return NeedsAccessCheck() ||
5741 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005742 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5743 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005744 }
5745
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005746 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005747 return NeedsAccessCheck() ||
5748 MustGenerateClinitCheck() ||
5749 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5750 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5751 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005752 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5753 GetLoadKind() == LoadKind::kBssEntry) &&
5754 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005755 }
5756
Calin Juravleacf735c2015-02-12 15:25:22 +00005757 ReferenceTypeInfo GetLoadedClassRTI() {
5758 return loaded_class_rti_;
5759 }
5760
5761 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5762 // Make sure we only set exact types (the loaded class should never be merged).
5763 DCHECK(rti.IsExact());
5764 loaded_class_rti_ = rti;
5765 }
5766
Andreas Gampea5b09a62016-11-17 15:21:22 -08005767 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 const DexFile& GetDexFile() const { return dex_file_; }
5769
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005770 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005771 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005772 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005773
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005774 static SideEffects SideEffectsForArchRuntimeCalls() {
5775 return SideEffects::CanTriggerGC();
5776 }
5777
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005778 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005779 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005780 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005781 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005782
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005783 void MarkInBootImage() {
5784 SetPackedFlag<kFlagIsInBootImage>(true);
5785 }
5786
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 void AddSpecialInput(HInstruction* special_input);
5788
5789 using HInstruction::GetInputRecords; // Keep the const version visible.
5790 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5791 return ArrayRef<HUserRecord<HInstruction*>>(
5792 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5793 }
5794
5795 Primitive::Type GetType() const OVERRIDE {
5796 return Primitive::kPrimNot;
5797 }
5798
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005799 Handle<mirror::Class> GetClass() const {
5800 return klass_;
5801 }
5802
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005803 DECLARE_INSTRUCTION(LoadClass);
5804
5805 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005807 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005808 // Whether this instruction must generate the initialization check.
5809 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005810 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005811 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5812 static constexpr size_t kFieldLoadKindSize =
5813 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5814 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005815 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5817
5818 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005819 return load_kind == LoadKind::kReferrersClass ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005820 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005821 load_kind == LoadKind::kBssEntry ||
5822 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 }
5824
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825 void SetLoadKindInternal(LoadKind load_kind);
5826
5827 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5828 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005829 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005831
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005832 // A type index and dex file where the class can be accessed. The dex file can be:
5833 // - The compiling method's dex file if the class is defined there too.
5834 // - The compiling method's dex file if the class is referenced there.
5835 // - The dex file where the class is defined. When the load kind can only be
5836 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005837 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005838 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005839
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005840 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005841
Calin Juravleacf735c2015-02-12 15:25:22 +00005842 ReferenceTypeInfo loaded_class_rti_;
5843
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005844 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5845};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005846std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5847
5848// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005850 // The special input is used for PC-relative loads on some architectures,
5851 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005852 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005853 GetLoadKind() == LoadKind::kBootImageAddress ||
5854 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 DCHECK(special_input_.GetInstruction() == nullptr);
5856 special_input_ = HUserRecord<HInstruction*>(special_input);
5857 special_input->AddUseAt(this, 0);
5858}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005859
Vladimir Marko372f10e2016-05-17 16:30:10 +01005860class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005861 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862 // Determines how to load the String.
5863 enum class LoadKind {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005864 // Use PC-relative boot image String* address that will be known at link time.
Vladimir Marko764d4542017-05-16 10:31:41 +01005865 // Used for boot image strings referenced by boot image code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 kBootImageLinkTimePcRelative,
5867
5868 // Use a known boot image String* address, embedded in the code by the codegen.
5869 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 kBootImageAddress,
5871
Vladimir Markoaad75c62016-10-03 08:46:48 +00005872 // Load from an entry in the .bss section using a PC-relative load.
5873 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5874 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005876 // Load from the root table associated with the JIT compiled method.
5877 kJitTableAddress,
5878
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 // Load from resolved strings array accessed through the class loaded from
5880 // the compiled method's own ArtMethod*. This is the default access type when
5881 // all other types are unavailable.
5882 kDexCacheViaMethod,
5883
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005884 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 };
5886
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005887 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005888 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889 const DexFile& dex_file,
5890 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005891 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5892 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005893 string_index_(string_index),
5894 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005895 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005896 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005897
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005898 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005899
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900 LoadKind GetLoadKind() const {
5901 return GetPackedField<LoadKindField>();
5902 }
5903
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005904 const DexFile& GetDexFile() const {
5905 return dex_file_;
5906 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005907
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005908 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 return string_index_;
5910 }
5911
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005912 Handle<mirror::String> GetString() const {
5913 return string_;
5914 }
5915
5916 void SetString(Handle<mirror::String> str) {
5917 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005918 }
5919
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005920 bool CanBeMoved() const OVERRIDE { return true; }
5921
Vladimir Marko372f10e2016-05-17 16:30:10 +01005922 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005923
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005924 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005925
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005926 // Will call the runtime if we need to load the string through
5927 // the dex cache and the string is not guaranteed to be there yet.
5928 bool NeedsEnvironment() const OVERRIDE {
5929 LoadKind load_kind = GetLoadKind();
Vladimir Marko764d4542017-05-16 10:31:41 +01005930 if (load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005931 load_kind == LoadKind::kBootImageAddress ||
5932 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005933 return false;
5934 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005935 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005936 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005937
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005938 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5939 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5940 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005941
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005942 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005944
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005945 static SideEffects SideEffectsForArchRuntimeCalls() {
5946 return SideEffects::CanTriggerGC();
5947 }
5948
Vladimir Marko372f10e2016-05-17 16:30:10 +01005949 void AddSpecialInput(HInstruction* special_input);
5950
5951 using HInstruction::GetInputRecords; // Keep the const version visible.
5952 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5953 return ArrayRef<HUserRecord<HInstruction*>>(
5954 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005955 }
5956
Vladimir Marko372f10e2016-05-17 16:30:10 +01005957 Primitive::Type GetType() const OVERRIDE {
5958 return Primitive::kPrimNot;
5959 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005961 DECLARE_INSTRUCTION(LoadString);
5962
5963 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005964 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005965 static constexpr size_t kFieldLoadKindSize =
5966 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5967 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005968 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005970
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 void SetLoadKindInternal(LoadKind load_kind);
5972
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5974 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005975 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005976 HUserRecord<HInstruction*> special_input_;
5977
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005978 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005979 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005981 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005982
5983 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5984};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5986
5987// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005988inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005989 // The special input is used for PC-relative loads on some architectures,
5990 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005992 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005993 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005994 // HLoadString::GetInputRecords() returns an empty array at this point,
5995 // so use the GetInputRecords() from the base class to set the input record.
5996 DCHECK(special_input_.GetInstruction() == nullptr);
5997 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 special_input->AddUseAt(this, 0);
5999}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001/**
6002 * Performs an initialization check on its Class object input.
6003 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006004class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006006 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006007 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006008 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006009 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6010 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 SetRawInputAt(0, constant);
6012 }
6013
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006014 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006015 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006016 return true;
6017 }
6018
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 bool NeedsEnvironment() const OVERRIDE {
6020 // May call runtime to initialize the class.
6021 return true;
6022 }
6023
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006024 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006026 HLoadClass* GetLoadClass() const {
6027 DCHECK(InputAt(0)->IsLoadClass());
6028 return InputAt(0)->AsLoadClass();
6029 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030
6031 DECLARE_INSTRUCTION(ClinitCheck);
6032
6033 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006034 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6035};
6036
Vladimir Markofcb503c2016-05-18 12:48:17 +01006037class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038 public:
6039 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006040 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006042 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006043 bool is_volatile,
6044 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006045 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006046 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006047 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006048 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006049 field_info_(field,
6050 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006051 field_type,
6052 is_volatile,
6053 field_idx,
6054 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006055 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006056 SetRawInputAt(0, cls);
6057 }
6058
Calin Juravle52c48962014-12-16 17:02:57 +00006059
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006060 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006061
Vladimir Marko372f10e2016-05-17 16:30:10 +01006062 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6063 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006064 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065 }
6066
6067 size_t ComputeHashCode() const OVERRIDE {
6068 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6069 }
6070
Calin Juravle52c48962014-12-16 17:02:57 +00006071 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006072 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6073 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006074 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075
6076 DECLARE_INSTRUCTION(StaticFieldGet);
6077
6078 private:
6079 const FieldInfo field_info_;
6080
6081 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6082};
6083
Vladimir Markofcb503c2016-05-18 12:48:17 +01006084class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006085 public:
6086 HStaticFieldSet(HInstruction* cls,
6087 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006088 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006091 bool is_volatile,
6092 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006093 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006094 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006095 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006096 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006097 field_info_(field,
6098 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006099 field_type,
6100 is_volatile,
6101 field_idx,
6102 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006103 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105 SetRawInputAt(0, cls);
6106 SetRawInputAt(1, value);
6107 }
6108
Calin Juravle52c48962014-12-16 17:02:57 +00006109 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6111 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006112 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006113
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006114 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006115 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6116 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006118 DECLARE_INSTRUCTION(StaticFieldSet);
6119
6120 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6122 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6123 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6124 "Too many packed fields.");
6125
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 const FieldInfo field_info_;
6127
6128 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6129};
6130
Vladimir Markofcb503c2016-05-18 12:48:17 +01006131class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006132 public:
6133 HUnresolvedInstanceFieldGet(HInstruction* obj,
6134 Primitive::Type field_type,
6135 uint32_t field_index,
6136 uint32_t dex_pc)
6137 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6138 field_index_(field_index) {
6139 SetRawInputAt(0, obj);
6140 }
6141
6142 bool NeedsEnvironment() const OVERRIDE { return true; }
6143 bool CanThrow() const OVERRIDE { return true; }
6144
6145 Primitive::Type GetFieldType() const { return GetType(); }
6146 uint32_t GetFieldIndex() const { return field_index_; }
6147
6148 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6149
6150 private:
6151 const uint32_t field_index_;
6152
6153 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6154};
6155
Vladimir Markofcb503c2016-05-18 12:48:17 +01006156class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006157 public:
6158 HUnresolvedInstanceFieldSet(HInstruction* obj,
6159 HInstruction* value,
6160 Primitive::Type field_type,
6161 uint32_t field_index,
6162 uint32_t dex_pc)
6163 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006166 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 SetRawInputAt(0, obj);
6168 SetRawInputAt(1, value);
6169 }
6170
6171 bool NeedsEnvironment() const OVERRIDE { return true; }
6172 bool CanThrow() const OVERRIDE { return true; }
6173
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006175 uint32_t GetFieldIndex() const { return field_index_; }
6176
6177 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6178
6179 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006180 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6181 static constexpr size_t kFieldFieldTypeSize =
6182 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6183 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6184 kFieldFieldType + kFieldFieldTypeSize;
6185 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6186 "Too many packed fields.");
6187 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6188
Calin Juravlee460d1d2015-09-29 04:52:17 +01006189 const uint32_t field_index_;
6190
6191 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6192};
6193
Vladimir Markofcb503c2016-05-18 12:48:17 +01006194class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006195 public:
6196 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6197 uint32_t field_index,
6198 uint32_t dex_pc)
6199 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6200 field_index_(field_index) {
6201 }
6202
6203 bool NeedsEnvironment() const OVERRIDE { return true; }
6204 bool CanThrow() const OVERRIDE { return true; }
6205
6206 Primitive::Type GetFieldType() const { return GetType(); }
6207 uint32_t GetFieldIndex() const { return field_index_; }
6208
6209 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6210
6211 private:
6212 const uint32_t field_index_;
6213
6214 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6215};
6216
Vladimir Markofcb503c2016-05-18 12:48:17 +01006217class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006218 public:
6219 HUnresolvedStaticFieldSet(HInstruction* value,
6220 Primitive::Type field_type,
6221 uint32_t field_index,
6222 uint32_t dex_pc)
6223 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006224 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006226 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006227 SetRawInputAt(0, value);
6228 }
6229
6230 bool NeedsEnvironment() const OVERRIDE { return true; }
6231 bool CanThrow() const OVERRIDE { return true; }
6232
Vladimir Markoa1de9182016-02-25 11:37:38 +00006233 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006234 uint32_t GetFieldIndex() const { return field_index_; }
6235
6236 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6237
6238 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6240 static constexpr size_t kFieldFieldTypeSize =
6241 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6242 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6243 kFieldFieldType + kFieldFieldTypeSize;
6244 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6245 "Too many packed fields.");
6246 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6247
Calin Juravlee460d1d2015-09-29 04:52:17 +01006248 const uint32_t field_index_;
6249
6250 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6251};
6252
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006253// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006254class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006255 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006256 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6257 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006258
David Brazdilbbd733e2015-08-18 17:48:17 +01006259 bool CanBeNull() const OVERRIDE { return false; }
6260
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006261 DECLARE_INSTRUCTION(LoadException);
6262
6263 private:
6264 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6265};
6266
David Brazdilcb1c0552015-08-04 16:22:25 +01006267// Implicit part of move-exception which clears thread-local exception storage.
6268// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006269class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006271 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6272 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006273
6274 DECLARE_INSTRUCTION(ClearException);
6275
6276 private:
6277 DISALLOW_COPY_AND_ASSIGN(HClearException);
6278};
6279
Vladimir Markofcb503c2016-05-18 12:48:17 +01006280class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006281 public:
6282 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006283 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006284 SetRawInputAt(0, exception);
6285 }
6286
6287 bool IsControlFlow() const OVERRIDE { return true; }
6288
6289 bool NeedsEnvironment() const OVERRIDE { return true; }
6290
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006291 bool CanThrow() const OVERRIDE { return true; }
6292
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006293
6294 DECLARE_INSTRUCTION(Throw);
6295
6296 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006297 DISALLOW_COPY_AND_ASSIGN(HThrow);
6298};
6299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300/**
6301 * Implementation strategies for the code generator of a HInstanceOf
6302 * or `HCheckCast`.
6303 */
6304enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006305 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 kExactCheck, // Can do a single class compare.
6307 kClassHierarchyCheck, // Can just walk the super class chain.
6308 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6309 kInterfaceCheck, // No optimization yet when checking against an interface.
6310 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006311 kArrayCheck, // No optimization yet when checking against a generic array.
6312 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313};
6314
Roland Levillain86503782016-02-11 19:07:30 +00006315std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6316
Vladimir Markofcb503c2016-05-18 12:48:17 +01006317class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006319 HInstanceOf(HInstruction* object,
6320 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006322 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006323 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006325 dex_pc) {
6326 SetPackedField<TypeCheckKindField>(check_kind);
6327 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006328 SetRawInputAt(0, object);
6329 SetRawInputAt(1, constant);
6330 }
6331
6332 bool CanBeMoved() const OVERRIDE { return true; }
6333
Vladimir Marko372f10e2016-05-17 16:30:10 +01006334 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 return true;
6336 }
6337
6338 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006339 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340 }
6341
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006342 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006343 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6344 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6345 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6346 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006347
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006348 static bool CanCallRuntime(TypeCheckKind check_kind) {
6349 // Mips currently does runtime calls for any other checks.
6350 return check_kind != TypeCheckKind::kExactCheck;
6351 }
6352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006354 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006355 }
6356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006358
6359 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6361 static constexpr size_t kFieldTypeCheckKindSize =
6362 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6363 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6364 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6365 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6366 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006367
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6369};
6370
Vladimir Markofcb503c2016-05-18 12:48:17 +01006371class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006372 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006373 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006374 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006375 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6376 SetPackedFlag<kFlagUpperCanBeNull>(true);
6377 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006378 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006379 SetRawInputAt(0, input);
6380 }
6381
David Brazdilf5552582015-12-27 13:36:12 +00006382 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006383 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006385 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006386
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006387 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006388 DCHECK(GetUpperCanBeNull() || !can_be_null);
6389 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006390 }
6391
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006393
Calin Juravleb1498f62015-02-16 13:13:29 +00006394 DECLARE_INSTRUCTION(BoundType);
6395
6396 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006397 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6398 // is false then CanBeNull() cannot be true).
6399 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6400 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6401 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6402 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6403
Calin Juravleb1498f62015-02-16 13:13:29 +00006404 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006405 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6406 // It is used to bound the type in cases like:
6407 // if (x instanceof ClassX) {
6408 // // uper_bound_ will be ClassX
6409 // }
David Brazdilf5552582015-12-27 13:36:12 +00006410 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006411
6412 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6413};
6414
Vladimir Markofcb503c2016-05-18 12:48:17 +01006415class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416 public:
6417 HCheckCast(HInstruction* object,
6418 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006420 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6422 SetPackedField<TypeCheckKindField>(check_kind);
6423 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006424 SetRawInputAt(0, object);
6425 SetRawInputAt(1, constant);
6426 }
6427
6428 bool CanBeMoved() const OVERRIDE { return true; }
6429
Vladimir Marko372f10e2016-05-17 16:30:10 +01006430 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006431 return true;
6432 }
6433
6434 bool NeedsEnvironment() const OVERRIDE {
6435 // Instruction may throw a CheckCastError.
6436 return true;
6437 }
6438
6439 bool CanThrow() const OVERRIDE { return true; }
6440
Vladimir Markoa1de9182016-02-25 11:37:38 +00006441 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6442 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6443 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6444 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006445
6446 DECLARE_INSTRUCTION(CheckCast);
6447
6448 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006449 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6450 static constexpr size_t kFieldTypeCheckKindSize =
6451 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6452 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6453 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6454 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6455 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006456
6457 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006458};
6459
Andreas Gampe26de38b2016-07-27 17:53:11 -07006460/**
6461 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6462 * @details We define the combined barrier types that are actually required
6463 * by the Java Memory Model, rather than using exactly the terminology from
6464 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6465 * primitives. Note that the JSR-133 cookbook generally does not deal with
6466 * store atomicity issues, and the recipes there are not always entirely sufficient.
6467 * The current recipe is as follows:
6468 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6469 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6470 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6471 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6472 * class has final fields.
6473 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6474 * store-to-memory instructions. Only generated together with non-temporal stores.
6475 */
6476enum MemBarrierKind {
6477 kAnyStore,
6478 kLoadAny,
6479 kStoreStore,
6480 kAnyAny,
6481 kNTStoreStore,
6482 kLastBarrierKind = kNTStoreStore
6483};
6484std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6485
Vladimir Markofcb503c2016-05-18 12:48:17 +01006486class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006487 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006488 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006489 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006490 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6491 SetPackedField<BarrierKindField>(barrier_kind);
6492 }
Calin Juravle27df7582015-04-17 19:12:31 +01006493
Vladimir Markoa1de9182016-02-25 11:37:38 +00006494 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006495
6496 DECLARE_INSTRUCTION(MemoryBarrier);
6497
6498 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006499 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6500 static constexpr size_t kFieldBarrierKindSize =
6501 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6502 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6503 kFieldBarrierKind + kFieldBarrierKindSize;
6504 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6505 "Too many packed fields.");
6506 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006507
6508 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6509};
6510
Igor Murashkind01745e2017-04-05 16:40:31 -07006511// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6512// the specified object(s), with respect to any subsequent store that might "publish"
6513// (i.e. make visible) the specified object to another thread.
6514//
6515// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6516// for all final fields (that were set) at the end of the invoked constructor.
6517//
6518// The constructor fence models the freeze actions for the final fields of an object
6519// being constructed (semantically at the end of the constructor). Constructor fences
6520// have a per-object affinity; two separate objects being constructed get two separate
6521// constructor fences.
6522//
6523// (Note: that if calling a super-constructor or forwarding to another constructor,
6524// the freezes would happen at the end of *that* constructor being invoked).
6525//
6526// The memory model guarantees that when the object being constructed is "published" after
6527// constructor completion (i.e. escapes the current thread via a store), then any final field
6528// writes must be observable on other threads (once they observe that publication).
6529//
6530// Further, anything written before the freeze, and read by dereferencing through the final field,
6531// must also be visible (so final object field could itself have an object with non-final fields;
6532// yet the freeze must also extend to them).
6533//
6534// Constructor example:
6535//
6536// class HasFinal {
6537// final int field; Optimizing IR for <init>()V:
6538// HasFinal() {
6539// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6540// // freeze(this.field); HConstructorFence(this)
6541// } HReturn
6542// }
6543//
6544// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6545// already-initialized classes; in that case the allocation must act as a "default-initializer"
6546// of the object which effectively writes the class pointer "final field".
6547//
6548// For example, we can model default-initialiation as roughly the equivalent of the following:
6549//
6550// class Object {
6551// private final Class header;
6552// }
6553//
6554// Java code: Optimizing IR:
6555//
6556// T new_instance<T>() {
6557// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6558// obj.header = T.class; // header write is done by above call.
6559// // freeze(obj.header) HConstructorFence(obj)
6560// return (T)obj;
6561// }
6562//
6563// See also:
6564// * CompilerDriver::RequiresConstructorBarrier
6565// * QuasiAtomic::ThreadFenceForConstructor
6566//
6567class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6568 // A fence has variable inputs because the inputs can be removed
6569 // after prepare_for_register_allocation phase.
6570 // (TODO: In the future a fence could freeze multiple objects
6571 // after merging two fences together.)
6572 public:
6573 // `fence_object` is the reference that needs to be protected for correct publication.
6574 //
6575 // It makes sense in the following situations:
6576 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6577 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6578 //
6579 // After construction the `fence_object` becomes the 0th input.
6580 // This is not an input in a real sense, but just a convenient place to stash the information
6581 // about the associated object.
6582 HConstructorFence(HInstruction* fence_object,
6583 uint32_t dex_pc,
6584 ArenaAllocator* arena)
6585 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6586 // constraints described in the class header. We claim that these SideEffects constraints
6587 // enforce a superset of the real constraints.
6588 //
6589 // The ordering described above is conservatively modeled with SideEffects as follows:
6590 //
6591 // * To prevent reordering of the publication stores:
6592 // ----> "Reads of objects" is the initial SideEffect.
6593 // * For every primitive final field store in the constructor:
6594 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6595 // * If there are any stores to reference final fields in the constructor:
6596 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6597 // that are reachable from `fence_object` also need to be prevented for reordering
6598 // (and we do not want to do alias analysis to figure out what those stores are).
6599 //
6600 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6601 // even more conservative approach than the one described above, and prevents all of the
6602 // above reordering without analyzing any of the instructions in the constructor.
6603 //
6604 // If in a later phase we discover that there are no writes to reference final fields,
6605 // we can refine the side effect to a smaller set of type reads (see above constraints).
6606 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6607 dex_pc,
6608 arena,
6609 /* number_of_inputs */ 1,
6610 kArenaAllocConstructorFenceInputs) {
6611 DCHECK(fence_object != nullptr);
6612 SetRawInputAt(0, fence_object);
6613 }
6614
6615 // The object associated with this constructor fence.
6616 //
6617 // (Note: This will be null after the prepare_for_register_allocation phase,
6618 // as all constructor fence inputs are removed there).
6619 HInstruction* GetFenceObject() const {
6620 return InputAt(0);
6621 }
6622
6623 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6624 // - Delete `fence_use` from `this`'s use list.
6625 // - Delete `this` from `fence_use`'s inputs list.
6626 // - If the `fence_use` is dead, remove it from the graph.
6627 //
6628 // A fence is considered dead once it no longer has any uses
6629 // and all of the inputs are dead.
6630 //
6631 // This must *not* be called during/after prepare_for_register_allocation,
6632 // because that removes all the inputs to the fences but the fence is actually
6633 // still considered live.
6634 static void RemoveConstructorFences(HInstruction* instruction);
6635
Igor Murashkin79d8fa72017-04-18 09:37:23 -07006636 // Check if this constructor fence is protecting
6637 // an HNewInstance or HNewArray that is also the immediate
6638 // predecessor of `this`.
6639 //
6640 // Returns the associated HNewArray or HNewInstance,
6641 // or null otherwise.
6642 HInstruction* GetAssociatedAllocation();
6643
Igor Murashkind01745e2017-04-05 16:40:31 -07006644 DECLARE_INSTRUCTION(ConstructorFence);
6645
6646 private:
6647 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6648};
6649
Vladimir Markofcb503c2016-05-18 12:48:17 +01006650class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006651 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006652 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006653 kEnter,
6654 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006655 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006656 };
6657
6658 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006659 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006660 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6661 dex_pc) {
6662 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006663 SetRawInputAt(0, object);
6664 }
6665
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006666 // Instruction may go into runtime, so we need an environment.
6667 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006668
6669 bool CanThrow() const OVERRIDE {
6670 // Verifier guarantees that monitor-exit cannot throw.
6671 // This is important because it allows the HGraphBuilder to remove
6672 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6673 return IsEnter();
6674 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006675
Vladimir Markoa1de9182016-02-25 11:37:38 +00006676 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6677 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006678
6679 DECLARE_INSTRUCTION(MonitorOperation);
6680
Calin Juravle52c48962014-12-16 17:02:57 +00006681 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006682 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6683 static constexpr size_t kFieldOperationKindSize =
6684 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6685 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6686 kFieldOperationKind + kFieldOperationKindSize;
6687 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6688 "Too many packed fields.");
6689 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006690
6691 private:
6692 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6693};
6694
Vladimir Markofcb503c2016-05-18 12:48:17 +01006695class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006696 public:
6697 HSelect(HInstruction* condition,
6698 HInstruction* true_value,
6699 HInstruction* false_value,
6700 uint32_t dex_pc)
6701 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6702 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6703
6704 // First input must be `true_value` or `false_value` to allow codegens to
6705 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6706 // that architectures which implement HSelect as a conditional move also
6707 // will not need to invert the condition.
6708 SetRawInputAt(0, false_value);
6709 SetRawInputAt(1, true_value);
6710 SetRawInputAt(2, condition);
6711 }
6712
6713 HInstruction* GetFalseValue() const { return InputAt(0); }
6714 HInstruction* GetTrueValue() const { return InputAt(1); }
6715 HInstruction* GetCondition() const { return InputAt(2); }
6716
6717 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006718 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6719 return true;
6720 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006721
6722 bool CanBeNull() const OVERRIDE {
6723 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6724 }
6725
6726 DECLARE_INSTRUCTION(Select);
6727
6728 private:
6729 DISALLOW_COPY_AND_ASSIGN(HSelect);
6730};
6731
Vladimir Markof9f64412015-09-02 14:05:49 +01006732class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006733 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006734 MoveOperands(Location source,
6735 Location destination,
6736 Primitive::Type type,
6737 HInstruction* instruction)
6738 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006739
6740 Location GetSource() const { return source_; }
6741 Location GetDestination() const { return destination_; }
6742
6743 void SetSource(Location value) { source_ = value; }
6744 void SetDestination(Location value) { destination_ = value; }
6745
6746 // The parallel move resolver marks moves as "in-progress" by clearing the
6747 // destination (but not the source).
6748 Location MarkPending() {
6749 DCHECK(!IsPending());
6750 Location dest = destination_;
6751 destination_ = Location::NoLocation();
6752 return dest;
6753 }
6754
6755 void ClearPending(Location dest) {
6756 DCHECK(IsPending());
6757 destination_ = dest;
6758 }
6759
6760 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006761 DCHECK(source_.IsValid() || destination_.IsInvalid());
6762 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006763 }
6764
6765 // True if this blocks a move from the given location.
6766 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006767 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006768 }
6769
6770 // A move is redundant if it's been eliminated, if its source and
6771 // destination are the same, or if its destination is unneeded.
6772 bool IsRedundant() const {
6773 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6774 }
6775
6776 // We clear both operands to indicate move that's been eliminated.
6777 void Eliminate() {
6778 source_ = destination_ = Location::NoLocation();
6779 }
6780
6781 bool IsEliminated() const {
6782 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6783 return source_.IsInvalid();
6784 }
6785
Alexey Frunze4dda3372015-06-01 18:31:49 -07006786 Primitive::Type GetType() const { return type_; }
6787
Nicolas Geoffray90218252015-04-15 11:56:51 +01006788 bool Is64BitMove() const {
6789 return Primitive::Is64BitType(type_);
6790 }
6791
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006792 HInstruction* GetInstruction() const { return instruction_; }
6793
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006794 private:
6795 Location source_;
6796 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006797 // The type this move is for.
6798 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006799 // The instruction this move is assocatied with. Null when this move is
6800 // for moving an input in the expected locations of user (including a phi user).
6801 // This is only used in debug mode, to ensure we do not connect interval siblings
6802 // in the same parallel move.
6803 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006804};
6805
Roland Levillainc9285912015-12-18 10:38:42 +00006806std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6807
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006808static constexpr size_t kDefaultNumberOfMoves = 4;
6809
Vladimir Markofcb503c2016-05-18 12:48:17 +01006810class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006811 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006812 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006813 : HTemplateInstruction(SideEffects::None(), dex_pc),
6814 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6815 moves_.reserve(kDefaultNumberOfMoves);
6816 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006817
Nicolas Geoffray90218252015-04-15 11:56:51 +01006818 void AddMove(Location source,
6819 Location destination,
6820 Primitive::Type type,
6821 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006822 DCHECK(source.IsValid());
6823 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006824 if (kIsDebugBuild) {
6825 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006826 for (const MoveOperands& move : moves_) {
6827 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006828 // Special case the situation where the move is for the spill slot
6829 // of the instruction.
6830 if ((GetPrevious() == instruction)
6831 || ((GetPrevious() == nullptr)
6832 && instruction->IsPhi()
6833 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006834 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006835 << "Doing parallel moves for the same instruction.";
6836 } else {
6837 DCHECK(false) << "Doing parallel moves for the same instruction.";
6838 }
6839 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006840 }
6841 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006842 for (const MoveOperands& move : moves_) {
6843 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006844 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006845 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006846 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006847 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006848 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006849 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006850 }
6851
Vladimir Marko225b6462015-09-28 12:17:40 +01006852 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006853 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006854 }
6855
Vladimir Marko225b6462015-09-28 12:17:40 +01006856 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006857
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006858 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006859
6860 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006861 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006862
6863 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6864};
6865
Mark Mendell0616ae02015-04-17 12:49:27 -04006866} // namespace art
6867
Aart Bikf8f5a162017-02-06 15:35:29 -08006868#include "nodes_vector.h"
6869
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006870#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6871#include "nodes_shared.h"
6872#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006873#ifdef ART_ENABLE_CODEGEN_arm
6874#include "nodes_arm.h"
6875#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006876#ifdef ART_ENABLE_CODEGEN_mips
6877#include "nodes_mips.h"
6878#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006879#ifdef ART_ENABLE_CODEGEN_x86
6880#include "nodes_x86.h"
6881#endif
6882
6883namespace art {
6884
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006885class HGraphVisitor : public ValueObject {
6886 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006887 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6888 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006889
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006890 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006891 virtual void VisitBasicBlock(HBasicBlock* block);
6892
Roland Levillain633021e2014-10-01 14:12:25 +01006893 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006894 void VisitInsertionOrder();
6895
Roland Levillain633021e2014-10-01 14:12:25 +01006896 // Visit the graph following dominator tree reverse post-order.
6897 void VisitReversePostOrder();
6898
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006899 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006900
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006901 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006902#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006903 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6904
6905 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6906
6907#undef DECLARE_VISIT_INSTRUCTION
6908
6909 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006910 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006911
6912 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6913};
6914
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006915class HGraphDelegateVisitor : public HGraphVisitor {
6916 public:
6917 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6918 virtual ~HGraphDelegateVisitor() {}
6919
6920 // Visit functions that delegate to to super class.
6921#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006922 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006923
6924 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6925
6926#undef DECLARE_VISIT_INSTRUCTION
6927
6928 private:
6929 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6930};
6931
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006932// Iterator over the blocks that art part of the loop. Includes blocks part
6933// of an inner loop. The order in which the blocks are iterated is on their
6934// block id.
6935class HBlocksInLoopIterator : public ValueObject {
6936 public:
6937 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6938 : blocks_in_loop_(info.GetBlocks()),
6939 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6940 index_(0) {
6941 if (!blocks_in_loop_.IsBitSet(index_)) {
6942 Advance();
6943 }
6944 }
6945
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006946 bool Done() const { return index_ == blocks_.size(); }
6947 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006948 void Advance() {
6949 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006950 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006951 if (blocks_in_loop_.IsBitSet(index_)) {
6952 break;
6953 }
6954 }
6955 }
6956
6957 private:
6958 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006959 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006960 size_t index_;
6961
6962 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6963};
6964
Mingyao Yang3584bce2015-05-19 16:01:59 -07006965// Iterator over the blocks that art part of the loop. Includes blocks part
6966// of an inner loop. The order in which the blocks are iterated is reverse
6967// post order.
6968class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6969 public:
6970 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6971 : blocks_in_loop_(info.GetBlocks()),
6972 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6973 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006974 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006975 Advance();
6976 }
6977 }
6978
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006979 bool Done() const { return index_ == blocks_.size(); }
6980 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006981 void Advance() {
6982 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006983 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6984 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006985 break;
6986 }
6987 }
6988 }
6989
6990 private:
6991 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006992 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006993 size_t index_;
6994
6995 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6996};
6997
Aart Bikf3e61ee2017-04-12 17:09:20 -07006998// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006999inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007000 if (constant->IsIntConstant()) {
7001 return constant->AsIntConstant()->GetValue();
7002 } else if (constant->IsLongConstant()) {
7003 return constant->AsLongConstant()->GetValue();
7004 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007005 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007006 return 0;
7007 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007008}
7009
Aart Bikf3e61ee2017-04-12 17:09:20 -07007010// Returns true iff instruction is an integral constant (and sets value on success).
7011inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7012 if (instruction->IsIntConstant()) {
7013 *value = instruction->AsIntConstant()->GetValue();
7014 return true;
7015 } else if (instruction->IsLongConstant()) {
7016 *value = instruction->AsLongConstant()->GetValue();
7017 return true;
7018 } else if (instruction->IsNullConstant()) {
7019 *value = 0;
7020 return true;
7021 }
7022 return false;
7023}
7024
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007025#define INSTRUCTION_TYPE_CHECK(type, super) \
7026 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7027 inline const H##type* HInstruction::As##type() const { \
7028 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7029 } \
7030 inline H##type* HInstruction::As##type() { \
7031 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7032 }
7033
7034 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7035#undef INSTRUCTION_TYPE_CHECK
7036
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007037// Create space in `blocks` for adding `number_of_new_blocks` entries
7038// starting at location `at`. Blocks after `at` are moved accordingly.
7039inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7040 size_t number_of_new_blocks,
7041 size_t after) {
7042 DCHECK_LT(after, blocks->size());
7043 size_t old_size = blocks->size();
7044 size_t new_size = old_size + number_of_new_blocks;
7045 blocks->resize(new_size);
7046 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7047}
7048
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007049/*
7050 * Hunt "under the hood" of array lengths (leading to array references),
7051 * null checks (also leading to array references), and new arrays
7052 * (leading to the actual length). This makes it more likely related
7053 * instructions become actually comparable.
7054 */
7055inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7056 while (instruction->IsArrayLength() ||
7057 instruction->IsNullCheck() ||
7058 instruction->IsNewArray()) {
7059 instruction = instruction->IsNewArray()
7060 ? instruction->AsNewArray()->GetLength()
7061 : instruction->InputAt(0);
7062 }
7063 return instruction;
7064}
7065
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007066} // namespace art
7067
7068#endif // ART_COMPILER_OPTIMIZING_NODES_H_