blob: 5f5a28c52064fbc3a7ce664b84579f0cc64dc048 [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 Geoffray6f8e2c92017-03-23 14:37:26 +00002084 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002085 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086
Alexandre Rames188d4312015-04-09 18:30:21 +01002087 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2088 // uses of this instruction by `other` are *not* updated.
2089 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2090 ReplaceWith(other);
2091 other->ReplaceInput(this, use_index);
2092 }
2093
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002094 // Move `this` instruction before `cursor`
2095 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002096
Vladimir Markofb337ea2015-11-25 15:25:10 +00002097 // Move `this` before its first user and out of any loops. If there is no
2098 // out-of-loop user that dominates all other users, move the instruction
2099 // to the end of the out-of-loop common dominator of the user's blocks.
2100 //
2101 // This can be used only on non-throwing instructions with no side effects that
2102 // have at least one use but no environment uses.
2103 void MoveBeforeFirstUserAndOutOfLoops();
2104
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002105#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 bool Is##type() const; \
2107 const H##type* As##type() const; \
2108 H##type* As##type();
2109
2110 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2111#undef INSTRUCTION_TYPE_CHECK
2112
2113#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002114 bool Is##type() const { return (As##type() != nullptr); } \
2115 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002116 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002117 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002118#undef INSTRUCTION_TYPE_CHECK
2119
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002120 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002121 // TODO: this method is used by LICM and GVN with possibly different
2122 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002123 virtual bool CanBeMoved() const { return false; }
2124
2125 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002126 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002127 return false;
2128 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002129
2130 // Returns whether any data encoded in the two instructions is equal.
2131 // This method does not look at the inputs. Both instructions must be
2132 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002133 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002134 return false;
2135 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002136
2137 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002138 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002139 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002140 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002141
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002142 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2143 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2144 // the virtual function because the __attribute__((__pure__)) doesn't really
2145 // apply the strong requirement for virtual functions, preventing optimizations.
2146 InstructionKind GetKind() const PURE;
2147 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002148
2149 virtual size_t ComputeHashCode() const {
2150 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002151 for (const HInstruction* input : GetInputs()) {
2152 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002153 }
2154 return result;
2155 }
2156
2157 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002158 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002159 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002160
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002161 size_t GetLifetimePosition() const { return lifetime_position_; }
2162 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2163 LiveInterval* GetLiveInterval() const { return live_interval_; }
2164 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2165 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2166
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002167 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2168
2169 // Returns whether the code generation of the instruction will require to have access
2170 // to the current method. Such instructions are:
2171 // (1): Instructions that require an environment, as calling the runtime requires
2172 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002173 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2174 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002175 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002176 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002177 }
2178
Vladimir Markodc151b22015-10-15 18:02:30 +01002179 // Returns whether the code generation of the instruction will require to have access
2180 // to the dex cache of the current method's declaring class via the current method.
2181 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002182
Mark Mendellc4701932015-04-10 13:18:51 -04002183 // Does this instruction have any use in an environment before
2184 // control flow hits 'other'?
2185 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2186
2187 // Remove all references to environment uses of this instruction.
2188 // The caller must ensure that this is safe to do.
2189 void RemoveEnvironmentUsers();
2190
Vladimir Markoa1de9182016-02-25 11:37:38 +00002191 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2192 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002193
David Brazdil1abb4192015-02-17 18:33:36 +00002194 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002195 // If set, the machine code for this instruction is assumed to be generated by
2196 // its users. Used by liveness analysis to compute use positions accordingly.
2197 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2198 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2199 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2200 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2201
Vladimir Marko372f10e2016-05-17 16:30:10 +01002202 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2203 return GetInputRecords()[i];
2204 }
2205
2206 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2207 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2208 input_records[index] = input;
2209 }
David Brazdil1abb4192015-02-17 18:33:36 +00002210
Vladimir Markoa1de9182016-02-25 11:37:38 +00002211 uint32_t GetPackedFields() const {
2212 return packed_fields_;
2213 }
2214
2215 template <size_t flag>
2216 bool GetPackedFlag() const {
2217 return (packed_fields_ & (1u << flag)) != 0u;
2218 }
2219
2220 template <size_t flag>
2221 void SetPackedFlag(bool value = true) {
2222 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2223 }
2224
2225 template <typename BitFieldType>
2226 typename BitFieldType::value_type GetPackedField() const {
2227 return BitFieldType::Decode(packed_fields_);
2228 }
2229
2230 template <typename BitFieldType>
2231 void SetPackedField(typename BitFieldType::value_type value) {
2232 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2233 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2234 }
2235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002236 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002237 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2238 auto before_use_node = uses_.before_begin();
2239 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2240 HInstruction* user = use_node->GetUser();
2241 size_t input_index = use_node->GetIndex();
2242 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2243 before_use_node = use_node;
2244 }
2245 }
2246
2247 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2248 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2249 if (next != uses_.end()) {
2250 HInstruction* next_user = next->GetUser();
2251 size_t next_index = next->GetIndex();
2252 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2253 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2254 }
2255 }
2256
2257 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2258 auto before_env_use_node = env_uses_.before_begin();
2259 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2260 HEnvironment* user = env_use_node->GetUser();
2261 size_t input_index = env_use_node->GetIndex();
2262 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2263 before_env_use_node = env_use_node;
2264 }
2265 }
2266
2267 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2268 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2269 if (next != env_uses_.end()) {
2270 HEnvironment* next_user = next->GetUser();
2271 size_t next_index = next->GetIndex();
2272 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2273 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2274 }
2275 }
David Brazdil1abb4192015-02-17 18:33:36 +00002276
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002277 HInstruction* previous_;
2278 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002279 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002280 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002281
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002282 // An instruction gets an id when it is added to the graph.
2283 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002284 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002285 int id_;
2286
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002287 // When doing liveness analysis, instructions that have uses get an SSA index.
2288 int ssa_index_;
2289
Vladimir Markoa1de9182016-02-25 11:37:38 +00002290 // Packed fields.
2291 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002292
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002293 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002294 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002295
2296 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002297 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002298
Nicolas Geoffray39468442014-09-02 15:17:15 +01002299 // The environment associated with this instruction. Not null if the instruction
2300 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002301 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002302
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002303 // Set by the code generator.
2304 LocationSummary* locations_;
2305
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002306 // Set by the liveness analysis.
2307 LiveInterval* live_interval_;
2308
2309 // Set by the liveness analysis, this is the position in a linear
2310 // order of blocks where this instruction's live interval start.
2311 size_t lifetime_position_;
2312
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002313 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002314
Vladimir Markoa1de9182016-02-25 11:37:38 +00002315 // The reference handle part of the reference type info.
2316 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002317 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002318 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002319
David Brazdil1abb4192015-02-17 18:33:36 +00002320 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002321 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002322 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002323 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002324 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002325
2326 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2327};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002328std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002330// Iterates over the instructions, while preserving the next instruction
2331// in case the current instruction gets removed from the list by the user
2332// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333class HInstructionIterator : public ValueObject {
2334 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002335 explicit HInstructionIterator(const HInstructionList& instructions)
2336 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002337 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338 }
2339
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002340 bool Done() const { return instruction_ == nullptr; }
2341 HInstruction* Current() const { return instruction_; }
2342 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002344 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002345 }
2346
2347 private:
2348 HInstruction* instruction_;
2349 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002350
2351 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002352};
2353
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002354// Iterates over the instructions without saving the next instruction,
2355// therefore handling changes in the graph potentially made by the user
2356// of this iterator.
2357class HInstructionIteratorHandleChanges : public ValueObject {
2358 public:
2359 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2360 : instruction_(instructions.first_instruction_) {
2361 }
2362
2363 bool Done() const { return instruction_ == nullptr; }
2364 HInstruction* Current() const { return instruction_; }
2365 void Advance() {
2366 instruction_ = instruction_->GetNext();
2367 }
2368
2369 private:
2370 HInstruction* instruction_;
2371
2372 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2373};
2374
2375
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002376class HBackwardInstructionIterator : public ValueObject {
2377 public:
2378 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2379 : instruction_(instructions.last_instruction_) {
2380 next_ = Done() ? nullptr : instruction_->GetPrevious();
2381 }
2382
2383 bool Done() const { return instruction_ == nullptr; }
2384 HInstruction* Current() const { return instruction_; }
2385 void Advance() {
2386 instruction_ = next_;
2387 next_ = Done() ? nullptr : instruction_->GetPrevious();
2388 }
2389
2390 private:
2391 HInstruction* instruction_;
2392 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002393
2394 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002395};
2396
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002397class HVariableInputSizeInstruction : public HInstruction {
2398 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002399 using HInstruction::GetInputRecords; // Keep the const version visible.
2400 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2401 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2402 }
2403
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002404 void AddInput(HInstruction* input);
2405 void InsertInputAt(size_t index, HInstruction* input);
2406 void RemoveInputAt(size_t index);
2407
2408 protected:
2409 HVariableInputSizeInstruction(SideEffects side_effects,
2410 uint32_t dex_pc,
2411 ArenaAllocator* arena,
2412 size_t number_of_inputs,
2413 ArenaAllocKind kind)
2414 : HInstruction(side_effects, dex_pc),
2415 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2416
2417 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2418
2419 private:
2420 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2421};
2422
Vladimir Markof9f64412015-09-02 14:05:49 +01002423template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002424class HTemplateInstruction: public HInstruction {
2425 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002426 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002427 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002428 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002429
Vladimir Marko372f10e2016-05-17 16:30:10 +01002430 using HInstruction::GetInputRecords; // Keep the const version visible.
2431 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2432 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002433 }
2434
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002435 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002436 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002437
2438 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002439};
2440
Vladimir Markof9f64412015-09-02 14:05:49 +01002441// HTemplateInstruction specialization for N=0.
2442template<>
2443class HTemplateInstruction<0>: public HInstruction {
2444 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002445 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002446 : HInstruction(side_effects, dex_pc) {}
2447
Vladimir Markof9f64412015-09-02 14:05:49 +01002448 virtual ~HTemplateInstruction() {}
2449
Vladimir Marko372f10e2016-05-17 16:30:10 +01002450 using HInstruction::GetInputRecords; // Keep the const version visible.
2451 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2452 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002453 }
2454
2455 private:
2456 friend class SsaBuilder;
2457};
2458
Dave Allison20dfc792014-06-16 20:44:29 -07002459template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002460class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002461 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002462 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002463 : HTemplateInstruction<N>(side_effects, dex_pc) {
2464 this->template SetPackedField<TypeField>(type);
2465 }
Dave Allison20dfc792014-06-16 20:44:29 -07002466 virtual ~HExpression() {}
2467
Vladimir Markoa1de9182016-02-25 11:37:38 +00002468 Primitive::Type GetType() const OVERRIDE {
2469 return TypeField::Decode(this->GetPackedFields());
2470 }
Dave Allison20dfc792014-06-16 20:44:29 -07002471
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002472 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002473 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2474 static constexpr size_t kFieldTypeSize =
2475 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2476 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2477 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2478 "Too many packed fields.");
2479 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002480};
2481
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002482// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2483// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002484class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002486 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2487 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002489 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002491 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002492
2493 private:
2494 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2495};
2496
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002497// Represents dex's RETURN opcodes. A HReturn is a control flow
2498// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002499class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002500 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002501 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2502 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002503 SetRawInputAt(0, value);
2504 }
2505
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002506 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002507
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002508 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002509
2510 private:
2511 DISALLOW_COPY_AND_ASSIGN(HReturn);
2512};
2513
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002514class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002515 public:
2516 HPhi(ArenaAllocator* arena,
2517 uint32_t reg_number,
2518 size_t number_of_inputs,
2519 Primitive::Type type,
2520 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002521 : HVariableInputSizeInstruction(
2522 SideEffects::None(),
2523 dex_pc,
2524 arena,
2525 number_of_inputs,
2526 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002527 reg_number_(reg_number) {
2528 SetPackedField<TypeField>(ToPhiType(type));
2529 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2530 // Phis are constructed live and marked dead if conflicting or unused.
2531 // Individual steps of SsaBuilder should assume that if a phi has been
2532 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2533 SetPackedFlag<kFlagIsLive>(true);
2534 SetPackedFlag<kFlagCanBeNull>(true);
2535 }
2536
2537 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2538 static Primitive::Type ToPhiType(Primitive::Type type) {
2539 return Primitive::PrimitiveKind(type);
2540 }
2541
2542 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2543
David Brazdildee58d62016-04-07 09:54:26 +00002544 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2545 void SetType(Primitive::Type new_type) {
2546 // Make sure that only valid type changes occur. The following are allowed:
2547 // (1) int -> float/ref (primitive type propagation),
2548 // (2) long -> double (primitive type propagation).
2549 DCHECK(GetType() == new_type ||
2550 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2551 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2552 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2553 SetPackedField<TypeField>(new_type);
2554 }
2555
2556 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2557 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2558
2559 uint32_t GetRegNumber() const { return reg_number_; }
2560
2561 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2562 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2563 bool IsDead() const { return !IsLive(); }
2564 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2565
Vladimir Markoe9004912016-06-16 16:50:52 +01002566 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002567 return other != nullptr
2568 && other->IsPhi()
2569 && other->AsPhi()->GetBlock() == GetBlock()
2570 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2571 }
2572
2573 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2574 // An equivalent phi is a phi having the same dex register and type.
2575 // It assumes that phis with the same dex register are adjacent.
2576 HPhi* GetNextEquivalentPhiWithSameType() {
2577 HInstruction* next = GetNext();
2578 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2579 if (next->GetType() == GetType()) {
2580 return next->AsPhi();
2581 }
2582 next = next->GetNext();
2583 }
2584 return nullptr;
2585 }
2586
2587 DECLARE_INSTRUCTION(Phi);
2588
David Brazdildee58d62016-04-07 09:54:26 +00002589 private:
2590 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2591 static constexpr size_t kFieldTypeSize =
2592 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2593 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2594 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2595 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2596 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2597 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2598
David Brazdildee58d62016-04-07 09:54:26 +00002599 const uint32_t reg_number_;
2600
2601 DISALLOW_COPY_AND_ASSIGN(HPhi);
2602};
2603
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002605// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002606// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002607class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002608 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002609 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002610
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002611 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002613 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002614
2615 private:
2616 DISALLOW_COPY_AND_ASSIGN(HExit);
2617};
2618
2619// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002620class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002622 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002623
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002624 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002625
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002626 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002627 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002628 }
2629
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002630 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002631
2632 private:
2633 DISALLOW_COPY_AND_ASSIGN(HGoto);
2634};
2635
Roland Levillain9867bc72015-08-05 10:21:34 +01002636class HConstant : public HExpression<0> {
2637 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002638 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2639 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002640
2641 bool CanBeMoved() const OVERRIDE { return true; }
2642
Roland Levillain1a653882016-03-18 18:05:57 +00002643 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002644 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002645 // Is this constant 0 in the arithmetic sense?
2646 virtual bool IsArithmeticZero() const { return false; }
2647 // Is this constant a 0-bit pattern?
2648 virtual bool IsZeroBitPattern() const { return false; }
2649 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002650 virtual bool IsOne() const { return false; }
2651
David Brazdil77a48ae2015-09-15 12:34:04 +00002652 virtual uint64_t GetValueAsUint64() const = 0;
2653
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002654 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002655
2656 private:
2657 DISALLOW_COPY_AND_ASSIGN(HConstant);
2658};
2659
Vladimir Markofcb503c2016-05-18 12:48:17 +01002660class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002661 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002662 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 return true;
2664 }
2665
David Brazdil77a48ae2015-09-15 12:34:04 +00002666 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2667
Roland Levillain9867bc72015-08-05 10:21:34 +01002668 size_t ComputeHashCode() const OVERRIDE { return 0; }
2669
Roland Levillain1a653882016-03-18 18:05:57 +00002670 // The null constant representation is a 0-bit pattern.
2671 virtual bool IsZeroBitPattern() const { return true; }
2672
Roland Levillain9867bc72015-08-05 10:21:34 +01002673 DECLARE_INSTRUCTION(NullConstant);
2674
2675 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002676 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002677
2678 friend class HGraph;
2679 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2680};
2681
2682// Constants of the type int. Those can be from Dex instructions, or
2683// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002684class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002685 public:
2686 int32_t GetValue() const { return value_; }
2687
David Brazdil9f389d42015-10-01 14:32:56 +01002688 uint64_t GetValueAsUint64() const OVERRIDE {
2689 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2690 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002691
Vladimir Marko372f10e2016-05-17 16:30:10 +01002692 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002693 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 return other->AsIntConstant()->value_ == value_;
2695 }
2696
2697 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2698
2699 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002700 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2701 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2703
Roland Levillain1a653882016-03-18 18:05:57 +00002704 // Integer constants are used to encode Boolean values as well,
2705 // where 1 means true and 0 means false.
2706 bool IsTrue() const { return GetValue() == 1; }
2707 bool IsFalse() const { return GetValue() == 0; }
2708
Roland Levillain9867bc72015-08-05 10:21:34 +01002709 DECLARE_INSTRUCTION(IntConstant);
2710
2711 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002712 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2713 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2714 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2715 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002716
2717 const int32_t value_;
2718
2719 friend class HGraph;
2720 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2721 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2722 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2723};
2724
Vladimir Markofcb503c2016-05-18 12:48:17 +01002725class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002726 public:
2727 int64_t GetValue() const { return value_; }
2728
David Brazdil77a48ae2015-09-15 12:34:04 +00002729 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2730
Vladimir Marko372f10e2016-05-17 16:30:10 +01002731 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002732 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002733 return other->AsLongConstant()->value_ == value_;
2734 }
2735
2736 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2737
2738 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002739 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2740 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002741 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2742
2743 DECLARE_INSTRUCTION(LongConstant);
2744
2745 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002746 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2747 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002748
2749 const int64_t value_;
2750
2751 friend class HGraph;
2752 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2753};
Dave Allison20dfc792014-06-16 20:44:29 -07002754
Vladimir Markofcb503c2016-05-18 12:48:17 +01002755class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002756 public:
2757 float GetValue() const { return value_; }
2758
2759 uint64_t GetValueAsUint64() const OVERRIDE {
2760 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2761 }
2762
Vladimir Marko372f10e2016-05-17 16:30:10 +01002763 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002764 DCHECK(other->IsFloatConstant()) << other->DebugName();
2765 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2766 }
2767
2768 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2769
2770 bool IsMinusOne() const OVERRIDE {
2771 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2772 }
Roland Levillain1a653882016-03-18 18:05:57 +00002773 bool IsArithmeticZero() const OVERRIDE {
2774 return std::fpclassify(value_) == FP_ZERO;
2775 }
2776 bool IsArithmeticPositiveZero() const {
2777 return IsArithmeticZero() && !std::signbit(value_);
2778 }
2779 bool IsArithmeticNegativeZero() const {
2780 return IsArithmeticZero() && std::signbit(value_);
2781 }
2782 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002783 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002784 }
2785 bool IsOne() const OVERRIDE {
2786 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2787 }
2788 bool IsNaN() const {
2789 return std::isnan(value_);
2790 }
2791
2792 DECLARE_INSTRUCTION(FloatConstant);
2793
2794 private:
2795 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2796 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2797 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2798 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2799
2800 const float value_;
2801
2802 // Only the SsaBuilder and HGraph can create floating-point constants.
2803 friend class SsaBuilder;
2804 friend class HGraph;
2805 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2806};
2807
Vladimir Markofcb503c2016-05-18 12:48:17 +01002808class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002809 public:
2810 double GetValue() const { return value_; }
2811
2812 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2813
Vladimir Marko372f10e2016-05-17 16:30:10 +01002814 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002815 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2816 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2817 }
2818
2819 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2820
2821 bool IsMinusOne() const OVERRIDE {
2822 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2823 }
Roland Levillain1a653882016-03-18 18:05:57 +00002824 bool IsArithmeticZero() const OVERRIDE {
2825 return std::fpclassify(value_) == FP_ZERO;
2826 }
2827 bool IsArithmeticPositiveZero() const {
2828 return IsArithmeticZero() && !std::signbit(value_);
2829 }
2830 bool IsArithmeticNegativeZero() const {
2831 return IsArithmeticZero() && std::signbit(value_);
2832 }
2833 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002834 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002835 }
2836 bool IsOne() const OVERRIDE {
2837 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2838 }
2839 bool IsNaN() const {
2840 return std::isnan(value_);
2841 }
2842
2843 DECLARE_INSTRUCTION(DoubleConstant);
2844
2845 private:
2846 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2847 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2848 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2849 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2850
2851 const double value_;
2852
2853 // Only the SsaBuilder and HGraph can create floating-point constants.
2854 friend class SsaBuilder;
2855 friend class HGraph;
2856 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2857};
2858
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002859// Conditional branch. A block ending with an HIf instruction must have
2860// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002861class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002862 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002863 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2864 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002865 SetRawInputAt(0, input);
2866 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002867
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002868 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002869
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002870 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002871 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002872 }
2873
2874 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002875 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002876 }
2877
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002878 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002879
2880 private:
2881 DISALLOW_COPY_AND_ASSIGN(HIf);
2882};
2883
David Brazdilfc6a86a2015-06-26 10:33:45 +00002884
2885// Abstract instruction which marks the beginning and/or end of a try block and
2886// links it to the respective exception handlers. Behaves the same as a Goto in
2887// non-exceptional control flow.
2888// Normal-flow successor is stored at index zero, exception handlers under
2889// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002890class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002891 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002892 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002893 kEntry,
2894 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002895 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002896 };
2897
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002898 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002899 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2900 SetPackedField<BoundaryKindField>(kind);
2901 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002902
2903 bool IsControlFlow() const OVERRIDE { return true; }
2904
2905 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002906 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002907
David Brazdild26a4112015-11-10 11:07:31 +00002908 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2909 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2910 }
2911
David Brazdilfc6a86a2015-06-26 10:33:45 +00002912 // Returns whether `handler` is among its exception handlers (non-zero index
2913 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002914 bool HasExceptionHandler(const HBasicBlock& handler) const {
2915 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002916 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002917 }
2918
2919 // If not present already, adds `handler` to its block's list of exception
2920 // handlers.
2921 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002922 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002923 GetBlock()->AddSuccessor(handler);
2924 }
2925 }
2926
Vladimir Markoa1de9182016-02-25 11:37:38 +00002927 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2928 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
David Brazdilffee3d32015-07-06 11:48:53 +01002930 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2931
David Brazdilfc6a86a2015-06-26 10:33:45 +00002932 DECLARE_INSTRUCTION(TryBoundary);
2933
2934 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002935 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2936 static constexpr size_t kFieldBoundaryKindSize =
2937 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2938 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2939 kFieldBoundaryKind + kFieldBoundaryKindSize;
2940 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2941 "Too many packed fields.");
2942 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002943
2944 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2945};
2946
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002947// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002948class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002949 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002950 enum class Kind {
2951 kBCE,
2952 kInline,
2953 kLast = kInline
2954 };
2955
2956 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2957 // across.
2958 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2959 : HVariableInputSizeInstruction(
2960 SideEffects::All(),
2961 dex_pc,
2962 arena,
2963 /* number_of_inputs */ 1,
2964 kArenaAllocMisc) {
2965 SetPackedFlag<kFieldCanBeMoved>(false);
2966 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002967 SetRawInputAt(0, cond);
2968 }
2969
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002970 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
2971 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
2972 // instead of `guard`.
2973 // We set CanTriggerGC to prevent any intermediate address to be live
2974 // at the point of the `HDeoptimize`.
2975 HDeoptimize(ArenaAllocator* arena,
2976 HInstruction* cond,
2977 HInstruction* guard,
2978 Kind kind,
2979 uint32_t dex_pc)
2980 : HVariableInputSizeInstruction(
2981 SideEffects::CanTriggerGC(),
2982 dex_pc,
2983 arena,
2984 /* number_of_inputs */ 2,
2985 kArenaAllocMisc) {
2986 SetPackedFlag<kFieldCanBeMoved>(true);
2987 SetPackedField<DeoptimizeKindField>(kind);
2988 SetRawInputAt(0, cond);
2989 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002990 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002991
2992 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
2993
2994 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
2995 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
2996 }
2997
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002998 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002999
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003000 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003001
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003002 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3003
3004 Primitive::Type GetType() const OVERRIDE {
3005 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3006 }
3007
3008 bool GuardsAnInput() const {
3009 return InputCount() == 2;
3010 }
3011
3012 HInstruction* GuardedInput() const {
3013 DCHECK(GuardsAnInput());
3014 return InputAt(1);
3015 }
3016
3017 void RemoveGuard() {
3018 RemoveInputAt(1);
3019 }
3020
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003021 DECLARE_INSTRUCTION(Deoptimize);
3022
3023 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003024 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3025 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3026 static constexpr size_t kFieldDeoptimizeKindSize =
3027 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3028 static constexpr size_t kNumberOfDeoptimizePackedBits =
3029 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3030 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3031 "Too many packed fields.");
3032 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3033
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003034 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3035};
3036
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003037std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3038
Mingyao Yang063fc772016-08-02 11:02:54 -07003039// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3040// The compiled code checks this flag value in a guard before devirtualized call and
3041// if it's true, starts to do deoptimization.
3042// It has a 4-byte slot on stack.
3043// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003044class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003045 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003046 // CHA guards are only optimized in a separate pass and it has no side effects
3047 // with regard to other passes.
3048 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3049 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003050 }
3051
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003052 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3053
3054 // We do all CHA guard elimination/motion in a single pass, after which there is no
3055 // further guard elimination/motion since a guard might have been used for justification
3056 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3057 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003058 bool CanBeMoved() const OVERRIDE { return false; }
3059
3060 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3061
3062 private:
3063 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3064};
3065
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003066// Represents the ArtMethod that was passed as a first argument to
3067// the method. It is used by instructions that depend on it, like
3068// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003069class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003070 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003071 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3072 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003073
3074 DECLARE_INSTRUCTION(CurrentMethod);
3075
3076 private:
3077 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3078};
3079
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003080// Fetches an ArtMethod from the virtual table or the interface method table
3081// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003082class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003083 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003084 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003085 kVTable,
3086 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003087 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003088 };
3089 HClassTableGet(HInstruction* cls,
3090 Primitive::Type type,
3091 TableKind kind,
3092 size_t index,
3093 uint32_t dex_pc)
3094 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003095 index_(index) {
3096 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003097 SetRawInputAt(0, cls);
3098 }
3099
3100 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003101 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003102 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003103 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003104 }
3105
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003107 size_t GetIndex() const { return index_; }
3108
3109 DECLARE_INSTRUCTION(ClassTableGet);
3110
3111 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003112 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3113 static constexpr size_t kFieldTableKindSize =
3114 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3115 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3116 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3117 "Too many packed fields.");
3118 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3119
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003120 // The index of the ArtMethod in the table.
3121 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003122
3123 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3124};
3125
Mark Mendellfe57faa2015-09-18 09:26:15 -04003126// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3127// have one successor for each entry in the switch table, and the final successor
3128// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003129class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003130 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003131 HPackedSwitch(int32_t start_value,
3132 uint32_t num_entries,
3133 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003134 uint32_t dex_pc = kNoDexPc)
3135 : HTemplateInstruction(SideEffects::None(), dex_pc),
3136 start_value_(start_value),
3137 num_entries_(num_entries) {
3138 SetRawInputAt(0, input);
3139 }
3140
3141 bool IsControlFlow() const OVERRIDE { return true; }
3142
3143 int32_t GetStartValue() const { return start_value_; }
3144
Vladimir Marko211c2112015-09-24 16:52:33 +01003145 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003146
3147 HBasicBlock* GetDefaultBlock() const {
3148 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003149 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003150 }
3151 DECLARE_INSTRUCTION(PackedSwitch);
3152
3153 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003154 const int32_t start_value_;
3155 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003156
3157 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3158};
3159
Roland Levillain88cb1752014-10-20 16:36:47 +01003160class HUnaryOperation : public HExpression<1> {
3161 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003162 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3163 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003164 SetRawInputAt(0, input);
3165 }
3166
3167 HInstruction* GetInput() const { return InputAt(0); }
3168 Primitive::Type GetResultType() const { return GetType(); }
3169
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003170 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003171 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003172 return true;
3173 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003174
Roland Levillain31dd3d62016-02-16 12:21:02 +00003175 // Try to statically evaluate `this` and return a HConstant
3176 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003177 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003178 HConstant* TryStaticEvaluation() const;
3179
3180 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3182 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003183 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3184 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003185
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003186 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003187
3188 private:
3189 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3190};
3191
Dave Allison20dfc792014-06-16 20:44:29 -07003192class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003193 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003194 HBinaryOperation(Primitive::Type result_type,
3195 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003196 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003197 SideEffects side_effects = SideEffects::None(),
3198 uint32_t dex_pc = kNoDexPc)
3199 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003200 SetRawInputAt(0, left);
3201 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003202 }
3203
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003204 HInstruction* GetLeft() const { return InputAt(0); }
3205 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003206 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003207
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003208 virtual bool IsCommutative() const { return false; }
3209
3210 // Put constant on the right.
3211 // Returns whether order is changed.
3212 bool OrderInputsWithConstantOnTheRight() {
3213 HInstruction* left = InputAt(0);
3214 HInstruction* right = InputAt(1);
3215 if (left->IsConstant() && !right->IsConstant()) {
3216 ReplaceInput(right, 0);
3217 ReplaceInput(left, 1);
3218 return true;
3219 }
3220 return false;
3221 }
3222
3223 // Order inputs by instruction id, but favor constant on the right side.
3224 // This helps GVN for commutative ops.
3225 void OrderInputs() {
3226 DCHECK(IsCommutative());
3227 HInstruction* left = InputAt(0);
3228 HInstruction* right = InputAt(1);
3229 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3230 return;
3231 }
3232 if (OrderInputsWithConstantOnTheRight()) {
3233 return;
3234 }
3235 // Order according to instruction id.
3236 if (left->GetId() > right->GetId()) {
3237 ReplaceInput(right, 0);
3238 ReplaceInput(left, 1);
3239 }
3240 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003241
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003242 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003243 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003244 return true;
3245 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003246
Roland Levillain31dd3d62016-02-16 12:21:02 +00003247 // Try to statically evaluate `this` and return a HConstant
3248 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003249 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003250 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003251
3252 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003253 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3254 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003255 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3256 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003257 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003258 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3259 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003260 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3261 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003262 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3263 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003264 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003265 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3266 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003267
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003268 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003269 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003270 HConstant* GetConstantRight() const;
3271
3272 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003273 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003274 HInstruction* GetLeastConstantLeft() const;
3275
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003276 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003277
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003278 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003279 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3280};
3281
Mark Mendellc4701932015-04-10 13:18:51 -04003282// The comparison bias applies for floating point operations and indicates how NaN
3283// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003284enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003285 kNoBias, // bias is not applicable (i.e. for long operation)
3286 kGtBias, // return 1 for NaN comparisons
3287 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003288 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003289};
3290
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3292
Dave Allison20dfc792014-06-16 20:44:29 -07003293class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003294 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003295 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003296 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3297 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3298 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003299
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003300 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003301 // `instruction`, and disregard moves in between.
3302 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003303
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003304 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003305
3306 virtual IfCondition GetCondition() const = 0;
3307
Mark Mendellc4701932015-04-10 13:18:51 -04003308 virtual IfCondition GetOppositeCondition() const = 0;
3309
Vladimir Markoa1de9182016-02-25 11:37:38 +00003310 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003311 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3312
Vladimir Markoa1de9182016-02-25 11:37:38 +00003313 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3314 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003315
Vladimir Marko372f10e2016-05-17 16:30:10 +01003316 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003317 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003318 }
3319
Roland Levillain4fa13f62015-07-06 18:11:54 +01003320 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003322 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003323 if (if_cond == kCondNE) {
3324 return true;
3325 } else if (if_cond == kCondEQ) {
3326 return false;
3327 }
3328 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003329 }
3330
3331 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003332 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003333 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003334 if (if_cond == kCondEQ) {
3335 return true;
3336 } else if (if_cond == kCondNE) {
3337 return false;
3338 }
3339 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003340 }
3341
Roland Levillain31dd3d62016-02-16 12:21:02 +00003342 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 // Needed if we merge a HCompare into a HCondition.
3344 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3345 static constexpr size_t kFieldComparisonBiasSize =
3346 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3347 static constexpr size_t kNumberOfConditionPackedBits =
3348 kFieldComparisonBias + kFieldComparisonBiasSize;
3349 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3350 using ComparisonBiasField =
3351 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3352
Roland Levillain31dd3d62016-02-16 12:21:02 +00003353 template <typename T>
3354 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3355
3356 template <typename T>
3357 int32_t CompareFP(T x, T y) const {
3358 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3359 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3360 // Handle the bias.
3361 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3362 }
3363
3364 // Return an integer constant containing the result of a condition evaluated at compile time.
3365 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3366 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3367 }
3368
Dave Allison20dfc792014-06-16 20:44:29 -07003369 private:
3370 DISALLOW_COPY_AND_ASSIGN(HCondition);
3371};
3372
3373// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003374class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003375 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003376 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3377 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003378
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003379 bool IsCommutative() const OVERRIDE { return true; }
3380
Vladimir Marko9e23df52015-11-10 17:14:35 +00003381 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3382 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003383 return MakeConstantCondition(true, GetDexPc());
3384 }
3385 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3386 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3387 }
3388 // In the following Evaluate methods, a HCompare instruction has
3389 // been merged into this HEqual instruction; evaluate it as
3390 // `Compare(x, y) == 0`.
3391 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3392 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3393 GetDexPc());
3394 }
3395 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3396 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3397 }
3398 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3399 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003400 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003401
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003402 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003403
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003404 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003405 return kCondEQ;
3406 }
3407
Mark Mendellc4701932015-04-10 13:18:51 -04003408 IfCondition GetOppositeCondition() const OVERRIDE {
3409 return kCondNE;
3410 }
3411
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003412 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003413 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003414
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003415 DISALLOW_COPY_AND_ASSIGN(HEqual);
3416};
3417
Vladimir Markofcb503c2016-05-18 12:48:17 +01003418class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3421 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003422
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003423 bool IsCommutative() const OVERRIDE { return true; }
3424
Vladimir Marko9e23df52015-11-10 17:14:35 +00003425 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3426 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(false, GetDexPc());
3428 }
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3431 }
3432 // In the following Evaluate methods, a HCompare instruction has
3433 // been merged into this HNotEqual instruction; evaluate it as
3434 // `Compare(x, y) != 0`.
3435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3436 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3437 }
3438 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3439 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3440 }
3441 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3442 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003443 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003444
Dave Allison20dfc792014-06-16 20:44:29 -07003445 DECLARE_INSTRUCTION(NotEqual);
3446
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003447 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003448 return kCondNE;
3449 }
3450
Mark Mendellc4701932015-04-10 13:18:51 -04003451 IfCondition GetOppositeCondition() const OVERRIDE {
3452 return kCondEQ;
3453 }
3454
Dave Allison20dfc792014-06-16 20:44:29 -07003455 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003456 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003457
Dave Allison20dfc792014-06-16 20:44:29 -07003458 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3459};
3460
Vladimir Markofcb503c2016-05-18 12:48:17 +01003461class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003462 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003463 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3464 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003465
Roland Levillain9867bc72015-08-05 10:21:34 +01003466 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003467 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003468 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003469 // In the following Evaluate methods, a HCompare instruction has
3470 // been merged into this HLessThan instruction; evaluate it as
3471 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003472 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003473 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3474 }
3475 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3476 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3477 }
3478 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3479 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003480 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003481
Dave Allison20dfc792014-06-16 20:44:29 -07003482 DECLARE_INSTRUCTION(LessThan);
3483
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003484 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003485 return kCondLT;
3486 }
3487
Mark Mendellc4701932015-04-10 13:18:51 -04003488 IfCondition GetOppositeCondition() const OVERRIDE {
3489 return kCondGE;
3490 }
3491
Dave Allison20dfc792014-06-16 20:44:29 -07003492 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003493 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003494
Dave Allison20dfc792014-06-16 20:44:29 -07003495 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3496};
3497
Vladimir Markofcb503c2016-05-18 12:48:17 +01003498class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003499 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003500 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3501 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003502
Roland Levillain9867bc72015-08-05 10:21:34 +01003503 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003504 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003505 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003506 // In the following Evaluate methods, a HCompare instruction has
3507 // been merged into this HLessThanOrEqual instruction; evaluate it as
3508 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003509 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003510 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3511 }
3512 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3513 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3514 }
3515 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3516 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003517 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003518
Dave Allison20dfc792014-06-16 20:44:29 -07003519 DECLARE_INSTRUCTION(LessThanOrEqual);
3520
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003521 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003522 return kCondLE;
3523 }
3524
Mark Mendellc4701932015-04-10 13:18:51 -04003525 IfCondition GetOppositeCondition() const OVERRIDE {
3526 return kCondGT;
3527 }
3528
Dave Allison20dfc792014-06-16 20:44:29 -07003529 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003530 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003531
Dave Allison20dfc792014-06-16 20:44:29 -07003532 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3533};
3534
Vladimir Markofcb503c2016-05-18 12:48:17 +01003535class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003536 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003537 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3538 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003539
Roland Levillain9867bc72015-08-05 10:21:34 +01003540 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003541 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003542 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003543 // In the following Evaluate methods, a HCompare instruction has
3544 // been merged into this HGreaterThan instruction; evaluate it as
3545 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003546 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3548 }
3549 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3550 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3551 }
3552 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3553 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003554 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003555
Dave Allison20dfc792014-06-16 20:44:29 -07003556 DECLARE_INSTRUCTION(GreaterThan);
3557
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003558 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003559 return kCondGT;
3560 }
3561
Mark Mendellc4701932015-04-10 13:18:51 -04003562 IfCondition GetOppositeCondition() const OVERRIDE {
3563 return kCondLE;
3564 }
3565
Dave Allison20dfc792014-06-16 20:44:29 -07003566 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003567 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003568
Dave Allison20dfc792014-06-16 20:44:29 -07003569 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3570};
3571
Vladimir Markofcb503c2016-05-18 12:48:17 +01003572class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003573 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003574 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3575 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003576
Roland Levillain9867bc72015-08-05 10:21:34 +01003577 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003578 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003579 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580 // In the following Evaluate methods, a HCompare instruction has
3581 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3582 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003583 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3585 }
3586 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3587 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3588 }
3589 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3590 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003591 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003592
Dave Allison20dfc792014-06-16 20:44:29 -07003593 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3594
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003595 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003596 return kCondGE;
3597 }
3598
Mark Mendellc4701932015-04-10 13:18:51 -04003599 IfCondition GetOppositeCondition() const OVERRIDE {
3600 return kCondLT;
3601 }
3602
Dave Allison20dfc792014-06-16 20:44:29 -07003603 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003604 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003605
Dave Allison20dfc792014-06-16 20:44:29 -07003606 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3607};
3608
Vladimir Markofcb503c2016-05-18 12:48:17 +01003609class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003610 public:
3611 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3612 : HCondition(first, second, dex_pc) {}
3613
3614 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003616 }
3617 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003618 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3619 }
3620 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3621 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3622 LOG(FATAL) << DebugName() << " is not defined for float values";
3623 UNREACHABLE();
3624 }
3625 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3626 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3627 LOG(FATAL) << DebugName() << " is not defined for double values";
3628 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003629 }
3630
3631 DECLARE_INSTRUCTION(Below);
3632
3633 IfCondition GetCondition() const OVERRIDE {
3634 return kCondB;
3635 }
3636
3637 IfCondition GetOppositeCondition() const OVERRIDE {
3638 return kCondAE;
3639 }
3640
3641 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003642 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 return MakeUnsigned(x) < MakeUnsigned(y);
3644 }
Aart Bike9f37602015-10-09 11:15:55 -07003645
3646 DISALLOW_COPY_AND_ASSIGN(HBelow);
3647};
3648
Vladimir Markofcb503c2016-05-18 12:48:17 +01003649class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003650 public:
3651 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3652 : HCondition(first, second, dex_pc) {}
3653
3654 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003656 }
3657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003658 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3659 }
3660 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3661 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3662 LOG(FATAL) << DebugName() << " is not defined for float values";
3663 UNREACHABLE();
3664 }
3665 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3666 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3667 LOG(FATAL) << DebugName() << " is not defined for double values";
3668 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003669 }
3670
3671 DECLARE_INSTRUCTION(BelowOrEqual);
3672
3673 IfCondition GetCondition() const OVERRIDE {
3674 return kCondBE;
3675 }
3676
3677 IfCondition GetOppositeCondition() const OVERRIDE {
3678 return kCondA;
3679 }
3680
3681 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003682 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeUnsigned(x) <= MakeUnsigned(y);
3684 }
Aart Bike9f37602015-10-09 11:15:55 -07003685
3686 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3687};
3688
Vladimir Markofcb503c2016-05-18 12:48:17 +01003689class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003690 public:
3691 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3692 : HCondition(first, second, dex_pc) {}
3693
3694 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003695 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003696 }
3697 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003698 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3699 }
3700 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3701 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3702 LOG(FATAL) << DebugName() << " is not defined for float values";
3703 UNREACHABLE();
3704 }
3705 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3706 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3707 LOG(FATAL) << DebugName() << " is not defined for double values";
3708 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003709 }
3710
3711 DECLARE_INSTRUCTION(Above);
3712
3713 IfCondition GetCondition() const OVERRIDE {
3714 return kCondA;
3715 }
3716
3717 IfCondition GetOppositeCondition() const OVERRIDE {
3718 return kCondBE;
3719 }
3720
3721 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003722 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 return MakeUnsigned(x) > MakeUnsigned(y);
3724 }
Aart Bike9f37602015-10-09 11:15:55 -07003725
3726 DISALLOW_COPY_AND_ASSIGN(HAbove);
3727};
3728
Vladimir Markofcb503c2016-05-18 12:48:17 +01003729class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003730 public:
3731 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3732 : HCondition(first, second, dex_pc) {}
3733
3734 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003735 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003736 }
3737 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003738 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3739 }
3740 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3741 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3742 LOG(FATAL) << DebugName() << " is not defined for float values";
3743 UNREACHABLE();
3744 }
3745 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3746 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3747 LOG(FATAL) << DebugName() << " is not defined for double values";
3748 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003749 }
3750
3751 DECLARE_INSTRUCTION(AboveOrEqual);
3752
3753 IfCondition GetCondition() const OVERRIDE {
3754 return kCondAE;
3755 }
3756
3757 IfCondition GetOppositeCondition() const OVERRIDE {
3758 return kCondB;
3759 }
3760
3761 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003762 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 return MakeUnsigned(x) >= MakeUnsigned(y);
3764 }
Aart Bike9f37602015-10-09 11:15:55 -07003765
3766 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3767};
Dave Allison20dfc792014-06-16 20:44:29 -07003768
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003769// Instruction to check how two inputs compare to each other.
3770// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003771class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003772 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003773 // Note that `comparison_type` is the type of comparison performed
3774 // between the comparison's inputs, not the type of the instantiated
3775 // HCompare instruction (which is always Primitive::kPrimInt).
3776 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003777 HInstruction* first,
3778 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003779 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003780 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003781 : HBinaryOperation(Primitive::kPrimInt,
3782 first,
3783 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003784 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003785 dex_pc) {
3786 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003787 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3788 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003789 }
3790
Roland Levillain9867bc72015-08-05 10:21:34 +01003791 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3793
3794 template <typename T>
3795 int32_t ComputeFP(T x, T y) const {
3796 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3797 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3798 // Handle the bias.
3799 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3800 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003801
Roland Levillain9867bc72015-08-05 10:21:34 +01003802 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003803 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3804 // reach this code path when processing a freshly built HIR
3805 // graph. However HCompare integer instructions can be synthesized
3806 // by the instruction simplifier to implement IntegerCompare and
3807 // IntegerSignum intrinsics, so we have to handle this case.
3808 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003809 }
3810 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003811 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3812 }
3813 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3814 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3815 }
3816 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3817 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003818 }
3819
Vladimir Marko372f10e2016-05-17 16:30:10 +01003820 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003821 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003822 }
3823
Vladimir Markoa1de9182016-02-25 11:37:38 +00003824 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003825
Roland Levillain31dd3d62016-02-16 12:21:02 +00003826 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003827 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 bool IsGtBias() const {
3829 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003830 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003831 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003832
Roland Levillain1693a1f2016-03-15 14:57:31 +00003833 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3834 // Comparisons do not require a runtime call in any back end.
3835 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003836 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003837
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003838 DECLARE_INSTRUCTION(Compare);
3839
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003841 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3842 static constexpr size_t kFieldComparisonBiasSize =
3843 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3844 static constexpr size_t kNumberOfComparePackedBits =
3845 kFieldComparisonBias + kFieldComparisonBiasSize;
3846 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3847 using ComparisonBiasField =
3848 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3849
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 // Return an integer constant containing the result of a comparison evaluated at compile time.
3851 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3852 DCHECK(value == -1 || value == 0 || value == 1) << value;
3853 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3854 }
3855
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003856 private:
3857 DISALLOW_COPY_AND_ASSIGN(HCompare);
3858};
3859
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003860class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003861 public:
3862 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003863 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003864 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003865 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003866 bool finalizable,
3867 QuickEntrypointEnum entrypoint)
3868 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3869 type_index_(type_index),
3870 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003871 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003872 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003873 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003874 }
3875
Andreas Gampea5b09a62016-11-17 15:21:22 -08003876 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003877 const DexFile& GetDexFile() const { return dex_file_; }
3878
3879 // Calls runtime so needs an environment.
3880 bool NeedsEnvironment() const OVERRIDE { return true; }
3881
Mingyao Yang062157f2016-03-02 10:15:36 -08003882 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3883 bool CanThrow() const OVERRIDE { return true; }
3884
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003885 bool NeedsChecks() const {
3886 return entrypoint_ == kQuickAllocObjectWithChecks;
3887 }
David Brazdil6de19382016-01-08 17:37:10 +00003888
Vladimir Markoa1de9182016-02-25 11:37:38 +00003889 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003890
3891 bool CanBeNull() const OVERRIDE { return false; }
3892
3893 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3894
3895 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3896 entrypoint_ = entrypoint;
3897 }
3898
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003899 HLoadClass* GetLoadClass() const {
3900 HInstruction* input = InputAt(0);
3901 if (input->IsClinitCheck()) {
3902 input = input->InputAt(0);
3903 }
3904 DCHECK(input->IsLoadClass());
3905 return input->AsLoadClass();
3906 }
3907
David Brazdil6de19382016-01-08 17:37:10 +00003908 bool IsStringAlloc() const;
3909
3910 DECLARE_INSTRUCTION(NewInstance);
3911
3912 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003913 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003914 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3915 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3916 "Too many packed fields.");
3917
Andreas Gampea5b09a62016-11-17 15:21:22 -08003918 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003919 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003920 QuickEntrypointEnum entrypoint_;
3921
3922 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3923};
3924
Agi Csaki05f20562015-08-19 14:58:14 -07003925enum IntrinsicNeedsEnvironmentOrCache {
3926 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3927 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003928};
3929
Aart Bik5d75afe2015-12-14 11:57:01 -08003930enum IntrinsicSideEffects {
3931 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3932 kReadSideEffects, // Intrinsic may read heap memory.
3933 kWriteSideEffects, // Intrinsic may write heap memory.
3934 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3935};
3936
3937enum IntrinsicExceptions {
3938 kNoThrow, // Intrinsic does not throw any exceptions.
3939 kCanThrow // Intrinsic may throw exceptions.
3940};
3941
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003942class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003943 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003944 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003945
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003946 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003947 SetRawInputAt(index, argument);
3948 }
3949
Roland Levillain3e3d7332015-04-28 11:00:54 +01003950 // Return the number of arguments. This number can be lower than
3951 // the number of inputs returned by InputCount(), as some invoke
3952 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3953 // inputs at the end of their list of inputs.
3954 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3955
Vladimir Markoa1de9182016-02-25 11:37:38 +00003956 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003957
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003958 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3959
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003960 InvokeType GetInvokeType() const {
3961 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003963
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003964 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003965 return intrinsic_;
3966 }
3967
Aart Bik5d75afe2015-12-14 11:57:01 -08003968 void SetIntrinsic(Intrinsics intrinsic,
3969 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3970 IntrinsicSideEffects side_effects,
3971 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003972
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003973 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003974 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003975 }
3976
Aart Bikff7d89c2016-11-07 08:49:28 -08003977 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3978
Vladimir Markoa1de9182016-02-25 11:37:38 +00003979 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003980
Aart Bik71bf7b42016-11-16 10:17:46 -08003981 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003982
Vladimir Marko372f10e2016-05-17 16:30:10 +01003983 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003984 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3985 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003986
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003987 uint32_t* GetIntrinsicOptimizations() {
3988 return &intrinsic_optimizations_;
3989 }
3990
3991 const uint32_t* GetIntrinsicOptimizations() const {
3992 return &intrinsic_optimizations_;
3993 }
3994
3995 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3996
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003997 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00003998 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003999
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004000 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004001
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004002 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004003 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4004 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004005 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4006 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004007 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004008 static constexpr size_t kFieldReturnTypeSize =
4009 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4010 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4011 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4012 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004013 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004014 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4015
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004016 HInvoke(ArenaAllocator* arena,
4017 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004018 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004019 Primitive::Type return_type,
4020 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004021 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004022 ArtMethod* resolved_method,
4023 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004024 : HVariableInputSizeInstruction(
4025 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4026 dex_pc,
4027 arena,
4028 number_of_arguments + number_of_other_inputs,
4029 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004030 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004031 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004032 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004033 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004034 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004036 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004037 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004038 }
4039
Roland Levillain3e3d7332015-04-28 11:00:54 +01004040 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004041 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004042 const uint32_t dex_method_index_;
4043 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004044
4045 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4046 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004047
4048 private:
4049 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4050};
4051
Vladimir Markofcb503c2016-05-18 12:48:17 +01004052class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004053 public:
4054 HInvokeUnresolved(ArenaAllocator* arena,
4055 uint32_t number_of_arguments,
4056 Primitive::Type return_type,
4057 uint32_t dex_pc,
4058 uint32_t dex_method_index,
4059 InvokeType invoke_type)
4060 : HInvoke(arena,
4061 number_of_arguments,
4062 0u /* number_of_other_inputs */,
4063 return_type,
4064 dex_pc,
4065 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004067 invoke_type) {
4068 }
4069
4070 DECLARE_INSTRUCTION(InvokeUnresolved);
4071
4072 private:
4073 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4074};
4075
Orion Hodsonac141392017-01-13 11:53:47 +00004076class HInvokePolymorphic FINAL : public HInvoke {
4077 public:
4078 HInvokePolymorphic(ArenaAllocator* arena,
4079 uint32_t number_of_arguments,
4080 Primitive::Type return_type,
4081 uint32_t dex_pc,
4082 uint32_t dex_method_index)
4083 : HInvoke(arena,
4084 number_of_arguments,
4085 0u /* number_of_other_inputs */,
4086 return_type,
4087 dex_pc,
4088 dex_method_index,
4089 nullptr,
4090 kVirtual) {}
4091
4092 DECLARE_INSTRUCTION(InvokePolymorphic);
4093
4094 private:
4095 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4096};
4097
Vladimir Markofcb503c2016-05-18 12:48:17 +01004098class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004099 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004100 // Requirements of this method call regarding the class
4101 // initialization (clinit) check of its declaring class.
4102 enum class ClinitCheckRequirement {
4103 kNone, // Class already initialized.
4104 kExplicit, // Static call having explicit clinit check as last input.
4105 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004106 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004107 };
4108
Vladimir Marko58155012015-08-19 12:49:41 +00004109 // Determines how to load the target ArtMethod*.
4110 enum class MethodLoadKind {
4111 // Use a String init ArtMethod* loaded from Thread entrypoints.
4112 kStringInit,
4113
4114 // Use the method's own ArtMethod* loaded by the register allocator.
4115 kRecursive,
4116
4117 // Use ArtMethod* at a known address, embed the direct address in the code.
4118 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4119 kDirectAddress,
4120
Vladimir Markoa1de9182016-02-25 11:37:38 +00004121 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004122 // Used when we need to use the dex cache, for example for invoke-static that
4123 // may cause class initialization (the entry may point to a resolution method),
4124 // and we know that we can access the dex cache arrays using a PC-relative load.
4125 kDexCachePcRelative,
4126
4127 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4128 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4129 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4130 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4131 kDexCacheViaMethod,
4132 };
4133
4134 // Determines the location of the code pointer.
4135 enum class CodePtrLocation {
4136 // Recursive call, use local PC-relative call instruction.
4137 kCallSelf,
4138
Vladimir Marko58155012015-08-19 12:49:41 +00004139 // Use code pointer from the ArtMethod*.
4140 // Used when we don't know the target code. This is also the last-resort-kind used when
4141 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4142 kCallArtMethod,
4143 };
4144
4145 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004146 MethodLoadKind method_load_kind;
4147 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004148 // The method load data holds
4149 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4150 // Note that there are multiple string init methods, each having its own offset.
4151 // - the method address for kDirectAddress
4152 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004153 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004154 };
4155
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004156 HInvokeStaticOrDirect(ArenaAllocator* arena,
4157 uint32_t number_of_arguments,
4158 Primitive::Type return_type,
4159 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004160 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004161 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004162 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004163 InvokeType invoke_type,
4164 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004165 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004166 : HInvoke(arena,
4167 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004168 // There is potentially one extra argument for the HCurrentMethod node, and
4169 // potentially one other if the clinit check is explicit, and potentially
4170 // one other if the method is a string factory.
4171 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004172 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004173 return_type,
4174 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004175 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004176 resolved_method,
4177 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004178 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004179 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004180 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4181 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004182
Vladimir Markodc151b22015-10-15 18:02:30 +01004183 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004184 bool had_current_method_input = HasCurrentMethodInput();
4185 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4186
4187 // Using the current method is the default and once we find a better
4188 // method load kind, we should not go back to using the current method.
4189 DCHECK(had_current_method_input || !needs_current_method_input);
4190
4191 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004192 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4193 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004194 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004195 dispatch_info_ = dispatch_info;
4196 }
4197
Vladimir Markoc53c0792015-11-19 15:48:33 +00004198 void AddSpecialInput(HInstruction* input) {
4199 // We allow only one special input.
4200 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4201 DCHECK(InputCount() == GetSpecialInputIndex() ||
4202 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4203 InsertInputAt(GetSpecialInputIndex(), input);
4204 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004205
Vladimir Marko372f10e2016-05-17 16:30:10 +01004206 using HInstruction::GetInputRecords; // Keep the const version visible.
4207 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4208 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4209 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4210 DCHECK(!input_records.empty());
4211 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4212 HInstruction* last_input = input_records.back().GetInstruction();
4213 // Note: `last_input` may be null during arguments setup.
4214 if (last_input != nullptr) {
4215 // `last_input` is the last input of a static invoke marked as having
4216 // an explicit clinit check. It must either be:
4217 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4218 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4219 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4220 }
4221 }
4222 return input_records;
4223 }
4224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004225 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004226 // We access the method via the dex cache so we can't do an implicit null check.
4227 // TODO: for intrinsics we can generate implicit null checks.
4228 return false;
4229 }
4230
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004231 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004232 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004233 }
4234
Vladimir Markoc53c0792015-11-19 15:48:33 +00004235 // Get the index of the special input, if any.
4236 //
David Brazdil6de19382016-01-08 17:37:10 +00004237 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4238 // method pointer; otherwise there may be one platform-specific special input,
4239 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004240 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004241 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004242
Vladimir Marko58155012015-08-19 12:49:41 +00004243 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4244 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4245 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004246 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004247 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004248 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004249 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004250 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4251 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004252 bool HasCurrentMethodInput() const {
4253 // This function can be called only after the invoke has been fully initialized by the builder.
4254 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004255 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004256 return true;
4257 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004258 DCHECK(InputCount() == GetSpecialInputIndex() ||
4259 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004260 return false;
4261 }
4262 }
Vladimir Marko58155012015-08-19 12:49:41 +00004263
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004264 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004265 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004266 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004267 }
4268
4269 uint64_t GetMethodAddress() const {
4270 DCHECK(HasMethodAddress());
4271 return dispatch_info_.method_load_data;
4272 }
4273
4274 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004275 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004276 return dispatch_info_.method_load_data;
4277 }
4278
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004279 const DexFile& GetDexFileForPcRelativeDexCache() const;
4280
Vladimir Markoa1de9182016-02-25 11:37:38 +00004281 ClinitCheckRequirement GetClinitCheckRequirement() const {
4282 return GetPackedField<ClinitCheckRequirementField>();
4283 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004284
Roland Levillain4c0eb422015-04-24 16:43:49 +01004285 // Is this instruction a call to a static method?
4286 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004287 return GetInvokeType() == kStatic;
4288 }
4289
4290 MethodReference GetTargetMethod() const {
4291 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004292 }
4293
Vladimir Markofbb184a2015-11-13 14:47:00 +00004294 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4295 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4296 // instruction; only relevant for static calls with explicit clinit check.
4297 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004298 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004299 size_t last_input_index = inputs_.size() - 1u;
4300 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004301 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004302 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004303 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004304 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004305 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004306 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004307 }
4308
4309 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004310 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004311 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004312 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004313 }
4314
4315 // Is this a call to a static method whose declaring class has an
4316 // implicit intialization check requirement?
4317 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004318 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004319 }
4320
Vladimir Markob554b5a2015-11-06 12:57:55 +00004321 // Does this method load kind need the current method as an input?
4322 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4323 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4324 }
4325
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004326 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004327
4328 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004329 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004330 static constexpr size_t kFieldClinitCheckRequirementSize =
4331 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4332 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4333 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4334 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4335 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4337 kFieldClinitCheckRequirement,
4338 kFieldClinitCheckRequirementSize>;
4339
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004340 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004341 MethodReference target_method_;
4342 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004343
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004344 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004345};
Vladimir Markof64242a2015-12-01 14:58:23 +00004346std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004347std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004348
Vladimir Markofcb503c2016-05-18 12:48:17 +01004349class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004350 public:
4351 HInvokeVirtual(ArenaAllocator* arena,
4352 uint32_t number_of_arguments,
4353 Primitive::Type return_type,
4354 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004355 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004356 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004357 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358 : HInvoke(arena,
4359 number_of_arguments,
4360 0u,
4361 return_type,
4362 dex_pc,
4363 dex_method_index,
4364 resolved_method,
4365 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004366 vtable_index_(vtable_index) {}
4367
Aart Bik71bf7b42016-11-16 10:17:46 -08004368 bool CanBeNull() const OVERRIDE {
4369 switch (GetIntrinsic()) {
4370 case Intrinsics::kThreadCurrentThread:
4371 case Intrinsics::kStringBufferAppend:
4372 case Intrinsics::kStringBufferToString:
4373 case Intrinsics::kStringBuilderAppend:
4374 case Intrinsics::kStringBuilderToString:
4375 return false;
4376 default:
4377 return HInvoke::CanBeNull();
4378 }
4379 }
4380
Calin Juravle641547a2015-04-21 22:08:51 +01004381 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004382 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004383 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004384 }
4385
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004386 uint32_t GetVTableIndex() const { return vtable_index_; }
4387
4388 DECLARE_INSTRUCTION(InvokeVirtual);
4389
4390 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004391 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004392 const uint32_t vtable_index_;
4393
4394 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4395};
4396
Vladimir Markofcb503c2016-05-18 12:48:17 +01004397class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004398 public:
4399 HInvokeInterface(ArenaAllocator* arena,
4400 uint32_t number_of_arguments,
4401 Primitive::Type return_type,
4402 uint32_t dex_pc,
4403 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004404 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004405 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004406 : HInvoke(arena,
4407 number_of_arguments,
4408 0u,
4409 return_type,
4410 dex_pc,
4411 dex_method_index,
4412 resolved_method,
4413 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004414 imt_index_(imt_index) {}
4415
Calin Juravle641547a2015-04-21 22:08:51 +01004416 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004417 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004418 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004419 }
4420
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004421 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4422 // The assembly stub currently needs it.
4423 return true;
4424 }
4425
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004426 uint32_t GetImtIndex() const { return imt_index_; }
4427 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4428
4429 DECLARE_INSTRUCTION(InvokeInterface);
4430
4431 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004432 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004433 const uint32_t imt_index_;
4434
4435 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4436};
4437
Vladimir Markofcb503c2016-05-18 12:48:17 +01004438class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004439 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004440 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004441 : HUnaryOperation(result_type, input, dex_pc) {
4442 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4443 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004444
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004445 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004446
4447 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004448 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004449 }
4450 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004451 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004452 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004453 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4454 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4455 }
4456 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4457 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4458 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004459
Roland Levillain88cb1752014-10-20 16:36:47 +01004460 DECLARE_INSTRUCTION(Neg);
4461
4462 private:
4463 DISALLOW_COPY_AND_ASSIGN(HNeg);
4464};
4465
Vladimir Markofcb503c2016-05-18 12:48:17 +01004466class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004467 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004468 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4469 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4470 SetRawInputAt(0, cls);
4471 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004472 }
4473
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004474 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004475 bool NeedsEnvironment() const OVERRIDE { return true; }
4476
Mingyao Yang0c365e62015-03-31 15:09:29 -07004477 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4478 bool CanThrow() const OVERRIDE { return true; }
4479
Calin Juravle10e244f2015-01-26 18:54:32 +00004480 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004481
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004482 HLoadClass* GetLoadClass() const {
4483 DCHECK(InputAt(0)->IsLoadClass());
4484 return InputAt(0)->AsLoadClass();
4485 }
4486
4487 HInstruction* GetLength() const {
4488 return InputAt(1);
4489 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004490
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004491 DECLARE_INSTRUCTION(NewArray);
4492
4493 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004494 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4495};
4496
Vladimir Markofcb503c2016-05-18 12:48:17 +01004497class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 HAdd(Primitive::Type result_type,
4500 HInstruction* left,
4501 HInstruction* right,
4502 uint32_t dex_pc = kNoDexPc)
4503 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004504
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004505 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004506
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004507 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004508
4509 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004510 return GetBlock()->GetGraph()->GetIntConstant(
4511 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004512 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004514 return GetBlock()->GetGraph()->GetLongConstant(
4515 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004516 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004517 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4518 return GetBlock()->GetGraph()->GetFloatConstant(
4519 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4520 }
4521 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4522 return GetBlock()->GetGraph()->GetDoubleConstant(
4523 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4524 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004525
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004526 DECLARE_INSTRUCTION(Add);
4527
4528 private:
4529 DISALLOW_COPY_AND_ASSIGN(HAdd);
4530};
4531
Vladimir Markofcb503c2016-05-18 12:48:17 +01004532class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004533 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004534 HSub(Primitive::Type result_type,
4535 HInstruction* left,
4536 HInstruction* right,
4537 uint32_t dex_pc = kNoDexPc)
4538 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004539
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004540 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004541
4542 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 return GetBlock()->GetGraph()->GetIntConstant(
4544 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004545 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004546 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004547 return GetBlock()->GetGraph()->GetLongConstant(
4548 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004549 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004550 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4551 return GetBlock()->GetGraph()->GetFloatConstant(
4552 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4553 }
4554 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4555 return GetBlock()->GetGraph()->GetDoubleConstant(
4556 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4557 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004559 DECLARE_INSTRUCTION(Sub);
4560
4561 private:
4562 DISALLOW_COPY_AND_ASSIGN(HSub);
4563};
4564
Vladimir Markofcb503c2016-05-18 12:48:17 +01004565class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004566 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004567 HMul(Primitive::Type result_type,
4568 HInstruction* left,
4569 HInstruction* right,
4570 uint32_t dex_pc = kNoDexPc)
4571 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004572
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004573 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004574
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004575 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004576
4577 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 return GetBlock()->GetGraph()->GetIntConstant(
4579 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 }
4581 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004582 return GetBlock()->GetGraph()->GetLongConstant(
4583 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004584 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004585 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4586 return GetBlock()->GetGraph()->GetFloatConstant(
4587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4588 }
4589 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4590 return GetBlock()->GetGraph()->GetDoubleConstant(
4591 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4592 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004593
4594 DECLARE_INSTRUCTION(Mul);
4595
4596 private:
4597 DISALLOW_COPY_AND_ASSIGN(HMul);
4598};
4599
Vladimir Markofcb503c2016-05-18 12:48:17 +01004600class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004601 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004602 HDiv(Primitive::Type result_type,
4603 HInstruction* left,
4604 HInstruction* right,
4605 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004606 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004607
Roland Levillain9867bc72015-08-05 10:21:34 +01004608 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004609 T ComputeIntegral(T x, T y) const {
4610 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 // Our graph structure ensures we never have 0 for `y` during
4612 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004613 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004614 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004615 return (y == -1) ? -x : x / y;
4616 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004617
Roland Levillain31dd3d62016-02-16 12:21:02 +00004618 template <typename T>
4619 T ComputeFP(T x, T y) const {
4620 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4621 return x / y;
4622 }
4623
Roland Levillain9867bc72015-08-05 10:21:34 +01004624 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004625 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004626 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
4628 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004629 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004630 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4631 }
4632 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4633 return GetBlock()->GetGraph()->GetFloatConstant(
4634 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4635 }
4636 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4637 return GetBlock()->GetGraph()->GetDoubleConstant(
4638 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004639 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004640
4641 DECLARE_INSTRUCTION(Div);
4642
4643 private:
4644 DISALLOW_COPY_AND_ASSIGN(HDiv);
4645};
4646
Vladimir Markofcb503c2016-05-18 12:48:17 +01004647class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004649 HRem(Primitive::Type result_type,
4650 HInstruction* left,
4651 HInstruction* right,
4652 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004653 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004654
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004656 T ComputeIntegral(T x, T y) const {
4657 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 // Our graph structure ensures we never have 0 for `y` during
4659 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004660 DCHECK_NE(y, 0);
4661 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4662 return (y == -1) ? 0 : x % y;
4663 }
4664
Roland Levillain31dd3d62016-02-16 12:21:02 +00004665 template <typename T>
4666 T ComputeFP(T x, T y) const {
4667 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4668 return std::fmod(x, y);
4669 }
4670
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004672 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004673 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004674 }
4675 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004676 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004677 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004678 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004679 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4680 return GetBlock()->GetGraph()->GetFloatConstant(
4681 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4682 }
4683 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4684 return GetBlock()->GetGraph()->GetDoubleConstant(
4685 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4686 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004687
4688 DECLARE_INSTRUCTION(Rem);
4689
4690 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004691 DISALLOW_COPY_AND_ASSIGN(HRem);
4692};
4693
Vladimir Markofcb503c2016-05-18 12:48:17 +01004694class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004695 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004696 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4697 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004698 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004699 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004700 SetRawInputAt(0, value);
4701 }
4702
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004703 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4704
Calin Juravled0d48522014-11-04 16:40:20 +00004705 bool CanBeMoved() const OVERRIDE { return true; }
4706
Vladimir Marko372f10e2016-05-17 16:30:10 +01004707 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004708 return true;
4709 }
4710
4711 bool NeedsEnvironment() const OVERRIDE { return true; }
4712 bool CanThrow() const OVERRIDE { return true; }
4713
Calin Juravled0d48522014-11-04 16:40:20 +00004714 DECLARE_INSTRUCTION(DivZeroCheck);
4715
4716 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004717 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4718};
4719
Vladimir Markofcb503c2016-05-18 12:48:17 +01004720class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004721 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004722 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004723 HInstruction* value,
4724 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004725 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004726 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4727 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4728 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004729 }
4730
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004732 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004733 return value << (distance & max_shift_distance);
4734 }
4735
4736 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004737 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004739 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004741 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004742 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004743 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4745 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4746 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4747 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004748 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4750 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004751 LOG(FATAL) << DebugName() << " is not defined for float values";
4752 UNREACHABLE();
4753 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4755 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004756 LOG(FATAL) << DebugName() << " is not defined for double values";
4757 UNREACHABLE();
4758 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004759
4760 DECLARE_INSTRUCTION(Shl);
4761
4762 private:
4763 DISALLOW_COPY_AND_ASSIGN(HShl);
4764};
4765
Vladimir Markofcb503c2016-05-18 12:48:17 +01004766class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004767 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004768 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HInstruction* value,
4770 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004771 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4773 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4774 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 }
4776
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004778 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 return value >> (distance & max_shift_distance);
4780 }
4781
4782 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004783 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004785 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004789 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4791 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4792 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4793 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004794 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004795 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4796 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004797 LOG(FATAL) << DebugName() << " is not defined for float values";
4798 UNREACHABLE();
4799 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4801 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004802 LOG(FATAL) << DebugName() << " is not defined for double values";
4803 UNREACHABLE();
4804 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004805
4806 DECLARE_INSTRUCTION(Shr);
4807
4808 private:
4809 DISALLOW_COPY_AND_ASSIGN(HShr);
4810};
4811
Vladimir Markofcb503c2016-05-18 12:48:17 +01004812class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 HInstruction* value,
4816 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4819 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4820 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004821 }
4822
Roland Levillain5b5b9312016-03-22 14:57:31 +00004823 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004824 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 typedef typename std::make_unsigned<T>::type V;
4826 V ux = static_cast<V>(value);
4827 return static_cast<T>(ux >> (distance & max_shift_distance));
4828 }
4829
4830 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004832 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004835 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004836 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004838 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4839 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4841 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004842 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004843 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4844 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004845 LOG(FATAL) << DebugName() << " is not defined for float values";
4846 UNREACHABLE();
4847 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4849 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004850 LOG(FATAL) << DebugName() << " is not defined for double values";
4851 UNREACHABLE();
4852 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004853
4854 DECLARE_INSTRUCTION(UShr);
4855
4856 private:
4857 DISALLOW_COPY_AND_ASSIGN(HUShr);
4858};
4859
Vladimir Markofcb503c2016-05-18 12:48:17 +01004860class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 HAnd(Primitive::Type result_type,
4863 HInstruction* left,
4864 HInstruction* right,
4865 uint32_t dex_pc = kNoDexPc)
4866 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004867
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004868 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004869
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004870 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004871
4872 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004873 return GetBlock()->GetGraph()->GetIntConstant(
4874 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004875 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004876 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004877 return GetBlock()->GetGraph()->GetLongConstant(
4878 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4881 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4882 LOG(FATAL) << DebugName() << " is not defined for float values";
4883 UNREACHABLE();
4884 }
4885 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4886 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4887 LOG(FATAL) << DebugName() << " is not defined for double values";
4888 UNREACHABLE();
4889 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004890
4891 DECLARE_INSTRUCTION(And);
4892
4893 private:
4894 DISALLOW_COPY_AND_ASSIGN(HAnd);
4895};
4896
Vladimir Markofcb503c2016-05-18 12:48:17 +01004897class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004898 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004899 HOr(Primitive::Type result_type,
4900 HInstruction* left,
4901 HInstruction* right,
4902 uint32_t dex_pc = kNoDexPc)
4903 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004904
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004905 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004906
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004907 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004908
4909 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 return GetBlock()->GetGraph()->GetIntConstant(
4911 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004912 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004913 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004914 return GetBlock()->GetGraph()->GetLongConstant(
4915 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004916 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004917 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4918 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4919 LOG(FATAL) << DebugName() << " is not defined for float values";
4920 UNREACHABLE();
4921 }
4922 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4923 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4924 LOG(FATAL) << DebugName() << " is not defined for double values";
4925 UNREACHABLE();
4926 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004927
4928 DECLARE_INSTRUCTION(Or);
4929
4930 private:
4931 DISALLOW_COPY_AND_ASSIGN(HOr);
4932};
4933
Vladimir Markofcb503c2016-05-18 12:48:17 +01004934class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004935 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 HXor(Primitive::Type result_type,
4937 HInstruction* left,
4938 HInstruction* right,
4939 uint32_t dex_pc = kNoDexPc)
4940 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004941
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004942 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004943
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004944 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004945
4946 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004947 return GetBlock()->GetGraph()->GetIntConstant(
4948 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004949 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004950 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004951 return GetBlock()->GetGraph()->GetLongConstant(
4952 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004953 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004954 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4955 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4956 LOG(FATAL) << DebugName() << " is not defined for float values";
4957 UNREACHABLE();
4958 }
4959 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4960 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4961 LOG(FATAL) << DebugName() << " is not defined for double values";
4962 UNREACHABLE();
4963 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004964
4965 DECLARE_INSTRUCTION(Xor);
4966
4967 private:
4968 DISALLOW_COPY_AND_ASSIGN(HXor);
4969};
4970
Vladimir Markofcb503c2016-05-18 12:48:17 +01004971class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004972 public:
4973 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004974 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004975 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4976 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004977 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004978
Roland Levillain5b5b9312016-03-22 14:57:31 +00004979 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004980 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004981 typedef typename std::make_unsigned<T>::type V;
4982 V ux = static_cast<V>(value);
4983 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004984 return static_cast<T>(ux);
4985 } else {
4986 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004987 return static_cast<T>(ux >> (distance & max_shift_value)) |
4988 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004989 }
4990 }
4991
Roland Levillain5b5b9312016-03-22 14:57:31 +00004992 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004993 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004994 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004995 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004996 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004997 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004998 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004999 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005000 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5001 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5002 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5003 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005004 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005005 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5006 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005007 LOG(FATAL) << DebugName() << " is not defined for float values";
5008 UNREACHABLE();
5009 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005010 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5011 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005012 LOG(FATAL) << DebugName() << " is not defined for double values";
5013 UNREACHABLE();
5014 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015
5016 DECLARE_INSTRUCTION(Ror);
5017
5018 private:
5019 DISALLOW_COPY_AND_ASSIGN(HRor);
5020};
5021
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005022// The value of a parameter in this method. Its location depends on
5023// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005024class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005025 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005026 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005027 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005028 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005029 Primitive::Type parameter_type,
5030 bool is_this = false)
5031 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005032 dex_file_(dex_file),
5033 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005034 index_(index) {
5035 SetPackedFlag<kFlagIsThis>(is_this);
5036 SetPackedFlag<kFlagCanBeNull>(!is_this);
5037 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005038
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005039 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005040 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005041 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005042 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005043
Vladimir Markoa1de9182016-02-25 11:37:38 +00005044 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5045 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005046
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005047 DECLARE_INSTRUCTION(ParameterValue);
5048
5049 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005050 // Whether or not the parameter value corresponds to 'this' argument.
5051 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5052 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5053 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5054 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5055 "Too many packed fields.");
5056
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005057 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005058 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005059 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005060 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005061 const uint8_t index_;
5062
5063 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5064};
5065
Vladimir Markofcb503c2016-05-18 12:48:17 +01005066class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005067 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005068 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5069 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005070
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005071 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005072 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005073 return true;
5074 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005075
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005076 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005077
5078 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005079 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005080 }
5081 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005082 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005083 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005084 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5085 LOG(FATAL) << DebugName() << " is not defined for float values";
5086 UNREACHABLE();
5087 }
5088 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5089 LOG(FATAL) << DebugName() << " is not defined for double values";
5090 UNREACHABLE();
5091 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005092
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005093 DECLARE_INSTRUCTION(Not);
5094
5095 private:
5096 DISALLOW_COPY_AND_ASSIGN(HNot);
5097};
5098
Vladimir Markofcb503c2016-05-18 12:48:17 +01005099class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005101 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5102 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005103
5104 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005105 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005106 return true;
5107 }
5108
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005109 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005110 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005111 return !x;
5112 }
5113
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005115 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005116 }
5117 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5118 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005119 UNREACHABLE();
5120 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005121 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5122 LOG(FATAL) << DebugName() << " is not defined for float values";
5123 UNREACHABLE();
5124 }
5125 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5126 LOG(FATAL) << DebugName() << " is not defined for double values";
5127 UNREACHABLE();
5128 }
David Brazdil66d126e2015-04-03 16:02:44 +01005129
5130 DECLARE_INSTRUCTION(BooleanNot);
5131
5132 private:
5133 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5134};
5135
Vladimir Markofcb503c2016-05-18 12:48:17 +01005136class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005137 public:
5138 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005139 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005140 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005141 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005142 // Invariant: We should never generate a conversion to a Boolean value.
5143 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005144 }
5145
5146 HInstruction* GetInput() const { return InputAt(0); }
5147 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5148 Primitive::Type GetResultType() const { return GetType(); }
5149
5150 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005151 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5152 return true;
5153 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005154
Mark Mendelle82549b2015-05-06 10:55:34 -04005155 // Try to statically evaluate the conversion and return a HConstant
5156 // containing the result. If the input cannot be converted, return nullptr.
5157 HConstant* TryStaticEvaluation() const;
5158
Roland Levillaindff1f282014-11-05 14:15:05 +00005159 DECLARE_INSTRUCTION(TypeConversion);
5160
5161 private:
5162 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5163};
5164
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005165static constexpr uint32_t kNoRegNumber = -1;
5166
Vladimir Markofcb503c2016-05-18 12:48:17 +01005167class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005168 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005169 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5170 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005172 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005173 SetRawInputAt(0, value);
5174 }
5175
Calin Juravle10e244f2015-01-26 18:54:32 +00005176 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005177 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005178 return true;
5179 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005180
Calin Juravle10e244f2015-01-26 18:54:32 +00005181 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005182
Calin Juravle10e244f2015-01-26 18:54:32 +00005183 bool CanThrow() const OVERRIDE { return true; }
5184
5185 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005186
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187
5188 DECLARE_INSTRUCTION(NullCheck);
5189
5190 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005191 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5192};
5193
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005194// Embeds an ArtField and all the information required by the compiler. We cache
5195// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005196class FieldInfo : public ValueObject {
5197 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005198 FieldInfo(ArtField* field,
5199 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005200 Primitive::Type field_type,
5201 bool is_volatile,
5202 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005203 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005204 const DexFile& dex_file)
5205 : field_(field),
5206 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005207 field_type_(field_type),
5208 is_volatile_(is_volatile),
5209 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005210 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005211 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005213 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005214 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005215 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005216 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005217 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005218 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005219 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005220
5221 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005222 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005223 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005224 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005225 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005226 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005227 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005228 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005229};
5230
Vladimir Markofcb503c2016-05-18 12:48:17 +01005231class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005232 public:
5233 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005234 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005235 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005236 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005237 bool is_volatile,
5238 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005239 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005240 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005241 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005242 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005243 field_info_(field,
5244 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005245 field_type,
5246 is_volatile,
5247 field_idx,
5248 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005249 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005250 SetRawInputAt(0, value);
5251 }
5252
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005253 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005254
Vladimir Marko372f10e2016-05-17 16:30:10 +01005255 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5256 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005257 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005258 }
5259
Calin Juravle641547a2015-04-21 22:08:51 +01005260 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005261 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005262 }
5263
5264 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005265 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5266 }
5267
Calin Juravle52c48962014-12-16 17:02:57 +00005268 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005269 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005270 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005271 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005272
5273 DECLARE_INSTRUCTION(InstanceFieldGet);
5274
5275 private:
5276 const FieldInfo field_info_;
5277
5278 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5279};
5280
Vladimir Markofcb503c2016-05-18 12:48:17 +01005281class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005282 public:
5283 HInstanceFieldSet(HInstruction* object,
5284 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005285 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005286 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005287 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005288 bool is_volatile,
5289 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005290 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005291 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005292 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005293 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005294 field_info_(field,
5295 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005296 field_type,
5297 is_volatile,
5298 field_idx,
5299 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005300 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005301 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005302 SetRawInputAt(0, object);
5303 SetRawInputAt(1, value);
5304 }
5305
Calin Juravle641547a2015-04-21 22:08:51 +01005306 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005307 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005308 }
5309
Calin Juravle52c48962014-12-16 17:02:57 +00005310 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005311 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005312 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005313 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005314 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005315 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5316 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005317
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005318 DECLARE_INSTRUCTION(InstanceFieldSet);
5319
5320 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005321 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5322 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5323 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5324 "Too many packed fields.");
5325
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005326 const FieldInfo field_info_;
5327
5328 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5329};
5330
Vladimir Markofcb503c2016-05-18 12:48:17 +01005331class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005333 HArrayGet(HInstruction* array,
5334 HInstruction* index,
5335 Primitive::Type type,
5336 uint32_t dex_pc,
5337 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005338 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005339 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 SetRawInputAt(0, array);
5341 SetRawInputAt(1, index);
5342 }
5343
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005344 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005345 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005346 return true;
5347 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005348 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005349 // TODO: We can be smarter here.
5350 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5351 // which generates the implicit null check. There are cases when these can be removed
5352 // to produce better code. If we ever add optimizations to do so we should allow an
5353 // implicit check here (as long as the address falls in the first page).
5354 return false;
5355 }
5356
David Brazdil4833f5a2015-12-16 10:37:39 +00005357 bool IsEquivalentOf(HArrayGet* other) const {
5358 bool result = (GetDexPc() == other->GetDexPc());
5359 if (kIsDebugBuild && result) {
5360 DCHECK_EQ(GetBlock(), other->GetBlock());
5361 DCHECK_EQ(GetArray(), other->GetArray());
5362 DCHECK_EQ(GetIndex(), other->GetIndex());
5363 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005364 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005365 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005366 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5367 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005368 }
5369 }
5370 return result;
5371 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005372
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005373 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5374
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005375 HInstruction* GetArray() const { return InputAt(0); }
5376 HInstruction* GetIndex() const { return InputAt(1); }
5377
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 DECLARE_INSTRUCTION(ArrayGet);
5379
5380 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005381 // We treat a String as an array, creating the HArrayGet from String.charAt()
5382 // intrinsic in the instruction simplifier. We can always determine whether
5383 // a particular HArrayGet is actually a String.charAt() by looking at the type
5384 // of the input but that requires holding the mutator lock, so we prefer to use
5385 // a flag, so that code generators don't need to do the locking.
5386 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5387 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5388 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5389 "Too many packed fields.");
5390
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5392};
5393
Vladimir Markofcb503c2016-05-18 12:48:17 +01005394class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 public:
5396 HArraySet(HInstruction* array,
5397 HInstruction* index,
5398 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005399 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005400 uint32_t dex_pc)
5401 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005402 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5403 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5404 SetPackedFlag<kFlagValueCanBeNull>(true);
5405 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 SetRawInputAt(0, array);
5407 SetRawInputAt(1, index);
5408 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005409 // Make a best guess now, may be refined during SSA building.
5410 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 }
5412
Calin Juravle77520bc2015-01-12 18:45:46 +00005413 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005414 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005415 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 }
5417
Mingyao Yang81014cb2015-06-02 03:16:27 -07005418 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005419 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005420
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005421 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005422 // TODO: Same as for ArrayGet.
5423 return false;
5424 }
5425
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005426 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005427 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005428 }
5429
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005430 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005431 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005432 }
5433
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005435 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 }
5437
Vladimir Markoa1de9182016-02-25 11:37:38 +00005438 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5439 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5440 bool StaticTypeOfArrayIsObjectArray() const {
5441 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5442 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005443
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005444 HInstruction* GetArray() const { return InputAt(0); }
5445 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005446 HInstruction* GetValue() const { return InputAt(2); }
5447
5448 Primitive::Type GetComponentType() const {
5449 // The Dex format does not type floating point index operations. Since the
5450 // `expected_component_type_` is set during building and can therefore not
5451 // be correct, we also check what is the value type. If it is a floating
5452 // point type, we must use that type.
5453 Primitive::Type value_type = GetValue()->GetType();
5454 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5455 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005457 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005458
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005459 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005460 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005461 }
5462
Aart Bik18b36ab2016-04-13 16:41:35 -07005463 void ComputeSideEffects() {
5464 Primitive::Type type = GetComponentType();
5465 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5466 SideEffectsForArchRuntimeCalls(type)));
5467 }
5468
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005469 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5470 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5471 }
5472
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005473 DECLARE_INSTRUCTION(ArraySet);
5474
5475 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005476 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5477 static constexpr size_t kFieldExpectedComponentTypeSize =
5478 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5479 static constexpr size_t kFlagNeedsTypeCheck =
5480 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5481 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005482 // Cached information for the reference_type_info_ so that codegen
5483 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005484 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5485 static constexpr size_t kNumberOfArraySetPackedBits =
5486 kFlagStaticTypeOfArrayIsObjectArray + 1;
5487 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5488 using ExpectedComponentTypeField =
5489 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005490
5491 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5492};
5493
Vladimir Markofcb503c2016-05-18 12:48:17 +01005494class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005496 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005497 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005498 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005499 // Note that arrays do not change length, so the instruction does not
5500 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005501 SetRawInputAt(0, array);
5502 }
5503
Calin Juravle77520bc2015-01-12 18:45:46 +00005504 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005505 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005506 return true;
5507 }
Calin Juravle641547a2015-04-21 22:08:51 +01005508 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5509 return obj == InputAt(0);
5510 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005511
Vladimir Markodce016e2016-04-28 13:10:02 +01005512 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5513
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514 DECLARE_INSTRUCTION(ArrayLength);
5515
5516 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005517 // We treat a String as an array, creating the HArrayLength from String.length()
5518 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5519 // determine whether a particular HArrayLength is actually a String.length() by
5520 // looking at the type of the input but that requires holding the mutator lock, so
5521 // we prefer to use a flag, so that code generators don't need to do the locking.
5522 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5523 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5524 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5525 "Too many packed fields.");
5526
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5528};
5529
Vladimir Markofcb503c2016-05-18 12:48:17 +01005530class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005532 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5533 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005534 HBoundsCheck(HInstruction* index,
5535 HInstruction* length,
5536 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005537 bool string_char_at = false)
5538 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005539 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005540 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005541 SetRawInputAt(0, index);
5542 SetRawInputAt(1, length);
5543 }
5544
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005545 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005546 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005547 return true;
5548 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005550 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005552 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005553
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005554 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005555
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005556 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557
5558 DECLARE_INSTRUCTION(BoundsCheck);
5559
5560 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005561 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005562
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005563 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5564};
5565
Vladimir Markofcb503c2016-05-18 12:48:17 +01005566class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005567 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005568 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005569 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005570
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005571 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005572 return true;
5573 }
5574
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005575 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5576 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005577
5578 DECLARE_INSTRUCTION(SuspendCheck);
5579
5580 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005581 // Only used for code generation, in order to share the same slow path between back edges
5582 // of a same loop.
5583 SlowPathCode* slow_path_;
5584
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005585 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5586};
5587
David Srbecky0cf44932015-12-09 14:09:59 +00005588// Pseudo-instruction which provides the native debugger with mapping information.
5589// It ensures that we can generate line number and local variables at this point.
5590class HNativeDebugInfo : public HTemplateInstruction<0> {
5591 public:
5592 explicit HNativeDebugInfo(uint32_t dex_pc)
5593 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5594
5595 bool NeedsEnvironment() const OVERRIDE {
5596 return true;
5597 }
5598
5599 DECLARE_INSTRUCTION(NativeDebugInfo);
5600
5601 private:
5602 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5603};
5604
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005605/**
5606 * Instruction to load a Class object.
5607 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005608class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 // Determines how to load the Class.
5611 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005612 // We cannot load this class. See HSharpening::SharpenLoadClass.
5613 kInvalid = -1,
5614
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005615 // Use the Class* from the method's own ArtMethod*.
5616 kReferrersClass,
5617
5618 // Use boot image Class* address that will be known at link time.
5619 // Used for boot image classes referenced by boot image code in non-PIC mode.
5620 kBootImageLinkTimeAddress,
5621
5622 // Use PC-relative boot image Class* address that will be known at link time.
5623 // Used for boot image classes referenced by boot image code in PIC mode.
5624 kBootImageLinkTimePcRelative,
5625
5626 // Use a known boot image Class* address, embedded in the code by the codegen.
5627 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005628 kBootImageAddress,
5629
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005630 // Load from an entry in the .bss section using a PC-relative load.
5631 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5632 kBssEntry,
5633
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005634 // Load from the root table associated with the JIT compiled method.
5635 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005636
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637 // Load from resolved types array accessed through the class loaded from
5638 // the compiled method's own ArtMethod*. This is the default access type when
5639 // all other types are unavailable.
5640 kDexCacheViaMethod,
5641
5642 kLast = kDexCacheViaMethod
5643 };
5644
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005645 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005646 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005647 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005648 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005649 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005650 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005651 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005652 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5653 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005654 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005655 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005656 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005657 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005658 // Referrers class should not need access check. We never inline unverified
5659 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005660 DCHECK(!is_referrers_class || !needs_access_check);
5661
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 SetPackedField<LoadKindField>(
5663 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005664 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005665 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005666 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 }
5668
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005669 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005671 LoadKind GetLoadKind() const {
5672 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005673 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005674
5675 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005676
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678
Andreas Gampea5b09a62016-11-17 15:21:22 -08005679 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005681 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005682
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005683 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005684 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005685 }
5686
Calin Juravle0ba218d2015-05-19 18:46:01 +01005687 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005688 // The entrypoint the code generator is going to call does not do
5689 // clinit of the class.
5690 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005691 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005692 }
5693
5694 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005695 return NeedsAccessCheck() ||
5696 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005697 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5698 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005699 }
5700
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005701 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005702 return NeedsAccessCheck() ||
5703 MustGenerateClinitCheck() ||
5704 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5705 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5706 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005707 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5708 GetLoadKind() == LoadKind::kBssEntry) &&
5709 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005710 }
5711
Calin Juravleacf735c2015-02-12 15:25:22 +00005712 ReferenceTypeInfo GetLoadedClassRTI() {
5713 return loaded_class_rti_;
5714 }
5715
5716 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5717 // Make sure we only set exact types (the loaded class should never be merged).
5718 DCHECK(rti.IsExact());
5719 loaded_class_rti_ = rti;
5720 }
5721
Andreas Gampea5b09a62016-11-17 15:21:22 -08005722 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723 const DexFile& GetDexFile() const { return dex_file_; }
5724
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005725 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005726 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005727 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005728
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005729 static SideEffects SideEffectsForArchRuntimeCalls() {
5730 return SideEffects::CanTriggerGC();
5731 }
5732
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005733 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005734 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005735 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005736 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005737
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005738 void MarkInBootImage() {
5739 SetPackedFlag<kFlagIsInBootImage>(true);
5740 }
5741
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005742 void AddSpecialInput(HInstruction* special_input);
5743
5744 using HInstruction::GetInputRecords; // Keep the const version visible.
5745 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5746 return ArrayRef<HUserRecord<HInstruction*>>(
5747 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5748 }
5749
5750 Primitive::Type GetType() const OVERRIDE {
5751 return Primitive::kPrimNot;
5752 }
5753
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005754 Handle<mirror::Class> GetClass() const {
5755 return klass_;
5756 }
5757
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005758 DECLARE_INSTRUCTION(LoadClass);
5759
5760 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005761 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005762 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005763 // Whether this instruction must generate the initialization check.
5764 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005765 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005766 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5767 static constexpr size_t kFieldLoadKindSize =
5768 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5769 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5772
5773 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005774 return load_kind == LoadKind::kReferrersClass ||
5775 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005776 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005777 load_kind == LoadKind::kBssEntry ||
5778 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005779 }
5780
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005781 void SetLoadKindInternal(LoadKind load_kind);
5782
5783 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5784 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005785 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005786 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005787
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005788 // A type index and dex file where the class can be accessed. The dex file can be:
5789 // - The compiling method's dex file if the class is defined there too.
5790 // - The compiling method's dex file if the class is referenced there.
5791 // - The dex file where the class is defined. When the load kind can only be
5792 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005793 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005794 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005795
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005796 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797
Calin Juravleacf735c2015-02-12 15:25:22 +00005798 ReferenceTypeInfo loaded_class_rti_;
5799
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005800 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5801};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005802std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5803
5804// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005806 // The special input is used for PC-relative loads on some architectures,
5807 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005809 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005810 GetLoadKind() == LoadKind::kBootImageAddress ||
5811 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005812 DCHECK(special_input_.GetInstruction() == nullptr);
5813 special_input_ = HUserRecord<HInstruction*>(special_input);
5814 special_input->AddUseAt(this, 0);
5815}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005816
Vladimir Marko372f10e2016-05-17 16:30:10 +01005817class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005818 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005819 // Determines how to load the String.
5820 enum class LoadKind {
5821 // Use boot image String* address that will be known at link time.
5822 // Used for boot image strings referenced by boot image code in non-PIC mode.
5823 kBootImageLinkTimeAddress,
5824
5825 // Use PC-relative boot image String* address that will be known at link time.
5826 // Used for boot image strings referenced by boot image code in PIC mode.
5827 kBootImageLinkTimePcRelative,
5828
5829 // Use a known boot image String* address, embedded in the code by the codegen.
5830 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005831 kBootImageAddress,
5832
Vladimir Markoaad75c62016-10-03 08:46:48 +00005833 // Load from an entry in the .bss section using a PC-relative load.
5834 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5835 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005836
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005837 // Load from the root table associated with the JIT compiled method.
5838 kJitTableAddress,
5839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005840 // Load from resolved strings array accessed through the class loaded from
5841 // the compiled method's own ArtMethod*. This is the default access type when
5842 // all other types are unavailable.
5843 kDexCacheViaMethod,
5844
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005845 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 };
5847
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005848 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005849 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005850 const DexFile& dex_file,
5851 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005852 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5853 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005854 string_index_(string_index),
5855 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005857 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005858
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005859 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 LoadKind GetLoadKind() const {
5862 return GetPackedField<LoadKindField>();
5863 }
5864
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005865 const DexFile& GetDexFile() const {
5866 return dex_file_;
5867 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005868
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005869 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 return string_index_;
5871 }
5872
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005873 Handle<mirror::String> GetString() const {
5874 return string_;
5875 }
5876
5877 void SetString(Handle<mirror::String> str) {
5878 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 }
5880
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005881 bool CanBeMoved() const OVERRIDE { return true; }
5882
Vladimir Marko372f10e2016-05-17 16:30:10 +01005883 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005884
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005885 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005886
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005887 // Will call the runtime if we need to load the string through
5888 // the dex cache and the string is not guaranteed to be there yet.
5889 bool NeedsEnvironment() const OVERRIDE {
5890 LoadKind load_kind = GetLoadKind();
5891 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5892 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005893 load_kind == LoadKind::kBootImageAddress ||
5894 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005895 return false;
5896 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005897 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005899
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5901 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5902 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005903
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005904 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005905 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005906
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005907 static SideEffects SideEffectsForArchRuntimeCalls() {
5908 return SideEffects::CanTriggerGC();
5909 }
5910
Vladimir Marko372f10e2016-05-17 16:30:10 +01005911 void AddSpecialInput(HInstruction* special_input);
5912
5913 using HInstruction::GetInputRecords; // Keep the const version visible.
5914 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5915 return ArrayRef<HUserRecord<HInstruction*>>(
5916 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 }
5918
Vladimir Marko372f10e2016-05-17 16:30:10 +01005919 Primitive::Type GetType() const OVERRIDE {
5920 return Primitive::kPrimNot;
5921 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005922
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005923 DECLARE_INSTRUCTION(LoadString);
5924
5925 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005926 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 static constexpr size_t kFieldLoadKindSize =
5928 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5929 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005930 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005931 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005932
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005933 void SetLoadKindInternal(LoadKind load_kind);
5934
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005935 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5936 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005937 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005938 HUserRecord<HInstruction*> special_input_;
5939
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005940 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005941 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005943 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005944
5945 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5946};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5948
5949// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005950inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005951 // The special input is used for PC-relative loads on some architectures,
5952 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005953 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005954 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005955 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5956 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005957 // HLoadString::GetInputRecords() returns an empty array at this point,
5958 // so use the GetInputRecords() from the base class to set the input record.
5959 DCHECK(special_input_.GetInstruction() == nullptr);
5960 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961 special_input->AddUseAt(this, 0);
5962}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964/**
5965 * Performs an initialization check on its Class object input.
5966 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005967class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005969 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005970 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005971 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005972 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5973 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 SetRawInputAt(0, constant);
5975 }
5976
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005977 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005978 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005979 return true;
5980 }
5981
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005982 bool NeedsEnvironment() const OVERRIDE {
5983 // May call runtime to initialize the class.
5984 return true;
5985 }
5986
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005987 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005988
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005989 HLoadClass* GetLoadClass() const {
5990 DCHECK(InputAt(0)->IsLoadClass());
5991 return InputAt(0)->AsLoadClass();
5992 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005993
5994 DECLARE_INSTRUCTION(ClinitCheck);
5995
5996 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5998};
5999
Vladimir Markofcb503c2016-05-18 12:48:17 +01006000class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001 public:
6002 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006003 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006005 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006006 bool is_volatile,
6007 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006008 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006009 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006010 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006011 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006012 field_info_(field,
6013 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006014 field_type,
6015 is_volatile,
6016 field_idx,
6017 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006018 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 SetRawInputAt(0, cls);
6020 }
6021
Calin Juravle52c48962014-12-16 17:02:57 +00006022
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006023 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006024
Vladimir Marko372f10e2016-05-17 16:30:10 +01006025 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6026 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006027 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006028 }
6029
6030 size_t ComputeHashCode() const OVERRIDE {
6031 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6032 }
6033
Calin Juravle52c48962014-12-16 17:02:57 +00006034 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006035 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6036 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006037 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038
6039 DECLARE_INSTRUCTION(StaticFieldGet);
6040
6041 private:
6042 const FieldInfo field_info_;
6043
6044 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6045};
6046
Vladimir Markofcb503c2016-05-18 12:48:17 +01006047class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006048 public:
6049 HStaticFieldSet(HInstruction* cls,
6050 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006051 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006052 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006053 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006054 bool is_volatile,
6055 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006056 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006057 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006058 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006059 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006060 field_info_(field,
6061 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006062 field_type,
6063 is_volatile,
6064 field_idx,
6065 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006066 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006067 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006068 SetRawInputAt(0, cls);
6069 SetRawInputAt(1, value);
6070 }
6071
Calin Juravle52c48962014-12-16 17:02:57 +00006072 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006073 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6074 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006075 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006076
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006077 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006078 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6079 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006080
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 DECLARE_INSTRUCTION(StaticFieldSet);
6082
6083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006084 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6085 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6086 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6087 "Too many packed fields.");
6088
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089 const FieldInfo field_info_;
6090
6091 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6092};
6093
Vladimir Markofcb503c2016-05-18 12:48:17 +01006094class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006095 public:
6096 HUnresolvedInstanceFieldGet(HInstruction* obj,
6097 Primitive::Type field_type,
6098 uint32_t field_index,
6099 uint32_t dex_pc)
6100 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6101 field_index_(field_index) {
6102 SetRawInputAt(0, obj);
6103 }
6104
6105 bool NeedsEnvironment() const OVERRIDE { return true; }
6106 bool CanThrow() const OVERRIDE { return true; }
6107
6108 Primitive::Type GetFieldType() const { return GetType(); }
6109 uint32_t GetFieldIndex() const { return field_index_; }
6110
6111 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6112
6113 private:
6114 const uint32_t field_index_;
6115
6116 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6117};
6118
Vladimir Markofcb503c2016-05-18 12:48:17 +01006119class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006120 public:
6121 HUnresolvedInstanceFieldSet(HInstruction* obj,
6122 HInstruction* value,
6123 Primitive::Type field_type,
6124 uint32_t field_index,
6125 uint32_t dex_pc)
6126 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006127 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006129 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006130 SetRawInputAt(0, obj);
6131 SetRawInputAt(1, value);
6132 }
6133
6134 bool NeedsEnvironment() const OVERRIDE { return true; }
6135 bool CanThrow() const OVERRIDE { return true; }
6136
Vladimir Markoa1de9182016-02-25 11:37:38 +00006137 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006138 uint32_t GetFieldIndex() const { return field_index_; }
6139
6140 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6141
6142 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006143 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6144 static constexpr size_t kFieldFieldTypeSize =
6145 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6146 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6147 kFieldFieldType + kFieldFieldTypeSize;
6148 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6149 "Too many packed fields.");
6150 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6151
Calin Juravlee460d1d2015-09-29 04:52:17 +01006152 const uint32_t field_index_;
6153
6154 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6155};
6156
Vladimir Markofcb503c2016-05-18 12:48:17 +01006157class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006158 public:
6159 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6160 uint32_t field_index,
6161 uint32_t dex_pc)
6162 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6163 field_index_(field_index) {
6164 }
6165
6166 bool NeedsEnvironment() const OVERRIDE { return true; }
6167 bool CanThrow() const OVERRIDE { return true; }
6168
6169 Primitive::Type GetFieldType() const { return GetType(); }
6170 uint32_t GetFieldIndex() const { return field_index_; }
6171
6172 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6173
6174 private:
6175 const uint32_t field_index_;
6176
6177 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6178};
6179
Vladimir Markofcb503c2016-05-18 12:48:17 +01006180class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006181 public:
6182 HUnresolvedStaticFieldSet(HInstruction* value,
6183 Primitive::Type field_type,
6184 uint32_t field_index,
6185 uint32_t dex_pc)
6186 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006187 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006188 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006189 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006190 SetRawInputAt(0, value);
6191 }
6192
6193 bool NeedsEnvironment() const OVERRIDE { return true; }
6194 bool CanThrow() const OVERRIDE { return true; }
6195
Vladimir Markoa1de9182016-02-25 11:37:38 +00006196 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006197 uint32_t GetFieldIndex() const { return field_index_; }
6198
6199 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6200
6201 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006202 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6203 static constexpr size_t kFieldFieldTypeSize =
6204 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6205 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6206 kFieldFieldType + kFieldFieldTypeSize;
6207 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6208 "Too many packed fields.");
6209 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6210
Calin Juravlee460d1d2015-09-29 04:52:17 +01006211 const uint32_t field_index_;
6212
6213 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6214};
6215
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006216// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006217class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006218 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006219 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6220 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006221
David Brazdilbbd733e2015-08-18 17:48:17 +01006222 bool CanBeNull() const OVERRIDE { return false; }
6223
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006224 DECLARE_INSTRUCTION(LoadException);
6225
6226 private:
6227 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6228};
6229
David Brazdilcb1c0552015-08-04 16:22:25 +01006230// Implicit part of move-exception which clears thread-local exception storage.
6231// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006232class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006233 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006234 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6235 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006236
6237 DECLARE_INSTRUCTION(ClearException);
6238
6239 private:
6240 DISALLOW_COPY_AND_ASSIGN(HClearException);
6241};
6242
Vladimir Markofcb503c2016-05-18 12:48:17 +01006243class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006244 public:
6245 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006246 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006247 SetRawInputAt(0, exception);
6248 }
6249
6250 bool IsControlFlow() const OVERRIDE { return true; }
6251
6252 bool NeedsEnvironment() const OVERRIDE { return true; }
6253
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006254 bool CanThrow() const OVERRIDE { return true; }
6255
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006256
6257 DECLARE_INSTRUCTION(Throw);
6258
6259 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006260 DISALLOW_COPY_AND_ASSIGN(HThrow);
6261};
6262
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006263/**
6264 * Implementation strategies for the code generator of a HInstanceOf
6265 * or `HCheckCast`.
6266 */
6267enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006268 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 kExactCheck, // Can do a single class compare.
6270 kClassHierarchyCheck, // Can just walk the super class chain.
6271 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6272 kInterfaceCheck, // No optimization yet when checking against an interface.
6273 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006274 kArrayCheck, // No optimization yet when checking against a generic array.
6275 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006276};
6277
Roland Levillain86503782016-02-11 19:07:30 +00006278std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6279
Vladimir Markofcb503c2016-05-18 12:48:17 +01006280class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006281 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006282 HInstanceOf(HInstruction* object,
6283 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006285 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006286 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006288 dex_pc) {
6289 SetPackedField<TypeCheckKindField>(check_kind);
6290 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006291 SetRawInputAt(0, object);
6292 SetRawInputAt(1, constant);
6293 }
6294
6295 bool CanBeMoved() const OVERRIDE { return true; }
6296
Vladimir Marko372f10e2016-05-17 16:30:10 +01006297 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006298 return true;
6299 }
6300
6301 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006302 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006303 }
6304
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006305 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006306 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6307 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6308 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6309 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006310
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006311 static bool CanCallRuntime(TypeCheckKind check_kind) {
6312 // Mips currently does runtime calls for any other checks.
6313 return check_kind != TypeCheckKind::kExactCheck;
6314 }
6315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006317 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006318 }
6319
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006321
6322 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006323 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6324 static constexpr size_t kFieldTypeCheckKindSize =
6325 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6326 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6327 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6328 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6329 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006330
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006331 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6332};
6333
Vladimir Markofcb503c2016-05-18 12:48:17 +01006334class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006335 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006336 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006337 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006338 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6339 SetPackedFlag<kFlagUpperCanBeNull>(true);
6340 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006341 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006342 SetRawInputAt(0, input);
6343 }
6344
David Brazdilf5552582015-12-27 13:36:12 +00006345 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006346 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006347 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006348 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006349
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006350 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006351 DCHECK(GetUpperCanBeNull() || !can_be_null);
6352 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006353 }
6354
Vladimir Markoa1de9182016-02-25 11:37:38 +00006355 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006356
Calin Juravleb1498f62015-02-16 13:13:29 +00006357 DECLARE_INSTRUCTION(BoundType);
6358
6359 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6361 // is false then CanBeNull() cannot be true).
6362 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6363 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6364 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6365 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6366
Calin Juravleb1498f62015-02-16 13:13:29 +00006367 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006368 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6369 // It is used to bound the type in cases like:
6370 // if (x instanceof ClassX) {
6371 // // uper_bound_ will be ClassX
6372 // }
David Brazdilf5552582015-12-27 13:36:12 +00006373 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006374
6375 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6376};
6377
Vladimir Markofcb503c2016-05-18 12:48:17 +01006378class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006379 public:
6380 HCheckCast(HInstruction* object,
6381 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006383 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6385 SetPackedField<TypeCheckKindField>(check_kind);
6386 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006387 SetRawInputAt(0, object);
6388 SetRawInputAt(1, constant);
6389 }
6390
6391 bool CanBeMoved() const OVERRIDE { return true; }
6392
Vladimir Marko372f10e2016-05-17 16:30:10 +01006393 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006394 return true;
6395 }
6396
6397 bool NeedsEnvironment() const OVERRIDE {
6398 // Instruction may throw a CheckCastError.
6399 return true;
6400 }
6401
6402 bool CanThrow() const OVERRIDE { return true; }
6403
Vladimir Markoa1de9182016-02-25 11:37:38 +00006404 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6405 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6406 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6407 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006408
6409 DECLARE_INSTRUCTION(CheckCast);
6410
6411 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006412 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6413 static constexpr size_t kFieldTypeCheckKindSize =
6414 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6415 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6416 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6417 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6418 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006419
6420 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006421};
6422
Andreas Gampe26de38b2016-07-27 17:53:11 -07006423/**
6424 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6425 * @details We define the combined barrier types that are actually required
6426 * by the Java Memory Model, rather than using exactly the terminology from
6427 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6428 * primitives. Note that the JSR-133 cookbook generally does not deal with
6429 * store atomicity issues, and the recipes there are not always entirely sufficient.
6430 * The current recipe is as follows:
6431 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6432 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6433 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6434 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6435 * class has final fields.
6436 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6437 * store-to-memory instructions. Only generated together with non-temporal stores.
6438 */
6439enum MemBarrierKind {
6440 kAnyStore,
6441 kLoadAny,
6442 kStoreStore,
6443 kAnyAny,
6444 kNTStoreStore,
6445 kLastBarrierKind = kNTStoreStore
6446};
6447std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6448
Vladimir Markofcb503c2016-05-18 12:48:17 +01006449class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006451 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006452 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006453 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6454 SetPackedField<BarrierKindField>(barrier_kind);
6455 }
Calin Juravle27df7582015-04-17 19:12:31 +01006456
Vladimir Markoa1de9182016-02-25 11:37:38 +00006457 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006458
6459 DECLARE_INSTRUCTION(MemoryBarrier);
6460
6461 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006462 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6463 static constexpr size_t kFieldBarrierKindSize =
6464 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6465 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6466 kFieldBarrierKind + kFieldBarrierKindSize;
6467 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6468 "Too many packed fields.");
6469 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006470
6471 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6472};
6473
Vladimir Markofcb503c2016-05-18 12:48:17 +01006474class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006475 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006477 kEnter,
6478 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006480 };
6481
6482 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006483 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006484 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6485 dex_pc) {
6486 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006487 SetRawInputAt(0, object);
6488 }
6489
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006490 // Instruction may go into runtime, so we need an environment.
6491 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006492
6493 bool CanThrow() const OVERRIDE {
6494 // Verifier guarantees that monitor-exit cannot throw.
6495 // This is important because it allows the HGraphBuilder to remove
6496 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6497 return IsEnter();
6498 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006499
Vladimir Markoa1de9182016-02-25 11:37:38 +00006500 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6501 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006502
6503 DECLARE_INSTRUCTION(MonitorOperation);
6504
Calin Juravle52c48962014-12-16 17:02:57 +00006505 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006506 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6507 static constexpr size_t kFieldOperationKindSize =
6508 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6509 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6510 kFieldOperationKind + kFieldOperationKindSize;
6511 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6512 "Too many packed fields.");
6513 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006514
6515 private:
6516 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6517};
6518
Vladimir Markofcb503c2016-05-18 12:48:17 +01006519class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006520 public:
6521 HSelect(HInstruction* condition,
6522 HInstruction* true_value,
6523 HInstruction* false_value,
6524 uint32_t dex_pc)
6525 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6526 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6527
6528 // First input must be `true_value` or `false_value` to allow codegens to
6529 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6530 // that architectures which implement HSelect as a conditional move also
6531 // will not need to invert the condition.
6532 SetRawInputAt(0, false_value);
6533 SetRawInputAt(1, true_value);
6534 SetRawInputAt(2, condition);
6535 }
6536
6537 HInstruction* GetFalseValue() const { return InputAt(0); }
6538 HInstruction* GetTrueValue() const { return InputAt(1); }
6539 HInstruction* GetCondition() const { return InputAt(2); }
6540
6541 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006542 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6543 return true;
6544 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006545
6546 bool CanBeNull() const OVERRIDE {
6547 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6548 }
6549
6550 DECLARE_INSTRUCTION(Select);
6551
6552 private:
6553 DISALLOW_COPY_AND_ASSIGN(HSelect);
6554};
6555
Vladimir Markof9f64412015-09-02 14:05:49 +01006556class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006557 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006558 MoveOperands(Location source,
6559 Location destination,
6560 Primitive::Type type,
6561 HInstruction* instruction)
6562 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006563
6564 Location GetSource() const { return source_; }
6565 Location GetDestination() const { return destination_; }
6566
6567 void SetSource(Location value) { source_ = value; }
6568 void SetDestination(Location value) { destination_ = value; }
6569
6570 // The parallel move resolver marks moves as "in-progress" by clearing the
6571 // destination (but not the source).
6572 Location MarkPending() {
6573 DCHECK(!IsPending());
6574 Location dest = destination_;
6575 destination_ = Location::NoLocation();
6576 return dest;
6577 }
6578
6579 void ClearPending(Location dest) {
6580 DCHECK(IsPending());
6581 destination_ = dest;
6582 }
6583
6584 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006585 DCHECK(source_.IsValid() || destination_.IsInvalid());
6586 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006587 }
6588
6589 // True if this blocks a move from the given location.
6590 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006591 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006592 }
6593
6594 // A move is redundant if it's been eliminated, if its source and
6595 // destination are the same, or if its destination is unneeded.
6596 bool IsRedundant() const {
6597 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6598 }
6599
6600 // We clear both operands to indicate move that's been eliminated.
6601 void Eliminate() {
6602 source_ = destination_ = Location::NoLocation();
6603 }
6604
6605 bool IsEliminated() const {
6606 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6607 return source_.IsInvalid();
6608 }
6609
Alexey Frunze4dda3372015-06-01 18:31:49 -07006610 Primitive::Type GetType() const { return type_; }
6611
Nicolas Geoffray90218252015-04-15 11:56:51 +01006612 bool Is64BitMove() const {
6613 return Primitive::Is64BitType(type_);
6614 }
6615
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006616 HInstruction* GetInstruction() const { return instruction_; }
6617
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006618 private:
6619 Location source_;
6620 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006621 // The type this move is for.
6622 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006623 // The instruction this move is assocatied with. Null when this move is
6624 // for moving an input in the expected locations of user (including a phi user).
6625 // This is only used in debug mode, to ensure we do not connect interval siblings
6626 // in the same parallel move.
6627 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006628};
6629
Roland Levillainc9285912015-12-18 10:38:42 +00006630std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6631
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006632static constexpr size_t kDefaultNumberOfMoves = 4;
6633
Vladimir Markofcb503c2016-05-18 12:48:17 +01006634class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006636 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006637 : HTemplateInstruction(SideEffects::None(), dex_pc),
6638 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6639 moves_.reserve(kDefaultNumberOfMoves);
6640 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006641
Nicolas Geoffray90218252015-04-15 11:56:51 +01006642 void AddMove(Location source,
6643 Location destination,
6644 Primitive::Type type,
6645 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006646 DCHECK(source.IsValid());
6647 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006648 if (kIsDebugBuild) {
6649 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006650 for (const MoveOperands& move : moves_) {
6651 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006652 // Special case the situation where the move is for the spill slot
6653 // of the instruction.
6654 if ((GetPrevious() == instruction)
6655 || ((GetPrevious() == nullptr)
6656 && instruction->IsPhi()
6657 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006658 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006659 << "Doing parallel moves for the same instruction.";
6660 } else {
6661 DCHECK(false) << "Doing parallel moves for the same instruction.";
6662 }
6663 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006664 }
6665 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006666 for (const MoveOperands& move : moves_) {
6667 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006668 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006669 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006670 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006671 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006672 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006673 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006674 }
6675
Vladimir Marko225b6462015-09-28 12:17:40 +01006676 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006677 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006678 }
6679
Vladimir Marko225b6462015-09-28 12:17:40 +01006680 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006682 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006683
6684 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006685 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006686
6687 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6688};
6689
Mark Mendell0616ae02015-04-17 12:49:27 -04006690} // namespace art
6691
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006692#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6693#include "nodes_shared.h"
6694#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006695#ifdef ART_ENABLE_CODEGEN_arm
6696#include "nodes_arm.h"
6697#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006698#ifdef ART_ENABLE_CODEGEN_mips
6699#include "nodes_mips.h"
6700#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006701#ifdef ART_ENABLE_CODEGEN_x86
6702#include "nodes_x86.h"
6703#endif
6704
6705namespace art {
6706
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006707class HGraphVisitor : public ValueObject {
6708 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006709 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6710 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006711
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006712 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006713 virtual void VisitBasicBlock(HBasicBlock* block);
6714
Roland Levillain633021e2014-10-01 14:12:25 +01006715 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006716 void VisitInsertionOrder();
6717
Roland Levillain633021e2014-10-01 14:12:25 +01006718 // Visit the graph following dominator tree reverse post-order.
6719 void VisitReversePostOrder();
6720
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006721 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006722
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006723 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006724#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006725 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6726
6727 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6728
6729#undef DECLARE_VISIT_INSTRUCTION
6730
6731 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006732 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006733
6734 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6735};
6736
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006737class HGraphDelegateVisitor : public HGraphVisitor {
6738 public:
6739 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6740 virtual ~HGraphDelegateVisitor() {}
6741
6742 // Visit functions that delegate to to super class.
6743#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006744 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006745
6746 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6747
6748#undef DECLARE_VISIT_INSTRUCTION
6749
6750 private:
6751 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6752};
6753
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006754// Iterator over the blocks that art part of the loop. Includes blocks part
6755// of an inner loop. The order in which the blocks are iterated is on their
6756// block id.
6757class HBlocksInLoopIterator : public ValueObject {
6758 public:
6759 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6760 : blocks_in_loop_(info.GetBlocks()),
6761 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6762 index_(0) {
6763 if (!blocks_in_loop_.IsBitSet(index_)) {
6764 Advance();
6765 }
6766 }
6767
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006768 bool Done() const { return index_ == blocks_.size(); }
6769 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006770 void Advance() {
6771 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006772 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006773 if (blocks_in_loop_.IsBitSet(index_)) {
6774 break;
6775 }
6776 }
6777 }
6778
6779 private:
6780 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006781 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006782 size_t index_;
6783
6784 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6785};
6786
Mingyao Yang3584bce2015-05-19 16:01:59 -07006787// Iterator over the blocks that art part of the loop. Includes blocks part
6788// of an inner loop. The order in which the blocks are iterated is reverse
6789// post order.
6790class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6791 public:
6792 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6793 : blocks_in_loop_(info.GetBlocks()),
6794 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6795 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006796 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006797 Advance();
6798 }
6799 }
6800
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006801 bool Done() const { return index_ == blocks_.size(); }
6802 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006803 void Advance() {
6804 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006805 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6806 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006807 break;
6808 }
6809 }
6810 }
6811
6812 private:
6813 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006814 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006815 size_t index_;
6816
6817 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6818};
6819
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006820inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006821 if (constant->IsIntConstant()) {
6822 return constant->AsIntConstant()->GetValue();
6823 } else if (constant->IsLongConstant()) {
6824 return constant->AsLongConstant()->GetValue();
6825 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006826 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006827 return 0;
6828 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006829}
6830
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006831#define INSTRUCTION_TYPE_CHECK(type, super) \
6832 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6833 inline const H##type* HInstruction::As##type() const { \
6834 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6835 } \
6836 inline H##type* HInstruction::As##type() { \
6837 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6838 }
6839
6840 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6841#undef INSTRUCTION_TYPE_CHECK
6842
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006843// Create space in `blocks` for adding `number_of_new_blocks` entries
6844// starting at location `at`. Blocks after `at` are moved accordingly.
6845inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6846 size_t number_of_new_blocks,
6847 size_t after) {
6848 DCHECK_LT(after, blocks->size());
6849 size_t old_size = blocks->size();
6850 size_t new_size = old_size + number_of_new_blocks;
6851 blocks->resize(new_size);
6852 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6853}
6854
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006855/*
6856 * Hunt "under the hood" of array lengths (leading to array references),
6857 * null checks (also leading to array references), and new arrays
6858 * (leading to the actual length). This makes it more likely related
6859 * instructions become actually comparable.
6860 */
6861inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6862 while (instruction->IsArrayLength() ||
6863 instruction->IsNullCheck() ||
6864 instruction->IsNewArray()) {
6865 instruction = instruction->IsNewArray()
6866 ? instruction->AsNewArray()->GetLength()
6867 : instruction->InputAt(0);
6868 }
6869 return instruction;
6870}
6871
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006872} // namespace art
6873
6874#endif // ART_COMPILER_OPTIMIZING_NODES_H_