blob: fb0c889792de1d3dccec4d97f9ea0df22d077fa6 [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 Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700326 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800327 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000328 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000329 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000330 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100331 dex_file_(dex_file),
332 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100333 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100334 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100335 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800336 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000338 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100339 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000343 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100344 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000345 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700346 osr_(osr),
347 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100348 blocks_.reserve(kDefaultNumberOfBlocks);
349 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350
David Brazdilbadd8262016-02-02 16:28:56 +0000351 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700352 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100355 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
356
David Brazdil69ba7b72015-06-23 18:27:30 +0100357 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000358 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100359
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000360 HBasicBlock* GetEntryBlock() const { return entry_block_; }
361 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100362 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000364 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
365 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000366
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100368
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100369 void ComputeDominanceInformation();
370 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000371 void ClearLoopInformation();
372 void FindBackEdges(ArenaBitVector* visited);
373 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100375 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376
David Brazdil4833f5a2015-12-16 10:37:39 +0000377 // Analyze all natural loops in this graph. Returns a code specifying that it
378 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000379 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000380 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381
David Brazdilffee3d32015-07-06 11:48:53 +0100382 // Iterate over blocks to compute try block membership. Needs reverse post
383 // order and loop information.
384 void ComputeTryBlockInformation();
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000387 // Returns the instruction to replace the invoke expression or null if the
388 // invoke is for a void method. Note that the caller is responsible for replacing
389 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000390 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000392 // Update the loop and try membership of `block`, which was spawned from `reference`.
393 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
394 // should be the new back edge.
395 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
396 HBasicBlock* reference,
397 bool replace_if_back_edge);
398
Mingyao Yang3584bce2015-05-19 16:01:59 -0700399 // Need to add a couple of blocks to test if the loop body is entered and
400 // put deoptimization instructions, etc.
401 void TransformLoopHeaderForBCE(HBasicBlock* header);
402
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000403 // Removes `block` from the graph. Assumes `block` has been disconnected from
404 // other blocks and has no instructions or phis.
405 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000406
David Brazdilfc6a86a2015-06-26 10:33:45 +0000407 // Splits the edge between `block` and `successor` while preserving the
408 // indices in the predecessor/successor lists. If there are multiple edges
409 // between the blocks, the lowest indices are used.
410 // Returns the new block which is empty and has the same dex pc as `successor`.
411 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
412
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100413 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
414 void SimplifyLoop(HBasicBlock* header);
415
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 int32_t GetNextInstructionId() {
417 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000418 return current_instruction_id_++;
419 }
420
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000421 int32_t GetCurrentInstructionId() const {
422 return current_instruction_id_;
423 }
424
425 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000426 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000427 current_instruction_id_ = id;
428 }
429
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100430 uint16_t GetMaximumNumberOfOutVRegs() const {
431 return maximum_number_of_out_vregs_;
432 }
433
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000434 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
435 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100436 }
437
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100438 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
439 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
440 }
441
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000442 void UpdateTemporariesVRegSlots(size_t slots) {
443 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444 }
445
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000446 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100447 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000448 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100449 }
450
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100451 void SetNumberOfVRegs(uint16_t number_of_vregs) {
452 number_of_vregs_ = number_of_vregs;
453 }
454
455 uint16_t GetNumberOfVRegs() const {
456 return number_of_vregs_;
457 }
458
459 void SetNumberOfInVRegs(uint16_t value) {
460 number_of_in_vregs_ = value;
461 }
462
David Brazdildee58d62016-04-07 09:54:26 +0000463 uint16_t GetNumberOfInVRegs() const {
464 return number_of_in_vregs_;
465 }
466
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100467 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100468 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100469 return number_of_vregs_ - number_of_in_vregs_;
470 }
471
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100472 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100473 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100474 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100475
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100476 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
477 DCHECK(GetReversePostOrder()[0] == entry_block_);
478 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
479 }
480
481 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
482 return ReverseRange(GetReversePostOrder());
483 }
484
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100485 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100486 return linear_order_;
487 }
488
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100489 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
490 return ReverseRange(GetLinearOrder());
491 }
492
Mark Mendell1152c922015-04-24 17:06:35 -0400493 bool HasBoundsChecks() const {
494 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800495 }
496
Mark Mendell1152c922015-04-24 17:06:35 -0400497 void SetHasBoundsChecks(bool value) {
498 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800499 }
500
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100501 bool ShouldGenerateConstructorBarrier() const {
502 return should_generate_constructor_barrier_;
503 }
504
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000505 bool IsDebuggable() const { return debuggable_; }
506
David Brazdil8d5b8b22015-03-24 10:51:52 +0000507 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 // already, it is created and inserted into the graph. This method is only for
509 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600510 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000511
512 // TODO: This is problematic for the consistency of reference type propagation
513 // because it can be created anytime after the pass and thus it will be left
514 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000516
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000529
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100530 HCurrentMethod* GetCurrentMethod();
531
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100532 const DexFile& GetDexFile() const {
533 return dex_file_;
534 }
535
536 uint32_t GetMethodIdx() const {
537 return method_idx_;
538 }
539
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100540 InvokeType GetInvokeType() const {
541 return invoke_type_;
542 }
543
Mark Mendellc4701932015-04-10 13:18:51 -0400544 InstructionSet GetInstructionSet() const {
545 return instruction_set_;
546 }
547
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000548 bool IsCompilingOsr() const { return osr_; }
549
Mingyao Yang063fc772016-08-02 11:02:54 -0700550 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
551 return cha_single_implementation_list_;
552 }
553
554 void AddCHASingleImplementationDependency(ArtMethod* method) {
555 cha_single_implementation_list_.insert(method);
556 }
557
558 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800559 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 }
561
David Brazdil77a48ae2015-09-15 12:34:04 +0000562 bool HasTryCatch() const { return has_try_catch_; }
563 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100564
Aart Bikb13c65b2017-03-21 20:14:07 -0700565 bool HasSIMD() const { return has_simd_; }
566 void SetHasSIMD(bool value) { has_simd_ = value; }
567
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800568 bool HasLoops() const { return has_loops_; }
569 void SetHasLoops(bool value) { has_loops_ = value; }
570
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000571 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
572 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
573
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100574 ArtMethod* GetArtMethod() const { return art_method_; }
575 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
576
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100577 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500578 // The instruction has been inserted into the graph, either as a constant, or
579 // before cursor.
580 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
581
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000582 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
583
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
585 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
586 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
587
David Brazdil2d7352b2015-04-20 14:52:42 +0100588 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000589 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100590 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 template <class InstructionType, typename ValueType>
593 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600594 ArenaSafeMap<ValueType, InstructionType*>* cache,
595 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 // Try to find an existing constant of the given value.
597 InstructionType* constant = nullptr;
598 auto cached_constant = cache->find(value);
599 if (cached_constant != cache->end()) {
600 constant = cached_constant->second;
601 }
602
603 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100604 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600606 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000607 cache->Overwrite(value, constant);
608 InsertConstant(constant);
609 }
610 return constant;
611 }
612
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 void InsertConstant(HConstant* instruction);
614
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 // Cache a float constant into the graph. This method should only be
616 // called by the SsaBuilder when creating "equivalent" instructions.
617 void CacheFloatConstant(HFloatConstant* constant);
618
619 // See CacheFloatConstant comment.
620 void CacheDoubleConstant(HDoubleConstant* constant);
621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623
624 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100627 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100628 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
Aart Bik281c6812016-08-26 11:31:48 -0700630 // List of blocks to perform a linear order tree traversal. Unlike the reverse
631 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100633
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000634 HBasicBlock* entry_block_;
635 HBasicBlock* exit_block_;
636
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100637 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100638 uint16_t maximum_number_of_out_vregs_;
639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100640 // The number of virtual registers in this method. Contains the parameters.
641 uint16_t number_of_vregs_;
642
643 // The number of virtual registers used by parameters of this method.
644 uint16_t number_of_in_vregs_;
645
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000646 // Number of vreg size slots that the temporaries use (used in baseline compiler).
647 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100648
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800649 // Flag whether there are bounds checks in the graph. We can skip
650 // BCE if it's false. It's only best effort to keep it up to date in
651 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400652 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800653
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800654 // Flag whether there are try/catch blocks in the graph. We will skip
655 // try/catch-related passes if it's false. It's only best effort to keep
656 // it up to date in the presence of code elimination so there might be
657 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000658 bool has_try_catch_;
659
Aart Bikb13c65b2017-03-21 20:14:07 -0700660 // Flag whether SIMD instructions appear in the graph. If true, the
661 // code generators may have to be more careful spilling the wider
662 // contents of SIMD registers.
663 bool has_simd_;
664
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800665 // Flag whether there are any loops in the graph. We can skip loop
666 // optimization if it's false. It's only best effort to keep it up
667 // to date in the presence of code elimination so there might be false
668 // positives.
669 bool has_loops_;
670
671 // Flag whether there are any irreducible loops in the graph. It's only
672 // best effort to keep it up to date in the presence of code elimination
673 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000674 bool has_irreducible_loops_;
675
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000676 // Indicates whether the graph should be compiled in a way that
677 // ensures full debuggability. If false, we can apply more
678 // aggressive optimizations that may limit the level of debugging.
679 const bool debuggable_;
680
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000681 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000682 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100684 // The dex file from which the method is from.
685 const DexFile& dex_file_;
686
687 // The method index in the dex file.
688 const uint32_t method_idx_;
689
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100690 // If inlined, this encodes how the callee is being invoked.
691 const InvokeType invoke_type_;
692
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100693 // Whether the graph has been transformed to SSA form. Only used
694 // in debug mode to ensure we are not using properties only valid
695 // for non-SSA form (like the number of temporaries).
696 bool in_ssa_form_;
697
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100698 const bool should_generate_constructor_barrier_;
699
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800700 // Number of CHA guards in the graph. Used to short-circuit the
701 // CHA guard optimization pass when there is no CHA guard left.
702 uint32_t number_of_cha_guards_;
703
Mathieu Chartiere401d142015-04-22 13:56:20 -0700704 const InstructionSet instruction_set_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000707 HNullConstant* cached_null_constant_;
708 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000709 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000710 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000711 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000712
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100713 HCurrentMethod* cached_current_method_;
714
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100715 // The ArtMethod this graph is for. Note that for AOT, it may be null,
716 // for example for methods whose declaring class could not be resolved
717 // (such as when the superclass could not be found).
718 ArtMethod* art_method_;
719
David Brazdil4833f5a2015-12-16 10:37:39 +0000720 // Keep the RTI of inexact Object to avoid having to pass stack handle
721 // collection pointer to passes which may create NullConstant.
722 ReferenceTypeInfo inexact_object_rti_;
723
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000724 // Whether we are compiling this graph for on stack replacement: this will
725 // make all loops seen as irreducible and emit special stack maps to mark
726 // compiled code entries which the interpreter can directly jump to.
727 const bool osr_;
728
Mingyao Yang063fc772016-08-02 11:02:54 -0700729 // List of methods that are assumed to have single implementation.
730 ArenaSet<ArtMethod*> cha_single_implementation_list_;
731
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000732 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100733 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000734 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000735 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000736 DISALLOW_COPY_AND_ASSIGN(HGraph);
737};
738
Vladimir Markof9f64412015-09-02 14:05:49 +0100739class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000740 public:
741 HLoopInformation(HBasicBlock* header, HGraph* graph)
742 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100743 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000744 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100745 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100746 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100747 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000748 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100749 back_edges_.reserve(kDefaultNumberOfBackEdges);
750 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100751
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100753 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000754
755 void Dump(std::ostream& os);
756
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757 HBasicBlock* GetHeader() const {
758 return header_;
759 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000760
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000761 void SetHeader(HBasicBlock* block) {
762 header_ = block;
763 }
764
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100765 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
766 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
767 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
768
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000771 }
772
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100774 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 }
776
David Brazdil46e2a392015-03-16 17:31:52 +0000777 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100778 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 }
780
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000781 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100782 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000783 }
784
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100786
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100787 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100788 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100789 }
790
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100791 // Returns the lifetime position of the back edge that has the
792 // greatest lifetime position.
793 size_t GetLifetimeEnd() const;
794
795 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100796 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100797 }
798
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000799 // Finds blocks that are part of this loop.
800 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100801
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802 // Returns whether this loop information contains `block`.
803 // Note that this loop information *must* be populated before entering this function.
804 bool Contains(const HBasicBlock& block) const;
805
806 // Returns whether this loop information is an inner loop of `other`.
807 // Note that `other` *must* be populated before entering this function.
808 bool IsIn(const HLoopInformation& other) const;
809
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800810 // Returns true if instruction is not defined within this loop.
811 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700812
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100813 const ArenaBitVector& GetBlocks() const { return blocks_; }
814
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000815 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000816 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000817
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000818 void ClearAllBlocks() {
819 blocks_.ClearAllBits();
820 }
821
David Brazdil3f4a5222016-05-06 12:46:21 +0100822 bool HasBackEdgeNotDominatedByHeader() const;
823
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100824 bool IsPopulated() const {
825 return blocks_.GetHighestBitSet() != -1;
826 }
827
Anton Shaminf89381f2016-05-16 16:44:13 +0600828 bool DominatesAllBackEdges(HBasicBlock* block);
829
David Sehrc757dec2016-11-04 15:48:34 -0700830 bool HasExitEdge() const;
831
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000832 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100833 // Internal recursive implementation of `Populate`.
834 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100835 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100836
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000837 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100838 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000839 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100840 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100841 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100842 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000843
844 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
845};
846
David Brazdilec16f792015-08-19 15:04:01 +0100847// Stores try/catch information for basic blocks.
848// Note that HGraph is constructed so that catch blocks cannot simultaneously
849// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100850class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100851 public:
852 // Try block information constructor.
853 explicit TryCatchInformation(const HTryBoundary& try_entry)
854 : try_entry_(&try_entry),
855 catch_dex_file_(nullptr),
856 catch_type_index_(DexFile::kDexNoIndex16) {
857 DCHECK(try_entry_ != nullptr);
858 }
859
860 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800861 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100862 : try_entry_(nullptr),
863 catch_dex_file_(&dex_file),
864 catch_type_index_(catch_type_index) {}
865
866 bool IsTryBlock() const { return try_entry_ != nullptr; }
867
868 const HTryBoundary& GetTryEntry() const {
869 DCHECK(IsTryBlock());
870 return *try_entry_;
871 }
872
873 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
874
875 bool IsCatchAllTypeIndex() const {
876 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800877 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100878 }
879
Andreas Gampea5b09a62016-11-17 15:21:22 -0800880 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100881 DCHECK(IsCatchBlock());
882 return catch_type_index_;
883 }
884
885 const DexFile& GetCatchDexFile() const {
886 DCHECK(IsCatchBlock());
887 return *catch_dex_file_;
888 }
889
890 private:
891 // One of possibly several TryBoundary instructions entering the block's try.
892 // Only set for try blocks.
893 const HTryBoundary* try_entry_;
894
895 // Exception type information. Only set for catch blocks.
896 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800897 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100898};
899
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100901static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100902
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000903// A block in a method. Contains the list of instructions represented
904// as a double linked list. Each block knows its predecessors and
905// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100906
Vladimir Markof9f64412015-09-02 14:05:49 +0100907class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700909 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000910 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000911 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
912 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000913 loop_information_(nullptr),
914 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000915 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100916 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100917 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100918 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000919 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000920 try_catch_information_(nullptr) {
921 predecessors_.reserve(kDefaultNumberOfPredecessors);
922 successors_.reserve(kDefaultNumberOfSuccessors);
923 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
924 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000925
Vladimir Marko60584552015-09-03 13:35:12 +0000926 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100927 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000928 }
929
Vladimir Marko60584552015-09-03 13:35:12 +0000930 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100931 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000932 }
933
David Brazdild26a4112015-11-10 11:07:31 +0000934 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
935 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
936
Vladimir Marko60584552015-09-03 13:35:12 +0000937 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
938 return ContainsElement(successors_, block, start_from);
939 }
940
941 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100942 return dominated_blocks_;
943 }
944
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100945 bool IsEntryBlock() const {
946 return graph_->GetEntryBlock() == this;
947 }
948
949 bool IsExitBlock() const {
950 return graph_->GetExitBlock() == this;
951 }
952
David Brazdil46e2a392015-03-16 17:31:52 +0000953 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000954 bool IsSingleTryBoundary() const;
955
956 // Returns true if this block emits nothing but a jump.
957 bool IsSingleJump() const {
958 HLoopInformation* loop_info = GetLoopInformation();
959 return (IsSingleGoto() || IsSingleTryBoundary())
960 // Back edges generate a suspend check.
961 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
962 }
David Brazdil46e2a392015-03-16 17:31:52 +0000963
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000964 void AddBackEdge(HBasicBlock* back_edge) {
965 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000966 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000967 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100968 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 loop_information_->AddBackEdge(back_edge);
970 }
971
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000972 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000973 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000974
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100975 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000976 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600977 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000978
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 HBasicBlock* GetDominator() const { return dominator_; }
980 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000981 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
982
983 void RemoveDominatedBlock(HBasicBlock* block) {
984 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100985 }
Vladimir Marko60584552015-09-03 13:35:12 +0000986
987 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
988 ReplaceElement(dominated_blocks_, existing, new_block);
989 }
990
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100991 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000992
993 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100994 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000995 }
996
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100997 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
998 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100999 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001000 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001001 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1002 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001003
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001004 HInstruction* GetFirstInstructionDisregardMoves() const;
1005
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001006 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001007 successors_.push_back(block);
1008 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1012 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001013 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001014 new_block->predecessors_.push_back(this);
1015 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001016 }
1017
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1019 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001020 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001021 new_block->successors_.push_back(this);
1022 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001023 }
1024
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001025 // Insert `this` between `predecessor` and `successor. This method
1026 // preserves the indicies, and will update the first edge found between
1027 // `predecessor` and `successor`.
1028 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1029 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001030 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001031 successor->predecessors_[predecessor_index] = this;
1032 predecessor->successors_[successor_index] = this;
1033 successors_.push_back(successor);
1034 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001035 }
1036
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001037 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001038 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001039 }
1040
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001042 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001043 }
1044
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001046 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001047 }
1048
1049 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001050 predecessors_.push_back(block);
1051 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001052 }
1053
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001054 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 DCHECK_EQ(predecessors_.size(), 2u);
1056 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001057 }
1058
David Brazdil769c9e52015-04-27 13:54:09 +01001059 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001060 DCHECK_EQ(successors_.size(), 2u);
1061 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001062 }
1063
David Brazdilfc6a86a2015-06-26 10:33:45 +00001064 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001065 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001066 }
1067
David Brazdilfc6a86a2015-06-26 10:33:45 +00001068 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001069 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001070 }
1071
David Brazdilfc6a86a2015-06-26 10:33:45 +00001072 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001073 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001074 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001075 }
1076
1077 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001078 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001079 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001080 }
1081
1082 // Returns whether the first occurrence of `predecessor` in the list of
1083 // predecessors is at index `idx`.
1084 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001085 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001086 return GetPredecessorIndexOf(predecessor) == idx;
1087 }
1088
David Brazdild7558da2015-09-22 13:04:14 +01001089 // Create a new block between this block and its predecessors. The new block
1090 // is added to the graph, all predecessor edges are relinked to it and an edge
1091 // is created to `this`. Returns the new empty block. Reverse post order or
1092 // loop and try/catch information are not updated.
1093 HBasicBlock* CreateImmediateDominator();
1094
David Brazdilfc6a86a2015-06-26 10:33:45 +00001095 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001096 // created, latter block. Note that this method will add the block to the
1097 // graph, create a Goto at the end of the former block and will create an edge
1098 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001099 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001100 HBasicBlock* SplitBefore(HInstruction* cursor);
1101
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001102 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001103 // created block. Note that this method just updates raw block information,
1104 // like predecessors, successors, dominators, and instruction list. It does not
1105 // update the graph, reverse post order, loop information, nor make sure the
1106 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001107 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1108
1109 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1110 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001111
1112 // Merge `other` at the end of `this`. Successors and dominated blocks of
1113 // `other` are changed to be successors and dominated blocks of `this`. Note
1114 // that this method does not update the graph, reverse post order, loop
1115 // information, nor make sure the blocks are consistent (for example ending
1116 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001117 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001118
1119 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1120 // of `this` are moved to `other`.
1121 // Note that this method does not update the graph, reverse post order, loop
1122 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001123 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001124 void ReplaceWith(HBasicBlock* other);
1125
Aart Bik6b69e0a2017-01-11 10:20:43 -08001126 // Merges the instructions of `other` at the end of `this`.
1127 void MergeInstructionsWith(HBasicBlock* other);
1128
David Brazdil2d7352b2015-04-20 14:52:42 +01001129 // Merge `other` at the end of `this`. This method updates loops, reverse post
1130 // order, links to predecessors, successors, dominators and deletes the block
1131 // from the graph. The two blocks must be successive, i.e. `this` the only
1132 // predecessor of `other` and vice versa.
1133 void MergeWith(HBasicBlock* other);
1134
1135 // Disconnects `this` from all its predecessors, successors and dominator,
1136 // removes it from all loops it is included in and eventually from the graph.
1137 // The block must not dominate any other block. Predecessors and successors
1138 // are safely updated.
1139 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001140
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001141 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001142 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001143 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001144 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001145 // Replace instruction `initial` with `replacement` within this block.
1146 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1147 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001148 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001149 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001150 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1151 // instruction list. With 'ensure_safety' set to true, it verifies that the
1152 // instruction is not in use and removes it from the use lists of its inputs.
1153 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1154 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001155 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001156
1157 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001158 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001159 }
1160
Roland Levillain6b879dd2014-09-22 17:13:44 +01001161 bool IsLoopPreHeaderFirstPredecessor() const {
1162 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001163 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001164 }
1165
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001166 bool IsFirstPredecessorBackEdge() const {
1167 DCHECK(IsLoopHeader());
1168 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1169 }
1170
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001171 HLoopInformation* GetLoopInformation() const {
1172 return loop_information_;
1173 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001174
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001176 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001177 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001178 void SetInLoop(HLoopInformation* info) {
1179 if (IsLoopHeader()) {
1180 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001181 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001182 loop_information_ = info;
1183 } else if (loop_information_->Contains(*info->GetHeader())) {
1184 // Block is currently part of an outer loop. Make it part of this inner loop.
1185 // Note that a non loop header having a loop information means this loop information
1186 // has already been populated
1187 loop_information_ = info;
1188 } else {
1189 // Block is part of an inner loop. Do not update the loop information.
1190 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1191 // at this point, because this method is being called while populating `info`.
1192 }
1193 }
1194
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001195 // Raw update of the loop information.
1196 void SetLoopInformation(HLoopInformation* info) {
1197 loop_information_ = info;
1198 }
1199
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001200 bool IsInLoop() const { return loop_information_ != nullptr; }
1201
David Brazdilec16f792015-08-19 15:04:01 +01001202 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1203
1204 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1205 try_catch_information_ = try_catch_information;
1206 }
1207
1208 bool IsTryBlock() const {
1209 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1210 }
1211
1212 bool IsCatchBlock() const {
1213 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1214 }
David Brazdilffee3d32015-07-06 11:48:53 +01001215
1216 // Returns the try entry that this block's successors should have. They will
1217 // be in the same try, unless the block ends in a try boundary. In that case,
1218 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001219 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001220
David Brazdild7558da2015-09-22 13:04:14 +01001221 bool HasThrowingInstructions() const;
1222
David Brazdila4b8c212015-05-07 09:59:30 +01001223 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001224 bool Dominates(HBasicBlock* block) const;
1225
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001226 size_t GetLifetimeStart() const { return lifetime_start_; }
1227 size_t GetLifetimeEnd() const { return lifetime_end_; }
1228
1229 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1230 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1231
David Brazdil8d5b8b22015-03-24 10:51:52 +00001232 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001234 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001235 bool HasSinglePhi() const;
1236
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001237 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001238 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001239 ArenaVector<HBasicBlock*> predecessors_;
1240 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001241 HInstructionList instructions_;
1242 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001243 HLoopInformation* loop_information_;
1244 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001245 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001246 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001247 // The dex program counter of the first instruction of this block.
1248 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001249 size_t lifetime_start_;
1250 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001251 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001252
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001253 friend class HGraph;
1254 friend class HInstruction;
1255
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001256 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1257};
1258
David Brazdilb2bd1c52015-03-25 11:17:37 +00001259// Iterates over the LoopInformation of all loops which contain 'block'
1260// from the innermost to the outermost.
1261class HLoopInformationOutwardIterator : public ValueObject {
1262 public:
1263 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1264 : current_(block.GetLoopInformation()) {}
1265
1266 bool Done() const { return current_ == nullptr; }
1267
1268 void Advance() {
1269 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001270 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001271 }
1272
1273 HLoopInformation* Current() const {
1274 DCHECK(!Done());
1275 return current_;
1276 }
1277
1278 private:
1279 HLoopInformation* current_;
1280
1281 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1282};
1283
Alexandre Ramesef20f712015-06-09 10:29:30 +01001284#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001285 M(Above, Condition) \
1286 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001287 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001288 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001289 M(ArrayGet, Instruction) \
1290 M(ArrayLength, Instruction) \
1291 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001292 M(Below, Condition) \
1293 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001294 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001296 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001297 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001298 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001299 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001300 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001301 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001302 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001303 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001304 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001306 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001308 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001309 M(Exit, Instruction) \
1310 M(FloatConstant, Constant) \
1311 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(GreaterThan, Condition) \
1313 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(InstanceFieldGet, Instruction) \
1316 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001317 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001319 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001320 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001321 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001323 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001324 M(LessThan, Condition) \
1325 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001326 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001327 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001328 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001329 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001330 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001331 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001333 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(Neg, UnaryOperation) \
1335 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001336 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001337 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001338 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001339 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001340 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001341 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001342 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001345 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001346 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001347 M(Return, Instruction) \
1348 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001349 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001350 M(Shl, BinaryOperation) \
1351 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001352 M(StaticFieldGet, Instruction) \
1353 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001354 M(UnresolvedInstanceFieldGet, Instruction) \
1355 M(UnresolvedInstanceFieldSet, Instruction) \
1356 M(UnresolvedStaticFieldGet, Instruction) \
1357 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001358 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001359 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001360 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001361 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001362 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001363 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001364 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001365 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001366
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001367/*
1368 * Instructions, shared across several (not all) architectures.
1369 */
1370#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1371#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1372#else
1373#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001374 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001375 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001376 M(MultiplyAccumulate, Instruction) \
1377 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001378#endif
1379
Vladimir Markob4536b72015-11-24 13:45:23 +00001380#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001381#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001382#else
1383#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1384 M(ArmDexCacheArraysBase, Instruction)
1385#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001386
1387#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1388
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001389#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001390#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001391#else
1392#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1393 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001394 M(MipsDexCacheArraysBase, Instruction) \
1395 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001396#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001397
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001398#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1399
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001400#ifndef ART_ENABLE_CODEGEN_x86
1401#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1402#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001403#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1404 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001405 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001406 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001407 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001408#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001409
1410#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1411
1412#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1413 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001414 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001415 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1416 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001417 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001418 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001419 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1420 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1421
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001422#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1423 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001424 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001425 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001426 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001427 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001428
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001429#define FOR_EACH_INSTRUCTION(M) \
1430 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1431 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1432
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001433#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001434FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1435#undef FORWARD_DECLARATION
1436
Vladimir Marko372f10e2016-05-17 16:30:10 +01001437#define DECLARE_INSTRUCTION(type) \
1438 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1439 const char* DebugName() const OVERRIDE { return #type; } \
1440 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1441 return other->Is##type(); \
1442 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001443 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001444
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001445#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1446 bool Is##type() const { return As##type() != nullptr; } \
1447 const H##type* As##type() const { return this; } \
1448 H##type* As##type() { return this; }
1449
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001450template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001451class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001452 public:
David Brazdiled596192015-01-23 10:39:45 +00001453 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001454 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001455 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001456
Vladimir Marko46817b82016-03-29 12:21:58 +01001457 // Hook for the IntrusiveForwardList<>.
1458 // TODO: Hide this better.
1459 IntrusiveForwardListHook hook;
1460
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001461 private:
David Brazdiled596192015-01-23 10:39:45 +00001462 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001463 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001464
1465 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001466 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001467
Vladimir Marko46817b82016-03-29 12:21:58 +01001468 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001469
1470 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1471};
1472
David Brazdiled596192015-01-23 10:39:45 +00001473template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001474using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001475
David Brazdil1abb4192015-02-17 18:33:36 +00001476// This class is used by HEnvironment and HInstruction classes to record the
1477// instructions they use and pointers to the corresponding HUseListNodes kept
1478// by the used instructions.
1479template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001480class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001481 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001482 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1483 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001484
Vladimir Marko46817b82016-03-29 12:21:58 +01001485 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1486 : HUserRecord(old_record.instruction_, before_use_node) {}
1487 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1488 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001489 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001490 }
1491
1492 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001493 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1494 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001495
1496 private:
1497 // Instruction used by the user.
1498 HInstruction* instruction_;
1499
Vladimir Marko46817b82016-03-29 12:21:58 +01001500 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1501 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001502};
1503
Vladimir Markoe9004912016-06-16 16:50:52 +01001504// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1505// This is used for HInstruction::GetInputs() to return a container wrapper providing
1506// HInstruction* values even though the underlying container has HUserRecord<>s.
1507struct HInputExtractor {
1508 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1509 return record.GetInstruction();
1510 }
1511 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1512 return record.GetInstruction();
1513 }
1514};
1515
1516using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1517using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1518
Aart Bik854a02b2015-07-14 16:07:00 -07001519/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001520 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001521 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001522 * For write/read dependences on fields/arrays, the dependence analysis uses
1523 * type disambiguation (e.g. a float field write cannot modify the value of an
1524 * integer field read) and the access type (e.g. a reference array write cannot
1525 * modify the value of a reference field read [although it may modify the
1526 * reference fetch prior to reading the field, which is represented by its own
1527 * write/read dependence]). The analysis makes conservative points-to
1528 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1529 * the same, and any reference read depends on any reference read without
1530 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001531 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001532 * The internal representation uses 38-bit and is described in the table below.
1533 * The first line indicates the side effect, and for field/array accesses the
1534 * second line indicates the type of the access (in the order of the
1535 * Primitive::Type enum).
1536 * The two numbered lines below indicate the bit position in the bitfield (read
1537 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001538 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001539 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1540 * +-------------+---------+---------+--------------+---------+---------+
1541 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1542 * | 3 |333333322|222222221| 1 |111111110|000000000|
1543 * | 7 |654321098|765432109| 8 |765432109|876543210|
1544 *
1545 * Note that, to ease the implementation, 'changes' bits are least significant
1546 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001547 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001548class SideEffects : public ValueObject {
1549 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001550 SideEffects() : flags_(0) {}
1551
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 static SideEffects None() {
1553 return SideEffects(0);
1554 }
1555
1556 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 return SideEffects(kAllChangeBits | kAllDependOnBits);
1558 }
1559
1560 static SideEffects AllChanges() {
1561 return SideEffects(kAllChangeBits);
1562 }
1563
1564 static SideEffects AllDependencies() {
1565 return SideEffects(kAllDependOnBits);
1566 }
1567
1568 static SideEffects AllExceptGCDependency() {
1569 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1570 }
1571
1572 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001573 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 }
1575
Aart Bik34c3ba92015-07-20 14:08:59 -07001576 static SideEffects AllWrites() {
1577 return SideEffects(kAllWrites);
1578 }
1579
1580 static SideEffects AllReads() {
1581 return SideEffects(kAllReads);
1582 }
1583
1584 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1585 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001587 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001588 }
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001591 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001592 }
1593
Aart Bik34c3ba92015-07-20 14:08:59 -07001594 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1595 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001596 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001597 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001598 }
1599
1600 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001601 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001602 }
1603
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001604 static SideEffects CanTriggerGC() {
1605 return SideEffects(1ULL << kCanTriggerGCBit);
1606 }
1607
1608 static SideEffects DependsOnGC() {
1609 return SideEffects(1ULL << kDependsOnGCBit);
1610 }
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001613 SideEffects Union(SideEffects other) const {
1614 return SideEffects(flags_ | other.flags_);
1615 }
1616
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001617 SideEffects Exclusion(SideEffects other) const {
1618 return SideEffects(flags_ & ~other.flags_);
1619 }
1620
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001621 void Add(SideEffects other) {
1622 flags_ |= other.flags_;
1623 }
1624
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 bool Includes(SideEffects other) const {
1626 return (other.flags_ & flags_) == other.flags_;
1627 }
1628
1629 bool HasSideEffects() const {
1630 return (flags_ & kAllChangeBits);
1631 }
1632
1633 bool HasDependencies() const {
1634 return (flags_ & kAllDependOnBits);
1635 }
1636
1637 // Returns true if there are no side effects or dependencies.
1638 bool DoesNothing() const {
1639 return flags_ == 0;
1640 }
1641
Aart Bik854a02b2015-07-14 16:07:00 -07001642 // Returns true if something is written.
1643 bool DoesAnyWrite() const {
1644 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001645 }
1646
Aart Bik854a02b2015-07-14 16:07:00 -07001647 // Returns true if something is read.
1648 bool DoesAnyRead() const {
1649 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001650 }
1651
Aart Bik854a02b2015-07-14 16:07:00 -07001652 // Returns true if potentially everything is written and read
1653 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 bool DoesAllReadWrite() const {
1655 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1656 }
1657
Aart Bik854a02b2015-07-14 16:07:00 -07001658 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001659 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001660 }
1661
Roland Levillain0d5a2812015-11-13 10:07:31 +00001662 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001663 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1665 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001666 }
1667
1668 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001669 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001670 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001671 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001672 for (int s = kLastBit; s >= 0; s--) {
1673 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1674 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1675 // This is a bit for the GC side effect.
1676 if (current_bit_is_set) {
1677 flags += "GC";
1678 }
Aart Bik854a02b2015-07-14 16:07:00 -07001679 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 } else {
1681 // This is a bit for the array/field analysis.
1682 // The underscore character stands for the 'can trigger GC' bit.
1683 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1684 if (current_bit_is_set) {
1685 flags += kDebug[s];
1686 }
1687 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1688 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1689 flags += "|";
1690 }
1691 }
Aart Bik854a02b2015-07-14 16:07:00 -07001692 }
1693 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001694 }
1695
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001697
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 private:
1699 static constexpr int kFieldArrayAnalysisBits = 9;
1700
1701 static constexpr int kFieldWriteOffset = 0;
1702 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1703 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1704 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1705
1706 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1707
1708 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1709 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1710 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1711 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1712
1713 static constexpr int kLastBit = kDependsOnGCBit;
1714 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1715
1716 // Aliases.
1717
1718 static_assert(kChangeBits == kDependOnBits,
1719 "the 'change' bits should match the 'depend on' bits.");
1720
1721 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1722 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1723 static constexpr uint64_t kAllWrites =
1724 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1725 static constexpr uint64_t kAllReads =
1726 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001727
Aart Bik854a02b2015-07-14 16:07:00 -07001728 // Translates type to bit flag.
1729 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1730 CHECK_NE(type, Primitive::kPrimVoid);
1731 const uint64_t one = 1;
1732 const int shift = type; // 0-based consecutive enum
1733 DCHECK_LE(kFieldWriteOffset, shift);
1734 DCHECK_LT(shift, kArrayWriteOffset);
1735 return one << (type + offset);
1736 }
1737
1738 // Private constructor on direct flags value.
1739 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1740
1741 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001742};
1743
David Brazdiled596192015-01-23 10:39:45 +00001744// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001745class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001746 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001747 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1748 size_t number_of_vregs,
1749 ArtMethod* method,
1750 uint32_t dex_pc,
1751 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001752 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1753 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001754 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001755 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001756 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001757 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001758 }
1759
Mingyao Yang01b47b02017-02-03 12:09:57 -08001760 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001761 : HEnvironment(arena,
1762 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001763 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001764 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001765 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001766
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001767 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001768 if (parent_ != nullptr) {
1769 parent_->SetAndCopyParentChain(allocator, parent);
1770 } else {
1771 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1772 parent_->CopyFrom(parent);
1773 if (parent->GetParent() != nullptr) {
1774 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1775 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001776 }
David Brazdiled596192015-01-23 10:39:45 +00001777 }
1778
Vladimir Marko71bf8092015-09-15 15:33:14 +01001779 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001780 void CopyFrom(HEnvironment* environment);
1781
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001782 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1783 // input to the loop phi instead. This is for inserting instructions that
1784 // require an environment (like HDeoptimization) in the loop pre-header.
1785 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001786
1787 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001788 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001789 }
1790
1791 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001792 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001793 }
1794
David Brazdil1abb4192015-02-17 18:33:36 +00001795 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001796
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001797 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001798
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001799 HEnvironment* GetParent() const { return parent_; }
1800
1801 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001802 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 }
1804
1805 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001806 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001807 }
1808
1809 uint32_t GetDexPc() const {
1810 return dex_pc_;
1811 }
1812
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001813 ArtMethod* GetMethod() const {
1814 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001815 }
1816
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001817 HInstruction* GetHolder() const {
1818 return holder_;
1819 }
1820
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001821
1822 bool IsFromInlinedInvoke() const {
1823 return GetParent() != nullptr;
1824 }
1825
David Brazdiled596192015-01-23 10:39:45 +00001826 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1828 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001829 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001830 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001831 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001832
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001833 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001834 HInstruction* const holder_;
1835
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001836 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001837
1838 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1839};
1840
Vladimir Markof9f64412015-09-02 14:05:49 +01001841class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001842 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001843 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001844 : previous_(nullptr),
1845 next_(nullptr),
1846 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001847 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001848 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001849 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001850 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001851 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001852 locations_(nullptr),
1853 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001854 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001855 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001856 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1857 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1858 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859
Dave Allison20dfc792014-06-16 20:44:29 -07001860 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001861
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001862#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001863 enum InstructionKind {
1864 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1865 };
1866#undef DECLARE_KIND
1867
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001868 HInstruction* GetNext() const { return next_; }
1869 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001870
Calin Juravle77520bc2015-01-12 18:45:46 +00001871 HInstruction* GetNextDisregardingMoves() const;
1872 HInstruction* GetPreviousDisregardingMoves() const;
1873
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001874 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001875 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001877 bool IsInBlock() const { return block_ != nullptr; }
1878 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001879 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1880 bool IsIrreducibleLoopHeaderPhi() const {
1881 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1882 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001883
Vladimir Marko372f10e2016-05-17 16:30:10 +01001884 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1885
1886 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1887 // One virtual method is enough, just const_cast<> and then re-add the const.
1888 return ArrayRef<const HUserRecord<HInstruction*>>(
1889 const_cast<HInstruction*>(this)->GetInputRecords());
1890 }
1891
Vladimir Markoe9004912016-06-16 16:50:52 +01001892 HInputsRef GetInputs() {
1893 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001894 }
1895
Vladimir Markoe9004912016-06-16 16:50:52 +01001896 HConstInputsRef GetInputs() const {
1897 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001898 }
1899
1900 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001901 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001902
Aart Bik2767f4b2016-10-28 15:03:53 -07001903 bool HasInput(HInstruction* input) const {
1904 for (const HInstruction* i : GetInputs()) {
1905 if (i == input) {
1906 return true;
1907 }
1908 }
1909 return false;
1910 }
1911
Vladimir Marko372f10e2016-05-17 16:30:10 +01001912 void SetRawInputAt(size_t index, HInstruction* input) {
1913 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1914 }
1915
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001916 virtual void Accept(HGraphVisitor* visitor) = 0;
1917 virtual const char* DebugName() const = 0;
1918
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1920
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001921 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001922
1923 uint32_t GetDexPc() const { return dex_pc_; }
1924
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001925 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001926
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001927 // Can the instruction throw?
1928 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1929 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001930 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001931 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001932
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001933 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001934 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001935
Calin Juravle10e244f2015-01-26 18:54:32 +00001936 // Does not apply for all instructions, but having this at top level greatly
1937 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001938 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001939 virtual bool CanBeNull() const {
1940 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1941 return true;
1942 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001943
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001944 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001945 return false;
1946 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001947
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001948 virtual bool IsActualObject() const {
1949 return GetType() == Primitive::kPrimNot;
1950 }
1951
Calin Juravle2e768302015-07-28 14:41:11 +00001952 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001953
Calin Juravle61d544b2015-02-23 16:46:57 +00001954 ReferenceTypeInfo GetReferenceTypeInfo() const {
1955 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001956 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001957 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001958 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001959
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001961 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001962 // Note: fixup_end remains valid across push_front().
1963 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1964 HUseListNode<HInstruction*>* new_node =
1965 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1966 uses_.push_front(*new_node);
1967 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001968 }
1969
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001970 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001971 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001972 // Note: env_fixup_end remains valid across push_front().
1973 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1974 HUseListNode<HEnvironment*>* new_node =
1975 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1976 env_uses_.push_front(*new_node);
1977 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001978 }
1979
David Brazdil1abb4192015-02-17 18:33:36 +00001980 void RemoveAsUserOfInput(size_t input) {
1981 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001982 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1983 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1984 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001985 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986
Vladimir Marko372f10e2016-05-17 16:30:10 +01001987 void RemoveAsUserOfAllInputs() {
1988 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1989 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1990 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1991 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1992 }
1993 }
1994
David Brazdil1abb4192015-02-17 18:33:36 +00001995 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1996 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997
Vladimir Marko46817b82016-03-29 12:21:58 +01001998 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1999 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2000 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002001 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002002 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002003 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002004
Aart Bikcc42be02016-10-20 16:14:16 -07002005 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002006 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002007 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002008 !CanThrow() &&
2009 !IsSuspendCheck() &&
2010 !IsControlFlow() &&
2011 !IsNativeDebugInfo() &&
2012 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002013 // If we added an explicit barrier then we should keep it.
2014 !IsMemoryBarrier();
2015 }
2016
Aart Bikcc42be02016-10-20 16:14:16 -07002017 bool IsDeadAndRemovable() const {
2018 return IsRemovable() && !HasUses();
2019 }
2020
Roland Levillain6c82d402014-10-13 16:10:27 +01002021 // Does this instruction strictly dominate `other_instruction`?
2022 // Returns false if this instruction and `other_instruction` are the same.
2023 // Aborts if this instruction and `other_instruction` are both phis.
2024 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002025
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002026 int GetId() const { return id_; }
2027 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002028
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002029 int GetSsaIndex() const { return ssa_index_; }
2030 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2031 bool HasSsaIndex() const { return ssa_index_ != -1; }
2032
2033 bool HasEnvironment() const { return environment_ != nullptr; }
2034 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002035 // Set the `environment_` field. Raw because this method does not
2036 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002037 void SetRawEnvironment(HEnvironment* environment) {
2038 DCHECK(environment_ == nullptr);
2039 DCHECK_EQ(environment->GetHolder(), this);
2040 environment_ = environment;
2041 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002042
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002043 void InsertRawEnvironment(HEnvironment* environment) {
2044 DCHECK(environment_ != nullptr);
2045 DCHECK_EQ(environment->GetHolder(), this);
2046 DCHECK(environment->GetParent() == nullptr);
2047 environment->parent_ = environment_;
2048 environment_ = environment;
2049 }
2050
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002051 void RemoveEnvironment();
2052
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002053 // Set the environment of this instruction, copying it from `environment`. While
2054 // copying, the uses lists are being updated.
2055 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002056 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002057 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002058 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002059 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002060 if (environment->GetParent() != nullptr) {
2061 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2062 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002063 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002065 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2066 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002067 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002068 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002069 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002070 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002071 if (environment->GetParent() != nullptr) {
2072 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2073 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002074 }
2075
Nicolas Geoffray39468442014-09-02 15:17:15 +01002076 // Returns the number of entries in the environment. Typically, that is the
2077 // number of dex registers in a method. It could be more in case of inlining.
2078 size_t EnvironmentSize() const;
2079
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002080 LocationSummary* GetLocations() const { return locations_; }
2081 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002083 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002084 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002085
Alexandre Rames188d4312015-04-09 18:30:21 +01002086 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2087 // uses of this instruction by `other` are *not* updated.
2088 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2089 ReplaceWith(other);
2090 other->ReplaceInput(this, use_index);
2091 }
2092
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002093 // Move `this` instruction before `cursor`
2094 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002095
Vladimir Markofb337ea2015-11-25 15:25:10 +00002096 // Move `this` before its first user and out of any loops. If there is no
2097 // out-of-loop user that dominates all other users, move the instruction
2098 // to the end of the out-of-loop common dominator of the user's blocks.
2099 //
2100 // This can be used only on non-throwing instructions with no side effects that
2101 // have at least one use but no environment uses.
2102 void MoveBeforeFirstUserAndOutOfLoops();
2103
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002104#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 bool Is##type() const; \
2106 const H##type* As##type() const; \
2107 H##type* As##type();
2108
2109 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2110#undef INSTRUCTION_TYPE_CHECK
2111
2112#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002113 bool Is##type() const { return (As##type() != nullptr); } \
2114 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002115 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117#undef INSTRUCTION_TYPE_CHECK
2118
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002120 // TODO: this method is used by LICM and GVN with possibly different
2121 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002122 virtual bool CanBeMoved() const { return false; }
2123
2124 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002125 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002126 return false;
2127 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002128
2129 // Returns whether any data encoded in the two instructions is equal.
2130 // This method does not look at the inputs. Both instructions must be
2131 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002132 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002133 return false;
2134 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002135
2136 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002137 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002138 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002139 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002140
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002141 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2142 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2143 // the virtual function because the __attribute__((__pure__)) doesn't really
2144 // apply the strong requirement for virtual functions, preventing optimizations.
2145 InstructionKind GetKind() const PURE;
2146 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002147
2148 virtual size_t ComputeHashCode() const {
2149 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002150 for (const HInstruction* input : GetInputs()) {
2151 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002152 }
2153 return result;
2154 }
2155
2156 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002157 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002158 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002159
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002160 size_t GetLifetimePosition() const { return lifetime_position_; }
2161 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2162 LiveInterval* GetLiveInterval() const { return live_interval_; }
2163 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2164 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2165
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002166 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2167
2168 // Returns whether the code generation of the instruction will require to have access
2169 // to the current method. Such instructions are:
2170 // (1): Instructions that require an environment, as calling the runtime requires
2171 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002172 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2173 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002174 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002175 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002176 }
2177
Vladimir Markodc151b22015-10-15 18:02:30 +01002178 // Returns whether the code generation of the instruction will require to have access
2179 // to the dex cache of the current method's declaring class via the current method.
2180 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002181
Mark Mendellc4701932015-04-10 13:18:51 -04002182 // Does this instruction have any use in an environment before
2183 // control flow hits 'other'?
2184 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2185
2186 // Remove all references to environment uses of this instruction.
2187 // The caller must ensure that this is safe to do.
2188 void RemoveEnvironmentUsers();
2189
Vladimir Markoa1de9182016-02-25 11:37:38 +00002190 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2191 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002192
David Brazdil1abb4192015-02-17 18:33:36 +00002193 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002194 // If set, the machine code for this instruction is assumed to be generated by
2195 // its users. Used by liveness analysis to compute use positions accordingly.
2196 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2197 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2198 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2199 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2200
Vladimir Marko372f10e2016-05-17 16:30:10 +01002201 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2202 return GetInputRecords()[i];
2203 }
2204
2205 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2206 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2207 input_records[index] = input;
2208 }
David Brazdil1abb4192015-02-17 18:33:36 +00002209
Vladimir Markoa1de9182016-02-25 11:37:38 +00002210 uint32_t GetPackedFields() const {
2211 return packed_fields_;
2212 }
2213
2214 template <size_t flag>
2215 bool GetPackedFlag() const {
2216 return (packed_fields_ & (1u << flag)) != 0u;
2217 }
2218
2219 template <size_t flag>
2220 void SetPackedFlag(bool value = true) {
2221 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2222 }
2223
2224 template <typename BitFieldType>
2225 typename BitFieldType::value_type GetPackedField() const {
2226 return BitFieldType::Decode(packed_fields_);
2227 }
2228
2229 template <typename BitFieldType>
2230 void SetPackedField(typename BitFieldType::value_type value) {
2231 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2232 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2233 }
2234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002235 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002236 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2237 auto before_use_node = uses_.before_begin();
2238 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2239 HInstruction* user = use_node->GetUser();
2240 size_t input_index = use_node->GetIndex();
2241 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2242 before_use_node = use_node;
2243 }
2244 }
2245
2246 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2247 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2248 if (next != uses_.end()) {
2249 HInstruction* next_user = next->GetUser();
2250 size_t next_index = next->GetIndex();
2251 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2252 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2253 }
2254 }
2255
2256 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2257 auto before_env_use_node = env_uses_.before_begin();
2258 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2259 HEnvironment* user = env_use_node->GetUser();
2260 size_t input_index = env_use_node->GetIndex();
2261 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2262 before_env_use_node = env_use_node;
2263 }
2264 }
2265
2266 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2267 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2268 if (next != env_uses_.end()) {
2269 HEnvironment* next_user = next->GetUser();
2270 size_t next_index = next->GetIndex();
2271 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2272 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2273 }
2274 }
David Brazdil1abb4192015-02-17 18:33:36 +00002275
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002276 HInstruction* previous_;
2277 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002278 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002279 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002280
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002281 // An instruction gets an id when it is added to the graph.
2282 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002283 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002284 int id_;
2285
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002286 // When doing liveness analysis, instructions that have uses get an SSA index.
2287 int ssa_index_;
2288
Vladimir Markoa1de9182016-02-25 11:37:38 +00002289 // Packed fields.
2290 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002291
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002292 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002293 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002294
2295 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002296 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002297
Nicolas Geoffray39468442014-09-02 15:17:15 +01002298 // The environment associated with this instruction. Not null if the instruction
2299 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002300 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002301
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002302 // Set by the code generator.
2303 LocationSummary* locations_;
2304
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002305 // Set by the liveness analysis.
2306 LiveInterval* live_interval_;
2307
2308 // Set by the liveness analysis, this is the position in a linear
2309 // order of blocks where this instruction's live interval start.
2310 size_t lifetime_position_;
2311
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002312 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002313
Vladimir Markoa1de9182016-02-25 11:37:38 +00002314 // The reference handle part of the reference type info.
2315 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002316 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002317 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002318
David Brazdil1abb4192015-02-17 18:33:36 +00002319 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002321 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002322 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002323 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324
2325 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2326};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002327std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002328
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002329// Iterates over the instructions, while preserving the next instruction
2330// in case the current instruction gets removed from the list by the user
2331// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002332class HInstructionIterator : public ValueObject {
2333 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002334 explicit HInstructionIterator(const HInstructionList& instructions)
2335 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002336 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002337 }
2338
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002339 bool Done() const { return instruction_ == nullptr; }
2340 HInstruction* Current() const { return instruction_; }
2341 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002343 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002344 }
2345
2346 private:
2347 HInstruction* instruction_;
2348 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002349
2350 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351};
2352
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002353// Iterates over the instructions without saving the next instruction,
2354// therefore handling changes in the graph potentially made by the user
2355// of this iterator.
2356class HInstructionIteratorHandleChanges : public ValueObject {
2357 public:
2358 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2359 : instruction_(instructions.first_instruction_) {
2360 }
2361
2362 bool Done() const { return instruction_ == nullptr; }
2363 HInstruction* Current() const { return instruction_; }
2364 void Advance() {
2365 instruction_ = instruction_->GetNext();
2366 }
2367
2368 private:
2369 HInstruction* instruction_;
2370
2371 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2372};
2373
2374
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002375class HBackwardInstructionIterator : public ValueObject {
2376 public:
2377 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2378 : instruction_(instructions.last_instruction_) {
2379 next_ = Done() ? nullptr : instruction_->GetPrevious();
2380 }
2381
2382 bool Done() const { return instruction_ == nullptr; }
2383 HInstruction* Current() const { return instruction_; }
2384 void Advance() {
2385 instruction_ = next_;
2386 next_ = Done() ? nullptr : instruction_->GetPrevious();
2387 }
2388
2389 private:
2390 HInstruction* instruction_;
2391 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002392
2393 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002394};
2395
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002396class HVariableInputSizeInstruction : public HInstruction {
2397 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002398 using HInstruction::GetInputRecords; // Keep the const version visible.
2399 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2400 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2401 }
2402
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002403 void AddInput(HInstruction* input);
2404 void InsertInputAt(size_t index, HInstruction* input);
2405 void RemoveInputAt(size_t index);
2406
2407 protected:
2408 HVariableInputSizeInstruction(SideEffects side_effects,
2409 uint32_t dex_pc,
2410 ArenaAllocator* arena,
2411 size_t number_of_inputs,
2412 ArenaAllocKind kind)
2413 : HInstruction(side_effects, dex_pc),
2414 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2415
2416 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2417
2418 private:
2419 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2420};
2421
Vladimir Markof9f64412015-09-02 14:05:49 +01002422template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002423class HTemplateInstruction: public HInstruction {
2424 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002425 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002426 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002427 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002428
Vladimir Marko372f10e2016-05-17 16:30:10 +01002429 using HInstruction::GetInputRecords; // Keep the const version visible.
2430 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2431 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002432 }
2433
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002434 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002435 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002436
2437 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002438};
2439
Vladimir Markof9f64412015-09-02 14:05:49 +01002440// HTemplateInstruction specialization for N=0.
2441template<>
2442class HTemplateInstruction<0>: public HInstruction {
2443 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002444 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002445 : HInstruction(side_effects, dex_pc) {}
2446
Vladimir Markof9f64412015-09-02 14:05:49 +01002447 virtual ~HTemplateInstruction() {}
2448
Vladimir Marko372f10e2016-05-17 16:30:10 +01002449 using HInstruction::GetInputRecords; // Keep the const version visible.
2450 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2451 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002452 }
2453
2454 private:
2455 friend class SsaBuilder;
2456};
2457
Dave Allison20dfc792014-06-16 20:44:29 -07002458template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002459class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002460 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002461 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002462 : HTemplateInstruction<N>(side_effects, dex_pc) {
2463 this->template SetPackedField<TypeField>(type);
2464 }
Dave Allison20dfc792014-06-16 20:44:29 -07002465 virtual ~HExpression() {}
2466
Vladimir Markoa1de9182016-02-25 11:37:38 +00002467 Primitive::Type GetType() const OVERRIDE {
2468 return TypeField::Decode(this->GetPackedFields());
2469 }
Dave Allison20dfc792014-06-16 20:44:29 -07002470
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002471 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002472 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2473 static constexpr size_t kFieldTypeSize =
2474 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2475 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2476 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2477 "Too many packed fields.");
2478 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002479};
2480
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002481// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2482// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002483class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002484 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002485 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2486 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002488 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002489
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002490 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002491
2492 private:
2493 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2494};
2495
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002496// Represents dex's RETURN opcodes. A HReturn is a control flow
2497// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002498class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002500 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2501 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002502 SetRawInputAt(0, value);
2503 }
2504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002505 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002506
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002507 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002508
2509 private:
2510 DISALLOW_COPY_AND_ASSIGN(HReturn);
2511};
2512
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002513class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002514 public:
2515 HPhi(ArenaAllocator* arena,
2516 uint32_t reg_number,
2517 size_t number_of_inputs,
2518 Primitive::Type type,
2519 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002520 : HVariableInputSizeInstruction(
2521 SideEffects::None(),
2522 dex_pc,
2523 arena,
2524 number_of_inputs,
2525 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002526 reg_number_(reg_number) {
2527 SetPackedField<TypeField>(ToPhiType(type));
2528 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2529 // Phis are constructed live and marked dead if conflicting or unused.
2530 // Individual steps of SsaBuilder should assume that if a phi has been
2531 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2532 SetPackedFlag<kFlagIsLive>(true);
2533 SetPackedFlag<kFlagCanBeNull>(true);
2534 }
2535
2536 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2537 static Primitive::Type ToPhiType(Primitive::Type type) {
2538 return Primitive::PrimitiveKind(type);
2539 }
2540
2541 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2542
David Brazdildee58d62016-04-07 09:54:26 +00002543 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2544 void SetType(Primitive::Type new_type) {
2545 // Make sure that only valid type changes occur. The following are allowed:
2546 // (1) int -> float/ref (primitive type propagation),
2547 // (2) long -> double (primitive type propagation).
2548 DCHECK(GetType() == new_type ||
2549 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2550 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2551 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2552 SetPackedField<TypeField>(new_type);
2553 }
2554
2555 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2556 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2557
2558 uint32_t GetRegNumber() const { return reg_number_; }
2559
2560 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2561 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2562 bool IsDead() const { return !IsLive(); }
2563 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2564
Vladimir Markoe9004912016-06-16 16:50:52 +01002565 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002566 return other != nullptr
2567 && other->IsPhi()
2568 && other->AsPhi()->GetBlock() == GetBlock()
2569 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2570 }
2571
2572 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2573 // An equivalent phi is a phi having the same dex register and type.
2574 // It assumes that phis with the same dex register are adjacent.
2575 HPhi* GetNextEquivalentPhiWithSameType() {
2576 HInstruction* next = GetNext();
2577 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2578 if (next->GetType() == GetType()) {
2579 return next->AsPhi();
2580 }
2581 next = next->GetNext();
2582 }
2583 return nullptr;
2584 }
2585
2586 DECLARE_INSTRUCTION(Phi);
2587
David Brazdildee58d62016-04-07 09:54:26 +00002588 private:
2589 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2590 static constexpr size_t kFieldTypeSize =
2591 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2592 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2593 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2594 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2595 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2596 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2597
David Brazdildee58d62016-04-07 09:54:26 +00002598 const uint32_t reg_number_;
2599
2600 DISALLOW_COPY_AND_ASSIGN(HPhi);
2601};
2602
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002603// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002604// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002605// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002606class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002609
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002610 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002611
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002612 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002613
2614 private:
2615 DISALLOW_COPY_AND_ASSIGN(HExit);
2616};
2617
2618// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002619class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002620 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002621 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002622
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002623 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002625 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002626 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002627 }
2628
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002629 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002630
2631 private:
2632 DISALLOW_COPY_AND_ASSIGN(HGoto);
2633};
2634
Roland Levillain9867bc72015-08-05 10:21:34 +01002635class HConstant : public HExpression<0> {
2636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002637 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2638 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002639
2640 bool CanBeMoved() const OVERRIDE { return true; }
2641
Roland Levillain1a653882016-03-18 18:05:57 +00002642 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002643 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002644 // Is this constant 0 in the arithmetic sense?
2645 virtual bool IsArithmeticZero() const { return false; }
2646 // Is this constant a 0-bit pattern?
2647 virtual bool IsZeroBitPattern() const { return false; }
2648 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002649 virtual bool IsOne() const { return false; }
2650
David Brazdil77a48ae2015-09-15 12:34:04 +00002651 virtual uint64_t GetValueAsUint64() const = 0;
2652
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002653 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002654
2655 private:
2656 DISALLOW_COPY_AND_ASSIGN(HConstant);
2657};
2658
Vladimir Markofcb503c2016-05-18 12:48:17 +01002659class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002660 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002661 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002662 return true;
2663 }
2664
David Brazdil77a48ae2015-09-15 12:34:04 +00002665 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2666
Roland Levillain9867bc72015-08-05 10:21:34 +01002667 size_t ComputeHashCode() const OVERRIDE { return 0; }
2668
Roland Levillain1a653882016-03-18 18:05:57 +00002669 // The null constant representation is a 0-bit pattern.
2670 virtual bool IsZeroBitPattern() const { return true; }
2671
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 DECLARE_INSTRUCTION(NullConstant);
2673
2674 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002675 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002676
2677 friend class HGraph;
2678 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2679};
2680
2681// Constants of the type int. Those can be from Dex instructions, or
2682// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002683class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002684 public:
2685 int32_t GetValue() const { return value_; }
2686
David Brazdil9f389d42015-10-01 14:32:56 +01002687 uint64_t GetValueAsUint64() const OVERRIDE {
2688 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2689 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002690
Vladimir Marko372f10e2016-05-17 16:30:10 +01002691 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002692 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 return other->AsIntConstant()->value_ == value_;
2694 }
2695
2696 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2697
2698 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002699 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2700 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002701 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2702
Roland Levillain1a653882016-03-18 18:05:57 +00002703 // Integer constants are used to encode Boolean values as well,
2704 // where 1 means true and 0 means false.
2705 bool IsTrue() const { return GetValue() == 1; }
2706 bool IsFalse() const { return GetValue() == 0; }
2707
Roland Levillain9867bc72015-08-05 10:21:34 +01002708 DECLARE_INSTRUCTION(IntConstant);
2709
2710 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002711 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2712 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2713 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2714 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002715
2716 const int32_t value_;
2717
2718 friend class HGraph;
2719 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2720 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2721 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2722};
2723
Vladimir Markofcb503c2016-05-18 12:48:17 +01002724class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002725 public:
2726 int64_t GetValue() const { return value_; }
2727
David Brazdil77a48ae2015-09-15 12:34:04 +00002728 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2729
Vladimir Marko372f10e2016-05-17 16:30:10 +01002730 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002731 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 return other->AsLongConstant()->value_ == value_;
2733 }
2734
2735 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2736
2737 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002738 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2739 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002740 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2741
2742 DECLARE_INSTRUCTION(LongConstant);
2743
2744 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002745 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2746 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002747
2748 const int64_t value_;
2749
2750 friend class HGraph;
2751 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2752};
Dave Allison20dfc792014-06-16 20:44:29 -07002753
Vladimir Markofcb503c2016-05-18 12:48:17 +01002754class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002755 public:
2756 float GetValue() const { return value_; }
2757
2758 uint64_t GetValueAsUint64() const OVERRIDE {
2759 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2760 }
2761
Vladimir Marko372f10e2016-05-17 16:30:10 +01002762 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002763 DCHECK(other->IsFloatConstant()) << other->DebugName();
2764 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2765 }
2766
2767 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2768
2769 bool IsMinusOne() const OVERRIDE {
2770 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2771 }
Roland Levillain1a653882016-03-18 18:05:57 +00002772 bool IsArithmeticZero() const OVERRIDE {
2773 return std::fpclassify(value_) == FP_ZERO;
2774 }
2775 bool IsArithmeticPositiveZero() const {
2776 return IsArithmeticZero() && !std::signbit(value_);
2777 }
2778 bool IsArithmeticNegativeZero() const {
2779 return IsArithmeticZero() && std::signbit(value_);
2780 }
2781 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002782 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002783 }
2784 bool IsOne() const OVERRIDE {
2785 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2786 }
2787 bool IsNaN() const {
2788 return std::isnan(value_);
2789 }
2790
2791 DECLARE_INSTRUCTION(FloatConstant);
2792
2793 private:
2794 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2795 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2796 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2797 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2798
2799 const float value_;
2800
2801 // Only the SsaBuilder and HGraph can create floating-point constants.
2802 friend class SsaBuilder;
2803 friend class HGraph;
2804 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2805};
2806
Vladimir Markofcb503c2016-05-18 12:48:17 +01002807class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002808 public:
2809 double GetValue() const { return value_; }
2810
2811 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2812
Vladimir Marko372f10e2016-05-17 16:30:10 +01002813 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002814 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2815 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2816 }
2817
2818 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2819
2820 bool IsMinusOne() const OVERRIDE {
2821 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2822 }
Roland Levillain1a653882016-03-18 18:05:57 +00002823 bool IsArithmeticZero() const OVERRIDE {
2824 return std::fpclassify(value_) == FP_ZERO;
2825 }
2826 bool IsArithmeticPositiveZero() const {
2827 return IsArithmeticZero() && !std::signbit(value_);
2828 }
2829 bool IsArithmeticNegativeZero() const {
2830 return IsArithmeticZero() && std::signbit(value_);
2831 }
2832 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002833 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002834 }
2835 bool IsOne() const OVERRIDE {
2836 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2837 }
2838 bool IsNaN() const {
2839 return std::isnan(value_);
2840 }
2841
2842 DECLARE_INSTRUCTION(DoubleConstant);
2843
2844 private:
2845 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2846 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2847 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2848 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2849
2850 const double value_;
2851
2852 // Only the SsaBuilder and HGraph can create floating-point constants.
2853 friend class SsaBuilder;
2854 friend class HGraph;
2855 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2856};
2857
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002858// Conditional branch. A block ending with an HIf instruction must have
2859// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002860class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002862 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2863 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002864 SetRawInputAt(0, input);
2865 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002866
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002867 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002868
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002869 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002870 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002871 }
2872
2873 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002874 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002875 }
2876
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002877 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002878
2879 private:
2880 DISALLOW_COPY_AND_ASSIGN(HIf);
2881};
2882
David Brazdilfc6a86a2015-06-26 10:33:45 +00002883
2884// Abstract instruction which marks the beginning and/or end of a try block and
2885// links it to the respective exception handlers. Behaves the same as a Goto in
2886// non-exceptional control flow.
2887// Normal-flow successor is stored at index zero, exception handlers under
2888// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002889class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002890 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002891 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002892 kEntry,
2893 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002894 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002895 };
2896
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002897 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002898 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2899 SetPackedField<BoundaryKindField>(kind);
2900 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002901
2902 bool IsControlFlow() const OVERRIDE { return true; }
2903
2904 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002905 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002906
David Brazdild26a4112015-11-10 11:07:31 +00002907 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2908 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2909 }
2910
David Brazdilfc6a86a2015-06-26 10:33:45 +00002911 // Returns whether `handler` is among its exception handlers (non-zero index
2912 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002913 bool HasExceptionHandler(const HBasicBlock& handler) const {
2914 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002915 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002916 }
2917
2918 // If not present already, adds `handler` to its block's list of exception
2919 // handlers.
2920 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002921 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002922 GetBlock()->AddSuccessor(handler);
2923 }
2924 }
2925
Vladimir Markoa1de9182016-02-25 11:37:38 +00002926 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2927 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002928
David Brazdilffee3d32015-07-06 11:48:53 +01002929 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2930
David Brazdilfc6a86a2015-06-26 10:33:45 +00002931 DECLARE_INSTRUCTION(TryBoundary);
2932
2933 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002934 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2935 static constexpr size_t kFieldBoundaryKindSize =
2936 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2937 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2938 kFieldBoundaryKind + kFieldBoundaryKindSize;
2939 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2940 "Too many packed fields.");
2941 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002942
2943 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2944};
2945
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002946// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002947class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002948 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002949 // We set CanTriggerGC to prevent any intermediate address to be live
2950 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002951 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002952 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953 SetRawInputAt(0, cond);
2954 }
2955
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002956 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002957 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002958 return true;
2959 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002960 bool NeedsEnvironment() const OVERRIDE { return true; }
2961 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002962
2963 DECLARE_INSTRUCTION(Deoptimize);
2964
2965 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002966 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2967};
2968
Mingyao Yang063fc772016-08-02 11:02:54 -07002969// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2970// The compiled code checks this flag value in a guard before devirtualized call and
2971// if it's true, starts to do deoptimization.
2972// It has a 4-byte slot on stack.
2973// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002974class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002975 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002976 // CHA guards are only optimized in a separate pass and it has no side effects
2977 // with regard to other passes.
2978 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2979 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002980 }
2981
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002982 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2983
2984 // We do all CHA guard elimination/motion in a single pass, after which there is no
2985 // further guard elimination/motion since a guard might have been used for justification
2986 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2987 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002988 bool CanBeMoved() const OVERRIDE { return false; }
2989
2990 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2991
2992 private:
2993 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2994};
2995
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002996// Represents the ArtMethod that was passed as a first argument to
2997// the method. It is used by instructions that depend on it, like
2998// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002999class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003000 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003001 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3002 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003003
3004 DECLARE_INSTRUCTION(CurrentMethod);
3005
3006 private:
3007 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3008};
3009
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003010// Fetches an ArtMethod from the virtual table or the interface method table
3011// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003012class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003013 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003014 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003015 kVTable,
3016 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003017 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003018 };
3019 HClassTableGet(HInstruction* cls,
3020 Primitive::Type type,
3021 TableKind kind,
3022 size_t index,
3023 uint32_t dex_pc)
3024 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003025 index_(index) {
3026 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003027 SetRawInputAt(0, cls);
3028 }
3029
3030 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003031 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003032 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003033 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003034 }
3035
Vladimir Markoa1de9182016-02-25 11:37:38 +00003036 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003037 size_t GetIndex() const { return index_; }
3038
3039 DECLARE_INSTRUCTION(ClassTableGet);
3040
3041 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003042 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3043 static constexpr size_t kFieldTableKindSize =
3044 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3045 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3046 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3047 "Too many packed fields.");
3048 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3049
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003050 // The index of the ArtMethod in the table.
3051 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003052
3053 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3054};
3055
Mark Mendellfe57faa2015-09-18 09:26:15 -04003056// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3057// have one successor for each entry in the switch table, and the final successor
3058// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003059class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003060 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003061 HPackedSwitch(int32_t start_value,
3062 uint32_t num_entries,
3063 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003064 uint32_t dex_pc = kNoDexPc)
3065 : HTemplateInstruction(SideEffects::None(), dex_pc),
3066 start_value_(start_value),
3067 num_entries_(num_entries) {
3068 SetRawInputAt(0, input);
3069 }
3070
3071 bool IsControlFlow() const OVERRIDE { return true; }
3072
3073 int32_t GetStartValue() const { return start_value_; }
3074
Vladimir Marko211c2112015-09-24 16:52:33 +01003075 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003076
3077 HBasicBlock* GetDefaultBlock() const {
3078 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003079 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003080 }
3081 DECLARE_INSTRUCTION(PackedSwitch);
3082
3083 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003084 const int32_t start_value_;
3085 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003086
3087 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3088};
3089
Roland Levillain88cb1752014-10-20 16:36:47 +01003090class HUnaryOperation : public HExpression<1> {
3091 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003092 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3093 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003094 SetRawInputAt(0, input);
3095 }
3096
3097 HInstruction* GetInput() const { return InputAt(0); }
3098 Primitive::Type GetResultType() const { return GetType(); }
3099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003100 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003101 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003102 return true;
3103 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003104
Roland Levillain31dd3d62016-02-16 12:21:02 +00003105 // Try to statically evaluate `this` and return a HConstant
3106 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003107 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003108 HConstant* TryStaticEvaluation() const;
3109
3110 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003111 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3112 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003113 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3114 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003115
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003116 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003117
3118 private:
3119 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3120};
3121
Dave Allison20dfc792014-06-16 20:44:29 -07003122class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003124 HBinaryOperation(Primitive::Type result_type,
3125 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003126 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003127 SideEffects side_effects = SideEffects::None(),
3128 uint32_t dex_pc = kNoDexPc)
3129 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003130 SetRawInputAt(0, left);
3131 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003132 }
3133
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003134 HInstruction* GetLeft() const { return InputAt(0); }
3135 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003136 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003137
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003138 virtual bool IsCommutative() const { return false; }
3139
3140 // Put constant on the right.
3141 // Returns whether order is changed.
3142 bool OrderInputsWithConstantOnTheRight() {
3143 HInstruction* left = InputAt(0);
3144 HInstruction* right = InputAt(1);
3145 if (left->IsConstant() && !right->IsConstant()) {
3146 ReplaceInput(right, 0);
3147 ReplaceInput(left, 1);
3148 return true;
3149 }
3150 return false;
3151 }
3152
3153 // Order inputs by instruction id, but favor constant on the right side.
3154 // This helps GVN for commutative ops.
3155 void OrderInputs() {
3156 DCHECK(IsCommutative());
3157 HInstruction* left = InputAt(0);
3158 HInstruction* right = InputAt(1);
3159 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3160 return;
3161 }
3162 if (OrderInputsWithConstantOnTheRight()) {
3163 return;
3164 }
3165 // Order according to instruction id.
3166 if (left->GetId() > right->GetId()) {
3167 ReplaceInput(right, 0);
3168 ReplaceInput(left, 1);
3169 }
3170 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003171
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003172 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003173 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003174 return true;
3175 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003176
Roland Levillain31dd3d62016-02-16 12:21:02 +00003177 // Try to statically evaluate `this` and return a HConstant
3178 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003179 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003180 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003181
3182 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003183 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3184 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003185 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3186 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003187 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003188 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3189 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003190 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3191 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003192 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3193 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003194 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003195 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3196 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003197
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003198 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003199 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003200 HConstant* GetConstantRight() const;
3201
3202 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003203 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003204 HInstruction* GetLeastConstantLeft() const;
3205
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003206 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003207
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003208 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003209 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3210};
3211
Mark Mendellc4701932015-04-10 13:18:51 -04003212// The comparison bias applies for floating point operations and indicates how NaN
3213// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003214enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003215 kNoBias, // bias is not applicable (i.e. for long operation)
3216 kGtBias, // return 1 for NaN comparisons
3217 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003218 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003219};
3220
Roland Levillain31dd3d62016-02-16 12:21:02 +00003221std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3222
Dave Allison20dfc792014-06-16 20:44:29 -07003223class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003224 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003225 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003226 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3227 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3228 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003229
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003230 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003231 // `instruction`, and disregard moves in between.
3232 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003233
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003234 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003235
3236 virtual IfCondition GetCondition() const = 0;
3237
Mark Mendellc4701932015-04-10 13:18:51 -04003238 virtual IfCondition GetOppositeCondition() const = 0;
3239
Vladimir Markoa1de9182016-02-25 11:37:38 +00003240 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003241 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3242
Vladimir Markoa1de9182016-02-25 11:37:38 +00003243 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3244 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003245
Vladimir Marko372f10e2016-05-17 16:30:10 +01003246 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003247 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003248 }
3249
Roland Levillain4fa13f62015-07-06 18:11:54 +01003250 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003251 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003252 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003253 if (if_cond == kCondNE) {
3254 return true;
3255 } else if (if_cond == kCondEQ) {
3256 return false;
3257 }
3258 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003259 }
3260
3261 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003262 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003263 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003264 if (if_cond == kCondEQ) {
3265 return true;
3266 } else if (if_cond == kCondNE) {
3267 return false;
3268 }
3269 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003270 }
3271
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003273 // Needed if we merge a HCompare into a HCondition.
3274 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3275 static constexpr size_t kFieldComparisonBiasSize =
3276 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3277 static constexpr size_t kNumberOfConditionPackedBits =
3278 kFieldComparisonBias + kFieldComparisonBiasSize;
3279 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3280 using ComparisonBiasField =
3281 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3282
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 template <typename T>
3284 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3285
3286 template <typename T>
3287 int32_t CompareFP(T x, T y) const {
3288 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3289 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3290 // Handle the bias.
3291 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3292 }
3293
3294 // Return an integer constant containing the result of a condition evaluated at compile time.
3295 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3296 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3297 }
3298
Dave Allison20dfc792014-06-16 20:44:29 -07003299 private:
3300 DISALLOW_COPY_AND_ASSIGN(HCondition);
3301};
3302
3303// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003304class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003305 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003306 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3307 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003308
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003309 bool IsCommutative() const OVERRIDE { return true; }
3310
Vladimir Marko9e23df52015-11-10 17:14:35 +00003311 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3312 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313 return MakeConstantCondition(true, GetDexPc());
3314 }
3315 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3317 }
3318 // In the following Evaluate methods, a HCompare instruction has
3319 // been merged into this HEqual instruction; evaluate it as
3320 // `Compare(x, y) == 0`.
3321 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3322 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3323 GetDexPc());
3324 }
3325 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3326 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3327 }
3328 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3329 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003330 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003331
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003332 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003333
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003334 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003335 return kCondEQ;
3336 }
3337
Mark Mendellc4701932015-04-10 13:18:51 -04003338 IfCondition GetOppositeCondition() const OVERRIDE {
3339 return kCondNE;
3340 }
3341
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003342 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003343 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003344
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003345 DISALLOW_COPY_AND_ASSIGN(HEqual);
3346};
3347
Vladimir Markofcb503c2016-05-18 12:48:17 +01003348class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003349 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003350 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3351 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003352
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003353 bool IsCommutative() const OVERRIDE { return true; }
3354
Vladimir Marko9e23df52015-11-10 17:14:35 +00003355 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3356 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 return MakeConstantCondition(false, GetDexPc());
3358 }
3359 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3361 }
3362 // In the following Evaluate methods, a HCompare instruction has
3363 // been merged into this HNotEqual instruction; evaluate it as
3364 // `Compare(x, y) != 0`.
3365 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3366 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3367 }
3368 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3369 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3370 }
3371 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3372 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003373 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003374
Dave Allison20dfc792014-06-16 20:44:29 -07003375 DECLARE_INSTRUCTION(NotEqual);
3376
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003377 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003378 return kCondNE;
3379 }
3380
Mark Mendellc4701932015-04-10 13:18:51 -04003381 IfCondition GetOppositeCondition() const OVERRIDE {
3382 return kCondEQ;
3383 }
3384
Dave Allison20dfc792014-06-16 20:44:29 -07003385 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003386 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003387
Dave Allison20dfc792014-06-16 20:44:29 -07003388 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3389};
3390
Vladimir Markofcb503c2016-05-18 12:48:17 +01003391class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003392 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003393 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3394 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003395
Roland Levillain9867bc72015-08-05 10:21:34 +01003396 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003398 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003399 // In the following Evaluate methods, a HCompare instruction has
3400 // been merged into this HLessThan instruction; evaluate it as
3401 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003402 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003403 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3404 }
3405 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3406 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3407 }
3408 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3409 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003410 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003411
Dave Allison20dfc792014-06-16 20:44:29 -07003412 DECLARE_INSTRUCTION(LessThan);
3413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003414 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003415 return kCondLT;
3416 }
3417
Mark Mendellc4701932015-04-10 13:18:51 -04003418 IfCondition GetOppositeCondition() const OVERRIDE {
3419 return kCondGE;
3420 }
3421
Dave Allison20dfc792014-06-16 20:44:29 -07003422 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003423 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003424
Dave Allison20dfc792014-06-16 20:44:29 -07003425 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3426};
3427
Vladimir Markofcb503c2016-05-18 12:48:17 +01003428class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003429 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003430 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3431 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003432
Roland Levillain9867bc72015-08-05 10:21:34 +01003433 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003434 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003435 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003436 // In the following Evaluate methods, a HCompare instruction has
3437 // been merged into this HLessThanOrEqual instruction; evaluate it as
3438 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003439 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003440 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3441 }
3442 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3443 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3444 }
3445 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3446 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003447 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003448
Dave Allison20dfc792014-06-16 20:44:29 -07003449 DECLARE_INSTRUCTION(LessThanOrEqual);
3450
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003451 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003452 return kCondLE;
3453 }
3454
Mark Mendellc4701932015-04-10 13:18:51 -04003455 IfCondition GetOppositeCondition() const OVERRIDE {
3456 return kCondGT;
3457 }
3458
Dave Allison20dfc792014-06-16 20:44:29 -07003459 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003460 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003461
Dave Allison20dfc792014-06-16 20:44:29 -07003462 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3463};
3464
Vladimir Markofcb503c2016-05-18 12:48:17 +01003465class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003466 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003467 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3468 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003469
Roland Levillain9867bc72015-08-05 10:21:34 +01003470 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003472 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003473 // In the following Evaluate methods, a HCompare instruction has
3474 // been merged into this HGreaterThan instruction; evaluate it as
3475 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003476 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003477 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3478 }
3479 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3480 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3481 }
3482 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3483 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003484 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003485
Dave Allison20dfc792014-06-16 20:44:29 -07003486 DECLARE_INSTRUCTION(GreaterThan);
3487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003488 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003489 return kCondGT;
3490 }
3491
Mark Mendellc4701932015-04-10 13:18:51 -04003492 IfCondition GetOppositeCondition() const OVERRIDE {
3493 return kCondLE;
3494 }
3495
Dave Allison20dfc792014-06-16 20:44:29 -07003496 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003497 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003498
Dave Allison20dfc792014-06-16 20:44:29 -07003499 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3500};
3501
Vladimir Markofcb503c2016-05-18 12:48:17 +01003502class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003503 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003504 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3505 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003506
Roland Levillain9867bc72015-08-05 10:21:34 +01003507 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003508 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003509 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 // In the following Evaluate methods, a HCompare instruction has
3511 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3512 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003513 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003514 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3515 }
3516 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3517 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3518 }
3519 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3520 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003521 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003522
Dave Allison20dfc792014-06-16 20:44:29 -07003523 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3524
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003525 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003526 return kCondGE;
3527 }
3528
Mark Mendellc4701932015-04-10 13:18:51 -04003529 IfCondition GetOppositeCondition() const OVERRIDE {
3530 return kCondLT;
3531 }
3532
Dave Allison20dfc792014-06-16 20:44:29 -07003533 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003534 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003535
Dave Allison20dfc792014-06-16 20:44:29 -07003536 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3537};
3538
Vladimir Markofcb503c2016-05-18 12:48:17 +01003539class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003540 public:
3541 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3542 : HCondition(first, second, dex_pc) {}
3543
3544 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003545 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003546 }
3547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3549 }
3550 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3551 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3552 LOG(FATAL) << DebugName() << " is not defined for float values";
3553 UNREACHABLE();
3554 }
3555 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3556 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3557 LOG(FATAL) << DebugName() << " is not defined for double values";
3558 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003559 }
3560
3561 DECLARE_INSTRUCTION(Below);
3562
3563 IfCondition GetCondition() const OVERRIDE {
3564 return kCondB;
3565 }
3566
3567 IfCondition GetOppositeCondition() const OVERRIDE {
3568 return kCondAE;
3569 }
3570
3571 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003572 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeUnsigned(x) < MakeUnsigned(y);
3574 }
Aart Bike9f37602015-10-09 11:15:55 -07003575
3576 DISALLOW_COPY_AND_ASSIGN(HBelow);
3577};
3578
Vladimir Markofcb503c2016-05-18 12:48:17 +01003579class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003580 public:
3581 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3582 : HCondition(first, second, dex_pc) {}
3583
3584 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003586 }
3587 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003588 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3589 }
3590 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3591 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3592 LOG(FATAL) << DebugName() << " is not defined for float values";
3593 UNREACHABLE();
3594 }
3595 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3596 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3597 LOG(FATAL) << DebugName() << " is not defined for double values";
3598 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003599 }
3600
3601 DECLARE_INSTRUCTION(BelowOrEqual);
3602
3603 IfCondition GetCondition() const OVERRIDE {
3604 return kCondBE;
3605 }
3606
3607 IfCondition GetOppositeCondition() const OVERRIDE {
3608 return kCondA;
3609 }
3610
3611 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003612 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 return MakeUnsigned(x) <= MakeUnsigned(y);
3614 }
Aart Bike9f37602015-10-09 11:15:55 -07003615
3616 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3617};
3618
Vladimir Markofcb503c2016-05-18 12:48:17 +01003619class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003620 public:
3621 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3622 : HCondition(first, second, dex_pc) {}
3623
3624 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003625 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003626 }
3627 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3629 }
3630 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3631 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3632 LOG(FATAL) << DebugName() << " is not defined for float values";
3633 UNREACHABLE();
3634 }
3635 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3636 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3637 LOG(FATAL) << DebugName() << " is not defined for double values";
3638 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003639 }
3640
3641 DECLARE_INSTRUCTION(Above);
3642
3643 IfCondition GetCondition() const OVERRIDE {
3644 return kCondA;
3645 }
3646
3647 IfCondition GetOppositeCondition() const OVERRIDE {
3648 return kCondBE;
3649 }
3650
3651 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003652 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003653 return MakeUnsigned(x) > MakeUnsigned(y);
3654 }
Aart Bike9f37602015-10-09 11:15:55 -07003655
3656 DISALLOW_COPY_AND_ASSIGN(HAbove);
3657};
3658
Vladimir Markofcb503c2016-05-18 12:48:17 +01003659class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003660 public:
3661 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3662 : HCondition(first, second, dex_pc) {}
3663
3664 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003665 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003666 }
3667 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3669 }
3670 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3671 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3672 LOG(FATAL) << DebugName() << " is not defined for float values";
3673 UNREACHABLE();
3674 }
3675 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3676 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3677 LOG(FATAL) << DebugName() << " is not defined for double values";
3678 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003679 }
3680
3681 DECLARE_INSTRUCTION(AboveOrEqual);
3682
3683 IfCondition GetCondition() const OVERRIDE {
3684 return kCondAE;
3685 }
3686
3687 IfCondition GetOppositeCondition() const OVERRIDE {
3688 return kCondB;
3689 }
3690
3691 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003692 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003693 return MakeUnsigned(x) >= MakeUnsigned(y);
3694 }
Aart Bike9f37602015-10-09 11:15:55 -07003695
3696 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3697};
Dave Allison20dfc792014-06-16 20:44:29 -07003698
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003699// Instruction to check how two inputs compare to each other.
3700// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003701class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003702 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003703 // Note that `comparison_type` is the type of comparison performed
3704 // between the comparison's inputs, not the type of the instantiated
3705 // HCompare instruction (which is always Primitive::kPrimInt).
3706 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003707 HInstruction* first,
3708 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003709 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003710 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003711 : HBinaryOperation(Primitive::kPrimInt,
3712 first,
3713 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003714 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003715 dex_pc) {
3716 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003717 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3718 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003719 }
3720
Roland Levillain9867bc72015-08-05 10:21:34 +01003721 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3723
3724 template <typename T>
3725 int32_t ComputeFP(T x, T y) const {
3726 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3727 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3728 // Handle the bias.
3729 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3730 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003731
Roland Levillain9867bc72015-08-05 10:21:34 +01003732 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003733 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3734 // reach this code path when processing a freshly built HIR
3735 // graph. However HCompare integer instructions can be synthesized
3736 // by the instruction simplifier to implement IntegerCompare and
3737 // IntegerSignum intrinsics, so we have to handle this case.
3738 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003739 }
3740 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003741 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3742 }
3743 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3744 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3745 }
3746 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3747 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003748 }
3749
Vladimir Marko372f10e2016-05-17 16:30:10 +01003750 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003752 }
3753
Vladimir Markoa1de9182016-02-25 11:37:38 +00003754 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003755
Roland Levillain31dd3d62016-02-16 12:21:02 +00003756 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003757 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003758 bool IsGtBias() const {
3759 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003760 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003762
Roland Levillain1693a1f2016-03-15 14:57:31 +00003763 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3764 // Comparisons do not require a runtime call in any back end.
3765 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003766 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003767
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003768 DECLARE_INSTRUCTION(Compare);
3769
Roland Levillain31dd3d62016-02-16 12:21:02 +00003770 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003771 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3772 static constexpr size_t kFieldComparisonBiasSize =
3773 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3774 static constexpr size_t kNumberOfComparePackedBits =
3775 kFieldComparisonBias + kFieldComparisonBiasSize;
3776 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3777 using ComparisonBiasField =
3778 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3779
Roland Levillain31dd3d62016-02-16 12:21:02 +00003780 // Return an integer constant containing the result of a comparison evaluated at compile time.
3781 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3782 DCHECK(value == -1 || value == 0 || value == 1) << value;
3783 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3784 }
3785
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003786 private:
3787 DISALLOW_COPY_AND_ASSIGN(HCompare);
3788};
3789
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003790class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003791 public:
3792 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003793 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003794 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003795 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003796 bool finalizable,
3797 QuickEntrypointEnum entrypoint)
3798 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3799 type_index_(type_index),
3800 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003801 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003802 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003803 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003804 }
3805
Andreas Gampea5b09a62016-11-17 15:21:22 -08003806 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003807 const DexFile& GetDexFile() const { return dex_file_; }
3808
3809 // Calls runtime so needs an environment.
3810 bool NeedsEnvironment() const OVERRIDE { return true; }
3811
Mingyao Yang062157f2016-03-02 10:15:36 -08003812 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3813 bool CanThrow() const OVERRIDE { return true; }
3814
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003815 bool NeedsChecks() const {
3816 return entrypoint_ == kQuickAllocObjectWithChecks;
3817 }
David Brazdil6de19382016-01-08 17:37:10 +00003818
Vladimir Markoa1de9182016-02-25 11:37:38 +00003819 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003820
3821 bool CanBeNull() const OVERRIDE { return false; }
3822
3823 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3824
3825 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3826 entrypoint_ = entrypoint;
3827 }
3828
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003829 HLoadClass* GetLoadClass() const {
3830 HInstruction* input = InputAt(0);
3831 if (input->IsClinitCheck()) {
3832 input = input->InputAt(0);
3833 }
3834 DCHECK(input->IsLoadClass());
3835 return input->AsLoadClass();
3836 }
3837
David Brazdil6de19382016-01-08 17:37:10 +00003838 bool IsStringAlloc() const;
3839
3840 DECLARE_INSTRUCTION(NewInstance);
3841
3842 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003843 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003844 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3845 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3846 "Too many packed fields.");
3847
Andreas Gampea5b09a62016-11-17 15:21:22 -08003848 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003849 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003850 QuickEntrypointEnum entrypoint_;
3851
3852 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3853};
3854
Agi Csaki05f20562015-08-19 14:58:14 -07003855enum IntrinsicNeedsEnvironmentOrCache {
3856 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3857 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003858};
3859
Aart Bik5d75afe2015-12-14 11:57:01 -08003860enum IntrinsicSideEffects {
3861 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3862 kReadSideEffects, // Intrinsic may read heap memory.
3863 kWriteSideEffects, // Intrinsic may write heap memory.
3864 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3865};
3866
3867enum IntrinsicExceptions {
3868 kNoThrow, // Intrinsic does not throw any exceptions.
3869 kCanThrow // Intrinsic may throw exceptions.
3870};
3871
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003872class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003873 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003874 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003875
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003876 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003877 SetRawInputAt(index, argument);
3878 }
3879
Roland Levillain3e3d7332015-04-28 11:00:54 +01003880 // Return the number of arguments. This number can be lower than
3881 // the number of inputs returned by InputCount(), as some invoke
3882 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3883 // inputs at the end of their list of inputs.
3884 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3885
Vladimir Markoa1de9182016-02-25 11:37:38 +00003886 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003887
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003888 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3889
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003890 InvokeType GetInvokeType() const {
3891 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003893
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003894 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003895 return intrinsic_;
3896 }
3897
Aart Bik5d75afe2015-12-14 11:57:01 -08003898 void SetIntrinsic(Intrinsics intrinsic,
3899 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3900 IntrinsicSideEffects side_effects,
3901 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003902
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003903 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003904 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003905 }
3906
Aart Bikff7d89c2016-11-07 08:49:28 -08003907 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3908
Vladimir Markoa1de9182016-02-25 11:37:38 +00003909 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003910
Aart Bik71bf7b42016-11-16 10:17:46 -08003911 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003912
Vladimir Marko372f10e2016-05-17 16:30:10 +01003913 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003914 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3915 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003916
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003917 uint32_t* GetIntrinsicOptimizations() {
3918 return &intrinsic_optimizations_;
3919 }
3920
3921 const uint32_t* GetIntrinsicOptimizations() const {
3922 return &intrinsic_optimizations_;
3923 }
3924
3925 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3926
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003927 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00003928 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003929
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003930 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003931
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003932 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003933 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3934 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003935 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3936 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003937 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003938 static constexpr size_t kFieldReturnTypeSize =
3939 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3940 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3941 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3942 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003943 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3945
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003946 HInvoke(ArenaAllocator* arena,
3947 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003948 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003949 Primitive::Type return_type,
3950 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003951 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003952 ArtMethod* resolved_method,
3953 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003954 : HVariableInputSizeInstruction(
3955 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3956 dex_pc,
3957 arena,
3958 number_of_arguments + number_of_other_inputs,
3959 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003960 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003961 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003962 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003963 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003964 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003965 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003966 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003967 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003968 }
3969
Roland Levillain3e3d7332015-04-28 11:00:54 +01003970 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00003971 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003972 const uint32_t dex_method_index_;
3973 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003974
3975 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3976 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003977
3978 private:
3979 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3980};
3981
Vladimir Markofcb503c2016-05-18 12:48:17 +01003982class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003983 public:
3984 HInvokeUnresolved(ArenaAllocator* arena,
3985 uint32_t number_of_arguments,
3986 Primitive::Type return_type,
3987 uint32_t dex_pc,
3988 uint32_t dex_method_index,
3989 InvokeType invoke_type)
3990 : HInvoke(arena,
3991 number_of_arguments,
3992 0u /* number_of_other_inputs */,
3993 return_type,
3994 dex_pc,
3995 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003996 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003997 invoke_type) {
3998 }
3999
4000 DECLARE_INSTRUCTION(InvokeUnresolved);
4001
4002 private:
4003 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4004};
4005
Orion Hodsonac141392017-01-13 11:53:47 +00004006class HInvokePolymorphic FINAL : public HInvoke {
4007 public:
4008 HInvokePolymorphic(ArenaAllocator* arena,
4009 uint32_t number_of_arguments,
4010 Primitive::Type return_type,
4011 uint32_t dex_pc,
4012 uint32_t dex_method_index)
4013 : HInvoke(arena,
4014 number_of_arguments,
4015 0u /* number_of_other_inputs */,
4016 return_type,
4017 dex_pc,
4018 dex_method_index,
4019 nullptr,
4020 kVirtual) {}
4021
4022 DECLARE_INSTRUCTION(InvokePolymorphic);
4023
4024 private:
4025 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4026};
4027
Vladimir Markofcb503c2016-05-18 12:48:17 +01004028class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004029 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004030 // Requirements of this method call regarding the class
4031 // initialization (clinit) check of its declaring class.
4032 enum class ClinitCheckRequirement {
4033 kNone, // Class already initialized.
4034 kExplicit, // Static call having explicit clinit check as last input.
4035 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004037 };
4038
Vladimir Marko58155012015-08-19 12:49:41 +00004039 // Determines how to load the target ArtMethod*.
4040 enum class MethodLoadKind {
4041 // Use a String init ArtMethod* loaded from Thread entrypoints.
4042 kStringInit,
4043
4044 // Use the method's own ArtMethod* loaded by the register allocator.
4045 kRecursive,
4046
4047 // Use ArtMethod* at a known address, embed the direct address in the code.
4048 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4049 kDirectAddress,
4050
Vladimir Markoa1de9182016-02-25 11:37:38 +00004051 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004052 // Used when we need to use the dex cache, for example for invoke-static that
4053 // may cause class initialization (the entry may point to a resolution method),
4054 // and we know that we can access the dex cache arrays using a PC-relative load.
4055 kDexCachePcRelative,
4056
4057 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4058 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4059 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4060 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4061 kDexCacheViaMethod,
4062 };
4063
4064 // Determines the location of the code pointer.
4065 enum class CodePtrLocation {
4066 // Recursive call, use local PC-relative call instruction.
4067 kCallSelf,
4068
Vladimir Marko58155012015-08-19 12:49:41 +00004069 // Use code pointer from the ArtMethod*.
4070 // Used when we don't know the target code. This is also the last-resort-kind used when
4071 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4072 kCallArtMethod,
4073 };
4074
4075 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004076 MethodLoadKind method_load_kind;
4077 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004078 // The method load data holds
4079 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4080 // Note that there are multiple string init methods, each having its own offset.
4081 // - the method address for kDirectAddress
4082 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004083 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004084 };
4085
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004086 HInvokeStaticOrDirect(ArenaAllocator* arena,
4087 uint32_t number_of_arguments,
4088 Primitive::Type return_type,
4089 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004090 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004091 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004092 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004093 InvokeType invoke_type,
4094 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004095 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004096 : HInvoke(arena,
4097 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004098 // There is potentially one extra argument for the HCurrentMethod node, and
4099 // potentially one other if the clinit check is explicit, and potentially
4100 // one other if the method is a string factory.
4101 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004102 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004103 return_type,
4104 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004105 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004106 resolved_method,
4107 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004108 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004110 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4111 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004112
Vladimir Markodc151b22015-10-15 18:02:30 +01004113 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004114 bool had_current_method_input = HasCurrentMethodInput();
4115 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4116
4117 // Using the current method is the default and once we find a better
4118 // method load kind, we should not go back to using the current method.
4119 DCHECK(had_current_method_input || !needs_current_method_input);
4120
4121 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004122 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4123 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004124 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004125 dispatch_info_ = dispatch_info;
4126 }
4127
Vladimir Markoc53c0792015-11-19 15:48:33 +00004128 void AddSpecialInput(HInstruction* input) {
4129 // We allow only one special input.
4130 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4131 DCHECK(InputCount() == GetSpecialInputIndex() ||
4132 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4133 InsertInputAt(GetSpecialInputIndex(), input);
4134 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004135
Vladimir Marko372f10e2016-05-17 16:30:10 +01004136 using HInstruction::GetInputRecords; // Keep the const version visible.
4137 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4138 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4139 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4140 DCHECK(!input_records.empty());
4141 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4142 HInstruction* last_input = input_records.back().GetInstruction();
4143 // Note: `last_input` may be null during arguments setup.
4144 if (last_input != nullptr) {
4145 // `last_input` is the last input of a static invoke marked as having
4146 // an explicit clinit check. It must either be:
4147 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4148 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4149 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4150 }
4151 }
4152 return input_records;
4153 }
4154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004155 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004156 // We access the method via the dex cache so we can't do an implicit null check.
4157 // TODO: for intrinsics we can generate implicit null checks.
4158 return false;
4159 }
4160
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004161 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004163 }
4164
Vladimir Markoc53c0792015-11-19 15:48:33 +00004165 // Get the index of the special input, if any.
4166 //
David Brazdil6de19382016-01-08 17:37:10 +00004167 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4168 // method pointer; otherwise there may be one platform-specific special input,
4169 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004170 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004171 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004172
Vladimir Marko58155012015-08-19 12:49:41 +00004173 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4174 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4175 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004176 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004177 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004178 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004179 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004180 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4181 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004182 bool HasCurrentMethodInput() const {
4183 // This function can be called only after the invoke has been fully initialized by the builder.
4184 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004185 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004186 return true;
4187 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004188 DCHECK(InputCount() == GetSpecialInputIndex() ||
4189 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004190 return false;
4191 }
4192 }
Vladimir Marko58155012015-08-19 12:49:41 +00004193
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004194 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004195 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004196 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004197 }
4198
4199 uint64_t GetMethodAddress() const {
4200 DCHECK(HasMethodAddress());
4201 return dispatch_info_.method_load_data;
4202 }
4203
4204 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004205 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004206 return dispatch_info_.method_load_data;
4207 }
4208
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004209 const DexFile& GetDexFileForPcRelativeDexCache() const;
4210
Vladimir Markoa1de9182016-02-25 11:37:38 +00004211 ClinitCheckRequirement GetClinitCheckRequirement() const {
4212 return GetPackedField<ClinitCheckRequirementField>();
4213 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004214
Roland Levillain4c0eb422015-04-24 16:43:49 +01004215 // Is this instruction a call to a static method?
4216 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004217 return GetInvokeType() == kStatic;
4218 }
4219
4220 MethodReference GetTargetMethod() const {
4221 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004222 }
4223
Vladimir Markofbb184a2015-11-13 14:47:00 +00004224 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4225 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4226 // instruction; only relevant for static calls with explicit clinit check.
4227 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004228 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004229 size_t last_input_index = inputs_.size() - 1u;
4230 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004231 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004232 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004233 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004234 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004235 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004236 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004237 }
4238
4239 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004240 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004241 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004243 }
4244
4245 // Is this a call to a static method whose declaring class has an
4246 // implicit intialization check requirement?
4247 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004248 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004249 }
4250
Vladimir Markob554b5a2015-11-06 12:57:55 +00004251 // Does this method load kind need the current method as an input?
4252 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4253 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4254 }
4255
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004256 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004257
4258 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004259 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004260 static constexpr size_t kFieldClinitCheckRequirementSize =
4261 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4262 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4263 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4264 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4265 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004266 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4267 kFieldClinitCheckRequirement,
4268 kFieldClinitCheckRequirementSize>;
4269
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004270 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004271 MethodReference target_method_;
4272 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004274 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004275};
Vladimir Markof64242a2015-12-01 14:58:23 +00004276std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004277std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004278
Vladimir Markofcb503c2016-05-18 12:48:17 +01004279class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004280 public:
4281 HInvokeVirtual(ArenaAllocator* arena,
4282 uint32_t number_of_arguments,
4283 Primitive::Type return_type,
4284 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004285 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004286 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004287 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004288 : HInvoke(arena,
4289 number_of_arguments,
4290 0u,
4291 return_type,
4292 dex_pc,
4293 dex_method_index,
4294 resolved_method,
4295 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004296 vtable_index_(vtable_index) {}
4297
Aart Bik71bf7b42016-11-16 10:17:46 -08004298 bool CanBeNull() const OVERRIDE {
4299 switch (GetIntrinsic()) {
4300 case Intrinsics::kThreadCurrentThread:
4301 case Intrinsics::kStringBufferAppend:
4302 case Intrinsics::kStringBufferToString:
4303 case Intrinsics::kStringBuilderAppend:
4304 case Intrinsics::kStringBuilderToString:
4305 return false;
4306 default:
4307 return HInvoke::CanBeNull();
4308 }
4309 }
4310
Calin Juravle641547a2015-04-21 22:08:51 +01004311 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004313 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004314 }
4315
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004316 uint32_t GetVTableIndex() const { return vtable_index_; }
4317
4318 DECLARE_INSTRUCTION(InvokeVirtual);
4319
4320 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004322 const uint32_t vtable_index_;
4323
4324 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4325};
4326
Vladimir Markofcb503c2016-05-18 12:48:17 +01004327class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004328 public:
4329 HInvokeInterface(ArenaAllocator* arena,
4330 uint32_t number_of_arguments,
4331 Primitive::Type return_type,
4332 uint32_t dex_pc,
4333 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004334 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004335 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004336 : HInvoke(arena,
4337 number_of_arguments,
4338 0u,
4339 return_type,
4340 dex_pc,
4341 dex_method_index,
4342 resolved_method,
4343 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004344 imt_index_(imt_index) {}
4345
Calin Juravle641547a2015-04-21 22:08:51 +01004346 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004347 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004348 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004349 }
4350
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004351 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4352 // The assembly stub currently needs it.
4353 return true;
4354 }
4355
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004356 uint32_t GetImtIndex() const { return imt_index_; }
4357 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4358
4359 DECLARE_INSTRUCTION(InvokeInterface);
4360
4361 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004362 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004363 const uint32_t imt_index_;
4364
4365 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4366};
4367
Vladimir Markofcb503c2016-05-18 12:48:17 +01004368class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004369 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004370 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004371 : HUnaryOperation(result_type, input, dex_pc) {
4372 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4373 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004374
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004375 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004376
4377 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004378 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 }
4380 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004381 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004382 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004383 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4384 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4385 }
4386 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4387 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4388 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004389
Roland Levillain88cb1752014-10-20 16:36:47 +01004390 DECLARE_INSTRUCTION(Neg);
4391
4392 private:
4393 DISALLOW_COPY_AND_ASSIGN(HNeg);
4394};
4395
Vladimir Markofcb503c2016-05-18 12:48:17 +01004396class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004397 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004398 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4399 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4400 SetRawInputAt(0, cls);
4401 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004402 }
4403
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004404 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004405 bool NeedsEnvironment() const OVERRIDE { return true; }
4406
Mingyao Yang0c365e62015-03-31 15:09:29 -07004407 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4408 bool CanThrow() const OVERRIDE { return true; }
4409
Calin Juravle10e244f2015-01-26 18:54:32 +00004410 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004411
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004412 HLoadClass* GetLoadClass() const {
4413 DCHECK(InputAt(0)->IsLoadClass());
4414 return InputAt(0)->AsLoadClass();
4415 }
4416
4417 HInstruction* GetLength() const {
4418 return InputAt(1);
4419 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004420
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004421 DECLARE_INSTRUCTION(NewArray);
4422
4423 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004424 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4425};
4426
Vladimir Markofcb503c2016-05-18 12:48:17 +01004427class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 HAdd(Primitive::Type result_type,
4430 HInstruction* left,
4431 HInstruction* right,
4432 uint32_t dex_pc = kNoDexPc)
4433 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004434
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004435 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004436
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004437 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004438
4439 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 return GetBlock()->GetGraph()->GetIntConstant(
4441 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004442 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004443 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 return GetBlock()->GetGraph()->GetLongConstant(
4445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004446 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004447 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4448 return GetBlock()->GetGraph()->GetFloatConstant(
4449 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4450 }
4451 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4452 return GetBlock()->GetGraph()->GetDoubleConstant(
4453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4454 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004455
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004456 DECLARE_INSTRUCTION(Add);
4457
4458 private:
4459 DISALLOW_COPY_AND_ASSIGN(HAdd);
4460};
4461
Vladimir Markofcb503c2016-05-18 12:48:17 +01004462class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004463 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004464 HSub(Primitive::Type result_type,
4465 HInstruction* left,
4466 HInstruction* right,
4467 uint32_t dex_pc = kNoDexPc)
4468 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004469
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004470 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004471
4472 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004473 return GetBlock()->GetGraph()->GetIntConstant(
4474 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004475 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004476 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetLongConstant(
4478 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004479 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004480 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4481 return GetBlock()->GetGraph()->GetFloatConstant(
4482 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4483 }
4484 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4485 return GetBlock()->GetGraph()->GetDoubleConstant(
4486 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004488
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004489 DECLARE_INSTRUCTION(Sub);
4490
4491 private:
4492 DISALLOW_COPY_AND_ASSIGN(HSub);
4493};
4494
Vladimir Markofcb503c2016-05-18 12:48:17 +01004495class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004496 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004497 HMul(Primitive::Type result_type,
4498 HInstruction* left,
4499 HInstruction* right,
4500 uint32_t dex_pc = kNoDexPc)
4501 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004502
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004503 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004504
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004505 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004506
4507 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004508 return GetBlock()->GetGraph()->GetIntConstant(
4509 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004510 }
4511 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004512 return GetBlock()->GetGraph()->GetLongConstant(
4513 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004514 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004515 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4516 return GetBlock()->GetGraph()->GetFloatConstant(
4517 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4518 }
4519 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4520 return GetBlock()->GetGraph()->GetDoubleConstant(
4521 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4522 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004523
4524 DECLARE_INSTRUCTION(Mul);
4525
4526 private:
4527 DISALLOW_COPY_AND_ASSIGN(HMul);
4528};
4529
Vladimir Markofcb503c2016-05-18 12:48:17 +01004530class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004531 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004532 HDiv(Primitive::Type result_type,
4533 HInstruction* left,
4534 HInstruction* right,
4535 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004536 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004537
Roland Levillain9867bc72015-08-05 10:21:34 +01004538 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004539 T ComputeIntegral(T x, T y) const {
4540 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004541 // Our graph structure ensures we never have 0 for `y` during
4542 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004543 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004544 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004545 return (y == -1) ? -x : x / y;
4546 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004547
Roland Levillain31dd3d62016-02-16 12:21:02 +00004548 template <typename T>
4549 T ComputeFP(T x, T y) const {
4550 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4551 return x / y;
4552 }
4553
Roland Levillain9867bc72015-08-05 10:21:34 +01004554 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004555 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004556 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004557 }
4558 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004559 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004560 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4561 }
4562 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4563 return GetBlock()->GetGraph()->GetFloatConstant(
4564 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4565 }
4566 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4567 return GetBlock()->GetGraph()->GetDoubleConstant(
4568 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004569 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004570
4571 DECLARE_INSTRUCTION(Div);
4572
4573 private:
4574 DISALLOW_COPY_AND_ASSIGN(HDiv);
4575};
4576
Vladimir Markofcb503c2016-05-18 12:48:17 +01004577class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004578 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004579 HRem(Primitive::Type result_type,
4580 HInstruction* left,
4581 HInstruction* right,
4582 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004583 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004584
Roland Levillain9867bc72015-08-05 10:21:34 +01004585 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004586 T ComputeIntegral(T x, T y) const {
4587 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004588 // Our graph structure ensures we never have 0 for `y` during
4589 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004590 DCHECK_NE(y, 0);
4591 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4592 return (y == -1) ? 0 : x % y;
4593 }
4594
Roland Levillain31dd3d62016-02-16 12:21:02 +00004595 template <typename T>
4596 T ComputeFP(T x, T y) const {
4597 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4598 return std::fmod(x, y);
4599 }
4600
Roland Levillain9867bc72015-08-05 10:21:34 +01004601 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004603 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004604 }
4605 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004607 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004608 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004609 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4610 return GetBlock()->GetGraph()->GetFloatConstant(
4611 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4612 }
4613 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4614 return GetBlock()->GetGraph()->GetDoubleConstant(
4615 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4616 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004617
4618 DECLARE_INSTRUCTION(Rem);
4619
4620 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004621 DISALLOW_COPY_AND_ASSIGN(HRem);
4622};
4623
Vladimir Markofcb503c2016-05-18 12:48:17 +01004624class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004625 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004626 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4627 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004628 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004629 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004630 SetRawInputAt(0, value);
4631 }
4632
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004633 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4634
Calin Juravled0d48522014-11-04 16:40:20 +00004635 bool CanBeMoved() const OVERRIDE { return true; }
4636
Vladimir Marko372f10e2016-05-17 16:30:10 +01004637 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004638 return true;
4639 }
4640
4641 bool NeedsEnvironment() const OVERRIDE { return true; }
4642 bool CanThrow() const OVERRIDE { return true; }
4643
Calin Juravled0d48522014-11-04 16:40:20 +00004644 DECLARE_INSTRUCTION(DivZeroCheck);
4645
4646 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004647 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4648};
4649
Vladimir Markofcb503c2016-05-18 12:48:17 +01004650class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 HInstruction* value,
4654 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4657 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4658 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
4660
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004662 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004663 return value << (distance & max_shift_distance);
4664 }
4665
4666 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004668 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004670 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004672 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004674 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4675 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4677 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004679 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4680 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004681 LOG(FATAL) << DebugName() << " is not defined for float values";
4682 UNREACHABLE();
4683 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4685 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 LOG(FATAL) << DebugName() << " is not defined for double values";
4687 UNREACHABLE();
4688 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004689
4690 DECLARE_INSTRUCTION(Shl);
4691
4692 private:
4693 DISALLOW_COPY_AND_ASSIGN(HShl);
4694};
4695
Vladimir Markofcb503c2016-05-18 12:48:17 +01004696class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 HInstruction* value,
4700 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4703 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4704 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 }
4706
Roland Levillain5b5b9312016-03-22 14:57:31 +00004707 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004708 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 return value >> (distance & max_shift_distance);
4710 }
4711
4712 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004713 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4721 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4722 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4723 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004724 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004725 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4726 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 LOG(FATAL) << DebugName() << " is not defined for float values";
4728 UNREACHABLE();
4729 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4731 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004732 LOG(FATAL) << DebugName() << " is not defined for double values";
4733 UNREACHABLE();
4734 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004735
4736 DECLARE_INSTRUCTION(Shr);
4737
4738 private:
4739 DISALLOW_COPY_AND_ASSIGN(HShr);
4740};
4741
Vladimir Markofcb503c2016-05-18 12:48:17 +01004742class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004743 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004744 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004745 HInstruction* value,
4746 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004747 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4749 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4750 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004751 }
4752
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004754 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 typedef typename std::make_unsigned<T>::type V;
4756 V ux = static_cast<V>(value);
4757 return static_cast<T>(ux >> (distance & max_shift_distance));
4758 }
4759
4760 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004761 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004765 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4769 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4770 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4771 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4774 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004775 LOG(FATAL) << DebugName() << " is not defined for float values";
4776 UNREACHABLE();
4777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4779 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004780 LOG(FATAL) << DebugName() << " is not defined for double values";
4781 UNREACHABLE();
4782 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004783
4784 DECLARE_INSTRUCTION(UShr);
4785
4786 private:
4787 DISALLOW_COPY_AND_ASSIGN(HUShr);
4788};
4789
Vladimir Markofcb503c2016-05-18 12:48:17 +01004790class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004791 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004792 HAnd(Primitive::Type result_type,
4793 HInstruction* left,
4794 HInstruction* right,
4795 uint32_t dex_pc = kNoDexPc)
4796 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004797
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004798 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004799
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004800 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004801
4802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004803 return GetBlock()->GetGraph()->GetIntConstant(
4804 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004806 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004807 return GetBlock()->GetGraph()->GetLongConstant(
4808 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004810 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4811 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4812 LOG(FATAL) << DebugName() << " is not defined for float values";
4813 UNREACHABLE();
4814 }
4815 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4816 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4817 LOG(FATAL) << DebugName() << " is not defined for double values";
4818 UNREACHABLE();
4819 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004820
4821 DECLARE_INSTRUCTION(And);
4822
4823 private:
4824 DISALLOW_COPY_AND_ASSIGN(HAnd);
4825};
4826
Vladimir Markofcb503c2016-05-18 12:48:17 +01004827class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004828 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004829 HOr(Primitive::Type result_type,
4830 HInstruction* left,
4831 HInstruction* right,
4832 uint32_t dex_pc = kNoDexPc)
4833 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004834
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004835 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004836
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004837 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004838
4839 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 return GetBlock()->GetGraph()->GetIntConstant(
4841 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004843 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 return GetBlock()->GetGraph()->GetLongConstant(
4845 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004846 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004847 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4848 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4849 LOG(FATAL) << DebugName() << " is not defined for float values";
4850 UNREACHABLE();
4851 }
4852 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4853 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4854 LOG(FATAL) << DebugName() << " is not defined for double values";
4855 UNREACHABLE();
4856 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004857
4858 DECLARE_INSTRUCTION(Or);
4859
4860 private:
4861 DISALLOW_COPY_AND_ASSIGN(HOr);
4862};
4863
Vladimir Markofcb503c2016-05-18 12:48:17 +01004864class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004865 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004866 HXor(Primitive::Type result_type,
4867 HInstruction* left,
4868 HInstruction* right,
4869 uint32_t dex_pc = kNoDexPc)
4870 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004871
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004872 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004873
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004874 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004875
4876 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004877 return GetBlock()->GetGraph()->GetIntConstant(
4878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004880 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004881 return GetBlock()->GetGraph()->GetLongConstant(
4882 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004884 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4885 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4886 LOG(FATAL) << DebugName() << " is not defined for float values";
4887 UNREACHABLE();
4888 }
4889 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4890 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4891 LOG(FATAL) << DebugName() << " is not defined for double values";
4892 UNREACHABLE();
4893 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004894
4895 DECLARE_INSTRUCTION(Xor);
4896
4897 private:
4898 DISALLOW_COPY_AND_ASSIGN(HXor);
4899};
4900
Vladimir Markofcb503c2016-05-18 12:48:17 +01004901class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004902 public:
4903 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004904 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004905 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4906 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004907 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004908
Roland Levillain5b5b9312016-03-22 14:57:31 +00004909 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004910 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004911 typedef typename std::make_unsigned<T>::type V;
4912 V ux = static_cast<V>(value);
4913 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004914 return static_cast<T>(ux);
4915 } else {
4916 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004917 return static_cast<T>(ux >> (distance & max_shift_value)) |
4918 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004919 }
4920 }
4921
Roland Levillain5b5b9312016-03-22 14:57:31 +00004922 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004923 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004924 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004925 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004926 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004927 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004928 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004929 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004930 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4931 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4932 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4933 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004934 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004935 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4936 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004937 LOG(FATAL) << DebugName() << " is not defined for float values";
4938 UNREACHABLE();
4939 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004940 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4941 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004942 LOG(FATAL) << DebugName() << " is not defined for double values";
4943 UNREACHABLE();
4944 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004945
4946 DECLARE_INSTRUCTION(Ror);
4947
4948 private:
4949 DISALLOW_COPY_AND_ASSIGN(HRor);
4950};
4951
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004952// The value of a parameter in this method. Its location depends on
4953// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004954class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004955 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004956 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004957 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004958 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004959 Primitive::Type parameter_type,
4960 bool is_this = false)
4961 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004962 dex_file_(dex_file),
4963 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004964 index_(index) {
4965 SetPackedFlag<kFlagIsThis>(is_this);
4966 SetPackedFlag<kFlagCanBeNull>(!is_this);
4967 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004968
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004969 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004970 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004971 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004972 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004973
Vladimir Markoa1de9182016-02-25 11:37:38 +00004974 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4975 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004976
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004977 DECLARE_INSTRUCTION(ParameterValue);
4978
4979 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004980 // Whether or not the parameter value corresponds to 'this' argument.
4981 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4982 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4983 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4984 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4985 "Too many packed fields.");
4986
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004987 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004988 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004989 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004990 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004991 const uint8_t index_;
4992
4993 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4994};
4995
Vladimir Markofcb503c2016-05-18 12:48:17 +01004996class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004997 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4999 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005000
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005001 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005002 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005003 return true;
5004 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005005
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005006 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005007
5008 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005009 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005010 }
5011 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005012 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005013 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005014 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5015 LOG(FATAL) << DebugName() << " is not defined for float values";
5016 UNREACHABLE();
5017 }
5018 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5019 LOG(FATAL) << DebugName() << " is not defined for double values";
5020 UNREACHABLE();
5021 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005022
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005023 DECLARE_INSTRUCTION(Not);
5024
5025 private:
5026 DISALLOW_COPY_AND_ASSIGN(HNot);
5027};
5028
Vladimir Markofcb503c2016-05-18 12:48:17 +01005029class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005030 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005031 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5032 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005033
5034 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005035 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005036 return true;
5037 }
5038
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005039 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005040 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005041 return !x;
5042 }
5043
Roland Levillain9867bc72015-08-05 10:21:34 +01005044 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005045 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005046 }
5047 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5048 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005049 UNREACHABLE();
5050 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005051 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5052 LOG(FATAL) << DebugName() << " is not defined for float values";
5053 UNREACHABLE();
5054 }
5055 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5056 LOG(FATAL) << DebugName() << " is not defined for double values";
5057 UNREACHABLE();
5058 }
David Brazdil66d126e2015-04-03 16:02:44 +01005059
5060 DECLARE_INSTRUCTION(BooleanNot);
5061
5062 private:
5063 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5064};
5065
Vladimir Markofcb503c2016-05-18 12:48:17 +01005066class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005067 public:
5068 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005069 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005070 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005071 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005072 // Invariant: We should never generate a conversion to a Boolean value.
5073 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005074 }
5075
5076 HInstruction* GetInput() const { return InputAt(0); }
5077 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5078 Primitive::Type GetResultType() const { return GetType(); }
5079
5080 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005081 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5082 return true;
5083 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005084
Mark Mendelle82549b2015-05-06 10:55:34 -04005085 // Try to statically evaluate the conversion and return a HConstant
5086 // containing the result. If the input cannot be converted, return nullptr.
5087 HConstant* TryStaticEvaluation() const;
5088
Roland Levillaindff1f282014-11-05 14:15:05 +00005089 DECLARE_INSTRUCTION(TypeConversion);
5090
5091 private:
5092 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5093};
5094
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005095static constexpr uint32_t kNoRegNumber = -1;
5096
Vladimir Markofcb503c2016-05-18 12:48:17 +01005097class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005098 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005099 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5100 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005102 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103 SetRawInputAt(0, value);
5104 }
5105
Calin Juravle10e244f2015-01-26 18:54:32 +00005106 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005107 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005108 return true;
5109 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005110
Calin Juravle10e244f2015-01-26 18:54:32 +00005111 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112
Calin Juravle10e244f2015-01-26 18:54:32 +00005113 bool CanThrow() const OVERRIDE { return true; }
5114
5115 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005116
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005117
5118 DECLARE_INSTRUCTION(NullCheck);
5119
5120 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5122};
5123
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005124// Embeds an ArtField and all the information required by the compiler. We cache
5125// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005126class FieldInfo : public ValueObject {
5127 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005128 FieldInfo(ArtField* field,
5129 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005130 Primitive::Type field_type,
5131 bool is_volatile,
5132 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005133 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005134 const DexFile& dex_file)
5135 : field_(field),
5136 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005137 field_type_(field_type),
5138 is_volatile_(is_volatile),
5139 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005140 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005141 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005142
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005143 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005144 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005145 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005146 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005147 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005148 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005149 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150
5151 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005152 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005153 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005154 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005155 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005156 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005157 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005158 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159};
5160
Vladimir Markofcb503c2016-05-18 12:48:17 +01005161class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162 public:
5163 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005164 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005166 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005167 bool is_volatile,
5168 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005169 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005170 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005171 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005172 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005173 field_info_(field,
5174 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005175 field_type,
5176 is_volatile,
5177 field_idx,
5178 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005179 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005180 SetRawInputAt(0, value);
5181 }
5182
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005183 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005184
Vladimir Marko372f10e2016-05-17 16:30:10 +01005185 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5186 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005187 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005188 }
5189
Calin Juravle641547a2015-04-21 22:08:51 +01005190 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005191 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005192 }
5193
5194 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005195 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5196 }
5197
Calin Juravle52c48962014-12-16 17:02:57 +00005198 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005199 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005200 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005201 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202
5203 DECLARE_INSTRUCTION(InstanceFieldGet);
5204
5205 private:
5206 const FieldInfo field_info_;
5207
5208 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5209};
5210
Vladimir Markofcb503c2016-05-18 12:48:17 +01005211class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212 public:
5213 HInstanceFieldSet(HInstruction* object,
5214 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005215 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005216 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005217 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005218 bool is_volatile,
5219 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005220 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005221 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005222 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005223 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005224 field_info_(field,
5225 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005226 field_type,
5227 is_volatile,
5228 field_idx,
5229 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005230 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005232 SetRawInputAt(0, object);
5233 SetRawInputAt(1, value);
5234 }
5235
Calin Juravle641547a2015-04-21 22:08:51 +01005236 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005237 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005238 }
5239
Calin Juravle52c48962014-12-16 17:02:57 +00005240 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005242 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005243 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005244 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005245 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5246 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005248 DECLARE_INSTRUCTION(InstanceFieldSet);
5249
5250 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005251 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5252 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5253 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5254 "Too many packed fields.");
5255
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005256 const FieldInfo field_info_;
5257
5258 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5259};
5260
Vladimir Markofcb503c2016-05-18 12:48:17 +01005261class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005263 HArrayGet(HInstruction* array,
5264 HInstruction* index,
5265 Primitive::Type type,
5266 uint32_t dex_pc,
5267 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005268 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005269 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 SetRawInputAt(0, array);
5271 SetRawInputAt(1, index);
5272 }
5273
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005274 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005275 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005276 return true;
5277 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005278 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005279 // TODO: We can be smarter here.
5280 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5281 // which generates the implicit null check. There are cases when these can be removed
5282 // to produce better code. If we ever add optimizations to do so we should allow an
5283 // implicit check here (as long as the address falls in the first page).
5284 return false;
5285 }
5286
David Brazdil4833f5a2015-12-16 10:37:39 +00005287 bool IsEquivalentOf(HArrayGet* other) const {
5288 bool result = (GetDexPc() == other->GetDexPc());
5289 if (kIsDebugBuild && result) {
5290 DCHECK_EQ(GetBlock(), other->GetBlock());
5291 DCHECK_EQ(GetArray(), other->GetArray());
5292 DCHECK_EQ(GetIndex(), other->GetIndex());
5293 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005294 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005295 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005296 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5297 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005298 }
5299 }
5300 return result;
5301 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005302
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005303 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5304
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005305 HInstruction* GetArray() const { return InputAt(0); }
5306 HInstruction* GetIndex() const { return InputAt(1); }
5307
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 DECLARE_INSTRUCTION(ArrayGet);
5309
5310 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005311 // We treat a String as an array, creating the HArrayGet from String.charAt()
5312 // intrinsic in the instruction simplifier. We can always determine whether
5313 // a particular HArrayGet is actually a String.charAt() by looking at the type
5314 // of the input but that requires holding the mutator lock, so we prefer to use
5315 // a flag, so that code generators don't need to do the locking.
5316 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5317 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5318 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5319 "Too many packed fields.");
5320
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5322};
5323
Vladimir Markofcb503c2016-05-18 12:48:17 +01005324class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 public:
5326 HArraySet(HInstruction* array,
5327 HInstruction* index,
5328 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005329 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005330 uint32_t dex_pc)
5331 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005332 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5333 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5334 SetPackedFlag<kFlagValueCanBeNull>(true);
5335 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 SetRawInputAt(0, array);
5337 SetRawInputAt(1, index);
5338 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005339 // Make a best guess now, may be refined during SSA building.
5340 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 }
5342
Calin Juravle77520bc2015-01-12 18:45:46 +00005343 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005344 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005345 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 }
5347
Mingyao Yang81014cb2015-06-02 03:16:27 -07005348 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005349 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005350
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005351 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 // TODO: Same as for ArrayGet.
5353 return false;
5354 }
5355
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005356 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005357 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005358 }
5359
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005360 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005361 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005362 }
5363
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005364 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005365 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005366 }
5367
Vladimir Markoa1de9182016-02-25 11:37:38 +00005368 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5369 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5370 bool StaticTypeOfArrayIsObjectArray() const {
5371 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5372 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005373
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005374 HInstruction* GetArray() const { return InputAt(0); }
5375 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005376 HInstruction* GetValue() const { return InputAt(2); }
5377
5378 Primitive::Type GetComponentType() const {
5379 // The Dex format does not type floating point index operations. Since the
5380 // `expected_component_type_` is set during building and can therefore not
5381 // be correct, we also check what is the value type. If it is a floating
5382 // point type, we must use that type.
5383 Primitive::Type value_type = GetValue()->GetType();
5384 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5385 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005386 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005387 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005388
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005389 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005390 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005391 }
5392
Aart Bik18b36ab2016-04-13 16:41:35 -07005393 void ComputeSideEffects() {
5394 Primitive::Type type = GetComponentType();
5395 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5396 SideEffectsForArchRuntimeCalls(type)));
5397 }
5398
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005399 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5400 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5401 }
5402
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 DECLARE_INSTRUCTION(ArraySet);
5404
5405 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005406 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5407 static constexpr size_t kFieldExpectedComponentTypeSize =
5408 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5409 static constexpr size_t kFlagNeedsTypeCheck =
5410 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5411 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 // Cached information for the reference_type_info_ so that codegen
5413 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005414 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5415 static constexpr size_t kNumberOfArraySetPackedBits =
5416 kFlagStaticTypeOfArrayIsObjectArray + 1;
5417 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5418 using ExpectedComponentTypeField =
5419 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420
5421 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5422};
5423
Vladimir Markofcb503c2016-05-18 12:48:17 +01005424class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005426 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005427 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005428 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005429 // Note that arrays do not change length, so the instruction does not
5430 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 SetRawInputAt(0, array);
5432 }
5433
Calin Juravle77520bc2015-01-12 18:45:46 +00005434 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005435 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005436 return true;
5437 }
Calin Juravle641547a2015-04-21 22:08:51 +01005438 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5439 return obj == InputAt(0);
5440 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005441
Vladimir Markodce016e2016-04-28 13:10:02 +01005442 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5443
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 DECLARE_INSTRUCTION(ArrayLength);
5445
5446 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005447 // We treat a String as an array, creating the HArrayLength from String.length()
5448 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5449 // determine whether a particular HArrayLength is actually a String.length() by
5450 // looking at the type of the input but that requires holding the mutator lock, so
5451 // we prefer to use a flag, so that code generators don't need to do the locking.
5452 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5453 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5454 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5455 "Too many packed fields.");
5456
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005457 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5458};
5459
Vladimir Markofcb503c2016-05-18 12:48:17 +01005460class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005461 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005462 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5463 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005464 HBoundsCheck(HInstruction* index,
5465 HInstruction* length,
5466 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005467 bool string_char_at = false)
5468 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005469 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005470 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005471 SetRawInputAt(0, index);
5472 SetRawInputAt(1, length);
5473 }
5474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005475 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005476 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005477 return true;
5478 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005479
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005480 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005481
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005482 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005483
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005484 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005485
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005486 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005487
5488 DECLARE_INSTRUCTION(BoundsCheck);
5489
5490 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005491 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005492
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005493 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5494};
5495
Vladimir Markofcb503c2016-05-18 12:48:17 +01005496class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005497 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005498 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005499 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005500
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005501 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005502 return true;
5503 }
5504
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005505 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5506 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005507
5508 DECLARE_INSTRUCTION(SuspendCheck);
5509
5510 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005511 // Only used for code generation, in order to share the same slow path between back edges
5512 // of a same loop.
5513 SlowPathCode* slow_path_;
5514
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005515 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5516};
5517
David Srbecky0cf44932015-12-09 14:09:59 +00005518// Pseudo-instruction which provides the native debugger with mapping information.
5519// It ensures that we can generate line number and local variables at this point.
5520class HNativeDebugInfo : public HTemplateInstruction<0> {
5521 public:
5522 explicit HNativeDebugInfo(uint32_t dex_pc)
5523 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5524
5525 bool NeedsEnvironment() const OVERRIDE {
5526 return true;
5527 }
5528
5529 DECLARE_INSTRUCTION(NativeDebugInfo);
5530
5531 private:
5532 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5533};
5534
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005535/**
5536 * Instruction to load a Class object.
5537 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005538class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005539 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 // Determines how to load the Class.
5541 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005542 // We cannot load this class. See HSharpening::SharpenLoadClass.
5543 kInvalid = -1,
5544
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005545 // Use the Class* from the method's own ArtMethod*.
5546 kReferrersClass,
5547
5548 // Use boot image Class* address that will be known at link time.
5549 // Used for boot image classes referenced by boot image code in non-PIC mode.
5550 kBootImageLinkTimeAddress,
5551
5552 // Use PC-relative boot image Class* address that will be known at link time.
5553 // Used for boot image classes referenced by boot image code in PIC mode.
5554 kBootImageLinkTimePcRelative,
5555
5556 // Use a known boot image Class* address, embedded in the code by the codegen.
5557 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 kBootImageAddress,
5559
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005560 // Load from an entry in the .bss section using a PC-relative load.
5561 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5562 kBssEntry,
5563
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005564 // Load from the root table associated with the JIT compiled method.
5565 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005567 // Load from resolved types array accessed through the class loaded from
5568 // the compiled method's own ArtMethod*. This is the default access type when
5569 // all other types are unavailable.
5570 kDexCacheViaMethod,
5571
5572 kLast = kDexCacheViaMethod
5573 };
5574
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005575 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005576 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005577 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005578 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005579 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005580 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005581 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005582 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5583 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005584 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005585 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005586 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005587 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005588 // Referrers class should not need access check. We never inline unverified
5589 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005590 DCHECK(!is_referrers_class || !needs_access_check);
5591
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005592 SetPackedField<LoadKindField>(
5593 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005594 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005595 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005596 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005597 }
5598
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005599 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005600
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005601 LoadKind GetLoadKind() const {
5602 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005603 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005604
5605 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005606
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005607 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005608
Andreas Gampea5b09a62016-11-17 15:21:22 -08005609 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005610
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005611 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005612
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005613 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005614 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005615 }
5616
Calin Juravle0ba218d2015-05-19 18:46:01 +01005617 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005618 // The entrypoint the code generator is going to call does not do
5619 // clinit of the class.
5620 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005621 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005622 }
5623
5624 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005625 return NeedsAccessCheck() ||
5626 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005627 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5628 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005629 }
5630
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005631 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005632 return NeedsAccessCheck() ||
5633 MustGenerateClinitCheck() ||
5634 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5635 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5636 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005637 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5638 GetLoadKind() == LoadKind::kBssEntry) &&
5639 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005640 }
5641
Calin Juravleacf735c2015-02-12 15:25:22 +00005642 ReferenceTypeInfo GetLoadedClassRTI() {
5643 return loaded_class_rti_;
5644 }
5645
5646 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5647 // Make sure we only set exact types (the loaded class should never be merged).
5648 DCHECK(rti.IsExact());
5649 loaded_class_rti_ = rti;
5650 }
5651
Andreas Gampea5b09a62016-11-17 15:21:22 -08005652 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005653 const DexFile& GetDexFile() const { return dex_file_; }
5654
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005655 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005656 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005657 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005658
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005659 static SideEffects SideEffectsForArchRuntimeCalls() {
5660 return SideEffects::CanTriggerGC();
5661 }
5662
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005663 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005664 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005665 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005666 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005667
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005668 void MarkInBootImage() {
5669 SetPackedFlag<kFlagIsInBootImage>(true);
5670 }
5671
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672 void AddSpecialInput(HInstruction* special_input);
5673
5674 using HInstruction::GetInputRecords; // Keep the const version visible.
5675 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5676 return ArrayRef<HUserRecord<HInstruction*>>(
5677 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5678 }
5679
5680 Primitive::Type GetType() const OVERRIDE {
5681 return Primitive::kPrimNot;
5682 }
5683
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005684 Handle<mirror::Class> GetClass() const {
5685 return klass_;
5686 }
5687
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005688 DECLARE_INSTRUCTION(LoadClass);
5689
5690 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005692 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005693 // Whether this instruction must generate the initialization check.
5694 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005695 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5697 static constexpr size_t kFieldLoadKindSize =
5698 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5699 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005700 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005701 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5702
5703 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005704 return load_kind == LoadKind::kReferrersClass ||
5705 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005707 load_kind == LoadKind::kBssEntry ||
5708 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005709 }
5710
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711 void SetLoadKindInternal(LoadKind load_kind);
5712
5713 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5714 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005715 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005718 // A type index and dex file where the class can be accessed. The dex file can be:
5719 // - The compiling method's dex file if the class is defined there too.
5720 // - The compiling method's dex file if the class is referenced there.
5721 // - The dex file where the class is defined. When the load kind can only be
5722 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005723 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005724 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005725
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005726 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005727
Calin Juravleacf735c2015-02-12 15:25:22 +00005728 ReferenceTypeInfo loaded_class_rti_;
5729
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5731};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005732std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5733
5734// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005735inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005736 // The special input is used for PC-relative loads on some architectures,
5737 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005738 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005739 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005740 GetLoadKind() == LoadKind::kBootImageAddress ||
5741 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005742 DCHECK(special_input_.GetInstruction() == nullptr);
5743 special_input_ = HUserRecord<HInstruction*>(special_input);
5744 special_input->AddUseAt(this, 0);
5745}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005746
Vladimir Marko372f10e2016-05-17 16:30:10 +01005747class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005748 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749 // Determines how to load the String.
5750 enum class LoadKind {
5751 // Use boot image String* address that will be known at link time.
5752 // Used for boot image strings referenced by boot image code in non-PIC mode.
5753 kBootImageLinkTimeAddress,
5754
5755 // Use PC-relative boot image String* address that will be known at link time.
5756 // Used for boot image strings referenced by boot image code in PIC mode.
5757 kBootImageLinkTimePcRelative,
5758
5759 // Use a known boot image String* address, embedded in the code by the codegen.
5760 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005761 kBootImageAddress,
5762
Vladimir Markoaad75c62016-10-03 08:46:48 +00005763 // Load from an entry in the .bss section using a PC-relative load.
5764 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5765 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005766
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005767 // Load from the root table associated with the JIT compiled method.
5768 kJitTableAddress,
5769
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770 // Load from resolved strings array accessed through the class loaded from
5771 // the compiled method's own ArtMethod*. This is the default access type when
5772 // all other types are unavailable.
5773 kDexCacheViaMethod,
5774
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005775 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 };
5777
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005778 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005779 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780 const DexFile& dex_file,
5781 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005782 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5783 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005784 string_index_(string_index),
5785 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005786 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005787 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005788
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005789 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 LoadKind GetLoadKind() const {
5792 return GetPackedField<LoadKindField>();
5793 }
5794
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005795 const DexFile& GetDexFile() const {
5796 return dex_file_;
5797 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005798
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005799 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 return string_index_;
5801 }
5802
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005803 Handle<mirror::String> GetString() const {
5804 return string_;
5805 }
5806
5807 void SetString(Handle<mirror::String> str) {
5808 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005809 }
5810
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005811 bool CanBeMoved() const OVERRIDE { return true; }
5812
Vladimir Marko372f10e2016-05-17 16:30:10 +01005813 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005814
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005815 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005816
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 // Will call the runtime if we need to load the string through
5818 // the dex cache and the string is not guaranteed to be there yet.
5819 bool NeedsEnvironment() const OVERRIDE {
5820 LoadKind load_kind = GetLoadKind();
5821 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5822 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005823 load_kind == LoadKind::kBootImageAddress ||
5824 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005825 return false;
5826 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005827 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005829
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005830 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5831 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5832 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005833
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005834 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005835 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005836
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005837 static SideEffects SideEffectsForArchRuntimeCalls() {
5838 return SideEffects::CanTriggerGC();
5839 }
5840
Vladimir Marko372f10e2016-05-17 16:30:10 +01005841 void AddSpecialInput(HInstruction* special_input);
5842
5843 using HInstruction::GetInputRecords; // Keep the const version visible.
5844 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5845 return ArrayRef<HUserRecord<HInstruction*>>(
5846 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005847 }
5848
Vladimir Marko372f10e2016-05-17 16:30:10 +01005849 Primitive::Type GetType() const OVERRIDE {
5850 return Primitive::kPrimNot;
5851 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005852
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005853 DECLARE_INSTRUCTION(LoadString);
5854
5855 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005856 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005857 static constexpr size_t kFieldLoadKindSize =
5858 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5859 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005860 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005862
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863 void SetLoadKindInternal(LoadKind load_kind);
5864
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5866 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005867 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005868 HUserRecord<HInstruction*> special_input_;
5869
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005870 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005871 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005873 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005874
5875 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5876};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005877std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5878
5879// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005881 // The special input is used for PC-relative loads on some architectures,
5882 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005883 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005884 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005885 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5886 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005887 // HLoadString::GetInputRecords() returns an empty array at this point,
5888 // so use the GetInputRecords() from the base class to set the input record.
5889 DCHECK(special_input_.GetInstruction() == nullptr);
5890 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 special_input->AddUseAt(this, 0);
5892}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005893
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005894/**
5895 * Performs an initialization check on its Class object input.
5896 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005897class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005898 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005899 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005900 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005901 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005902 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5903 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005904 SetRawInputAt(0, constant);
5905 }
5906
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005907 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005908 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005909 return true;
5910 }
5911
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005912 bool NeedsEnvironment() const OVERRIDE {
5913 // May call runtime to initialize the class.
5914 return true;
5915 }
5916
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005917 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005919 HLoadClass* GetLoadClass() const {
5920 DCHECK(InputAt(0)->IsLoadClass());
5921 return InputAt(0)->AsLoadClass();
5922 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923
5924 DECLARE_INSTRUCTION(ClinitCheck);
5925
5926 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5928};
5929
Vladimir Markofcb503c2016-05-18 12:48:17 +01005930class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005931 public:
5932 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005933 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005935 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005936 bool is_volatile,
5937 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005938 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005939 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005940 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005941 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005942 field_info_(field,
5943 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005944 field_type,
5945 is_volatile,
5946 field_idx,
5947 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005948 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 SetRawInputAt(0, cls);
5950 }
5951
Calin Juravle52c48962014-12-16 17:02:57 +00005952
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005953 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005954
Vladimir Marko372f10e2016-05-17 16:30:10 +01005955 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5956 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005957 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958 }
5959
5960 size_t ComputeHashCode() const OVERRIDE {
5961 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5962 }
5963
Calin Juravle52c48962014-12-16 17:02:57 +00005964 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005965 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5966 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005967 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968
5969 DECLARE_INSTRUCTION(StaticFieldGet);
5970
5971 private:
5972 const FieldInfo field_info_;
5973
5974 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5975};
5976
Vladimir Markofcb503c2016-05-18 12:48:17 +01005977class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 public:
5979 HStaticFieldSet(HInstruction* cls,
5980 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005981 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005982 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005983 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005984 bool is_volatile,
5985 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005986 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005987 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005988 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005989 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005990 field_info_(field,
5991 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005992 field_type,
5993 is_volatile,
5994 field_idx,
5995 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005996 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005997 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005998 SetRawInputAt(0, cls);
5999 SetRawInputAt(1, value);
6000 }
6001
Calin Juravle52c48962014-12-16 17:02:57 +00006002 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6004 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006005 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006007 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006008 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6009 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006010
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 DECLARE_INSTRUCTION(StaticFieldSet);
6012
6013 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006014 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6015 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6016 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6017 "Too many packed fields.");
6018
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 const FieldInfo field_info_;
6020
6021 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6022};
6023
Vladimir Markofcb503c2016-05-18 12:48:17 +01006024class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006025 public:
6026 HUnresolvedInstanceFieldGet(HInstruction* obj,
6027 Primitive::Type field_type,
6028 uint32_t field_index,
6029 uint32_t dex_pc)
6030 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6031 field_index_(field_index) {
6032 SetRawInputAt(0, obj);
6033 }
6034
6035 bool NeedsEnvironment() const OVERRIDE { return true; }
6036 bool CanThrow() const OVERRIDE { return true; }
6037
6038 Primitive::Type GetFieldType() const { return GetType(); }
6039 uint32_t GetFieldIndex() const { return field_index_; }
6040
6041 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6042
6043 private:
6044 const uint32_t field_index_;
6045
6046 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6047};
6048
Vladimir Markofcb503c2016-05-18 12:48:17 +01006049class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006050 public:
6051 HUnresolvedInstanceFieldSet(HInstruction* obj,
6052 HInstruction* value,
6053 Primitive::Type field_type,
6054 uint32_t field_index,
6055 uint32_t dex_pc)
6056 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006057 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006058 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006059 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006060 SetRawInputAt(0, obj);
6061 SetRawInputAt(1, value);
6062 }
6063
6064 bool NeedsEnvironment() const OVERRIDE { return true; }
6065 bool CanThrow() const OVERRIDE { return true; }
6066
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006068 uint32_t GetFieldIndex() const { return field_index_; }
6069
6070 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6071
6072 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006073 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6074 static constexpr size_t kFieldFieldTypeSize =
6075 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6076 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6077 kFieldFieldType + kFieldFieldTypeSize;
6078 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6079 "Too many packed fields.");
6080 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6081
Calin Juravlee460d1d2015-09-29 04:52:17 +01006082 const uint32_t field_index_;
6083
6084 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6085};
6086
Vladimir Markofcb503c2016-05-18 12:48:17 +01006087class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006088 public:
6089 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6090 uint32_t field_index,
6091 uint32_t dex_pc)
6092 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6093 field_index_(field_index) {
6094 }
6095
6096 bool NeedsEnvironment() const OVERRIDE { return true; }
6097 bool CanThrow() const OVERRIDE { return true; }
6098
6099 Primitive::Type GetFieldType() const { return GetType(); }
6100 uint32_t GetFieldIndex() const { return field_index_; }
6101
6102 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6103
6104 private:
6105 const uint32_t field_index_;
6106
6107 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6108};
6109
Vladimir Markofcb503c2016-05-18 12:48:17 +01006110class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006111 public:
6112 HUnresolvedStaticFieldSet(HInstruction* value,
6113 Primitive::Type field_type,
6114 uint32_t field_index,
6115 uint32_t dex_pc)
6116 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006117 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006118 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006119 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006120 SetRawInputAt(0, value);
6121 }
6122
6123 bool NeedsEnvironment() const OVERRIDE { return true; }
6124 bool CanThrow() const OVERRIDE { return true; }
6125
Vladimir Markoa1de9182016-02-25 11:37:38 +00006126 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006127 uint32_t GetFieldIndex() const { return field_index_; }
6128
6129 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6130
6131 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006132 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6133 static constexpr size_t kFieldFieldTypeSize =
6134 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6135 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6136 kFieldFieldType + kFieldFieldTypeSize;
6137 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6138 "Too many packed fields.");
6139 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6140
Calin Juravlee460d1d2015-09-29 04:52:17 +01006141 const uint32_t field_index_;
6142
6143 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6144};
6145
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006146// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006147class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006149 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6150 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006151
David Brazdilbbd733e2015-08-18 17:48:17 +01006152 bool CanBeNull() const OVERRIDE { return false; }
6153
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006154 DECLARE_INSTRUCTION(LoadException);
6155
6156 private:
6157 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6158};
6159
David Brazdilcb1c0552015-08-04 16:22:25 +01006160// Implicit part of move-exception which clears thread-local exception storage.
6161// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006162class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006163 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006164 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6165 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006166
6167 DECLARE_INSTRUCTION(ClearException);
6168
6169 private:
6170 DISALLOW_COPY_AND_ASSIGN(HClearException);
6171};
6172
Vladimir Markofcb503c2016-05-18 12:48:17 +01006173class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006174 public:
6175 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006176 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006177 SetRawInputAt(0, exception);
6178 }
6179
6180 bool IsControlFlow() const OVERRIDE { return true; }
6181
6182 bool NeedsEnvironment() const OVERRIDE { return true; }
6183
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006184 bool CanThrow() const OVERRIDE { return true; }
6185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006186
6187 DECLARE_INSTRUCTION(Throw);
6188
6189 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006190 DISALLOW_COPY_AND_ASSIGN(HThrow);
6191};
6192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193/**
6194 * Implementation strategies for the code generator of a HInstanceOf
6195 * or `HCheckCast`.
6196 */
6197enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006198 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 kExactCheck, // Can do a single class compare.
6200 kClassHierarchyCheck, // Can just walk the super class chain.
6201 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6202 kInterfaceCheck, // No optimization yet when checking against an interface.
6203 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006204 kArrayCheck, // No optimization yet when checking against a generic array.
6205 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206};
6207
Roland Levillain86503782016-02-11 19:07:30 +00006208std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6209
Vladimir Markofcb503c2016-05-18 12:48:17 +01006210class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006211 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006212 HInstanceOf(HInstruction* object,
6213 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006215 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006216 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 dex_pc) {
6219 SetPackedField<TypeCheckKindField>(check_kind);
6220 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006221 SetRawInputAt(0, object);
6222 SetRawInputAt(1, constant);
6223 }
6224
6225 bool CanBeMoved() const OVERRIDE { return true; }
6226
Vladimir Marko372f10e2016-05-17 16:30:10 +01006227 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006228 return true;
6229 }
6230
6231 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006233 }
6234
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006235 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6237 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6238 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6239 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006240
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006241 static bool CanCallRuntime(TypeCheckKind check_kind) {
6242 // Mips currently does runtime calls for any other checks.
6243 return check_kind != TypeCheckKind::kExactCheck;
6244 }
6245
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006247 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006248 }
6249
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006250 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006251
6252 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006253 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6254 static constexpr size_t kFieldTypeCheckKindSize =
6255 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6256 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6257 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6258 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6259 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006260
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006261 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6262};
6263
Vladimir Markofcb503c2016-05-18 12:48:17 +01006264class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006265 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006266 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006267 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006268 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6269 SetPackedFlag<kFlagUpperCanBeNull>(true);
6270 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006271 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006272 SetRawInputAt(0, input);
6273 }
6274
David Brazdilf5552582015-12-27 13:36:12 +00006275 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006276 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006277 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006278 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006279
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006280 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 DCHECK(GetUpperCanBeNull() || !can_be_null);
6282 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006283 }
6284
Vladimir Markoa1de9182016-02-25 11:37:38 +00006285 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006286
Calin Juravleb1498f62015-02-16 13:13:29 +00006287 DECLARE_INSTRUCTION(BoundType);
6288
6289 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006290 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6291 // is false then CanBeNull() cannot be true).
6292 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6293 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6294 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6295 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6296
Calin Juravleb1498f62015-02-16 13:13:29 +00006297 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006298 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6299 // It is used to bound the type in cases like:
6300 // if (x instanceof ClassX) {
6301 // // uper_bound_ will be ClassX
6302 // }
David Brazdilf5552582015-12-27 13:36:12 +00006303 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006304
6305 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6306};
6307
Vladimir Markofcb503c2016-05-18 12:48:17 +01006308class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006309 public:
6310 HCheckCast(HInstruction* object,
6311 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006313 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006314 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6315 SetPackedField<TypeCheckKindField>(check_kind);
6316 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006317 SetRawInputAt(0, object);
6318 SetRawInputAt(1, constant);
6319 }
6320
6321 bool CanBeMoved() const OVERRIDE { return true; }
6322
Vladimir Marko372f10e2016-05-17 16:30:10 +01006323 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006324 return true;
6325 }
6326
6327 bool NeedsEnvironment() const OVERRIDE {
6328 // Instruction may throw a CheckCastError.
6329 return true;
6330 }
6331
6332 bool CanThrow() const OVERRIDE { return true; }
6333
Vladimir Markoa1de9182016-02-25 11:37:38 +00006334 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6335 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6336 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6337 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006338
6339 DECLARE_INSTRUCTION(CheckCast);
6340
6341 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6343 static constexpr size_t kFieldTypeCheckKindSize =
6344 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6345 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6346 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6347 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6348 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006349
6350 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006351};
6352
Andreas Gampe26de38b2016-07-27 17:53:11 -07006353/**
6354 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6355 * @details We define the combined barrier types that are actually required
6356 * by the Java Memory Model, rather than using exactly the terminology from
6357 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6358 * primitives. Note that the JSR-133 cookbook generally does not deal with
6359 * store atomicity issues, and the recipes there are not always entirely sufficient.
6360 * The current recipe is as follows:
6361 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6362 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6363 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6364 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6365 * class has final fields.
6366 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6367 * store-to-memory instructions. Only generated together with non-temporal stores.
6368 */
6369enum MemBarrierKind {
6370 kAnyStore,
6371 kLoadAny,
6372 kStoreStore,
6373 kAnyAny,
6374 kNTStoreStore,
6375 kLastBarrierKind = kNTStoreStore
6376};
6377std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6378
Vladimir Markofcb503c2016-05-18 12:48:17 +01006379class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006380 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006381 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006382 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006383 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6384 SetPackedField<BarrierKindField>(barrier_kind);
6385 }
Calin Juravle27df7582015-04-17 19:12:31 +01006386
Vladimir Markoa1de9182016-02-25 11:37:38 +00006387 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006388
6389 DECLARE_INSTRUCTION(MemoryBarrier);
6390
6391 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6393 static constexpr size_t kFieldBarrierKindSize =
6394 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6395 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6396 kFieldBarrierKind + kFieldBarrierKindSize;
6397 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6398 "Too many packed fields.");
6399 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006400
6401 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6402};
6403
Vladimir Markofcb503c2016-05-18 12:48:17 +01006404class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006405 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006406 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006407 kEnter,
6408 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006410 };
6411
6412 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006413 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6415 dex_pc) {
6416 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006417 SetRawInputAt(0, object);
6418 }
6419
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006420 // Instruction may go into runtime, so we need an environment.
6421 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006422
6423 bool CanThrow() const OVERRIDE {
6424 // Verifier guarantees that monitor-exit cannot throw.
6425 // This is important because it allows the HGraphBuilder to remove
6426 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6427 return IsEnter();
6428 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006429
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6431 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006432
6433 DECLARE_INSTRUCTION(MonitorOperation);
6434
Calin Juravle52c48962014-12-16 17:02:57 +00006435 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006436 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6437 static constexpr size_t kFieldOperationKindSize =
6438 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6439 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6440 kFieldOperationKind + kFieldOperationKindSize;
6441 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6442 "Too many packed fields.");
6443 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006444
6445 private:
6446 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6447};
6448
Vladimir Markofcb503c2016-05-18 12:48:17 +01006449class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006450 public:
6451 HSelect(HInstruction* condition,
6452 HInstruction* true_value,
6453 HInstruction* false_value,
6454 uint32_t dex_pc)
6455 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6456 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6457
6458 // First input must be `true_value` or `false_value` to allow codegens to
6459 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6460 // that architectures which implement HSelect as a conditional move also
6461 // will not need to invert the condition.
6462 SetRawInputAt(0, false_value);
6463 SetRawInputAt(1, true_value);
6464 SetRawInputAt(2, condition);
6465 }
6466
6467 HInstruction* GetFalseValue() const { return InputAt(0); }
6468 HInstruction* GetTrueValue() const { return InputAt(1); }
6469 HInstruction* GetCondition() const { return InputAt(2); }
6470
6471 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006472 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6473 return true;
6474 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006475
6476 bool CanBeNull() const OVERRIDE {
6477 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6478 }
6479
6480 DECLARE_INSTRUCTION(Select);
6481
6482 private:
6483 DISALLOW_COPY_AND_ASSIGN(HSelect);
6484};
6485
Vladimir Markof9f64412015-09-02 14:05:49 +01006486class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006487 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006488 MoveOperands(Location source,
6489 Location destination,
6490 Primitive::Type type,
6491 HInstruction* instruction)
6492 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006493
6494 Location GetSource() const { return source_; }
6495 Location GetDestination() const { return destination_; }
6496
6497 void SetSource(Location value) { source_ = value; }
6498 void SetDestination(Location value) { destination_ = value; }
6499
6500 // The parallel move resolver marks moves as "in-progress" by clearing the
6501 // destination (but not the source).
6502 Location MarkPending() {
6503 DCHECK(!IsPending());
6504 Location dest = destination_;
6505 destination_ = Location::NoLocation();
6506 return dest;
6507 }
6508
6509 void ClearPending(Location dest) {
6510 DCHECK(IsPending());
6511 destination_ = dest;
6512 }
6513
6514 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006515 DCHECK(source_.IsValid() || destination_.IsInvalid());
6516 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006517 }
6518
6519 // True if this blocks a move from the given location.
6520 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006521 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006522 }
6523
6524 // A move is redundant if it's been eliminated, if its source and
6525 // destination are the same, or if its destination is unneeded.
6526 bool IsRedundant() const {
6527 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6528 }
6529
6530 // We clear both operands to indicate move that's been eliminated.
6531 void Eliminate() {
6532 source_ = destination_ = Location::NoLocation();
6533 }
6534
6535 bool IsEliminated() const {
6536 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6537 return source_.IsInvalid();
6538 }
6539
Alexey Frunze4dda3372015-06-01 18:31:49 -07006540 Primitive::Type GetType() const { return type_; }
6541
Nicolas Geoffray90218252015-04-15 11:56:51 +01006542 bool Is64BitMove() const {
6543 return Primitive::Is64BitType(type_);
6544 }
6545
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006546 HInstruction* GetInstruction() const { return instruction_; }
6547
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006548 private:
6549 Location source_;
6550 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006551 // The type this move is for.
6552 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006553 // The instruction this move is assocatied with. Null when this move is
6554 // for moving an input in the expected locations of user (including a phi user).
6555 // This is only used in debug mode, to ensure we do not connect interval siblings
6556 // in the same parallel move.
6557 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006558};
6559
Roland Levillainc9285912015-12-18 10:38:42 +00006560std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6561
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006562static constexpr size_t kDefaultNumberOfMoves = 4;
6563
Vladimir Markofcb503c2016-05-18 12:48:17 +01006564class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006565 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006566 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006567 : HTemplateInstruction(SideEffects::None(), dex_pc),
6568 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6569 moves_.reserve(kDefaultNumberOfMoves);
6570 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006571
Nicolas Geoffray90218252015-04-15 11:56:51 +01006572 void AddMove(Location source,
6573 Location destination,
6574 Primitive::Type type,
6575 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006576 DCHECK(source.IsValid());
6577 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006578 if (kIsDebugBuild) {
6579 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006580 for (const MoveOperands& move : moves_) {
6581 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006582 // Special case the situation where the move is for the spill slot
6583 // of the instruction.
6584 if ((GetPrevious() == instruction)
6585 || ((GetPrevious() == nullptr)
6586 && instruction->IsPhi()
6587 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006588 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006589 << "Doing parallel moves for the same instruction.";
6590 } else {
6591 DCHECK(false) << "Doing parallel moves for the same instruction.";
6592 }
6593 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006594 }
6595 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006596 for (const MoveOperands& move : moves_) {
6597 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006598 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006599 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006600 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006601 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006602 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006603 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006604 }
6605
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006607 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006608 }
6609
Vladimir Marko225b6462015-09-28 12:17:40 +01006610 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006611
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006612 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006613
6614 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006615 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616
6617 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6618};
6619
Mark Mendell0616ae02015-04-17 12:49:27 -04006620} // namespace art
6621
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006622#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6623#include "nodes_shared.h"
6624#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006625#ifdef ART_ENABLE_CODEGEN_arm
6626#include "nodes_arm.h"
6627#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006628#ifdef ART_ENABLE_CODEGEN_mips
6629#include "nodes_mips.h"
6630#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006631#ifdef ART_ENABLE_CODEGEN_x86
6632#include "nodes_x86.h"
6633#endif
6634
6635namespace art {
6636
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006637class HGraphVisitor : public ValueObject {
6638 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006639 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6640 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006641
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006642 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006643 virtual void VisitBasicBlock(HBasicBlock* block);
6644
Roland Levillain633021e2014-10-01 14:12:25 +01006645 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006646 void VisitInsertionOrder();
6647
Roland Levillain633021e2014-10-01 14:12:25 +01006648 // Visit the graph following dominator tree reverse post-order.
6649 void VisitReversePostOrder();
6650
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006651 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006652
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006653 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006654#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006655 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6656
6657 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6658
6659#undef DECLARE_VISIT_INSTRUCTION
6660
6661 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006662 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006663
6664 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6665};
6666
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006667class HGraphDelegateVisitor : public HGraphVisitor {
6668 public:
6669 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6670 virtual ~HGraphDelegateVisitor() {}
6671
6672 // Visit functions that delegate to to super class.
6673#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006674 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006675
6676 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6677
6678#undef DECLARE_VISIT_INSTRUCTION
6679
6680 private:
6681 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6682};
6683
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006684// Iterator over the blocks that art part of the loop. Includes blocks part
6685// of an inner loop. The order in which the blocks are iterated is on their
6686// block id.
6687class HBlocksInLoopIterator : public ValueObject {
6688 public:
6689 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6690 : blocks_in_loop_(info.GetBlocks()),
6691 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6692 index_(0) {
6693 if (!blocks_in_loop_.IsBitSet(index_)) {
6694 Advance();
6695 }
6696 }
6697
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006698 bool Done() const { return index_ == blocks_.size(); }
6699 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006700 void Advance() {
6701 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006702 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006703 if (blocks_in_loop_.IsBitSet(index_)) {
6704 break;
6705 }
6706 }
6707 }
6708
6709 private:
6710 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006711 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006712 size_t index_;
6713
6714 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6715};
6716
Mingyao Yang3584bce2015-05-19 16:01:59 -07006717// Iterator over the blocks that art part of the loop. Includes blocks part
6718// of an inner loop. The order in which the blocks are iterated is reverse
6719// post order.
6720class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6721 public:
6722 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6723 : blocks_in_loop_(info.GetBlocks()),
6724 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6725 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006726 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006727 Advance();
6728 }
6729 }
6730
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006731 bool Done() const { return index_ == blocks_.size(); }
6732 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006733 void Advance() {
6734 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006735 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6736 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006737 break;
6738 }
6739 }
6740 }
6741
6742 private:
6743 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006744 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006745 size_t index_;
6746
6747 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6748};
6749
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006750inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006751 if (constant->IsIntConstant()) {
6752 return constant->AsIntConstant()->GetValue();
6753 } else if (constant->IsLongConstant()) {
6754 return constant->AsLongConstant()->GetValue();
6755 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006756 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006757 return 0;
6758 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006759}
6760
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006761#define INSTRUCTION_TYPE_CHECK(type, super) \
6762 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6763 inline const H##type* HInstruction::As##type() const { \
6764 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6765 } \
6766 inline H##type* HInstruction::As##type() { \
6767 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6768 }
6769
6770 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6771#undef INSTRUCTION_TYPE_CHECK
6772
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006773// Create space in `blocks` for adding `number_of_new_blocks` entries
6774// starting at location `at`. Blocks after `at` are moved accordingly.
6775inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6776 size_t number_of_new_blocks,
6777 size_t after) {
6778 DCHECK_LT(after, blocks->size());
6779 size_t old_size = blocks->size();
6780 size_t new_size = old_size + number_of_new_blocks;
6781 blocks->resize(new_size);
6782 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6783}
6784
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006785/*
6786 * Hunt "under the hood" of array lengths (leading to array references),
6787 * null checks (also leading to array references), and new arrays
6788 * (leading to the actual length). This makes it more likely related
6789 * instructions become actually comparable.
6790 */
6791inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6792 while (instruction->IsArrayLength() ||
6793 instruction->IsNullCheck() ||
6794 instruction->IsNewArray()) {
6795 instruction = instruction->IsNewArray()
6796 ? instruction->AsNewArray()->GetLength()
6797 : instruction->InputAt(0);
6798 }
6799 return instruction;
6800}
6801
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006802} // namespace art
6803
6804#endif // ART_COMPILER_OPTIMIZING_NODES_H_