blob: 52a02c22858cff41edf4fa08f0bab4c4e4112f24 [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
Aart Bikf8f5a162017-02-06 15:35:29 -0800403 // Adds a new loop directly after the loop with the given header and exit.
404 // Returns the new preheader.
405 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
406 HBasicBlock* body,
407 HBasicBlock* exit);
408
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000409 // Removes `block` from the graph. Assumes `block` has been disconnected from
410 // other blocks and has no instructions or phis.
411 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000412
David Brazdilfc6a86a2015-06-26 10:33:45 +0000413 // Splits the edge between `block` and `successor` while preserving the
414 // indices in the predecessor/successor lists. If there are multiple edges
415 // between the blocks, the lowest indices are used.
416 // Returns the new block which is empty and has the same dex pc as `successor`.
417 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
420 void SimplifyLoop(HBasicBlock* header);
421
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000422 int32_t GetNextInstructionId() {
423 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 return current_instruction_id_++;
425 }
426
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000427 int32_t GetCurrentInstructionId() const {
428 return current_instruction_id_;
429 }
430
431 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000432 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 current_instruction_id_ = id;
434 }
435
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100436 uint16_t GetMaximumNumberOfOutVRegs() const {
437 return maximum_number_of_out_vregs_;
438 }
439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
441 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100442 }
443
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
445 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
446 }
447
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000448 void UpdateTemporariesVRegSlots(size_t slots) {
449 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100450 }
451
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000452 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100453 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 }
456
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100457 void SetNumberOfVRegs(uint16_t number_of_vregs) {
458 number_of_vregs_ = number_of_vregs;
459 }
460
461 uint16_t GetNumberOfVRegs() const {
462 return number_of_vregs_;
463 }
464
465 void SetNumberOfInVRegs(uint16_t value) {
466 number_of_in_vregs_ = value;
467 }
468
David Brazdildee58d62016-04-07 09:54:26 +0000469 uint16_t GetNumberOfInVRegs() const {
470 return number_of_in_vregs_;
471 }
472
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100473 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100474 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100475 return number_of_vregs_ - number_of_in_vregs_;
476 }
477
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100478 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100479 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100481
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100482 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
483 DCHECK(GetReversePostOrder()[0] == entry_block_);
484 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
485 }
486
487 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
488 return ReverseRange(GetReversePostOrder());
489 }
490
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100491 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100492 return linear_order_;
493 }
494
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100495 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
496 return ReverseRange(GetLinearOrder());
497 }
498
Mark Mendell1152c922015-04-24 17:06:35 -0400499 bool HasBoundsChecks() const {
500 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800501 }
502
Mark Mendell1152c922015-04-24 17:06:35 -0400503 void SetHasBoundsChecks(bool value) {
504 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800505 }
506
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100507 bool ShouldGenerateConstructorBarrier() const {
508 return should_generate_constructor_barrier_;
509 }
510
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000511 bool IsDebuggable() const { return debuggable_; }
512
David Brazdil8d5b8b22015-03-24 10:51:52 +0000513 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000514 // already, it is created and inserted into the graph. This method is only for
515 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600516 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000517
518 // TODO: This is problematic for the consistency of reference type propagation
519 // because it can be created anytime after the pass and thus it will be left
520 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000522
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000528 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600529 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
530 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000531 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600532 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
533 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000534 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000535
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100536 HCurrentMethod* GetCurrentMethod();
537
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100538 const DexFile& GetDexFile() const {
539 return dex_file_;
540 }
541
542 uint32_t GetMethodIdx() const {
543 return method_idx_;
544 }
545
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100546 InvokeType GetInvokeType() const {
547 return invoke_type_;
548 }
549
Mark Mendellc4701932015-04-10 13:18:51 -0400550 InstructionSet GetInstructionSet() const {
551 return instruction_set_;
552 }
553
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000554 bool IsCompilingOsr() const { return osr_; }
555
Mingyao Yang063fc772016-08-02 11:02:54 -0700556 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
557 return cha_single_implementation_list_;
558 }
559
560 void AddCHASingleImplementationDependency(ArtMethod* method) {
561 cha_single_implementation_list_.insert(method);
562 }
563
564 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800565 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700566 }
567
David Brazdil77a48ae2015-09-15 12:34:04 +0000568 bool HasTryCatch() const { return has_try_catch_; }
569 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100570
Aart Bikb13c65b2017-03-21 20:14:07 -0700571 bool HasSIMD() const { return has_simd_; }
572 void SetHasSIMD(bool value) { has_simd_ = value; }
573
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800574 bool HasLoops() const { return has_loops_; }
575 void SetHasLoops(bool value) { has_loops_ = value; }
576
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000577 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
578 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
579
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100580 ArtMethod* GetArtMethod() const { return art_method_; }
581 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
582
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100583 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500584 // The instruction has been inserted into the graph, either as a constant, or
585 // before cursor.
586 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
587
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000588 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
589
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800590 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
591 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
592 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
593
David Brazdil2d7352b2015-04-20 14:52:42 +0100594 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000595 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100596 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000597
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 template <class InstructionType, typename ValueType>
599 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600600 ArenaSafeMap<ValueType, InstructionType*>* cache,
601 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 // Try to find an existing constant of the given value.
603 InstructionType* constant = nullptr;
604 auto cached_constant = cache->find(value);
605 if (cached_constant != cache->end()) {
606 constant = cached_constant->second;
607 }
608
609 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100610 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000611 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600612 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000613 cache->Overwrite(value, constant);
614 InsertConstant(constant);
615 }
616 return constant;
617 }
618
David Brazdil8d5b8b22015-03-24 10:51:52 +0000619 void InsertConstant(HConstant* instruction);
620
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000621 // Cache a float constant into the graph. This method should only be
622 // called by the SsaBuilder when creating "equivalent" instructions.
623 void CacheFloatConstant(HFloatConstant* constant);
624
625 // See CacheFloatConstant comment.
626 void CacheDoubleConstant(HDoubleConstant* constant);
627
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000628 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
630 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000632
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100633 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100634 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000635
Aart Bik281c6812016-08-26 11:31:48 -0700636 // List of blocks to perform a linear order tree traversal. Unlike the reverse
637 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100638 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100639
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000640 HBasicBlock* entry_block_;
641 HBasicBlock* exit_block_;
642
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100643 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100644 uint16_t maximum_number_of_out_vregs_;
645
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100646 // The number of virtual registers in this method. Contains the parameters.
647 uint16_t number_of_vregs_;
648
649 // The number of virtual registers used by parameters of this method.
650 uint16_t number_of_in_vregs_;
651
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000652 // Number of vreg size slots that the temporaries use (used in baseline compiler).
653 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100654
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800655 // Flag whether there are bounds checks in the graph. We can skip
656 // BCE if it's false. It's only best effort to keep it up to date in
657 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400658 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800659
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800660 // Flag whether there are try/catch blocks in the graph. We will skip
661 // try/catch-related passes if it's false. It's only best effort to keep
662 // it up to date in the presence of code elimination so there might be
663 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000664 bool has_try_catch_;
665
Aart Bikb13c65b2017-03-21 20:14:07 -0700666 // Flag whether SIMD instructions appear in the graph. If true, the
667 // code generators may have to be more careful spilling the wider
668 // contents of SIMD registers.
669 bool has_simd_;
670
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800671 // Flag whether there are any loops in the graph. We can skip loop
672 // optimization if it's false. It's only best effort to keep it up
673 // to date in the presence of code elimination so there might be false
674 // positives.
675 bool has_loops_;
676
677 // Flag whether there are any irreducible loops in the graph. It's only
678 // best effort to keep it up to date in the presence of code elimination
679 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000680 bool has_irreducible_loops_;
681
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000682 // Indicates whether the graph should be compiled in a way that
683 // ensures full debuggability. If false, we can apply more
684 // aggressive optimizations that may limit the level of debugging.
685 const bool debuggable_;
686
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000687 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000688 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100690 // The dex file from which the method is from.
691 const DexFile& dex_file_;
692
693 // The method index in the dex file.
694 const uint32_t method_idx_;
695
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100696 // If inlined, this encodes how the callee is being invoked.
697 const InvokeType invoke_type_;
698
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100699 // Whether the graph has been transformed to SSA form. Only used
700 // in debug mode to ensure we are not using properties only valid
701 // for non-SSA form (like the number of temporaries).
702 bool in_ssa_form_;
703
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100704 const bool should_generate_constructor_barrier_;
705
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800706 // Number of CHA guards in the graph. Used to short-circuit the
707 // CHA guard optimization pass when there is no CHA guard left.
708 uint32_t number_of_cha_guards_;
709
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710 const InstructionSet instruction_set_;
711
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000712 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000713 HNullConstant* cached_null_constant_;
714 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000715 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000716 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000718
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100719 HCurrentMethod* cached_current_method_;
720
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100721 // The ArtMethod this graph is for. Note that for AOT, it may be null,
722 // for example for methods whose declaring class could not be resolved
723 // (such as when the superclass could not be found).
724 ArtMethod* art_method_;
725
David Brazdil4833f5a2015-12-16 10:37:39 +0000726 // Keep the RTI of inexact Object to avoid having to pass stack handle
727 // collection pointer to passes which may create NullConstant.
728 ReferenceTypeInfo inexact_object_rti_;
729
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000730 // Whether we are compiling this graph for on stack replacement: this will
731 // make all loops seen as irreducible and emit special stack maps to mark
732 // compiled code entries which the interpreter can directly jump to.
733 const bool osr_;
734
Mingyao Yang063fc772016-08-02 11:02:54 -0700735 // List of methods that are assumed to have single implementation.
736 ArenaSet<ArtMethod*> cha_single_implementation_list_;
737
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000738 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100739 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000740 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000741 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 DISALLOW_COPY_AND_ASSIGN(HGraph);
743};
744
Vladimir Markof9f64412015-09-02 14:05:49 +0100745class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000746 public:
747 HLoopInformation(HBasicBlock* header, HGraph* graph)
748 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100749 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000750 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100751 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100753 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000754 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100755 back_edges_.reserve(kDefaultNumberOfBackEdges);
756 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000758 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100759 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760
761 void Dump(std::ostream& os);
762
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 HBasicBlock* GetHeader() const {
764 return header_;
765 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 void SetHeader(HBasicBlock* block) {
768 header_ = block;
769 }
770
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
772 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
773 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
774
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000775 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 }
778
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 }
782
David Brazdil46e2a392015-03-16 17:31:52 +0000783 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100784 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000787 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100788 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789 }
790
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100793 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100795 }
796
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100797 // Returns the lifetime position of the back edge that has the
798 // greatest lifetime position.
799 size_t GetLifetimeEnd() const;
800
801 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100803 }
804
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000805 // Finds blocks that are part of this loop.
806 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 // Returns whether this loop information contains `block`.
809 // Note that this loop information *must* be populated before entering this function.
810 bool Contains(const HBasicBlock& block) const;
811
812 // Returns whether this loop information is an inner loop of `other`.
813 // Note that `other` *must* be populated before entering this function.
814 bool IsIn(const HLoopInformation& other) const;
815
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800816 // Returns true if instruction is not defined within this loop.
817 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700818
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100819 const ArenaBitVector& GetBlocks() const { return blocks_; }
820
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000822 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000824 void ClearAllBlocks() {
825 blocks_.ClearAllBits();
826 }
827
David Brazdil3f4a5222016-05-06 12:46:21 +0100828 bool HasBackEdgeNotDominatedByHeader() const;
829
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100830 bool IsPopulated() const {
831 return blocks_.GetHighestBitSet() != -1;
832 }
833
Anton Shaminf89381f2016-05-16 16:44:13 +0600834 bool DominatesAllBackEdges(HBasicBlock* block);
835
David Sehrc757dec2016-11-04 15:48:34 -0700836 bool HasExitEdge() const;
837
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000838 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100839 // Internal recursive implementation of `Populate`.
840 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100841 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100842
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000843 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100844 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000845 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100846 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100847 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000849
850 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
851};
852
David Brazdilec16f792015-08-19 15:04:01 +0100853// Stores try/catch information for basic blocks.
854// Note that HGraph is constructed so that catch blocks cannot simultaneously
855// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100856class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100857 public:
858 // Try block information constructor.
859 explicit TryCatchInformation(const HTryBoundary& try_entry)
860 : try_entry_(&try_entry),
861 catch_dex_file_(nullptr),
862 catch_type_index_(DexFile::kDexNoIndex16) {
863 DCHECK(try_entry_ != nullptr);
864 }
865
866 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800867 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100868 : try_entry_(nullptr),
869 catch_dex_file_(&dex_file),
870 catch_type_index_(catch_type_index) {}
871
872 bool IsTryBlock() const { return try_entry_ != nullptr; }
873
874 const HTryBoundary& GetTryEntry() const {
875 DCHECK(IsTryBlock());
876 return *try_entry_;
877 }
878
879 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
880
881 bool IsCatchAllTypeIndex() const {
882 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800883 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100884 }
885
Andreas Gampea5b09a62016-11-17 15:21:22 -0800886 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100887 DCHECK(IsCatchBlock());
888 return catch_type_index_;
889 }
890
891 const DexFile& GetCatchDexFile() const {
892 DCHECK(IsCatchBlock());
893 return *catch_dex_file_;
894 }
895
896 private:
897 // One of possibly several TryBoundary instructions entering the block's try.
898 // Only set for try blocks.
899 const HTryBoundary* try_entry_;
900
901 // Exception type information. Only set for catch blocks.
902 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800903 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100904};
905
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100906static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100907static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909// A block in a method. Contains the list of instructions represented
910// as a double linked list. Each block knows its predecessors and
911// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100912
Vladimir Markof9f64412015-09-02 14:05:49 +0100913class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700915 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000917 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
918 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000919 loop_information_(nullptr),
920 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000921 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100922 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100923 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100924 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000925 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000926 try_catch_information_(nullptr) {
927 predecessors_.reserve(kDefaultNumberOfPredecessors);
928 successors_.reserve(kDefaultNumberOfSuccessors);
929 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
930 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931
Vladimir Marko60584552015-09-03 13:35:12 +0000932 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934 }
935
Vladimir Marko60584552015-09-03 13:35:12 +0000936 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938 }
939
David Brazdild26a4112015-11-10 11:07:31 +0000940 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
941 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
942
Vladimir Marko60584552015-09-03 13:35:12 +0000943 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
944 return ContainsElement(successors_, block, start_from);
945 }
946
947 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100948 return dominated_blocks_;
949 }
950
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100951 bool IsEntryBlock() const {
952 return graph_->GetEntryBlock() == this;
953 }
954
955 bool IsExitBlock() const {
956 return graph_->GetExitBlock() == this;
957 }
958
David Brazdil46e2a392015-03-16 17:31:52 +0000959 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000960 bool IsSingleTryBoundary() const;
961
962 // Returns true if this block emits nothing but a jump.
963 bool IsSingleJump() const {
964 HLoopInformation* loop_info = GetLoopInformation();
965 return (IsSingleGoto() || IsSingleTryBoundary())
966 // Back edges generate a suspend check.
967 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
968 }
David Brazdil46e2a392015-03-16 17:31:52 +0000969
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000970 void AddBackEdge(HBasicBlock* back_edge) {
971 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000972 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000975 loop_information_->AddBackEdge(back_edge);
976 }
977
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000978 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000979 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100981 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600983 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 HBasicBlock* GetDominator() const { return dominator_; }
986 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000987 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
988
989 void RemoveDominatedBlock(HBasicBlock* block) {
990 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100991 }
Vladimir Marko60584552015-09-03 13:35:12 +0000992
993 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
994 ReplaceElement(dominated_blocks_, existing, new_block);
995 }
996
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100997 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000998
999 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001000 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001001 }
1002
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001003 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1004 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001005 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001006 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001007 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1008 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001010 HInstruction* GetFirstInstructionDisregardMoves() const;
1011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001013 successors_.push_back(block);
1014 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 }
1016
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001017 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1018 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001019 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001020 new_block->predecessors_.push_back(this);
1021 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001022 }
1023
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1025 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001027 new_block->successors_.push_back(this);
1028 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 }
1030
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001031 // Insert `this` between `predecessor` and `successor. This method
1032 // preserves the indicies, and will update the first edge found between
1033 // `predecessor` and `successor`.
1034 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1035 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001036 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001037 successor->predecessors_[predecessor_index] = this;
1038 predecessor->successors_[successor_index] = this;
1039 successors_.push_back(successor);
1040 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001041 }
1042
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001043 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001044 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 }
1046
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001047 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001048 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 }
1050
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001053 }
1054
1055 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001056 predecessors_.push_back(block);
1057 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001058 }
1059
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001060 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001061 DCHECK_EQ(predecessors_.size(), 2u);
1062 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001063 }
1064
David Brazdil769c9e52015-04-27 13:54:09 +01001065 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001066 DCHECK_EQ(successors_.size(), 2u);
1067 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001068 }
1069
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001072 }
1073
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001075 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001076 }
1077
David Brazdilfc6a86a2015-06-26 10:33:45 +00001078 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001079 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001080 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 }
1082
1083 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001084 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001085 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001086 }
1087
1088 // Returns whether the first occurrence of `predecessor` in the list of
1089 // predecessors is at index `idx`.
1090 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001091 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001092 return GetPredecessorIndexOf(predecessor) == idx;
1093 }
1094
David Brazdild7558da2015-09-22 13:04:14 +01001095 // Create a new block between this block and its predecessors. The new block
1096 // is added to the graph, all predecessor edges are relinked to it and an edge
1097 // is created to `this`. Returns the new empty block. Reverse post order or
1098 // loop and try/catch information are not updated.
1099 HBasicBlock* CreateImmediateDominator();
1100
David Brazdilfc6a86a2015-06-26 10:33:45 +00001101 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001102 // created, latter block. Note that this method will add the block to the
1103 // graph, create a Goto at the end of the former block and will create an edge
1104 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001105 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001106 HBasicBlock* SplitBefore(HInstruction* cursor);
1107
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001108 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109 // created block. Note that this method just updates raw block information,
1110 // like predecessors, successors, dominators, and instruction list. It does not
1111 // update the graph, reverse post order, loop information, nor make sure the
1112 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001113 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1114
1115 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1116 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117
1118 // Merge `other` at the end of `this`. Successors and dominated blocks of
1119 // `other` are changed to be successors and dominated blocks of `this`. Note
1120 // that this method does not update the graph, reverse post order, loop
1121 // information, nor make sure the blocks are consistent (for example ending
1122 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001123 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001124
1125 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1126 // of `this` are moved to `other`.
1127 // Note that this method does not update the graph, reverse post order, loop
1128 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001129 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 void ReplaceWith(HBasicBlock* other);
1131
Aart Bik6b69e0a2017-01-11 10:20:43 -08001132 // Merges the instructions of `other` at the end of `this`.
1133 void MergeInstructionsWith(HBasicBlock* other);
1134
David Brazdil2d7352b2015-04-20 14:52:42 +01001135 // Merge `other` at the end of `this`. This method updates loops, reverse post
1136 // order, links to predecessors, successors, dominators and deletes the block
1137 // from the graph. The two blocks must be successive, i.e. `this` the only
1138 // predecessor of `other` and vice versa.
1139 void MergeWith(HBasicBlock* other);
1140
1141 // Disconnects `this` from all its predecessors, successors and dominator,
1142 // removes it from all loops it is included in and eventually from the graph.
1143 // The block must not dominate any other block. Predecessors and successors
1144 // are safely updated.
1145 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001148 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001149 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001150 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001151 // Replace instruction `initial` with `replacement` within this block.
1152 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1153 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001155 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001156 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1157 // instruction list. With 'ensure_safety' set to true, it verifies that the
1158 // instruction is not in use and removes it from the use lists of its inputs.
1159 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1160 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001161 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162
1163 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001164 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 }
1166
Roland Levillain6b879dd2014-09-22 17:13:44 +01001167 bool IsLoopPreHeaderFirstPredecessor() const {
1168 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001169 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001170 }
1171
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001172 bool IsFirstPredecessorBackEdge() const {
1173 DCHECK(IsLoopHeader());
1174 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1175 }
1176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177 HLoopInformation* GetLoopInformation() const {
1178 return loop_information_;
1179 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001180
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001181 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001182 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001183 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001184 void SetInLoop(HLoopInformation* info) {
1185 if (IsLoopHeader()) {
1186 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001187 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001188 loop_information_ = info;
1189 } else if (loop_information_->Contains(*info->GetHeader())) {
1190 // Block is currently part of an outer loop. Make it part of this inner loop.
1191 // Note that a non loop header having a loop information means this loop information
1192 // has already been populated
1193 loop_information_ = info;
1194 } else {
1195 // Block is part of an inner loop. Do not update the loop information.
1196 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1197 // at this point, because this method is being called while populating `info`.
1198 }
1199 }
1200
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001201 // Raw update of the loop information.
1202 void SetLoopInformation(HLoopInformation* info) {
1203 loop_information_ = info;
1204 }
1205
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001206 bool IsInLoop() const { return loop_information_ != nullptr; }
1207
David Brazdilec16f792015-08-19 15:04:01 +01001208 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1209
1210 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1211 try_catch_information_ = try_catch_information;
1212 }
1213
1214 bool IsTryBlock() const {
1215 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1216 }
1217
1218 bool IsCatchBlock() const {
1219 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1220 }
David Brazdilffee3d32015-07-06 11:48:53 +01001221
1222 // Returns the try entry that this block's successors should have. They will
1223 // be in the same try, unless the block ends in a try boundary. In that case,
1224 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001225 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001226
David Brazdild7558da2015-09-22 13:04:14 +01001227 bool HasThrowingInstructions() const;
1228
David Brazdila4b8c212015-05-07 09:59:30 +01001229 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001230 bool Dominates(HBasicBlock* block) const;
1231
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001232 size_t GetLifetimeStart() const { return lifetime_start_; }
1233 size_t GetLifetimeEnd() const { return lifetime_end_; }
1234
1235 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1236 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1237
David Brazdil8d5b8b22015-03-24 10:51:52 +00001238 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001239 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001240 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001241 bool HasSinglePhi() const;
1242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001244 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001245 ArenaVector<HBasicBlock*> predecessors_;
1246 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001247 HInstructionList instructions_;
1248 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001249 HLoopInformation* loop_information_;
1250 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001251 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001252 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001253 // The dex program counter of the first instruction of this block.
1254 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001255 size_t lifetime_start_;
1256 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001258
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001259 friend class HGraph;
1260 friend class HInstruction;
1261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1263};
1264
David Brazdilb2bd1c52015-03-25 11:17:37 +00001265// Iterates over the LoopInformation of all loops which contain 'block'
1266// from the innermost to the outermost.
1267class HLoopInformationOutwardIterator : public ValueObject {
1268 public:
1269 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1270 : current_(block.GetLoopInformation()) {}
1271
1272 bool Done() const { return current_ == nullptr; }
1273
1274 void Advance() {
1275 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001276 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001277 }
1278
1279 HLoopInformation* Current() const {
1280 DCHECK(!Done());
1281 return current_;
1282 }
1283
1284 private:
1285 HLoopInformation* current_;
1286
1287 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1288};
1289
Alexandre Ramesef20f712015-06-09 10:29:30 +01001290#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001291 M(Above, Condition) \
1292 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001294 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(ArrayGet, Instruction) \
1296 M(ArrayLength, Instruction) \
1297 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001298 M(Below, Condition) \
1299 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001300 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001301 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001302 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001303 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001304 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001305 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001306 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001308 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001309 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001310 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001311 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001312 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001314 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001315 M(Exit, Instruction) \
1316 M(FloatConstant, Constant) \
1317 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001318 M(GreaterThan, Condition) \
1319 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001321 M(InstanceFieldGet, Instruction) \
1322 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001323 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001324 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001325 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001326 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001327 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001329 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001330 M(LessThan, Condition) \
1331 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001332 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001333 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001334 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001335 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001336 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001337 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001338 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001339 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001340 M(Neg, UnaryOperation) \
1341 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001343 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001344 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001345 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001346 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001347 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001348 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001350 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001351 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001352 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001353 M(Return, Instruction) \
1354 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001355 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001356 M(Shl, BinaryOperation) \
1357 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001358 M(StaticFieldGet, Instruction) \
1359 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001360 M(UnresolvedInstanceFieldGet, Instruction) \
1361 M(UnresolvedInstanceFieldSet, Instruction) \
1362 M(UnresolvedStaticFieldGet, Instruction) \
1363 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001364 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001365 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001367 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001368 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001369 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001370 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001371 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001372 M(VecReplicateScalar, VecUnaryOperation) \
1373 M(VecSetScalars, VecUnaryOperation) \
1374 M(VecSumReduce, VecUnaryOperation) \
1375 M(VecCnv, VecUnaryOperation) \
1376 M(VecNeg, VecUnaryOperation) \
1377 M(VecNot, VecUnaryOperation) \
1378 M(VecAdd, VecBinaryOperation) \
1379 M(VecSub, VecBinaryOperation) \
1380 M(VecMul, VecBinaryOperation) \
1381 M(VecDiv, VecBinaryOperation) \
1382 M(VecAnd, VecBinaryOperation) \
1383 M(VecAndNot, VecBinaryOperation) \
1384 M(VecOr, VecBinaryOperation) \
1385 M(VecXor, VecBinaryOperation) \
1386 M(VecShl, VecBinaryOperation) \
1387 M(VecShr, VecBinaryOperation) \
1388 M(VecUShr, VecBinaryOperation) \
1389 M(VecLoad, VecMemoryOperation) \
1390 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001391
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001392/*
1393 * Instructions, shared across several (not all) architectures.
1394 */
1395#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1396#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1397#else
1398#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001399 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001400 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001401 M(MultiplyAccumulate, Instruction) \
1402 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001403#endif
1404
Vladimir Markob4536b72015-11-24 13:45:23 +00001405#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001406#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001407#else
1408#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1409 M(ArmDexCacheArraysBase, Instruction)
1410#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001411
1412#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1413
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001414#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001416#else
1417#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1418 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001419 M(MipsDexCacheArraysBase, Instruction) \
1420 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001421#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001422
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001423#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1424
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001425#ifndef ART_ENABLE_CODEGEN_x86
1426#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1427#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001428#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1429 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001430 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001431 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001432 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001433#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001434
1435#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1436
1437#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1438 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001439 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001440 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1441 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001442 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001443 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001444 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1445 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1446
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001447#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1448 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001449 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001450 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001451 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001452 M(Invoke, Instruction) \
1453 M(VecOperation, Instruction) \
1454 M(VecUnaryOperation, VecOperation) \
1455 M(VecBinaryOperation, VecOperation) \
1456 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001457
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001458#define FOR_EACH_INSTRUCTION(M) \
1459 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1460 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1461
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001462#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001463FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1464#undef FORWARD_DECLARATION
1465
Vladimir Marko372f10e2016-05-17 16:30:10 +01001466#define DECLARE_INSTRUCTION(type) \
1467 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1468 const char* DebugName() const OVERRIDE { return #type; } \
1469 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1470 return other->Is##type(); \
1471 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001472 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001473
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001474#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1475 bool Is##type() const { return As##type() != nullptr; } \
1476 const H##type* As##type() const { return this; } \
1477 H##type* As##type() { return this; }
1478
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001479template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001480class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001481 public:
David Brazdiled596192015-01-23 10:39:45 +00001482 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001484 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001485
Vladimir Marko46817b82016-03-29 12:21:58 +01001486 // Hook for the IntrusiveForwardList<>.
1487 // TODO: Hide this better.
1488 IntrusiveForwardListHook hook;
1489
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001490 private:
David Brazdiled596192015-01-23 10:39:45 +00001491 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001492 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001493
1494 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001495 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001496
Vladimir Marko46817b82016-03-29 12:21:58 +01001497 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001498
1499 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1500};
1501
David Brazdiled596192015-01-23 10:39:45 +00001502template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001503using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001504
David Brazdil1abb4192015-02-17 18:33:36 +00001505// This class is used by HEnvironment and HInstruction classes to record the
1506// instructions they use and pointers to the corresponding HUseListNodes kept
1507// by the used instructions.
1508template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001509class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001510 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001511 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1512 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001513
Vladimir Marko46817b82016-03-29 12:21:58 +01001514 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1515 : HUserRecord(old_record.instruction_, before_use_node) {}
1516 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1517 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001518 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001519 }
1520
1521 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001522 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1523 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001524
1525 private:
1526 // Instruction used by the user.
1527 HInstruction* instruction_;
1528
Vladimir Marko46817b82016-03-29 12:21:58 +01001529 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1530 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001531};
1532
Vladimir Markoe9004912016-06-16 16:50:52 +01001533// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1534// This is used for HInstruction::GetInputs() to return a container wrapper providing
1535// HInstruction* values even though the underlying container has HUserRecord<>s.
1536struct HInputExtractor {
1537 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1538 return record.GetInstruction();
1539 }
1540 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1541 return record.GetInstruction();
1542 }
1543};
1544
1545using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1546using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1547
Aart Bik854a02b2015-07-14 16:07:00 -07001548/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001549 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001550 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001551 * For write/read dependences on fields/arrays, the dependence analysis uses
1552 * type disambiguation (e.g. a float field write cannot modify the value of an
1553 * integer field read) and the access type (e.g. a reference array write cannot
1554 * modify the value of a reference field read [although it may modify the
1555 * reference fetch prior to reading the field, which is represented by its own
1556 * write/read dependence]). The analysis makes conservative points-to
1557 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1558 * the same, and any reference read depends on any reference read without
1559 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001560 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 * The internal representation uses 38-bit and is described in the table below.
1562 * The first line indicates the side effect, and for field/array accesses the
1563 * second line indicates the type of the access (in the order of the
1564 * Primitive::Type enum).
1565 * The two numbered lines below indicate the bit position in the bitfield (read
1566 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001567 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001568 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1569 * +-------------+---------+---------+--------------+---------+---------+
1570 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1571 * | 3 |333333322|222222221| 1 |111111110|000000000|
1572 * | 7 |654321098|765432109| 8 |765432109|876543210|
1573 *
1574 * Note that, to ease the implementation, 'changes' bits are least significant
1575 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001576 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001577class SideEffects : public ValueObject {
1578 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 SideEffects() : flags_(0) {}
1580
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001581 static SideEffects None() {
1582 return SideEffects(0);
1583 }
1584
1585 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 return SideEffects(kAllChangeBits | kAllDependOnBits);
1587 }
1588
1589 static SideEffects AllChanges() {
1590 return SideEffects(kAllChangeBits);
1591 }
1592
1593 static SideEffects AllDependencies() {
1594 return SideEffects(kAllDependOnBits);
1595 }
1596
1597 static SideEffects AllExceptGCDependency() {
1598 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1599 }
1600
1601 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001602 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001603 }
1604
Aart Bik34c3ba92015-07-20 14:08:59 -07001605 static SideEffects AllWrites() {
1606 return SideEffects(kAllWrites);
1607 }
1608
1609 static SideEffects AllReads() {
1610 return SideEffects(kAllReads);
1611 }
1612
1613 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1614 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001616 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001617 }
1618
Aart Bik854a02b2015-07-14 16:07:00 -07001619 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001620 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001621 }
1622
Aart Bik34c3ba92015-07-20 14:08:59 -07001623 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1624 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001626 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001627 }
1628
1629 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001630 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001631 }
1632
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001633 static SideEffects CanTriggerGC() {
1634 return SideEffects(1ULL << kCanTriggerGCBit);
1635 }
1636
1637 static SideEffects DependsOnGC() {
1638 return SideEffects(1ULL << kDependsOnGCBit);
1639 }
1640
Aart Bik854a02b2015-07-14 16:07:00 -07001641 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001642 SideEffects Union(SideEffects other) const {
1643 return SideEffects(flags_ | other.flags_);
1644 }
1645
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 SideEffects Exclusion(SideEffects other) const {
1647 return SideEffects(flags_ & ~other.flags_);
1648 }
1649
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001650 void Add(SideEffects other) {
1651 flags_ |= other.flags_;
1652 }
1653
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 bool Includes(SideEffects other) const {
1655 return (other.flags_ & flags_) == other.flags_;
1656 }
1657
1658 bool HasSideEffects() const {
1659 return (flags_ & kAllChangeBits);
1660 }
1661
1662 bool HasDependencies() const {
1663 return (flags_ & kAllDependOnBits);
1664 }
1665
1666 // Returns true if there are no side effects or dependencies.
1667 bool DoesNothing() const {
1668 return flags_ == 0;
1669 }
1670
Aart Bik854a02b2015-07-14 16:07:00 -07001671 // Returns true if something is written.
1672 bool DoesAnyWrite() const {
1673 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001674 }
1675
Aart Bik854a02b2015-07-14 16:07:00 -07001676 // Returns true if something is read.
1677 bool DoesAnyRead() const {
1678 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001679 }
1680
Aart Bik854a02b2015-07-14 16:07:00 -07001681 // Returns true if potentially everything is written and read
1682 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001683 bool DoesAllReadWrite() const {
1684 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1685 }
1686
Aart Bik854a02b2015-07-14 16:07:00 -07001687 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001689 }
1690
Roland Levillain0d5a2812015-11-13 10:07:31 +00001691 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001692 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001693 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1694 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001695 }
1696
1697 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001699 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001700 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001701 for (int s = kLastBit; s >= 0; s--) {
1702 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1703 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1704 // This is a bit for the GC side effect.
1705 if (current_bit_is_set) {
1706 flags += "GC";
1707 }
Aart Bik854a02b2015-07-14 16:07:00 -07001708 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001709 } else {
1710 // This is a bit for the array/field analysis.
1711 // The underscore character stands for the 'can trigger GC' bit.
1712 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1713 if (current_bit_is_set) {
1714 flags += kDebug[s];
1715 }
1716 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1717 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1718 flags += "|";
1719 }
1720 }
Aart Bik854a02b2015-07-14 16:07:00 -07001721 }
1722 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001723 }
1724
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001726
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001727 private:
1728 static constexpr int kFieldArrayAnalysisBits = 9;
1729
1730 static constexpr int kFieldWriteOffset = 0;
1731 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1732 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1733 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1734
1735 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1736
1737 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1738 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1739 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1740 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1741
1742 static constexpr int kLastBit = kDependsOnGCBit;
1743 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1744
1745 // Aliases.
1746
1747 static_assert(kChangeBits == kDependOnBits,
1748 "the 'change' bits should match the 'depend on' bits.");
1749
1750 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1751 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1752 static constexpr uint64_t kAllWrites =
1753 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1754 static constexpr uint64_t kAllReads =
1755 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001756
Aart Bik854a02b2015-07-14 16:07:00 -07001757 // Translates type to bit flag.
1758 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1759 CHECK_NE(type, Primitive::kPrimVoid);
1760 const uint64_t one = 1;
1761 const int shift = type; // 0-based consecutive enum
1762 DCHECK_LE(kFieldWriteOffset, shift);
1763 DCHECK_LT(shift, kArrayWriteOffset);
1764 return one << (type + offset);
1765 }
1766
1767 // Private constructor on direct flags value.
1768 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1769
1770 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001771};
1772
David Brazdiled596192015-01-23 10:39:45 +00001773// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001774class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001775 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001776 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1777 size_t number_of_vregs,
1778 ArtMethod* method,
1779 uint32_t dex_pc,
1780 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1782 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001784 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001785 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001786 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001787 }
1788
Mingyao Yang01b47b02017-02-03 12:09:57 -08001789 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001790 : HEnvironment(arena,
1791 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001792 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001793 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001794 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001795
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001796 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001797 if (parent_ != nullptr) {
1798 parent_->SetAndCopyParentChain(allocator, parent);
1799 } else {
1800 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1801 parent_->CopyFrom(parent);
1802 if (parent->GetParent() != nullptr) {
1803 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1804 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 }
David Brazdiled596192015-01-23 10:39:45 +00001806 }
1807
Vladimir Marko71bf8092015-09-15 15:33:14 +01001808 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001809 void CopyFrom(HEnvironment* environment);
1810
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001811 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1812 // input to the loop phi instead. This is for inserting instructions that
1813 // require an environment (like HDeoptimization) in the loop pre-header.
1814 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001815
1816 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001817 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001818 }
1819
1820 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001821 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001822 }
1823
David Brazdil1abb4192015-02-17 18:33:36 +00001824 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001825
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001826 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001827
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001828 HEnvironment* GetParent() const { return parent_; }
1829
1830 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001831 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001832 }
1833
1834 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001835 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001836 }
1837
1838 uint32_t GetDexPc() const {
1839 return dex_pc_;
1840 }
1841
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001842 ArtMethod* GetMethod() const {
1843 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001844 }
1845
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001846 HInstruction* GetHolder() const {
1847 return holder_;
1848 }
1849
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001850
1851 bool IsFromInlinedInvoke() const {
1852 return GetParent() != nullptr;
1853 }
1854
David Brazdiled596192015-01-23 10:39:45 +00001855 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001856 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1857 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001858 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001859 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001860 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001861
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001862 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001863 HInstruction* const holder_;
1864
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001865 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001866
1867 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1868};
1869
Vladimir Markof9f64412015-09-02 14:05:49 +01001870class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001871 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001872 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873 : previous_(nullptr),
1874 next_(nullptr),
1875 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001876 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001877 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001878 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001879 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001880 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001881 locations_(nullptr),
1882 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001883 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001884 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001885 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1886 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1887 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001888
Dave Allison20dfc792014-06-16 20:44:29 -07001889 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001890
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001891#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001892 enum InstructionKind {
1893 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1894 };
1895#undef DECLARE_KIND
1896
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001897 HInstruction* GetNext() const { return next_; }
1898 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899
Calin Juravle77520bc2015-01-12 18:45:46 +00001900 HInstruction* GetNextDisregardingMoves() const;
1901 HInstruction* GetPreviousDisregardingMoves() const;
1902
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001903 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001904 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001905 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001906 bool IsInBlock() const { return block_ != nullptr; }
1907 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001908 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1909 bool IsIrreducibleLoopHeaderPhi() const {
1910 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1911 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001912
Vladimir Marko372f10e2016-05-17 16:30:10 +01001913 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1914
1915 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1916 // One virtual method is enough, just const_cast<> and then re-add the const.
1917 return ArrayRef<const HUserRecord<HInstruction*>>(
1918 const_cast<HInstruction*>(this)->GetInputRecords());
1919 }
1920
Vladimir Markoe9004912016-06-16 16:50:52 +01001921 HInputsRef GetInputs() {
1922 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001923 }
1924
Vladimir Markoe9004912016-06-16 16:50:52 +01001925 HConstInputsRef GetInputs() const {
1926 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001927 }
1928
1929 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001930 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001931
Aart Bik2767f4b2016-10-28 15:03:53 -07001932 bool HasInput(HInstruction* input) const {
1933 for (const HInstruction* i : GetInputs()) {
1934 if (i == input) {
1935 return true;
1936 }
1937 }
1938 return false;
1939 }
1940
Vladimir Marko372f10e2016-05-17 16:30:10 +01001941 void SetRawInputAt(size_t index, HInstruction* input) {
1942 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1943 }
1944
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001945 virtual void Accept(HGraphVisitor* visitor) = 0;
1946 virtual const char* DebugName() const = 0;
1947
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1949
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001950 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001951
1952 uint32_t GetDexPc() const { return dex_pc_; }
1953
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001954 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001955
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001956 // Can the instruction throw?
1957 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1958 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001959 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001960 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001961
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001962 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001963 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001964
Calin Juravle10e244f2015-01-26 18:54:32 +00001965 // Does not apply for all instructions, but having this at top level greatly
1966 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001967 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001968 virtual bool CanBeNull() const {
1969 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1970 return true;
1971 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001973 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001974 return false;
1975 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001976
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001977 virtual bool IsActualObject() const {
1978 return GetType() == Primitive::kPrimNot;
1979 }
1980
Calin Juravle2e768302015-07-28 14:41:11 +00001981 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001982
Calin Juravle61d544b2015-02-23 16:46:57 +00001983 ReferenceTypeInfo GetReferenceTypeInfo() const {
1984 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001985 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001986 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001987 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001988
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001989 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001990 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001991 // Note: fixup_end remains valid across push_front().
1992 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1993 HUseListNode<HInstruction*>* new_node =
1994 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1995 uses_.push_front(*new_node);
1996 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001997 }
1998
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001999 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002000 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002001 // Note: env_fixup_end remains valid across push_front().
2002 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2003 HUseListNode<HEnvironment*>* new_node =
2004 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2005 env_uses_.push_front(*new_node);
2006 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002007 }
2008
David Brazdil1abb4192015-02-17 18:33:36 +00002009 void RemoveAsUserOfInput(size_t input) {
2010 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002011 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2012 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2013 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002014 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002015
Vladimir Marko372f10e2016-05-17 16:30:10 +01002016 void RemoveAsUserOfAllInputs() {
2017 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2018 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2019 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2020 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2021 }
2022 }
2023
David Brazdil1abb4192015-02-17 18:33:36 +00002024 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2025 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026
Vladimir Marko46817b82016-03-29 12:21:58 +01002027 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2028 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2029 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002030 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002031 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002032 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002033
Aart Bikcc42be02016-10-20 16:14:16 -07002034 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002035 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002036 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002037 !CanThrow() &&
2038 !IsSuspendCheck() &&
2039 !IsControlFlow() &&
2040 !IsNativeDebugInfo() &&
2041 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002042 // If we added an explicit barrier then we should keep it.
2043 !IsMemoryBarrier();
2044 }
2045
Aart Bikcc42be02016-10-20 16:14:16 -07002046 bool IsDeadAndRemovable() const {
2047 return IsRemovable() && !HasUses();
2048 }
2049
Roland Levillain6c82d402014-10-13 16:10:27 +01002050 // Does this instruction strictly dominate `other_instruction`?
2051 // Returns false if this instruction and `other_instruction` are the same.
2052 // Aborts if this instruction and `other_instruction` are both phis.
2053 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002054
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002055 int GetId() const { return id_; }
2056 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002057
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002058 int GetSsaIndex() const { return ssa_index_; }
2059 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2060 bool HasSsaIndex() const { return ssa_index_ != -1; }
2061
2062 bool HasEnvironment() const { return environment_ != nullptr; }
2063 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002064 // Set the `environment_` field. Raw because this method does not
2065 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002066 void SetRawEnvironment(HEnvironment* environment) {
2067 DCHECK(environment_ == nullptr);
2068 DCHECK_EQ(environment->GetHolder(), this);
2069 environment_ = environment;
2070 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002071
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002072 void InsertRawEnvironment(HEnvironment* environment) {
2073 DCHECK(environment_ != nullptr);
2074 DCHECK_EQ(environment->GetHolder(), this);
2075 DCHECK(environment->GetParent() == nullptr);
2076 environment->parent_ = environment_;
2077 environment_ = environment;
2078 }
2079
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002080 void RemoveEnvironment();
2081
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002082 // Set the environment of this instruction, copying it from `environment`. While
2083 // copying, the uses lists are being updated.
2084 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002085 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002086 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002087 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002088 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002089 if (environment->GetParent() != nullptr) {
2090 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2091 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002092 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002093
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002094 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2095 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002096 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002097 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002098 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002099 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002100 if (environment->GetParent() != nullptr) {
2101 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2102 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002103 }
2104
Nicolas Geoffray39468442014-09-02 15:17:15 +01002105 // Returns the number of entries in the environment. Typically, that is the
2106 // number of dex registers in a method. It could be more in case of inlining.
2107 size_t EnvironmentSize() const;
2108
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002109 LocationSummary* GetLocations() const { return locations_; }
2110 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002111
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002113 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002114 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002115
Alexandre Rames188d4312015-04-09 18:30:21 +01002116 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2117 // uses of this instruction by `other` are *not* updated.
2118 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2119 ReplaceWith(other);
2120 other->ReplaceInput(this, use_index);
2121 }
2122
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002123 // Move `this` instruction before `cursor`
2124 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002125
Vladimir Markofb337ea2015-11-25 15:25:10 +00002126 // Move `this` before its first user and out of any loops. If there is no
2127 // out-of-loop user that dominates all other users, move the instruction
2128 // to the end of the out-of-loop common dominator of the user's blocks.
2129 //
2130 // This can be used only on non-throwing instructions with no side effects that
2131 // have at least one use but no environment uses.
2132 void MoveBeforeFirstUserAndOutOfLoops();
2133
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002134#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002135 bool Is##type() const; \
2136 const H##type* As##type() const; \
2137 H##type* As##type();
2138
2139 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2140#undef INSTRUCTION_TYPE_CHECK
2141
2142#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002143 bool Is##type() const { return (As##type() != nullptr); } \
2144 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002147#undef INSTRUCTION_TYPE_CHECK
2148
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002149 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002150 // TODO: this method is used by LICM and GVN with possibly different
2151 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002152 virtual bool CanBeMoved() const { return false; }
2153
2154 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002155 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002156 return false;
2157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158
2159 // Returns whether any data encoded in the two instructions is equal.
2160 // This method does not look at the inputs. Both instructions must be
2161 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002162 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002163 return false;
2164 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002165
2166 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002167 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002169 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002170
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002171 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2172 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2173 // the virtual function because the __attribute__((__pure__)) doesn't really
2174 // apply the strong requirement for virtual functions, preventing optimizations.
2175 InstructionKind GetKind() const PURE;
2176 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002177
2178 virtual size_t ComputeHashCode() const {
2179 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002180 for (const HInstruction* input : GetInputs()) {
2181 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002182 }
2183 return result;
2184 }
2185
2186 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002187 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002188 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002189
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002190 size_t GetLifetimePosition() const { return lifetime_position_; }
2191 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2192 LiveInterval* GetLiveInterval() const { return live_interval_; }
2193 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2194 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2195
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002196 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2197
2198 // Returns whether the code generation of the instruction will require to have access
2199 // to the current method. Such instructions are:
2200 // (1): Instructions that require an environment, as calling the runtime requires
2201 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002202 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2203 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002204 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002205 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002206 }
2207
Vladimir Markodc151b22015-10-15 18:02:30 +01002208 // Returns whether the code generation of the instruction will require to have access
2209 // to the dex cache of the current method's declaring class via the current method.
2210 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002211
Mark Mendellc4701932015-04-10 13:18:51 -04002212 // Does this instruction have any use in an environment before
2213 // control flow hits 'other'?
2214 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2215
2216 // Remove all references to environment uses of this instruction.
2217 // The caller must ensure that this is safe to do.
2218 void RemoveEnvironmentUsers();
2219
Vladimir Markoa1de9182016-02-25 11:37:38 +00002220 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2221 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002222
David Brazdil1abb4192015-02-17 18:33:36 +00002223 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002224 // If set, the machine code for this instruction is assumed to be generated by
2225 // its users. Used by liveness analysis to compute use positions accordingly.
2226 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2227 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2228 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2229 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2230
Vladimir Marko372f10e2016-05-17 16:30:10 +01002231 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2232 return GetInputRecords()[i];
2233 }
2234
2235 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2236 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2237 input_records[index] = input;
2238 }
David Brazdil1abb4192015-02-17 18:33:36 +00002239
Vladimir Markoa1de9182016-02-25 11:37:38 +00002240 uint32_t GetPackedFields() const {
2241 return packed_fields_;
2242 }
2243
2244 template <size_t flag>
2245 bool GetPackedFlag() const {
2246 return (packed_fields_ & (1u << flag)) != 0u;
2247 }
2248
2249 template <size_t flag>
2250 void SetPackedFlag(bool value = true) {
2251 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2252 }
2253
2254 template <typename BitFieldType>
2255 typename BitFieldType::value_type GetPackedField() const {
2256 return BitFieldType::Decode(packed_fields_);
2257 }
2258
2259 template <typename BitFieldType>
2260 void SetPackedField(typename BitFieldType::value_type value) {
2261 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2262 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2263 }
2264
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002265 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002266 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2267 auto before_use_node = uses_.before_begin();
2268 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2269 HInstruction* user = use_node->GetUser();
2270 size_t input_index = use_node->GetIndex();
2271 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2272 before_use_node = use_node;
2273 }
2274 }
2275
2276 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2277 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2278 if (next != uses_.end()) {
2279 HInstruction* next_user = next->GetUser();
2280 size_t next_index = next->GetIndex();
2281 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2282 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2283 }
2284 }
2285
2286 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2287 auto before_env_use_node = env_uses_.before_begin();
2288 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2289 HEnvironment* user = env_use_node->GetUser();
2290 size_t input_index = env_use_node->GetIndex();
2291 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2292 before_env_use_node = env_use_node;
2293 }
2294 }
2295
2296 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2297 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2298 if (next != env_uses_.end()) {
2299 HEnvironment* next_user = next->GetUser();
2300 size_t next_index = next->GetIndex();
2301 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2302 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2303 }
2304 }
David Brazdil1abb4192015-02-17 18:33:36 +00002305
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002306 HInstruction* previous_;
2307 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002308 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002309 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002310
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002311 // An instruction gets an id when it is added to the graph.
2312 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002313 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002314 int id_;
2315
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002316 // When doing liveness analysis, instructions that have uses get an SSA index.
2317 int ssa_index_;
2318
Vladimir Markoa1de9182016-02-25 11:37:38 +00002319 // Packed fields.
2320 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002321
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002323 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002324
2325 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002326 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002327
Nicolas Geoffray39468442014-09-02 15:17:15 +01002328 // The environment associated with this instruction. Not null if the instruction
2329 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002330 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002331
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002332 // Set by the code generator.
2333 LocationSummary* locations_;
2334
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002335 // Set by the liveness analysis.
2336 LiveInterval* live_interval_;
2337
2338 // Set by the liveness analysis, this is the position in a linear
2339 // order of blocks where this instruction's live interval start.
2340 size_t lifetime_position_;
2341
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002342 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002343
Vladimir Markoa1de9182016-02-25 11:37:38 +00002344 // The reference handle part of the reference type info.
2345 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002346 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002347 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002348
David Brazdil1abb4192015-02-17 18:33:36 +00002349 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002350 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002351 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002352 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002353 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354
2355 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2356};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002357std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002359// Iterates over the instructions, while preserving the next instruction
2360// in case the current instruction gets removed from the list by the user
2361// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362class HInstructionIterator : public ValueObject {
2363 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364 explicit HInstructionIterator(const HInstructionList& instructions)
2365 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002366 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367 }
2368
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002369 bool Done() const { return instruction_ == nullptr; }
2370 HInstruction* Current() const { return instruction_; }
2371 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002372 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002373 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002374 }
2375
2376 private:
2377 HInstruction* instruction_;
2378 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002379
2380 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002381};
2382
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002383// Iterates over the instructions without saving the next instruction,
2384// therefore handling changes in the graph potentially made by the user
2385// of this iterator.
2386class HInstructionIteratorHandleChanges : public ValueObject {
2387 public:
2388 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2389 : instruction_(instructions.first_instruction_) {
2390 }
2391
2392 bool Done() const { return instruction_ == nullptr; }
2393 HInstruction* Current() const { return instruction_; }
2394 void Advance() {
2395 instruction_ = instruction_->GetNext();
2396 }
2397
2398 private:
2399 HInstruction* instruction_;
2400
2401 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2402};
2403
2404
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002405class HBackwardInstructionIterator : public ValueObject {
2406 public:
2407 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2408 : instruction_(instructions.last_instruction_) {
2409 next_ = Done() ? nullptr : instruction_->GetPrevious();
2410 }
2411
2412 bool Done() const { return instruction_ == nullptr; }
2413 HInstruction* Current() const { return instruction_; }
2414 void Advance() {
2415 instruction_ = next_;
2416 next_ = Done() ? nullptr : instruction_->GetPrevious();
2417 }
2418
2419 private:
2420 HInstruction* instruction_;
2421 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002422
2423 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002424};
2425
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002426class HVariableInputSizeInstruction : public HInstruction {
2427 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002428 using HInstruction::GetInputRecords; // Keep the const version visible.
2429 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2430 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2431 }
2432
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002433 void AddInput(HInstruction* input);
2434 void InsertInputAt(size_t index, HInstruction* input);
2435 void RemoveInputAt(size_t index);
2436
2437 protected:
2438 HVariableInputSizeInstruction(SideEffects side_effects,
2439 uint32_t dex_pc,
2440 ArenaAllocator* arena,
2441 size_t number_of_inputs,
2442 ArenaAllocKind kind)
2443 : HInstruction(side_effects, dex_pc),
2444 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2445
2446 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2447
2448 private:
2449 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2450};
2451
Vladimir Markof9f64412015-09-02 14:05:49 +01002452template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002453class HTemplateInstruction: public HInstruction {
2454 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002455 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002456 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002457 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002458
Vladimir Marko372f10e2016-05-17 16:30:10 +01002459 using HInstruction::GetInputRecords; // Keep the const version visible.
2460 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2461 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002462 }
2463
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002465 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002466
2467 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468};
2469
Vladimir Markof9f64412015-09-02 14:05:49 +01002470// HTemplateInstruction specialization for N=0.
2471template<>
2472class HTemplateInstruction<0>: public HInstruction {
2473 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002474 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002475 : HInstruction(side_effects, dex_pc) {}
2476
Vladimir Markof9f64412015-09-02 14:05:49 +01002477 virtual ~HTemplateInstruction() {}
2478
Vladimir Marko372f10e2016-05-17 16:30:10 +01002479 using HInstruction::GetInputRecords; // Keep the const version visible.
2480 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2481 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002482 }
2483
2484 private:
2485 friend class SsaBuilder;
2486};
2487
Dave Allison20dfc792014-06-16 20:44:29 -07002488template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002489class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002490 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002491 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002492 : HTemplateInstruction<N>(side_effects, dex_pc) {
2493 this->template SetPackedField<TypeField>(type);
2494 }
Dave Allison20dfc792014-06-16 20:44:29 -07002495 virtual ~HExpression() {}
2496
Vladimir Markoa1de9182016-02-25 11:37:38 +00002497 Primitive::Type GetType() const OVERRIDE {
2498 return TypeField::Decode(this->GetPackedFields());
2499 }
Dave Allison20dfc792014-06-16 20:44:29 -07002500
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002501 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002502 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2503 static constexpr size_t kFieldTypeSize =
2504 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2505 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2506 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2507 "Too many packed fields.");
2508 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002509};
2510
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2512// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002513class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002515 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2516 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002517
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002518 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002520 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002521
2522 private:
2523 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2524};
2525
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002526// Represents dex's RETURN opcodes. A HReturn is a control flow
2527// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002528class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2531 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002532 SetRawInputAt(0, value);
2533 }
2534
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002535 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002537 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002538
2539 private:
2540 DISALLOW_COPY_AND_ASSIGN(HReturn);
2541};
2542
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002543class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002544 public:
2545 HPhi(ArenaAllocator* arena,
2546 uint32_t reg_number,
2547 size_t number_of_inputs,
2548 Primitive::Type type,
2549 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002550 : HVariableInputSizeInstruction(
2551 SideEffects::None(),
2552 dex_pc,
2553 arena,
2554 number_of_inputs,
2555 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002556 reg_number_(reg_number) {
2557 SetPackedField<TypeField>(ToPhiType(type));
2558 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2559 // Phis are constructed live and marked dead if conflicting or unused.
2560 // Individual steps of SsaBuilder should assume that if a phi has been
2561 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2562 SetPackedFlag<kFlagIsLive>(true);
2563 SetPackedFlag<kFlagCanBeNull>(true);
2564 }
2565
2566 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2567 static Primitive::Type ToPhiType(Primitive::Type type) {
2568 return Primitive::PrimitiveKind(type);
2569 }
2570
2571 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2572
David Brazdildee58d62016-04-07 09:54:26 +00002573 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2574 void SetType(Primitive::Type new_type) {
2575 // Make sure that only valid type changes occur. The following are allowed:
2576 // (1) int -> float/ref (primitive type propagation),
2577 // (2) long -> double (primitive type propagation).
2578 DCHECK(GetType() == new_type ||
2579 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2580 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2581 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2582 SetPackedField<TypeField>(new_type);
2583 }
2584
2585 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2586 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2587
2588 uint32_t GetRegNumber() const { return reg_number_; }
2589
2590 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2591 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2592 bool IsDead() const { return !IsLive(); }
2593 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2594
Vladimir Markoe9004912016-06-16 16:50:52 +01002595 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002596 return other != nullptr
2597 && other->IsPhi()
2598 && other->AsPhi()->GetBlock() == GetBlock()
2599 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2600 }
2601
2602 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2603 // An equivalent phi is a phi having the same dex register and type.
2604 // It assumes that phis with the same dex register are adjacent.
2605 HPhi* GetNextEquivalentPhiWithSameType() {
2606 HInstruction* next = GetNext();
2607 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2608 if (next->GetType() == GetType()) {
2609 return next->AsPhi();
2610 }
2611 next = next->GetNext();
2612 }
2613 return nullptr;
2614 }
2615
2616 DECLARE_INSTRUCTION(Phi);
2617
David Brazdildee58d62016-04-07 09:54:26 +00002618 private:
2619 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2620 static constexpr size_t kFieldTypeSize =
2621 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2622 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2623 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2624 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2625 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2626 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2627
David Brazdildee58d62016-04-07 09:54:26 +00002628 const uint32_t reg_number_;
2629
2630 DISALLOW_COPY_AND_ASSIGN(HPhi);
2631};
2632
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002634// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002636class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002637 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002638 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002639
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002640 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002641
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002642 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002643
2644 private:
2645 DISALLOW_COPY_AND_ASSIGN(HExit);
2646};
2647
2648// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002649class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002651 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002652
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002653 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002654
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002655 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002656 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002657 }
2658
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002659 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002660
2661 private:
2662 DISALLOW_COPY_AND_ASSIGN(HGoto);
2663};
2664
Roland Levillain9867bc72015-08-05 10:21:34 +01002665class HConstant : public HExpression<0> {
2666 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002667 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2668 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002669
2670 bool CanBeMoved() const OVERRIDE { return true; }
2671
Roland Levillain1a653882016-03-18 18:05:57 +00002672 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002673 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002674 // Is this constant 0 in the arithmetic sense?
2675 virtual bool IsArithmeticZero() const { return false; }
2676 // Is this constant a 0-bit pattern?
2677 virtual bool IsZeroBitPattern() const { return false; }
2678 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002679 virtual bool IsOne() const { return false; }
2680
David Brazdil77a48ae2015-09-15 12:34:04 +00002681 virtual uint64_t GetValueAsUint64() const = 0;
2682
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002683 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002684
2685 private:
2686 DISALLOW_COPY_AND_ASSIGN(HConstant);
2687};
2688
Vladimir Markofcb503c2016-05-18 12:48:17 +01002689class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002691 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 return true;
2693 }
2694
David Brazdil77a48ae2015-09-15 12:34:04 +00002695 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2696
Roland Levillain9867bc72015-08-05 10:21:34 +01002697 size_t ComputeHashCode() const OVERRIDE { return 0; }
2698
Roland Levillain1a653882016-03-18 18:05:57 +00002699 // The null constant representation is a 0-bit pattern.
2700 virtual bool IsZeroBitPattern() const { return true; }
2701
Roland Levillain9867bc72015-08-05 10:21:34 +01002702 DECLARE_INSTRUCTION(NullConstant);
2703
2704 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002705 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002706
2707 friend class HGraph;
2708 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2709};
2710
2711// Constants of the type int. Those can be from Dex instructions, or
2712// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002713class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002714 public:
2715 int32_t GetValue() const { return value_; }
2716
David Brazdil9f389d42015-10-01 14:32:56 +01002717 uint64_t GetValueAsUint64() const OVERRIDE {
2718 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2719 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002720
Vladimir Marko372f10e2016-05-17 16:30:10 +01002721 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002722 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002723 return other->AsIntConstant()->value_ == value_;
2724 }
2725
2726 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2727
2728 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002729 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2730 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2732
Roland Levillain1a653882016-03-18 18:05:57 +00002733 // Integer constants are used to encode Boolean values as well,
2734 // where 1 means true and 0 means false.
2735 bool IsTrue() const { return GetValue() == 1; }
2736 bool IsFalse() const { return GetValue() == 0; }
2737
Roland Levillain9867bc72015-08-05 10:21:34 +01002738 DECLARE_INSTRUCTION(IntConstant);
2739
2740 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002741 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2742 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2743 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2744 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002745
2746 const int32_t value_;
2747
2748 friend class HGraph;
2749 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2750 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2751 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2752};
2753
Vladimir Markofcb503c2016-05-18 12:48:17 +01002754class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 public:
2756 int64_t GetValue() const { return value_; }
2757
David Brazdil77a48ae2015-09-15 12:34:04 +00002758 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2759
Vladimir Marko372f10e2016-05-17 16:30:10 +01002760 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002761 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002762 return other->AsLongConstant()->value_ == value_;
2763 }
2764
2765 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2766
2767 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002768 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2769 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002770 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2771
2772 DECLARE_INSTRUCTION(LongConstant);
2773
2774 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002775 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2776 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002777
2778 const int64_t value_;
2779
2780 friend class HGraph;
2781 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2782};
Dave Allison20dfc792014-06-16 20:44:29 -07002783
Vladimir Markofcb503c2016-05-18 12:48:17 +01002784class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002785 public:
2786 float GetValue() const { return value_; }
2787
2788 uint64_t GetValueAsUint64() const OVERRIDE {
2789 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2790 }
2791
Vladimir Marko372f10e2016-05-17 16:30:10 +01002792 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002793 DCHECK(other->IsFloatConstant()) << other->DebugName();
2794 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2795 }
2796
2797 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2798
2799 bool IsMinusOne() const OVERRIDE {
2800 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2801 }
Roland Levillain1a653882016-03-18 18:05:57 +00002802 bool IsArithmeticZero() const OVERRIDE {
2803 return std::fpclassify(value_) == FP_ZERO;
2804 }
2805 bool IsArithmeticPositiveZero() const {
2806 return IsArithmeticZero() && !std::signbit(value_);
2807 }
2808 bool IsArithmeticNegativeZero() const {
2809 return IsArithmeticZero() && std::signbit(value_);
2810 }
2811 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002812 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002813 }
2814 bool IsOne() const OVERRIDE {
2815 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2816 }
2817 bool IsNaN() const {
2818 return std::isnan(value_);
2819 }
2820
2821 DECLARE_INSTRUCTION(FloatConstant);
2822
2823 private:
2824 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2825 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2826 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2827 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2828
2829 const float value_;
2830
2831 // Only the SsaBuilder and HGraph can create floating-point constants.
2832 friend class SsaBuilder;
2833 friend class HGraph;
2834 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2835};
2836
Vladimir Markofcb503c2016-05-18 12:48:17 +01002837class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002838 public:
2839 double GetValue() const { return value_; }
2840
2841 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2842
Vladimir Marko372f10e2016-05-17 16:30:10 +01002843 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002844 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2845 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2846 }
2847
2848 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2849
2850 bool IsMinusOne() const OVERRIDE {
2851 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2852 }
Roland Levillain1a653882016-03-18 18:05:57 +00002853 bool IsArithmeticZero() const OVERRIDE {
2854 return std::fpclassify(value_) == FP_ZERO;
2855 }
2856 bool IsArithmeticPositiveZero() const {
2857 return IsArithmeticZero() && !std::signbit(value_);
2858 }
2859 bool IsArithmeticNegativeZero() const {
2860 return IsArithmeticZero() && std::signbit(value_);
2861 }
2862 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002863 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002864 }
2865 bool IsOne() const OVERRIDE {
2866 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2867 }
2868 bool IsNaN() const {
2869 return std::isnan(value_);
2870 }
2871
2872 DECLARE_INSTRUCTION(DoubleConstant);
2873
2874 private:
2875 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2876 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2877 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2878 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2879
2880 const double value_;
2881
2882 // Only the SsaBuilder and HGraph can create floating-point constants.
2883 friend class SsaBuilder;
2884 friend class HGraph;
2885 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2886};
2887
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002888// Conditional branch. A block ending with an HIf instruction must have
2889// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002890class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002892 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2893 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002894 SetRawInputAt(0, input);
2895 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002896
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002897 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002898
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002899 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002900 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002901 }
2902
2903 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002904 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002905 }
2906
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002907 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002908
2909 private:
2910 DISALLOW_COPY_AND_ASSIGN(HIf);
2911};
2912
David Brazdilfc6a86a2015-06-26 10:33:45 +00002913
2914// Abstract instruction which marks the beginning and/or end of a try block and
2915// links it to the respective exception handlers. Behaves the same as a Goto in
2916// non-exceptional control flow.
2917// Normal-flow successor is stored at index zero, exception handlers under
2918// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002919class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002920 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002922 kEntry,
2923 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002924 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002925 };
2926
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002927 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002928 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2929 SetPackedField<BoundaryKindField>(kind);
2930 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002931
2932 bool IsControlFlow() const OVERRIDE { return true; }
2933
2934 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002935 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002936
David Brazdild26a4112015-11-10 11:07:31 +00002937 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2938 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2939 }
2940
David Brazdilfc6a86a2015-06-26 10:33:45 +00002941 // Returns whether `handler` is among its exception handlers (non-zero index
2942 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002943 bool HasExceptionHandler(const HBasicBlock& handler) const {
2944 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002945 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002946 }
2947
2948 // If not present already, adds `handler` to its block's list of exception
2949 // handlers.
2950 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002951 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002952 GetBlock()->AddSuccessor(handler);
2953 }
2954 }
2955
Vladimir Markoa1de9182016-02-25 11:37:38 +00002956 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2957 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002958
David Brazdilffee3d32015-07-06 11:48:53 +01002959 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2960
David Brazdilfc6a86a2015-06-26 10:33:45 +00002961 DECLARE_INSTRUCTION(TryBoundary);
2962
2963 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002964 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2965 static constexpr size_t kFieldBoundaryKindSize =
2966 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2967 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2968 kFieldBoundaryKind + kFieldBoundaryKindSize;
2969 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2970 "Too many packed fields.");
2971 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002972
2973 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2974};
2975
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002976// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002977class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002978 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002979 enum class Kind {
2980 kBCE,
2981 kInline,
2982 kLast = kInline
2983 };
2984
2985 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2986 // across.
2987 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2988 : HVariableInputSizeInstruction(
2989 SideEffects::All(),
2990 dex_pc,
2991 arena,
2992 /* number_of_inputs */ 1,
2993 kArenaAllocMisc) {
2994 SetPackedFlag<kFieldCanBeMoved>(false);
2995 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002996 SetRawInputAt(0, cond);
2997 }
2998
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002999 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3000 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3001 // instead of `guard`.
3002 // We set CanTriggerGC to prevent any intermediate address to be live
3003 // at the point of the `HDeoptimize`.
3004 HDeoptimize(ArenaAllocator* arena,
3005 HInstruction* cond,
3006 HInstruction* guard,
3007 Kind kind,
3008 uint32_t dex_pc)
3009 : HVariableInputSizeInstruction(
3010 SideEffects::CanTriggerGC(),
3011 dex_pc,
3012 arena,
3013 /* number_of_inputs */ 2,
3014 kArenaAllocMisc) {
3015 SetPackedFlag<kFieldCanBeMoved>(true);
3016 SetPackedField<DeoptimizeKindField>(kind);
3017 SetRawInputAt(0, cond);
3018 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003019 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003020
3021 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3022
3023 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3024 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3025 }
3026
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003028
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003029 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003030
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003031 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3032
3033 Primitive::Type GetType() const OVERRIDE {
3034 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3035 }
3036
3037 bool GuardsAnInput() const {
3038 return InputCount() == 2;
3039 }
3040
3041 HInstruction* GuardedInput() const {
3042 DCHECK(GuardsAnInput());
3043 return InputAt(1);
3044 }
3045
3046 void RemoveGuard() {
3047 RemoveInputAt(1);
3048 }
3049
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003050 DECLARE_INSTRUCTION(Deoptimize);
3051
3052 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003053 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3054 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3055 static constexpr size_t kFieldDeoptimizeKindSize =
3056 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3057 static constexpr size_t kNumberOfDeoptimizePackedBits =
3058 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3059 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3060 "Too many packed fields.");
3061 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3062
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003063 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3064};
3065
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003066std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3067
Mingyao Yang063fc772016-08-02 11:02:54 -07003068// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3069// The compiled code checks this flag value in a guard before devirtualized call and
3070// if it's true, starts to do deoptimization.
3071// It has a 4-byte slot on stack.
3072// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003073class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003074 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003075 // CHA guards are only optimized in a separate pass and it has no side effects
3076 // with regard to other passes.
3077 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3078 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003079 }
3080
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003081 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3082
3083 // We do all CHA guard elimination/motion in a single pass, after which there is no
3084 // further guard elimination/motion since a guard might have been used for justification
3085 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3086 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003087 bool CanBeMoved() const OVERRIDE { return false; }
3088
3089 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3090
3091 private:
3092 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3093};
3094
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003095// Represents the ArtMethod that was passed as a first argument to
3096// the method. It is used by instructions that depend on it, like
3097// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003098class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003099 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003100 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3101 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003102
3103 DECLARE_INSTRUCTION(CurrentMethod);
3104
3105 private:
3106 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3107};
3108
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003109// Fetches an ArtMethod from the virtual table or the interface method table
3110// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003111class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003112 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003113 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003114 kVTable,
3115 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003116 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003117 };
3118 HClassTableGet(HInstruction* cls,
3119 Primitive::Type type,
3120 TableKind kind,
3121 size_t index,
3122 uint32_t dex_pc)
3123 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003124 index_(index) {
3125 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003126 SetRawInputAt(0, cls);
3127 }
3128
3129 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003130 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003131 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003132 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003133 }
3134
Vladimir Markoa1de9182016-02-25 11:37:38 +00003135 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003136 size_t GetIndex() const { return index_; }
3137
3138 DECLARE_INSTRUCTION(ClassTableGet);
3139
3140 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003141 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3142 static constexpr size_t kFieldTableKindSize =
3143 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3144 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3145 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3146 "Too many packed fields.");
3147 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3148
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003149 // The index of the ArtMethod in the table.
3150 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151
3152 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3153};
3154
Mark Mendellfe57faa2015-09-18 09:26:15 -04003155// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3156// have one successor for each entry in the switch table, and the final successor
3157// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003158class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003159 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003160 HPackedSwitch(int32_t start_value,
3161 uint32_t num_entries,
3162 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003163 uint32_t dex_pc = kNoDexPc)
3164 : HTemplateInstruction(SideEffects::None(), dex_pc),
3165 start_value_(start_value),
3166 num_entries_(num_entries) {
3167 SetRawInputAt(0, input);
3168 }
3169
3170 bool IsControlFlow() const OVERRIDE { return true; }
3171
3172 int32_t GetStartValue() const { return start_value_; }
3173
Vladimir Marko211c2112015-09-24 16:52:33 +01003174 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003175
3176 HBasicBlock* GetDefaultBlock() const {
3177 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003178 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003179 }
3180 DECLARE_INSTRUCTION(PackedSwitch);
3181
3182 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003183 const int32_t start_value_;
3184 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003185
3186 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3187};
3188
Roland Levillain88cb1752014-10-20 16:36:47 +01003189class HUnaryOperation : public HExpression<1> {
3190 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003191 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3192 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003193 SetRawInputAt(0, input);
3194 }
3195
3196 HInstruction* GetInput() const { return InputAt(0); }
3197 Primitive::Type GetResultType() const { return GetType(); }
3198
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003199 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003200 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003201 return true;
3202 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003203
Roland Levillain31dd3d62016-02-16 12:21:02 +00003204 // Try to statically evaluate `this` and return a HConstant
3205 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003206 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003207 HConstant* TryStaticEvaluation() const;
3208
3209 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003210 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3211 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003212 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3213 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003214
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003215 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003216
3217 private:
3218 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3219};
3220
Dave Allison20dfc792014-06-16 20:44:29 -07003221class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003222 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003223 HBinaryOperation(Primitive::Type result_type,
3224 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003225 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003226 SideEffects side_effects = SideEffects::None(),
3227 uint32_t dex_pc = kNoDexPc)
3228 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003229 SetRawInputAt(0, left);
3230 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003231 }
3232
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003233 HInstruction* GetLeft() const { return InputAt(0); }
3234 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003235 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003236
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003237 virtual bool IsCommutative() const { return false; }
3238
3239 // Put constant on the right.
3240 // Returns whether order is changed.
3241 bool OrderInputsWithConstantOnTheRight() {
3242 HInstruction* left = InputAt(0);
3243 HInstruction* right = InputAt(1);
3244 if (left->IsConstant() && !right->IsConstant()) {
3245 ReplaceInput(right, 0);
3246 ReplaceInput(left, 1);
3247 return true;
3248 }
3249 return false;
3250 }
3251
3252 // Order inputs by instruction id, but favor constant on the right side.
3253 // This helps GVN for commutative ops.
3254 void OrderInputs() {
3255 DCHECK(IsCommutative());
3256 HInstruction* left = InputAt(0);
3257 HInstruction* right = InputAt(1);
3258 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3259 return;
3260 }
3261 if (OrderInputsWithConstantOnTheRight()) {
3262 return;
3263 }
3264 // Order according to instruction id.
3265 if (left->GetId() > right->GetId()) {
3266 ReplaceInput(right, 0);
3267 ReplaceInput(left, 1);
3268 }
3269 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003270
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003271 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003272 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003273 return true;
3274 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003275
Roland Levillain31dd3d62016-02-16 12:21:02 +00003276 // Try to statically evaluate `this` and return a HConstant
3277 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003278 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003279 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003280
3281 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3283 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003284 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3285 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003286 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003287 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3288 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003289 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3290 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003291 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3292 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003293 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003294 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3295 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003296
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003297 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003298 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003299 HConstant* GetConstantRight() const;
3300
3301 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003302 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003303 HInstruction* GetLeastConstantLeft() const;
3304
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003305 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003306
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003307 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003308 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3309};
3310
Mark Mendellc4701932015-04-10 13:18:51 -04003311// The comparison bias applies for floating point operations and indicates how NaN
3312// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003313enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003314 kNoBias, // bias is not applicable (i.e. for long operation)
3315 kGtBias, // return 1 for NaN comparisons
3316 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003317 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003318};
3319
Roland Levillain31dd3d62016-02-16 12:21:02 +00003320std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3321
Dave Allison20dfc792014-06-16 20:44:29 -07003322class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003323 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003324 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003325 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3326 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3327 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003328
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003329 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003330 // `instruction`, and disregard moves in between.
3331 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003332
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003333 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003334
3335 virtual IfCondition GetCondition() const = 0;
3336
Mark Mendellc4701932015-04-10 13:18:51 -04003337 virtual IfCondition GetOppositeCondition() const = 0;
3338
Vladimir Markoa1de9182016-02-25 11:37:38 +00003339 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003340 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3341
Vladimir Markoa1de9182016-02-25 11:37:38 +00003342 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3343 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003344
Vladimir Marko372f10e2016-05-17 16:30:10 +01003345 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003346 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003347 }
3348
Roland Levillain4fa13f62015-07-06 18:11:54 +01003349 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003350 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003351 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003352 if (if_cond == kCondNE) {
3353 return true;
3354 } else if (if_cond == kCondEQ) {
3355 return false;
3356 }
3357 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003358 }
3359
3360 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003361 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003362 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003363 if (if_cond == kCondEQ) {
3364 return true;
3365 } else if (if_cond == kCondNE) {
3366 return false;
3367 }
3368 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003369 }
3370
Roland Levillain31dd3d62016-02-16 12:21:02 +00003371 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003372 // Needed if we merge a HCompare into a HCondition.
3373 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3374 static constexpr size_t kFieldComparisonBiasSize =
3375 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3376 static constexpr size_t kNumberOfConditionPackedBits =
3377 kFieldComparisonBias + kFieldComparisonBiasSize;
3378 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3379 using ComparisonBiasField =
3380 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3381
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 template <typename T>
3383 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3384
3385 template <typename T>
3386 int32_t CompareFP(T x, T y) const {
3387 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3388 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3389 // Handle the bias.
3390 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3391 }
3392
3393 // Return an integer constant containing the result of a condition evaluated at compile time.
3394 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3395 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3396 }
3397
Dave Allison20dfc792014-06-16 20:44:29 -07003398 private:
3399 DISALLOW_COPY_AND_ASSIGN(HCondition);
3400};
3401
3402// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003403class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003404 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003405 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3406 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003407
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003408 bool IsCommutative() const OVERRIDE { return true; }
3409
Vladimir Marko9e23df52015-11-10 17:14:35 +00003410 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3411 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003412 return MakeConstantCondition(true, GetDexPc());
3413 }
3414 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3415 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3416 }
3417 // In the following Evaluate methods, a HCompare instruction has
3418 // been merged into this HEqual instruction; evaluate it as
3419 // `Compare(x, y) == 0`.
3420 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3421 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3422 GetDexPc());
3423 }
3424 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3425 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3426 }
3427 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3428 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003429 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003430
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003431 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003433 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003434 return kCondEQ;
3435 }
3436
Mark Mendellc4701932015-04-10 13:18:51 -04003437 IfCondition GetOppositeCondition() const OVERRIDE {
3438 return kCondNE;
3439 }
3440
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003441 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003442 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003443
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003444 DISALLOW_COPY_AND_ASSIGN(HEqual);
3445};
3446
Vladimir Markofcb503c2016-05-18 12:48:17 +01003447class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003449 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3450 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003451
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003452 bool IsCommutative() const OVERRIDE { return true; }
3453
Vladimir Marko9e23df52015-11-10 17:14:35 +00003454 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3455 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 return MakeConstantCondition(false, GetDexPc());
3457 }
3458 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3459 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3460 }
3461 // In the following Evaluate methods, a HCompare instruction has
3462 // been merged into this HNotEqual instruction; evaluate it as
3463 // `Compare(x, y) != 0`.
3464 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3465 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3466 }
3467 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3468 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3469 }
3470 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3471 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003472 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003473
Dave Allison20dfc792014-06-16 20:44:29 -07003474 DECLARE_INSTRUCTION(NotEqual);
3475
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003476 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003477 return kCondNE;
3478 }
3479
Mark Mendellc4701932015-04-10 13:18:51 -04003480 IfCondition GetOppositeCondition() const OVERRIDE {
3481 return kCondEQ;
3482 }
3483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003485 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003486
Dave Allison20dfc792014-06-16 20:44:29 -07003487 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3488};
3489
Vladimir Markofcb503c2016-05-18 12:48:17 +01003490class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003491 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003492 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3493 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003494
Roland Levillain9867bc72015-08-05 10:21:34 +01003495 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003497 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 // In the following Evaluate methods, a HCompare instruction has
3499 // been merged into this HLessThan instruction; evaluate it as
3500 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003501 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003502 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3503 }
3504 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3505 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3506 }
3507 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3508 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003509 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003510
Dave Allison20dfc792014-06-16 20:44:29 -07003511 DECLARE_INSTRUCTION(LessThan);
3512
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003513 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003514 return kCondLT;
3515 }
3516
Mark Mendellc4701932015-04-10 13:18:51 -04003517 IfCondition GetOppositeCondition() const OVERRIDE {
3518 return kCondGE;
3519 }
3520
Dave Allison20dfc792014-06-16 20:44:29 -07003521 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003522 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003523
Dave Allison20dfc792014-06-16 20:44:29 -07003524 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3525};
3526
Vladimir Markofcb503c2016-05-18 12:48:17 +01003527class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003528 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003529 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3530 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003531
Roland Levillain9867bc72015-08-05 10:21:34 +01003532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003534 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 // In the following Evaluate methods, a HCompare instruction has
3536 // been merged into this HLessThanOrEqual instruction; evaluate it as
3537 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3540 }
3541 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3542 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3543 }
3544 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3545 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003546 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003547
Dave Allison20dfc792014-06-16 20:44:29 -07003548 DECLARE_INSTRUCTION(LessThanOrEqual);
3549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003550 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003551 return kCondLE;
3552 }
3553
Mark Mendellc4701932015-04-10 13:18:51 -04003554 IfCondition GetOppositeCondition() const OVERRIDE {
3555 return kCondGT;
3556 }
3557
Dave Allison20dfc792014-06-16 20:44:29 -07003558 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003559 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003560
Dave Allison20dfc792014-06-16 20:44:29 -07003561 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3562};
3563
Vladimir Markofcb503c2016-05-18 12:48:17 +01003564class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003565 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003566 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3567 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003568
Roland Levillain9867bc72015-08-05 10:21:34 +01003569 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003570 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003571 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 // In the following Evaluate methods, a HCompare instruction has
3573 // been merged into this HGreaterThan instruction; evaluate it as
3574 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3577 }
3578 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3579 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3580 }
3581 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3582 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003583 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003584
Dave Allison20dfc792014-06-16 20:44:29 -07003585 DECLARE_INSTRUCTION(GreaterThan);
3586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003587 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003588 return kCondGT;
3589 }
3590
Mark Mendellc4701932015-04-10 13:18:51 -04003591 IfCondition GetOppositeCondition() const OVERRIDE {
3592 return kCondLE;
3593 }
3594
Dave Allison20dfc792014-06-16 20:44:29 -07003595 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003596 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003597
Dave Allison20dfc792014-06-16 20:44:29 -07003598 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3599};
3600
Vladimir Markofcb503c2016-05-18 12:48:17 +01003601class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003602 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003603 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3604 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003605
Roland Levillain9867bc72015-08-05 10:21:34 +01003606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003608 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003609 // In the following Evaluate methods, a HCompare instruction has
3610 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3611 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003612 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3614 }
3615 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3616 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3617 }
3618 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3619 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003620 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003621
Dave Allison20dfc792014-06-16 20:44:29 -07003622 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3623
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003624 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003625 return kCondGE;
3626 }
3627
Mark Mendellc4701932015-04-10 13:18:51 -04003628 IfCondition GetOppositeCondition() const OVERRIDE {
3629 return kCondLT;
3630 }
3631
Dave Allison20dfc792014-06-16 20:44:29 -07003632 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003633 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003634
Dave Allison20dfc792014-06-16 20:44:29 -07003635 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3636};
3637
Vladimir Markofcb503c2016-05-18 12:48:17 +01003638class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003639 public:
3640 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3641 : HCondition(first, second, dex_pc) {}
3642
3643 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003644 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003645 }
3646 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003647 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3648 }
3649 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3650 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3651 LOG(FATAL) << DebugName() << " is not defined for float values";
3652 UNREACHABLE();
3653 }
3654 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3655 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3656 LOG(FATAL) << DebugName() << " is not defined for double values";
3657 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003658 }
3659
3660 DECLARE_INSTRUCTION(Below);
3661
3662 IfCondition GetCondition() const OVERRIDE {
3663 return kCondB;
3664 }
3665
3666 IfCondition GetOppositeCondition() const OVERRIDE {
3667 return kCondAE;
3668 }
3669
3670 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003671 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003672 return MakeUnsigned(x) < MakeUnsigned(y);
3673 }
Aart Bike9f37602015-10-09 11:15:55 -07003674
3675 DISALLOW_COPY_AND_ASSIGN(HBelow);
3676};
3677
Vladimir Markofcb503c2016-05-18 12:48:17 +01003678class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003679 public:
3680 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3681 : HCondition(first, second, dex_pc) {}
3682
3683 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003685 }
3686 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003687 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3688 }
3689 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3690 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3691 LOG(FATAL) << DebugName() << " is not defined for float values";
3692 UNREACHABLE();
3693 }
3694 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3695 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3696 LOG(FATAL) << DebugName() << " is not defined for double values";
3697 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003698 }
3699
3700 DECLARE_INSTRUCTION(BelowOrEqual);
3701
3702 IfCondition GetCondition() const OVERRIDE {
3703 return kCondBE;
3704 }
3705
3706 IfCondition GetOppositeCondition() const OVERRIDE {
3707 return kCondA;
3708 }
3709
3710 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003711 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003712 return MakeUnsigned(x) <= MakeUnsigned(y);
3713 }
Aart Bike9f37602015-10-09 11:15:55 -07003714
3715 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3716};
3717
Vladimir Markofcb503c2016-05-18 12:48:17 +01003718class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003719 public:
3720 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3721 : HCondition(first, second, dex_pc) {}
3722
3723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003725 }
3726 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3728 }
3729 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3730 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3731 LOG(FATAL) << DebugName() << " is not defined for float values";
3732 UNREACHABLE();
3733 }
3734 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3735 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3736 LOG(FATAL) << DebugName() << " is not defined for double values";
3737 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003738 }
3739
3740 DECLARE_INSTRUCTION(Above);
3741
3742 IfCondition GetCondition() const OVERRIDE {
3743 return kCondA;
3744 }
3745
3746 IfCondition GetOppositeCondition() const OVERRIDE {
3747 return kCondBE;
3748 }
3749
3750 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003751 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003752 return MakeUnsigned(x) > MakeUnsigned(y);
3753 }
Aart Bike9f37602015-10-09 11:15:55 -07003754
3755 DISALLOW_COPY_AND_ASSIGN(HAbove);
3756};
3757
Vladimir Markofcb503c2016-05-18 12:48:17 +01003758class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003759 public:
3760 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3761 : HCondition(first, second, dex_pc) {}
3762
3763 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003764 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003765 }
3766 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3768 }
3769 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3770 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3771 LOG(FATAL) << DebugName() << " is not defined for float values";
3772 UNREACHABLE();
3773 }
3774 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3775 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3776 LOG(FATAL) << DebugName() << " is not defined for double values";
3777 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003778 }
3779
3780 DECLARE_INSTRUCTION(AboveOrEqual);
3781
3782 IfCondition GetCondition() const OVERRIDE {
3783 return kCondAE;
3784 }
3785
3786 IfCondition GetOppositeCondition() const OVERRIDE {
3787 return kCondB;
3788 }
3789
3790 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003791 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003792 return MakeUnsigned(x) >= MakeUnsigned(y);
3793 }
Aart Bike9f37602015-10-09 11:15:55 -07003794
3795 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3796};
Dave Allison20dfc792014-06-16 20:44:29 -07003797
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003798// Instruction to check how two inputs compare to each other.
3799// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003800class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003801 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003802 // Note that `comparison_type` is the type of comparison performed
3803 // between the comparison's inputs, not the type of the instantiated
3804 // HCompare instruction (which is always Primitive::kPrimInt).
3805 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003806 HInstruction* first,
3807 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003808 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003809 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003810 : HBinaryOperation(Primitive::kPrimInt,
3811 first,
3812 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003813 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003814 dex_pc) {
3815 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003816 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3817 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003818 }
3819
Roland Levillain9867bc72015-08-05 10:21:34 +01003820 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003821 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3822
3823 template <typename T>
3824 int32_t ComputeFP(T x, T y) const {
3825 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3826 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3827 // Handle the bias.
3828 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3829 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003830
Roland Levillain9867bc72015-08-05 10:21:34 +01003831 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003832 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3833 // reach this code path when processing a freshly built HIR
3834 // graph. However HCompare integer instructions can be synthesized
3835 // by the instruction simplifier to implement IntegerCompare and
3836 // IntegerSignum intrinsics, so we have to handle this case.
3837 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003838 }
3839 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003840 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3841 }
3842 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3843 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3844 }
3845 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3846 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003847 }
3848
Vladimir Marko372f10e2016-05-17 16:30:10 +01003849 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003850 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003851 }
3852
Vladimir Markoa1de9182016-02-25 11:37:38 +00003853 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003854
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003856 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 bool IsGtBias() const {
3858 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003859 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003860 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003861
Roland Levillain1693a1f2016-03-15 14:57:31 +00003862 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3863 // Comparisons do not require a runtime call in any back end.
3864 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003865 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003866
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003867 DECLARE_INSTRUCTION(Compare);
3868
Roland Levillain31dd3d62016-02-16 12:21:02 +00003869 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003870 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3871 static constexpr size_t kFieldComparisonBiasSize =
3872 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3873 static constexpr size_t kNumberOfComparePackedBits =
3874 kFieldComparisonBias + kFieldComparisonBiasSize;
3875 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3876 using ComparisonBiasField =
3877 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3878
Roland Levillain31dd3d62016-02-16 12:21:02 +00003879 // Return an integer constant containing the result of a comparison evaluated at compile time.
3880 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3881 DCHECK(value == -1 || value == 0 || value == 1) << value;
3882 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3883 }
3884
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003885 private:
3886 DISALLOW_COPY_AND_ASSIGN(HCompare);
3887};
3888
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003889class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003890 public:
3891 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003892 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003893 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003894 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003895 bool finalizable,
3896 QuickEntrypointEnum entrypoint)
3897 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3898 type_index_(type_index),
3899 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003900 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003901 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003902 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003903 }
3904
Andreas Gampea5b09a62016-11-17 15:21:22 -08003905 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003906 const DexFile& GetDexFile() const { return dex_file_; }
3907
3908 // Calls runtime so needs an environment.
3909 bool NeedsEnvironment() const OVERRIDE { return true; }
3910
Mingyao Yang062157f2016-03-02 10:15:36 -08003911 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3912 bool CanThrow() const OVERRIDE { return true; }
3913
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003914 bool NeedsChecks() const {
3915 return entrypoint_ == kQuickAllocObjectWithChecks;
3916 }
David Brazdil6de19382016-01-08 17:37:10 +00003917
Vladimir Markoa1de9182016-02-25 11:37:38 +00003918 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003919
3920 bool CanBeNull() const OVERRIDE { return false; }
3921
3922 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3923
3924 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3925 entrypoint_ = entrypoint;
3926 }
3927
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003928 HLoadClass* GetLoadClass() const {
3929 HInstruction* input = InputAt(0);
3930 if (input->IsClinitCheck()) {
3931 input = input->InputAt(0);
3932 }
3933 DCHECK(input->IsLoadClass());
3934 return input->AsLoadClass();
3935 }
3936
David Brazdil6de19382016-01-08 17:37:10 +00003937 bool IsStringAlloc() const;
3938
3939 DECLARE_INSTRUCTION(NewInstance);
3940
3941 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003942 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003943 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3944 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3945 "Too many packed fields.");
3946
Andreas Gampea5b09a62016-11-17 15:21:22 -08003947 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003948 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003949 QuickEntrypointEnum entrypoint_;
3950
3951 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3952};
3953
Agi Csaki05f20562015-08-19 14:58:14 -07003954enum IntrinsicNeedsEnvironmentOrCache {
3955 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3956 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003957};
3958
Aart Bik5d75afe2015-12-14 11:57:01 -08003959enum IntrinsicSideEffects {
3960 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3961 kReadSideEffects, // Intrinsic may read heap memory.
3962 kWriteSideEffects, // Intrinsic may write heap memory.
3963 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3964};
3965
3966enum IntrinsicExceptions {
3967 kNoThrow, // Intrinsic does not throw any exceptions.
3968 kCanThrow // Intrinsic may throw exceptions.
3969};
3970
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003971class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003972 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003973 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003974
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003975 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003976 SetRawInputAt(index, argument);
3977 }
3978
Roland Levillain3e3d7332015-04-28 11:00:54 +01003979 // Return the number of arguments. This number can be lower than
3980 // the number of inputs returned by InputCount(), as some invoke
3981 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3982 // inputs at the end of their list of inputs.
3983 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3984
Vladimir Markoa1de9182016-02-25 11:37:38 +00003985 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003986
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003987 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3988
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003989 InvokeType GetInvokeType() const {
3990 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003991 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003992
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003993 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003994 return intrinsic_;
3995 }
3996
Aart Bik5d75afe2015-12-14 11:57:01 -08003997 void SetIntrinsic(Intrinsics intrinsic,
3998 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3999 IntrinsicSideEffects side_effects,
4000 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004001
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004002 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004003 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004004 }
4005
Aart Bikff7d89c2016-11-07 08:49:28 -08004006 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4007
Vladimir Markoa1de9182016-02-25 11:37:38 +00004008 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004009
Aart Bik71bf7b42016-11-16 10:17:46 -08004010 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004011
Vladimir Marko372f10e2016-05-17 16:30:10 +01004012 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004013 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4014 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004015
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004016 uint32_t* GetIntrinsicOptimizations() {
4017 return &intrinsic_optimizations_;
4018 }
4019
4020 const uint32_t* GetIntrinsicOptimizations() const {
4021 return &intrinsic_optimizations_;
4022 }
4023
4024 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4025
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004026 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004027 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004028
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004029 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004030
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004031 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004032 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4033 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004034 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4035 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004036 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004037 static constexpr size_t kFieldReturnTypeSize =
4038 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4039 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4040 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4041 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004042 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004043 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4044
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004045 HInvoke(ArenaAllocator* arena,
4046 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004047 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004048 Primitive::Type return_type,
4049 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004050 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 ArtMethod* resolved_method,
4052 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004053 : HVariableInputSizeInstruction(
4054 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4055 dex_pc,
4056 arena,
4057 number_of_arguments + number_of_other_inputs,
4058 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004059 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004060 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004061 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004062 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004063 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004065 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004066 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004067 }
4068
Roland Levillain3e3d7332015-04-28 11:00:54 +01004069 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004070 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004071 const uint32_t dex_method_index_;
4072 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004073
4074 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4075 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004076
4077 private:
4078 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4079};
4080
Vladimir Markofcb503c2016-05-18 12:48:17 +01004081class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004082 public:
4083 HInvokeUnresolved(ArenaAllocator* arena,
4084 uint32_t number_of_arguments,
4085 Primitive::Type return_type,
4086 uint32_t dex_pc,
4087 uint32_t dex_method_index,
4088 InvokeType invoke_type)
4089 : HInvoke(arena,
4090 number_of_arguments,
4091 0u /* number_of_other_inputs */,
4092 return_type,
4093 dex_pc,
4094 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004095 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004096 invoke_type) {
4097 }
4098
4099 DECLARE_INSTRUCTION(InvokeUnresolved);
4100
4101 private:
4102 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4103};
4104
Orion Hodsonac141392017-01-13 11:53:47 +00004105class HInvokePolymorphic FINAL : public HInvoke {
4106 public:
4107 HInvokePolymorphic(ArenaAllocator* arena,
4108 uint32_t number_of_arguments,
4109 Primitive::Type return_type,
4110 uint32_t dex_pc,
4111 uint32_t dex_method_index)
4112 : HInvoke(arena,
4113 number_of_arguments,
4114 0u /* number_of_other_inputs */,
4115 return_type,
4116 dex_pc,
4117 dex_method_index,
4118 nullptr,
4119 kVirtual) {}
4120
4121 DECLARE_INSTRUCTION(InvokePolymorphic);
4122
4123 private:
4124 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4125};
4126
Vladimir Markofcb503c2016-05-18 12:48:17 +01004127class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004128 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004129 // Requirements of this method call regarding the class
4130 // initialization (clinit) check of its declaring class.
4131 enum class ClinitCheckRequirement {
4132 kNone, // Class already initialized.
4133 kExplicit, // Static call having explicit clinit check as last input.
4134 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004135 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004136 };
4137
Vladimir Marko58155012015-08-19 12:49:41 +00004138 // Determines how to load the target ArtMethod*.
4139 enum class MethodLoadKind {
4140 // Use a String init ArtMethod* loaded from Thread entrypoints.
4141 kStringInit,
4142
4143 // Use the method's own ArtMethod* loaded by the register allocator.
4144 kRecursive,
4145
4146 // Use ArtMethod* at a known address, embed the direct address in the code.
4147 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4148 kDirectAddress,
4149
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004151 // Used when we need to use the dex cache, for example for invoke-static that
4152 // may cause class initialization (the entry may point to a resolution method),
4153 // and we know that we can access the dex cache arrays using a PC-relative load.
4154 kDexCachePcRelative,
4155
4156 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4157 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4158 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4159 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4160 kDexCacheViaMethod,
4161 };
4162
4163 // Determines the location of the code pointer.
4164 enum class CodePtrLocation {
4165 // Recursive call, use local PC-relative call instruction.
4166 kCallSelf,
4167
Vladimir Marko58155012015-08-19 12:49:41 +00004168 // Use code pointer from the ArtMethod*.
4169 // Used when we don't know the target code. This is also the last-resort-kind used when
4170 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4171 kCallArtMethod,
4172 };
4173
4174 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004175 MethodLoadKind method_load_kind;
4176 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004177 // The method load data holds
4178 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4179 // Note that there are multiple string init methods, each having its own offset.
4180 // - the method address for kDirectAddress
4181 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004182 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004183 };
4184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004185 HInvokeStaticOrDirect(ArenaAllocator* arena,
4186 uint32_t number_of_arguments,
4187 Primitive::Type return_type,
4188 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004189 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004190 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004191 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004192 InvokeType invoke_type,
4193 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004194 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004195 : HInvoke(arena,
4196 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004197 // There is potentially one extra argument for the HCurrentMethod node, and
4198 // potentially one other if the clinit check is explicit, and potentially
4199 // one other if the method is a string factory.
4200 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004201 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004202 return_type,
4203 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004204 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004205 resolved_method,
4206 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004207 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004208 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4210 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004211
Vladimir Markodc151b22015-10-15 18:02:30 +01004212 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004213 bool had_current_method_input = HasCurrentMethodInput();
4214 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4215
4216 // Using the current method is the default and once we find a better
4217 // method load kind, we should not go back to using the current method.
4218 DCHECK(had_current_method_input || !needs_current_method_input);
4219
4220 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004221 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4222 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004223 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004224 dispatch_info_ = dispatch_info;
4225 }
4226
Vladimir Markoc53c0792015-11-19 15:48:33 +00004227 void AddSpecialInput(HInstruction* input) {
4228 // We allow only one special input.
4229 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4230 DCHECK(InputCount() == GetSpecialInputIndex() ||
4231 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4232 InsertInputAt(GetSpecialInputIndex(), input);
4233 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004234
Vladimir Marko372f10e2016-05-17 16:30:10 +01004235 using HInstruction::GetInputRecords; // Keep the const version visible.
4236 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4237 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4238 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4239 DCHECK(!input_records.empty());
4240 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4241 HInstruction* last_input = input_records.back().GetInstruction();
4242 // Note: `last_input` may be null during arguments setup.
4243 if (last_input != nullptr) {
4244 // `last_input` is the last input of a static invoke marked as having
4245 // an explicit clinit check. It must either be:
4246 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4247 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4248 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4249 }
4250 }
4251 return input_records;
4252 }
4253
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004254 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004255 // We access the method via the dex cache so we can't do an implicit null check.
4256 // TODO: for intrinsics we can generate implicit null checks.
4257 return false;
4258 }
4259
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004260 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004261 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004262 }
4263
Vladimir Markoc53c0792015-11-19 15:48:33 +00004264 // Get the index of the special input, if any.
4265 //
David Brazdil6de19382016-01-08 17:37:10 +00004266 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4267 // method pointer; otherwise there may be one platform-specific special input,
4268 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004269 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004270 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004271
Vladimir Marko58155012015-08-19 12:49:41 +00004272 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4273 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4274 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004275 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004276 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004277 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004278 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004279 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4280 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004281 bool HasCurrentMethodInput() const {
4282 // This function can be called only after the invoke has been fully initialized by the builder.
4283 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004284 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004285 return true;
4286 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004287 DCHECK(InputCount() == GetSpecialInputIndex() ||
4288 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004289 return false;
4290 }
4291 }
Vladimir Marko58155012015-08-19 12:49:41 +00004292
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004293 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004294 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004295 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004296 }
4297
4298 uint64_t GetMethodAddress() const {
4299 DCHECK(HasMethodAddress());
4300 return dispatch_info_.method_load_data;
4301 }
4302
4303 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004304 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004305 return dispatch_info_.method_load_data;
4306 }
4307
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004308 const DexFile& GetDexFileForPcRelativeDexCache() const;
4309
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 ClinitCheckRequirement GetClinitCheckRequirement() const {
4311 return GetPackedField<ClinitCheckRequirementField>();
4312 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004313
Roland Levillain4c0eb422015-04-24 16:43:49 +01004314 // Is this instruction a call to a static method?
4315 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 return GetInvokeType() == kStatic;
4317 }
4318
4319 MethodReference GetTargetMethod() const {
4320 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004321 }
4322
Vladimir Markofbb184a2015-11-13 14:47:00 +00004323 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4324 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4325 // instruction; only relevant for static calls with explicit clinit check.
4326 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004327 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004328 size_t last_input_index = inputs_.size() - 1u;
4329 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004330 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004331 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004333 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004334 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004335 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004336 }
4337
4338 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004339 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004340 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004341 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004342 }
4343
4344 // Is this a call to a static method whose declaring class has an
4345 // implicit intialization check requirement?
4346 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004347 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004348 }
4349
Vladimir Markob554b5a2015-11-06 12:57:55 +00004350 // Does this method load kind need the current method as an input?
4351 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4352 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4353 }
4354
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004355 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004356
4357 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004359 static constexpr size_t kFieldClinitCheckRequirementSize =
4360 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4361 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4362 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4363 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4364 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004365 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4366 kFieldClinitCheckRequirement,
4367 kFieldClinitCheckRequirementSize>;
4368
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004370 MethodReference target_method_;
4371 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004373 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374};
Vladimir Markof64242a2015-12-01 14:58:23 +00004375std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004376std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Vladimir Markofcb503c2016-05-18 12:48:17 +01004378class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004379 public:
4380 HInvokeVirtual(ArenaAllocator* arena,
4381 uint32_t number_of_arguments,
4382 Primitive::Type return_type,
4383 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004384 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004385 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004386 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 : HInvoke(arena,
4388 number_of_arguments,
4389 0u,
4390 return_type,
4391 dex_pc,
4392 dex_method_index,
4393 resolved_method,
4394 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004395 vtable_index_(vtable_index) {}
4396
Aart Bik71bf7b42016-11-16 10:17:46 -08004397 bool CanBeNull() const OVERRIDE {
4398 switch (GetIntrinsic()) {
4399 case Intrinsics::kThreadCurrentThread:
4400 case Intrinsics::kStringBufferAppend:
4401 case Intrinsics::kStringBufferToString:
4402 case Intrinsics::kStringBuilderAppend:
4403 case Intrinsics::kStringBuilderToString:
4404 return false;
4405 default:
4406 return HInvoke::CanBeNull();
4407 }
4408 }
4409
Calin Juravle641547a2015-04-21 22:08:51 +01004410 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004411 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004412 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004415 uint32_t GetVTableIndex() const { return vtable_index_; }
4416
4417 DECLARE_INSTRUCTION(InvokeVirtual);
4418
4419 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004420 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004421 const uint32_t vtable_index_;
4422
4423 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4424};
4425
Vladimir Markofcb503c2016-05-18 12:48:17 +01004426class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004427 public:
4428 HInvokeInterface(ArenaAllocator* arena,
4429 uint32_t number_of_arguments,
4430 Primitive::Type return_type,
4431 uint32_t dex_pc,
4432 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004433 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004434 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 : HInvoke(arena,
4436 number_of_arguments,
4437 0u,
4438 return_type,
4439 dex_pc,
4440 dex_method_index,
4441 resolved_method,
4442 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004443 imt_index_(imt_index) {}
4444
Calin Juravle641547a2015-04-21 22:08:51 +01004445 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004447 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004448 }
4449
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004450 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4451 // The assembly stub currently needs it.
4452 return true;
4453 }
4454
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004455 uint32_t GetImtIndex() const { return imt_index_; }
4456 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4457
4458 DECLARE_INSTRUCTION(InvokeInterface);
4459
4460 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004461 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004462 const uint32_t imt_index_;
4463
4464 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4465};
4466
Vladimir Markofcb503c2016-05-18 12:48:17 +01004467class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004469 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004470 : HUnaryOperation(result_type, input, dex_pc) {
4471 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4472 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004473
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004474 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004475
4476 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004478 }
4479 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004480 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004482 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4483 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4484 }
4485 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4486 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4487 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004488
Roland Levillain88cb1752014-10-20 16:36:47 +01004489 DECLARE_INSTRUCTION(Neg);
4490
4491 private:
4492 DISALLOW_COPY_AND_ASSIGN(HNeg);
4493};
4494
Vladimir Markofcb503c2016-05-18 12:48:17 +01004495class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004496 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004497 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4498 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4499 SetRawInputAt(0, cls);
4500 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004501 }
4502
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004503 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004504 bool NeedsEnvironment() const OVERRIDE { return true; }
4505
Mingyao Yang0c365e62015-03-31 15:09:29 -07004506 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4507 bool CanThrow() const OVERRIDE { return true; }
4508
Calin Juravle10e244f2015-01-26 18:54:32 +00004509 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004510
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004511 HLoadClass* GetLoadClass() const {
4512 DCHECK(InputAt(0)->IsLoadClass());
4513 return InputAt(0)->AsLoadClass();
4514 }
4515
4516 HInstruction* GetLength() const {
4517 return InputAt(1);
4518 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004519
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004520 DECLARE_INSTRUCTION(NewArray);
4521
4522 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004523 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4524};
4525
Vladimir Markofcb503c2016-05-18 12:48:17 +01004526class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 HAdd(Primitive::Type result_type,
4529 HInstruction* left,
4530 HInstruction* right,
4531 uint32_t dex_pc = kNoDexPc)
4532 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004534 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004535
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004536 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004537
4538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 return GetBlock()->GetGraph()->GetIntConstant(
4540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004541 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 return GetBlock()->GetGraph()->GetLongConstant(
4544 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004545 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004546 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4547 return GetBlock()->GetGraph()->GetFloatConstant(
4548 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4549 }
4550 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4551 return GetBlock()->GetGraph()->GetDoubleConstant(
4552 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4553 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004554
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004555 DECLARE_INSTRUCTION(Add);
4556
4557 private:
4558 DISALLOW_COPY_AND_ASSIGN(HAdd);
4559};
4560
Vladimir Markofcb503c2016-05-18 12:48:17 +01004561class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004563 HSub(Primitive::Type result_type,
4564 HInstruction* left,
4565 HInstruction* right,
4566 uint32_t dex_pc = kNoDexPc)
4567 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004568
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004569 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004570
4571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004572 return GetBlock()->GetGraph()->GetIntConstant(
4573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 return GetBlock()->GetGraph()->GetLongConstant(
4577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004578 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4580 return GetBlock()->GetGraph()->GetFloatConstant(
4581 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4582 }
4583 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4584 return GetBlock()->GetGraph()->GetDoubleConstant(
4585 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4586 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004588 DECLARE_INSTRUCTION(Sub);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HSub);
4592};
4593
Vladimir Markofcb503c2016-05-18 12:48:17 +01004594class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004596 HMul(Primitive::Type result_type,
4597 HInstruction* left,
4598 HInstruction* right,
4599 uint32_t dex_pc = kNoDexPc)
4600 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004602 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004603
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004604 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004605
4606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 return GetBlock()->GetGraph()->GetIntConstant(
4608 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
4610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetLongConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4615 return GetBlock()->GetGraph()->GetFloatConstant(
4616 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4617 }
4618 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4619 return GetBlock()->GetGraph()->GetDoubleConstant(
4620 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4621 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004622
4623 DECLARE_INSTRUCTION(Mul);
4624
4625 private:
4626 DISALLOW_COPY_AND_ASSIGN(HMul);
4627};
4628
Vladimir Markofcb503c2016-05-18 12:48:17 +01004629class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 HDiv(Primitive::Type result_type,
4632 HInstruction* left,
4633 HInstruction* right,
4634 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004635 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004636
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004638 T ComputeIntegral(T x, T y) const {
4639 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 // Our graph structure ensures we never have 0 for `y` during
4641 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004642 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004643 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004644 return (y == -1) ? -x : x / y;
4645 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004646
Roland Levillain31dd3d62016-02-16 12:21:02 +00004647 template <typename T>
4648 T ComputeFP(T x, T y) const {
4649 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4650 return x / y;
4651 }
4652
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
4657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004659 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4660 }
4661 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4662 return GetBlock()->GetGraph()->GetFloatConstant(
4663 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4664 }
4665 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4666 return GetBlock()->GetGraph()->GetDoubleConstant(
4667 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004668 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004669
4670 DECLARE_INSTRUCTION(Div);
4671
4672 private:
4673 DISALLOW_COPY_AND_ASSIGN(HDiv);
4674};
4675
Vladimir Markofcb503c2016-05-18 12:48:17 +01004676class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 HRem(Primitive::Type result_type,
4679 HInstruction* left,
4680 HInstruction* right,
4681 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004682 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004683
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 T ComputeIntegral(T x, T y) const {
4686 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 // Our graph structure ensures we never have 0 for `y` during
4688 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004689 DCHECK_NE(y, 0);
4690 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4691 return (y == -1) ? 0 : x % y;
4692 }
4693
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 template <typename T>
4695 T ComputeFP(T x, T y) const {
4696 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4697 return std::fmod(x, y);
4698 }
4699
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004702 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 }
4704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004706 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004707 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004708 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4709 return GetBlock()->GetGraph()->GetFloatConstant(
4710 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4711 }
4712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4713 return GetBlock()->GetGraph()->GetDoubleConstant(
4714 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4715 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004716
4717 DECLARE_INSTRUCTION(Rem);
4718
4719 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004720 DISALLOW_COPY_AND_ASSIGN(HRem);
4721};
4722
Vladimir Markofcb503c2016-05-18 12:48:17 +01004723class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004724 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004725 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4726 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004727 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004728 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004729 SetRawInputAt(0, value);
4730 }
4731
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004732 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4733
Calin Juravled0d48522014-11-04 16:40:20 +00004734 bool CanBeMoved() const OVERRIDE { return true; }
4735
Vladimir Marko372f10e2016-05-17 16:30:10 +01004736 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004737 return true;
4738 }
4739
4740 bool NeedsEnvironment() const OVERRIDE { return true; }
4741 bool CanThrow() const OVERRIDE { return true; }
4742
Calin Juravled0d48522014-11-04 16:40:20 +00004743 DECLARE_INSTRUCTION(DivZeroCheck);
4744
4745 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004746 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4747};
4748
Vladimir Markofcb503c2016-05-18 12:48:17 +01004749class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 HInstruction* value,
4753 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4756 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4757 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004758 }
4759
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004761 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 return value << (distance & max_shift_distance);
4763 }
4764
4765 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004766 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4774 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4775 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4776 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4779 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004780 LOG(FATAL) << DebugName() << " is not defined for float values";
4781 UNREACHABLE();
4782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4784 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for double values";
4786 UNREACHABLE();
4787 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004788
4789 DECLARE_INSTRUCTION(Shl);
4790
4791 private:
4792 DISALLOW_COPY_AND_ASSIGN(HShl);
4793};
4794
Vladimir Markofcb503c2016-05-18 12:48:17 +01004795class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 HInstruction* value,
4799 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004800 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4802 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4803 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 }
4805
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004807 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 return value >> (distance & max_shift_distance);
4809 }
4810
4811 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004812 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004817 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4820 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4821 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4822 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4825 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004826 LOG(FATAL) << DebugName() << " is not defined for float values";
4827 UNREACHABLE();
4828 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004829 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4830 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 LOG(FATAL) << DebugName() << " is not defined for double values";
4832 UNREACHABLE();
4833 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004834
4835 DECLARE_INSTRUCTION(Shr);
4836
4837 private:
4838 DISALLOW_COPY_AND_ASSIGN(HShr);
4839};
4840
Vladimir Markofcb503c2016-05-18 12:48:17 +01004841class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 HInstruction* value,
4845 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004847 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4848 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4849 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004850 }
4851
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004853 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004854 typedef typename std::make_unsigned<T>::type V;
4855 V ux = static_cast<V>(value);
4856 return static_cast<T>(ux >> (distance & max_shift_distance));
4857 }
4858
4859 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004864 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004866 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4868 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4869 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4870 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004871 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4873 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 LOG(FATAL) << DebugName() << " is not defined for float values";
4875 UNREACHABLE();
4876 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4878 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 LOG(FATAL) << DebugName() << " is not defined for double values";
4880 UNREACHABLE();
4881 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004882
4883 DECLARE_INSTRUCTION(UShr);
4884
4885 private:
4886 DISALLOW_COPY_AND_ASSIGN(HUShr);
4887};
4888
Vladimir Markofcb503c2016-05-18 12:48:17 +01004889class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004890 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 HAnd(Primitive::Type result_type,
4892 HInstruction* left,
4893 HInstruction* right,
4894 uint32_t dex_pc = kNoDexPc)
4895 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004896
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004897 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004898
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004899 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004900
4901 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 return GetBlock()->GetGraph()->GetIntConstant(
4903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004904 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 return GetBlock()->GetGraph()->GetLongConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004909 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4910 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4911 LOG(FATAL) << DebugName() << " is not defined for float values";
4912 UNREACHABLE();
4913 }
4914 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4915 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for double values";
4917 UNREACHABLE();
4918 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004919
4920 DECLARE_INSTRUCTION(And);
4921
4922 private:
4923 DISALLOW_COPY_AND_ASSIGN(HAnd);
4924};
4925
Vladimir Markofcb503c2016-05-18 12:48:17 +01004926class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 HOr(Primitive::Type result_type,
4929 HInstruction* left,
4930 HInstruction* right,
4931 uint32_t dex_pc = kNoDexPc)
4932 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004933
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004934 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004935
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004936 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004937
4938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetIntConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004941 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 return GetBlock()->GetGraph()->GetLongConstant(
4944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004945 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004946 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4947 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4948 LOG(FATAL) << DebugName() << " is not defined for float values";
4949 UNREACHABLE();
4950 }
4951 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4952 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4953 LOG(FATAL) << DebugName() << " is not defined for double values";
4954 UNREACHABLE();
4955 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004956
4957 DECLARE_INSTRUCTION(Or);
4958
4959 private:
4960 DISALLOW_COPY_AND_ASSIGN(HOr);
4961};
4962
Vladimir Markofcb503c2016-05-18 12:48:17 +01004963class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004964 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004965 HXor(Primitive::Type result_type,
4966 HInstruction* left,
4967 HInstruction* right,
4968 uint32_t dex_pc = kNoDexPc)
4969 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004970
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004971 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004972
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004973 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004974
4975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 return GetBlock()->GetGraph()->GetIntConstant(
4977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetLongConstant(
4981 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4984 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4985 LOG(FATAL) << DebugName() << " is not defined for float values";
4986 UNREACHABLE();
4987 }
4988 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4989 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4990 LOG(FATAL) << DebugName() << " is not defined for double values";
4991 UNREACHABLE();
4992 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004993
4994 DECLARE_INSTRUCTION(Xor);
4995
4996 private:
4997 DISALLOW_COPY_AND_ASSIGN(HXor);
4998};
4999
Vladimir Markofcb503c2016-05-18 12:48:17 +01005000class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005001 public:
5002 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005003 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005004 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5005 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005006 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005007
Roland Levillain5b5b9312016-03-22 14:57:31 +00005008 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005009 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005010 typedef typename std::make_unsigned<T>::type V;
5011 V ux = static_cast<V>(value);
5012 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005013 return static_cast<T>(ux);
5014 } else {
5015 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005016 return static_cast<T>(ux >> (distance & max_shift_value)) |
5017 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005018 }
5019 }
5020
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005022 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005024 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005025 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005027 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005028 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5030 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5031 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5032 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5035 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 LOG(FATAL) << DebugName() << " is not defined for float values";
5037 UNREACHABLE();
5038 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5040 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 LOG(FATAL) << DebugName() << " is not defined for double values";
5042 UNREACHABLE();
5043 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005044
5045 DECLARE_INSTRUCTION(Ror);
5046
5047 private:
5048 DISALLOW_COPY_AND_ASSIGN(HRor);
5049};
5050
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005051// The value of a parameter in this method. Its location depends on
5052// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005053class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005054 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005055 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005056 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005057 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005058 Primitive::Type parameter_type,
5059 bool is_this = false)
5060 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005061 dex_file_(dex_file),
5062 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005063 index_(index) {
5064 SetPackedFlag<kFlagIsThis>(is_this);
5065 SetPackedFlag<kFlagCanBeNull>(!is_this);
5066 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005067
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005068 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005069 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005070 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005071 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072
Vladimir Markoa1de9182016-02-25 11:37:38 +00005073 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5074 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005076 DECLARE_INSTRUCTION(ParameterValue);
5077
5078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005079 // Whether or not the parameter value corresponds to 'this' argument.
5080 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5081 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5082 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5083 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5084 "Too many packed fields.");
5085
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005086 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005087 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005088 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005089 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005090 const uint8_t index_;
5091
5092 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5093};
5094
Vladimir Markofcb503c2016-05-18 12:48:17 +01005095class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5098 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005100 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005101 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005102 return true;
5103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005104
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005105 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005106
5107 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005108 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
5110 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005112 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005113 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5114 LOG(FATAL) << DebugName() << " is not defined for float values";
5115 UNREACHABLE();
5116 }
5117 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5118 LOG(FATAL) << DebugName() << " is not defined for double values";
5119 UNREACHABLE();
5120 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005121
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005122 DECLARE_INSTRUCTION(Not);
5123
5124 private:
5125 DISALLOW_COPY_AND_ASSIGN(HNot);
5126};
5127
Vladimir Markofcb503c2016-05-18 12:48:17 +01005128class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005129 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5131 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005132
5133 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005134 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005135 return true;
5136 }
5137
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005138 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005140 return !x;
5141 }
5142
Roland Levillain9867bc72015-08-05 10:21:34 +01005143 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 }
5146 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5147 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005148 UNREACHABLE();
5149 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005150 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5151 LOG(FATAL) << DebugName() << " is not defined for float values";
5152 UNREACHABLE();
5153 }
5154 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5155 LOG(FATAL) << DebugName() << " is not defined for double values";
5156 UNREACHABLE();
5157 }
David Brazdil66d126e2015-04-03 16:02:44 +01005158
5159 DECLARE_INSTRUCTION(BooleanNot);
5160
5161 private:
5162 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5163};
5164
Vladimir Markofcb503c2016-05-18 12:48:17 +01005165class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005166 public:
5167 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005168 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005169 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005170 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005171 // Invariant: We should never generate a conversion to a Boolean value.
5172 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005173 }
5174
5175 HInstruction* GetInput() const { return InputAt(0); }
5176 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5177 Primitive::Type GetResultType() const { return GetType(); }
5178
5179 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005180 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5181 return true;
5182 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005183
Mark Mendelle82549b2015-05-06 10:55:34 -04005184 // Try to statically evaluate the conversion and return a HConstant
5185 // containing the result. If the input cannot be converted, return nullptr.
5186 HConstant* TryStaticEvaluation() const;
5187
Roland Levillaindff1f282014-11-05 14:15:05 +00005188 DECLARE_INSTRUCTION(TypeConversion);
5189
5190 private:
5191 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5192};
5193
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005194static constexpr uint32_t kNoRegNumber = -1;
5195
Vladimir Markofcb503c2016-05-18 12:48:17 +01005196class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005197 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005198 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5199 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005201 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 SetRawInputAt(0, value);
5203 }
5204
Calin Juravle10e244f2015-01-26 18:54:32 +00005205 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005206 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005207 return true;
5208 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005209
Calin Juravle10e244f2015-01-26 18:54:32 +00005210 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211
Calin Juravle10e244f2015-01-26 18:54:32 +00005212 bool CanThrow() const OVERRIDE { return true; }
5213
5214 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005215
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216
5217 DECLARE_INSTRUCTION(NullCheck);
5218
5219 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005220 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5221};
5222
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005223// Embeds an ArtField and all the information required by the compiler. We cache
5224// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225class FieldInfo : public ValueObject {
5226 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005227 FieldInfo(ArtField* field,
5228 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005229 Primitive::Type field_type,
5230 bool is_volatile,
5231 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005232 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005233 const DexFile& dex_file)
5234 : field_(field),
5235 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005236 field_type_(field_type),
5237 is_volatile_(is_volatile),
5238 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005239 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005240 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005242 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005244 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005245 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005246 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005247 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005248 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249
5250 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005251 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005252 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005253 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005254 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005255 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005256 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005257 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258};
5259
Vladimir Markofcb503c2016-05-18 12:48:17 +01005260class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261 public:
5262 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005263 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005264 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005265 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005266 bool is_volatile,
5267 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005268 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005269 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005270 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005271 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005272 field_info_(field,
5273 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005274 field_type,
5275 is_volatile,
5276 field_idx,
5277 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005278 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005279 SetRawInputAt(0, value);
5280 }
5281
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005282 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005283
Vladimir Marko372f10e2016-05-17 16:30:10 +01005284 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5285 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005286 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005287 }
5288
Calin Juravle641547a2015-04-21 22:08:51 +01005289 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005290 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005291 }
5292
5293 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005294 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5295 }
5296
Calin Juravle52c48962014-12-16 17:02:57 +00005297 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005299 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005300 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005301
5302 DECLARE_INSTRUCTION(InstanceFieldGet);
5303
5304 private:
5305 const FieldInfo field_info_;
5306
5307 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5308};
5309
Vladimir Markofcb503c2016-05-18 12:48:17 +01005310class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005311 public:
5312 HInstanceFieldSet(HInstruction* object,
5313 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005314 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005315 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005316 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005317 bool is_volatile,
5318 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005319 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005320 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005321 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005322 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005323 field_info_(field,
5324 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005325 field_type,
5326 is_volatile,
5327 field_idx,
5328 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005329 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005330 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331 SetRawInputAt(0, object);
5332 SetRawInputAt(1, value);
5333 }
5334
Calin Juravle641547a2015-04-21 22:08:51 +01005335 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005336 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 }
5338
Calin Juravle52c48962014-12-16 17:02:57 +00005339 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005340 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005341 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005342 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005343 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005344 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5345 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347 DECLARE_INSTRUCTION(InstanceFieldSet);
5348
5349 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005350 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5351 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5352 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5353 "Too many packed fields.");
5354
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005355 const FieldInfo field_info_;
5356
5357 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5358};
5359
Vladimir Markofcb503c2016-05-18 12:48:17 +01005360class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005362 HArrayGet(HInstruction* array,
5363 HInstruction* index,
5364 Primitive::Type type,
5365 uint32_t dex_pc,
5366 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005367 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005368 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369 SetRawInputAt(0, array);
5370 SetRawInputAt(1, index);
5371 }
5372
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005373 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005374 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005375 return true;
5376 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005377 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005378 // TODO: We can be smarter here.
5379 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5380 // which generates the implicit null check. There are cases when these can be removed
5381 // to produce better code. If we ever add optimizations to do so we should allow an
5382 // implicit check here (as long as the address falls in the first page).
5383 return false;
5384 }
5385
David Brazdil4833f5a2015-12-16 10:37:39 +00005386 bool IsEquivalentOf(HArrayGet* other) const {
5387 bool result = (GetDexPc() == other->GetDexPc());
5388 if (kIsDebugBuild && result) {
5389 DCHECK_EQ(GetBlock(), other->GetBlock());
5390 DCHECK_EQ(GetArray(), other->GetArray());
5391 DCHECK_EQ(GetIndex(), other->GetIndex());
5392 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005393 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005394 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005395 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5396 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005397 }
5398 }
5399 return result;
5400 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005401
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005402 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5403
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005404 HInstruction* GetArray() const { return InputAt(0); }
5405 HInstruction* GetIndex() const { return InputAt(1); }
5406
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 DECLARE_INSTRUCTION(ArrayGet);
5408
5409 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005410 // We treat a String as an array, creating the HArrayGet from String.charAt()
5411 // intrinsic in the instruction simplifier. We can always determine whether
5412 // a particular HArrayGet is actually a String.charAt() by looking at the type
5413 // of the input but that requires holding the mutator lock, so we prefer to use
5414 // a flag, so that code generators don't need to do the locking.
5415 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5416 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5417 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5418 "Too many packed fields.");
5419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5421};
5422
Vladimir Markofcb503c2016-05-18 12:48:17 +01005423class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 public:
5425 HArraySet(HInstruction* array,
5426 HInstruction* index,
5427 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005428 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005429 uint32_t dex_pc)
5430 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005431 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5432 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5433 SetPackedFlag<kFlagValueCanBeNull>(true);
5434 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 SetRawInputAt(0, array);
5436 SetRawInputAt(1, index);
5437 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005438 // Make a best guess now, may be refined during SSA building.
5439 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 }
5441
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005443 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005444 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 }
5446
Mingyao Yang81014cb2015-06-02 03:16:27 -07005447 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005448 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005449
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005450 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005451 // TODO: Same as for ArrayGet.
5452 return false;
5453 }
5454
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005455 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005457 }
5458
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005460 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005461 }
5462
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005463 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005465 }
5466
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5468 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5469 bool StaticTypeOfArrayIsObjectArray() const {
5470 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5471 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005472
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005473 HInstruction* GetArray() const { return InputAt(0); }
5474 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005475 HInstruction* GetValue() const { return InputAt(2); }
5476
5477 Primitive::Type GetComponentType() const {
5478 // The Dex format does not type floating point index operations. Since the
5479 // `expected_component_type_` is set during building and can therefore not
5480 // be correct, we also check what is the value type. If it is a floating
5481 // point type, we must use that type.
5482 Primitive::Type value_type = GetValue()->GetType();
5483 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5484 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005485 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005486 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005487
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005488 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005490 }
5491
Aart Bik18b36ab2016-04-13 16:41:35 -07005492 void ComputeSideEffects() {
5493 Primitive::Type type = GetComponentType();
5494 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5495 SideEffectsForArchRuntimeCalls(type)));
5496 }
5497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005498 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5499 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5500 }
5501
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005502 DECLARE_INSTRUCTION(ArraySet);
5503
5504 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5506 static constexpr size_t kFieldExpectedComponentTypeSize =
5507 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5508 static constexpr size_t kFlagNeedsTypeCheck =
5509 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5510 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005511 // Cached information for the reference_type_info_ so that codegen
5512 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5514 static constexpr size_t kNumberOfArraySetPackedBits =
5515 kFlagStaticTypeOfArrayIsObjectArray + 1;
5516 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5517 using ExpectedComponentTypeField =
5518 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519
5520 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5521};
5522
Vladimir Markofcb503c2016-05-18 12:48:17 +01005523class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005525 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005527 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005528 // Note that arrays do not change length, so the instruction does not
5529 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530 SetRawInputAt(0, array);
5531 }
5532
Calin Juravle77520bc2015-01-12 18:45:46 +00005533 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005534 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005535 return true;
5536 }
Calin Juravle641547a2015-04-21 22:08:51 +01005537 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5538 return obj == InputAt(0);
5539 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005540
Vladimir Markodce016e2016-04-28 13:10:02 +01005541 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 DECLARE_INSTRUCTION(ArrayLength);
5544
5545 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005546 // We treat a String as an array, creating the HArrayLength from String.length()
5547 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5548 // determine whether a particular HArrayLength is actually a String.length() by
5549 // looking at the type of the input but that requires holding the mutator lock, so
5550 // we prefer to use a flag, so that code generators don't need to do the locking.
5551 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5552 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5553 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5554 "Too many packed fields.");
5555
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5557};
5558
Vladimir Markofcb503c2016-05-18 12:48:17 +01005559class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005560 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005561 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5562 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005563 HBoundsCheck(HInstruction* index,
5564 HInstruction* length,
5565 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005566 bool string_char_at = false)
5567 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005568 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005569 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570 SetRawInputAt(0, index);
5571 SetRawInputAt(1, length);
5572 }
5573
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005574 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005575 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005576 return true;
5577 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005578
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005579 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005581 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005582
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005583 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005584
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005585 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586
5587 DECLARE_INSTRUCTION(BoundsCheck);
5588
5589 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005590 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5593};
5594
Vladimir Markofcb503c2016-05-18 12:48:17 +01005595class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005596 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005597 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005598 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005600 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005601 return true;
5602 }
5603
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005604 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5605 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005606
5607 DECLARE_INSTRUCTION(SuspendCheck);
5608
5609 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005610 // Only used for code generation, in order to share the same slow path between back edges
5611 // of a same loop.
5612 SlowPathCode* slow_path_;
5613
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005614 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5615};
5616
David Srbecky0cf44932015-12-09 14:09:59 +00005617// Pseudo-instruction which provides the native debugger with mapping information.
5618// It ensures that we can generate line number and local variables at this point.
5619class HNativeDebugInfo : public HTemplateInstruction<0> {
5620 public:
5621 explicit HNativeDebugInfo(uint32_t dex_pc)
5622 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5623
5624 bool NeedsEnvironment() const OVERRIDE {
5625 return true;
5626 }
5627
5628 DECLARE_INSTRUCTION(NativeDebugInfo);
5629
5630 private:
5631 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5632};
5633
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005634/**
5635 * Instruction to load a Class object.
5636 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005638 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005639 // Determines how to load the Class.
5640 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005641 // We cannot load this class. See HSharpening::SharpenLoadClass.
5642 kInvalid = -1,
5643
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005644 // Use the Class* from the method's own ArtMethod*.
5645 kReferrersClass,
5646
5647 // Use boot image Class* address that will be known at link time.
5648 // Used for boot image classes referenced by boot image code in non-PIC mode.
5649 kBootImageLinkTimeAddress,
5650
5651 // Use PC-relative boot image Class* address that will be known at link time.
5652 // Used for boot image classes referenced by boot image code in PIC mode.
5653 kBootImageLinkTimePcRelative,
5654
5655 // Use a known boot image Class* address, embedded in the code by the codegen.
5656 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005657 kBootImageAddress,
5658
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005659 // Load from an entry in the .bss section using a PC-relative load.
5660 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5661 kBssEntry,
5662
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005663 // Load from the root table associated with the JIT compiled method.
5664 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666 // Load from resolved types array accessed through the class loaded from
5667 // the compiled method's own ArtMethod*. This is the default access type when
5668 // all other types are unavailable.
5669 kDexCacheViaMethod,
5670
5671 kLast = kDexCacheViaMethod
5672 };
5673
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005674 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005675 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005676 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005677 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005679 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005680 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005681 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5682 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005683 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005684 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005685 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005686 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005687 // Referrers class should not need access check. We never inline unverified
5688 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005689 DCHECK(!is_referrers_class || !needs_access_check);
5690
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 SetPackedField<LoadKindField>(
5692 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005693 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005694 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 }
5697
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005698 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005699
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 LoadKind GetLoadKind() const {
5701 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005702 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005703
5704 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005705
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005707
Andreas Gampea5b09a62016-11-17 15:21:22 -08005708 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005709
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005710 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005712 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005713 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005714 }
5715
Calin Juravle0ba218d2015-05-19 18:46:01 +01005716 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005717 // The entrypoint the code generator is going to call does not do
5718 // clinit of the class.
5719 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005721 }
5722
5723 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005724 return NeedsAccessCheck() ||
5725 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005726 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5727 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005728 }
5729
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005730 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005731 return NeedsAccessCheck() ||
5732 MustGenerateClinitCheck() ||
5733 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5734 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5735 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005736 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5737 GetLoadKind() == LoadKind::kBssEntry) &&
5738 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005739 }
5740
Calin Juravleacf735c2015-02-12 15:25:22 +00005741 ReferenceTypeInfo GetLoadedClassRTI() {
5742 return loaded_class_rti_;
5743 }
5744
5745 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5746 // Make sure we only set exact types (the loaded class should never be merged).
5747 DCHECK(rti.IsExact());
5748 loaded_class_rti_ = rti;
5749 }
5750
Andreas Gampea5b09a62016-11-17 15:21:22 -08005751 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005752 const DexFile& GetDexFile() const { return dex_file_; }
5753
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005754 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005755 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005756 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005757
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005758 static SideEffects SideEffectsForArchRuntimeCalls() {
5759 return SideEffects::CanTriggerGC();
5760 }
5761
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005762 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005763 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005764 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005766
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005767 void MarkInBootImage() {
5768 SetPackedFlag<kFlagIsInBootImage>(true);
5769 }
5770
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 void AddSpecialInput(HInstruction* special_input);
5772
5773 using HInstruction::GetInputRecords; // Keep the const version visible.
5774 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5775 return ArrayRef<HUserRecord<HInstruction*>>(
5776 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5777 }
5778
5779 Primitive::Type GetType() const OVERRIDE {
5780 return Primitive::kPrimNot;
5781 }
5782
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005783 Handle<mirror::Class> GetClass() const {
5784 return klass_;
5785 }
5786
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005787 DECLARE_INSTRUCTION(LoadClass);
5788
5789 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005791 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005792 // Whether this instruction must generate the initialization check.
5793 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005794 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005795 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5796 static constexpr size_t kFieldLoadKindSize =
5797 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5798 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005799 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5801
5802 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005803 return load_kind == LoadKind::kReferrersClass ||
5804 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005806 load_kind == LoadKind::kBssEntry ||
5807 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 }
5809
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005810 void SetLoadKindInternal(LoadKind load_kind);
5811
5812 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5813 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005814 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005816
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005817 // A type index and dex file where the class can be accessed. The dex file can be:
5818 // - The compiling method's dex file if the class is defined there too.
5819 // - The compiling method's dex file if the class is referenced there.
5820 // - The dex file where the class is defined. When the load kind can only be
5821 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005822 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005823 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005824
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005825 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826
Calin Juravleacf735c2015-02-12 15:25:22 +00005827 ReferenceTypeInfo loaded_class_rti_;
5828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005829 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5830};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5832
5833// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005835 // The special input is used for PC-relative loads on some architectures,
5836 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005837 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005838 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005839 GetLoadKind() == LoadKind::kBootImageAddress ||
5840 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005841 DCHECK(special_input_.GetInstruction() == nullptr);
5842 special_input_ = HUserRecord<HInstruction*>(special_input);
5843 special_input->AddUseAt(this, 0);
5844}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005845
Vladimir Marko372f10e2016-05-17 16:30:10 +01005846class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005848 // Determines how to load the String.
5849 enum class LoadKind {
5850 // Use boot image String* address that will be known at link time.
5851 // Used for boot image strings referenced by boot image code in non-PIC mode.
5852 kBootImageLinkTimeAddress,
5853
5854 // Use PC-relative boot image String* address that will be known at link time.
5855 // Used for boot image strings referenced by boot image code in PIC mode.
5856 kBootImageLinkTimePcRelative,
5857
5858 // Use a known boot image String* address, embedded in the code by the codegen.
5859 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 kBootImageAddress,
5861
Vladimir Markoaad75c62016-10-03 08:46:48 +00005862 // Load from an entry in the .bss section using a PC-relative load.
5863 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5864 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005866 // Load from the root table associated with the JIT compiled method.
5867 kJitTableAddress,
5868
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005869 // Load from resolved strings array accessed through the class loaded from
5870 // the compiled method's own ArtMethod*. This is the default access type when
5871 // all other types are unavailable.
5872 kDexCacheViaMethod,
5873
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005874 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875 };
5876
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005877 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005878 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 const DexFile& dex_file,
5880 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005881 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5882 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005883 string_index_(string_index),
5884 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005886 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005887
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005888 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 LoadKind GetLoadKind() const {
5891 return GetPackedField<LoadKindField>();
5892 }
5893
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005894 const DexFile& GetDexFile() const {
5895 return dex_file_;
5896 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005898 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005899 return string_index_;
5900 }
5901
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 Handle<mirror::String> GetString() const {
5903 return string_;
5904 }
5905
5906 void SetString(Handle<mirror::String> str) {
5907 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908 }
5909
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005910 bool CanBeMoved() const OVERRIDE { return true; }
5911
Vladimir Marko372f10e2016-05-17 16:30:10 +01005912 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005913
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005914 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005915
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 // Will call the runtime if we need to load the string through
5917 // the dex cache and the string is not guaranteed to be there yet.
5918 bool NeedsEnvironment() const OVERRIDE {
5919 LoadKind load_kind = GetLoadKind();
5920 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5921 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005922 load_kind == LoadKind::kBootImageAddress ||
5923 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 return false;
5925 }
Vladimir Marko4d1be492017-01-06 14:43:11 +00005926 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005928
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5930 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5931 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005932
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005933 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005935
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005936 static SideEffects SideEffectsForArchRuntimeCalls() {
5937 return SideEffects::CanTriggerGC();
5938 }
5939
Vladimir Marko372f10e2016-05-17 16:30:10 +01005940 void AddSpecialInput(HInstruction* special_input);
5941
5942 using HInstruction::GetInputRecords; // Keep the const version visible.
5943 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5944 return ArrayRef<HUserRecord<HInstruction*>>(
5945 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005946 }
5947
Vladimir Marko372f10e2016-05-17 16:30:10 +01005948 Primitive::Type GetType() const OVERRIDE {
5949 return Primitive::kPrimNot;
5950 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005951
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005952 DECLARE_INSTRUCTION(LoadString);
5953
5954 private:
Vladimir Marko4d1be492017-01-06 14:43:11 +00005955 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956 static constexpr size_t kFieldLoadKindSize =
5957 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5958 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005962 void SetLoadKindInternal(LoadKind load_kind);
5963
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005964 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5965 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005966 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005967 HUserRecord<HInstruction*> special_input_;
5968
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005969 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005970 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005972 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005973
5974 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5975};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5977
5978// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005980 // The special input is used for PC-relative loads on some architectures,
5981 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005983 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005984 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5985 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005986 // HLoadString::GetInputRecords() returns an empty array at this point,
5987 // so use the GetInputRecords() from the base class to set the input record.
5988 DCHECK(special_input_.GetInstruction() == nullptr);
5989 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 special_input->AddUseAt(this, 0);
5991}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005992
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005993/**
5994 * Performs an initialization check on its Class object input.
5995 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005996class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005998 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005999 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006000 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006001 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6002 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003 SetRawInputAt(0, constant);
6004 }
6005
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006006 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006007 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006008 return true;
6009 }
6010
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 bool NeedsEnvironment() const OVERRIDE {
6012 // May call runtime to initialize the class.
6013 return true;
6014 }
6015
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006016 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006018 HLoadClass* GetLoadClass() const {
6019 DCHECK(InputAt(0)->IsLoadClass());
6020 return InputAt(0)->AsLoadClass();
6021 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022
6023 DECLARE_INSTRUCTION(ClinitCheck);
6024
6025 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6027};
6028
Vladimir Markofcb503c2016-05-18 12:48:17 +01006029class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 public:
6031 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006032 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006033 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006034 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006035 bool is_volatile,
6036 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006037 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006038 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006039 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006040 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006041 field_info_(field,
6042 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006043 field_type,
6044 is_volatile,
6045 field_idx,
6046 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006047 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006048 SetRawInputAt(0, cls);
6049 }
6050
Calin Juravle52c48962014-12-16 17:02:57 +00006051
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006052 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006053
Vladimir Marko372f10e2016-05-17 16:30:10 +01006054 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6055 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006056 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057 }
6058
6059 size_t ComputeHashCode() const OVERRIDE {
6060 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6061 }
6062
Calin Juravle52c48962014-12-16 17:02:57 +00006063 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6065 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006066 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067
6068 DECLARE_INSTRUCTION(StaticFieldGet);
6069
6070 private:
6071 const FieldInfo field_info_;
6072
6073 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6074};
6075
Vladimir Markofcb503c2016-05-18 12:48:17 +01006076class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006077 public:
6078 HStaticFieldSet(HInstruction* cls,
6079 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006080 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006083 bool is_volatile,
6084 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006085 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006086 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006087 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006088 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006089 field_info_(field,
6090 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006091 field_type,
6092 is_volatile,
6093 field_idx,
6094 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006095 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006097 SetRawInputAt(0, cls);
6098 SetRawInputAt(1, value);
6099 }
6100
Calin Juravle52c48962014-12-16 17:02:57 +00006101 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6103 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006104 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006106 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6108 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006109
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110 DECLARE_INSTRUCTION(StaticFieldSet);
6111
6112 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006113 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6114 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6115 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6116 "Too many packed fields.");
6117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006118 const FieldInfo field_info_;
6119
6120 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6121};
6122
Vladimir Markofcb503c2016-05-18 12:48:17 +01006123class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006124 public:
6125 HUnresolvedInstanceFieldGet(HInstruction* obj,
6126 Primitive::Type field_type,
6127 uint32_t field_index,
6128 uint32_t dex_pc)
6129 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6130 field_index_(field_index) {
6131 SetRawInputAt(0, obj);
6132 }
6133
6134 bool NeedsEnvironment() const OVERRIDE { return true; }
6135 bool CanThrow() const OVERRIDE { return true; }
6136
6137 Primitive::Type GetFieldType() const { return GetType(); }
6138 uint32_t GetFieldIndex() const { return field_index_; }
6139
6140 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6141
6142 private:
6143 const uint32_t field_index_;
6144
6145 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6146};
6147
Vladimir Markofcb503c2016-05-18 12:48:17 +01006148class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006149 public:
6150 HUnresolvedInstanceFieldSet(HInstruction* obj,
6151 HInstruction* value,
6152 Primitive::Type field_type,
6153 uint32_t field_index,
6154 uint32_t dex_pc)
6155 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006156 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006158 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006159 SetRawInputAt(0, obj);
6160 SetRawInputAt(1, value);
6161 }
6162
6163 bool NeedsEnvironment() const OVERRIDE { return true; }
6164 bool CanThrow() const OVERRIDE { return true; }
6165
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 uint32_t GetFieldIndex() const { return field_index_; }
6168
6169 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6170
6171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6173 static constexpr size_t kFieldFieldTypeSize =
6174 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6175 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6176 kFieldFieldType + kFieldFieldTypeSize;
6177 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6178 "Too many packed fields.");
6179 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6180
Calin Juravlee460d1d2015-09-29 04:52:17 +01006181 const uint32_t field_index_;
6182
6183 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6184};
6185
Vladimir Markofcb503c2016-05-18 12:48:17 +01006186class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006187 public:
6188 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6189 uint32_t field_index,
6190 uint32_t dex_pc)
6191 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6192 field_index_(field_index) {
6193 }
6194
6195 bool NeedsEnvironment() const OVERRIDE { return true; }
6196 bool CanThrow() const OVERRIDE { return true; }
6197
6198 Primitive::Type GetFieldType() const { return GetType(); }
6199 uint32_t GetFieldIndex() const { return field_index_; }
6200
6201 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6202
6203 private:
6204 const uint32_t field_index_;
6205
6206 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6207};
6208
Vladimir Markofcb503c2016-05-18 12:48:17 +01006209class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006210 public:
6211 HUnresolvedStaticFieldSet(HInstruction* value,
6212 Primitive::Type field_type,
6213 uint32_t field_index,
6214 uint32_t dex_pc)
6215 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006216 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006217 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006218 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006219 SetRawInputAt(0, value);
6220 }
6221
6222 bool NeedsEnvironment() const OVERRIDE { return true; }
6223 bool CanThrow() const OVERRIDE { return true; }
6224
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006226 uint32_t GetFieldIndex() const { return field_index_; }
6227
6228 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6229
6230 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6232 static constexpr size_t kFieldFieldTypeSize =
6233 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6234 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6235 kFieldFieldType + kFieldFieldTypeSize;
6236 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6237 "Too many packed fields.");
6238 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6239
Calin Juravlee460d1d2015-09-29 04:52:17 +01006240 const uint32_t field_index_;
6241
6242 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6243};
6244
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006245// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006246class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006247 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006248 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6249 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250
David Brazdilbbd733e2015-08-18 17:48:17 +01006251 bool CanBeNull() const OVERRIDE { return false; }
6252
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006253 DECLARE_INSTRUCTION(LoadException);
6254
6255 private:
6256 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6257};
6258
David Brazdilcb1c0552015-08-04 16:22:25 +01006259// Implicit part of move-exception which clears thread-local exception storage.
6260// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006261class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006263 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6264 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006265
6266 DECLARE_INSTRUCTION(ClearException);
6267
6268 private:
6269 DISALLOW_COPY_AND_ASSIGN(HClearException);
6270};
6271
Vladimir Markofcb503c2016-05-18 12:48:17 +01006272class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006273 public:
6274 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006275 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006276 SetRawInputAt(0, exception);
6277 }
6278
6279 bool IsControlFlow() const OVERRIDE { return true; }
6280
6281 bool NeedsEnvironment() const OVERRIDE { return true; }
6282
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006283 bool CanThrow() const OVERRIDE { return true; }
6284
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006285
6286 DECLARE_INSTRUCTION(Throw);
6287
6288 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006289 DISALLOW_COPY_AND_ASSIGN(HThrow);
6290};
6291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292/**
6293 * Implementation strategies for the code generator of a HInstanceOf
6294 * or `HCheckCast`.
6295 */
6296enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006297 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 kExactCheck, // Can do a single class compare.
6299 kClassHierarchyCheck, // Can just walk the super class chain.
6300 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6301 kInterfaceCheck, // No optimization yet when checking against an interface.
6302 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 kArrayCheck, // No optimization yet when checking against a generic array.
6304 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305};
6306
Roland Levillain86503782016-02-11 19:07:30 +00006307std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6308
Vladimir Markofcb503c2016-05-18 12:48:17 +01006309class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006311 HInstanceOf(HInstruction* object,
6312 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006314 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006315 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 dex_pc) {
6318 SetPackedField<TypeCheckKindField>(check_kind);
6319 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006320 SetRawInputAt(0, object);
6321 SetRawInputAt(1, constant);
6322 }
6323
6324 bool CanBeMoved() const OVERRIDE { return true; }
6325
Vladimir Marko372f10e2016-05-17 16:30:10 +01006326 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006327 return true;
6328 }
6329
6330 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006331 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006332 }
6333
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006334 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6336 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6337 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6338 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006339
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006340 static bool CanCallRuntime(TypeCheckKind check_kind) {
6341 // Mips currently does runtime calls for any other checks.
6342 return check_kind != TypeCheckKind::kExactCheck;
6343 }
6344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006346 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006347 }
6348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006349 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006350
6351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006352 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6353 static constexpr size_t kFieldTypeCheckKindSize =
6354 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6355 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6356 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6357 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6358 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6361};
6362
Vladimir Markofcb503c2016-05-18 12:48:17 +01006363class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006364 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006365 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006366 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6368 SetPackedFlag<kFlagUpperCanBeNull>(true);
6369 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006370 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006371 SetRawInputAt(0, input);
6372 }
6373
David Brazdilf5552582015-12-27 13:36:12 +00006374 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006375 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006376 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006377 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006378
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006379 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006380 DCHECK(GetUpperCanBeNull() || !can_be_null);
6381 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006382 }
6383
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006385
Calin Juravleb1498f62015-02-16 13:13:29 +00006386 DECLARE_INSTRUCTION(BoundType);
6387
6388 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6390 // is false then CanBeNull() cannot be true).
6391 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6392 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6393 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6394 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6395
Calin Juravleb1498f62015-02-16 13:13:29 +00006396 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006397 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6398 // It is used to bound the type in cases like:
6399 // if (x instanceof ClassX) {
6400 // // uper_bound_ will be ClassX
6401 // }
David Brazdilf5552582015-12-27 13:36:12 +00006402 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006403
6404 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6405};
6406
Vladimir Markofcb503c2016-05-18 12:48:17 +01006407class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006408 public:
6409 HCheckCast(HInstruction* object,
6410 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006412 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006413 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6414 SetPackedField<TypeCheckKindField>(check_kind);
6415 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416 SetRawInputAt(0, object);
6417 SetRawInputAt(1, constant);
6418 }
6419
6420 bool CanBeMoved() const OVERRIDE { return true; }
6421
Vladimir Marko372f10e2016-05-17 16:30:10 +01006422 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006423 return true;
6424 }
6425
6426 bool NeedsEnvironment() const OVERRIDE {
6427 // Instruction may throw a CheckCastError.
6428 return true;
6429 }
6430
6431 bool CanThrow() const OVERRIDE { return true; }
6432
Vladimir Markoa1de9182016-02-25 11:37:38 +00006433 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6434 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6435 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6436 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006437
6438 DECLARE_INSTRUCTION(CheckCast);
6439
6440 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006441 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6442 static constexpr size_t kFieldTypeCheckKindSize =
6443 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6444 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6445 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6446 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6447 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006448
6449 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450};
6451
Andreas Gampe26de38b2016-07-27 17:53:11 -07006452/**
6453 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6454 * @details We define the combined barrier types that are actually required
6455 * by the Java Memory Model, rather than using exactly the terminology from
6456 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6457 * primitives. Note that the JSR-133 cookbook generally does not deal with
6458 * store atomicity issues, and the recipes there are not always entirely sufficient.
6459 * The current recipe is as follows:
6460 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6461 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6462 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6463 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6464 * class has final fields.
6465 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6466 * store-to-memory instructions. Only generated together with non-temporal stores.
6467 */
6468enum MemBarrierKind {
6469 kAnyStore,
6470 kLoadAny,
6471 kStoreStore,
6472 kAnyAny,
6473 kNTStoreStore,
6474 kLastBarrierKind = kNTStoreStore
6475};
6476std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6477
Vladimir Markofcb503c2016-05-18 12:48:17 +01006478class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006480 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006481 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006482 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6483 SetPackedField<BarrierKindField>(barrier_kind);
6484 }
Calin Juravle27df7582015-04-17 19:12:31 +01006485
Vladimir Markoa1de9182016-02-25 11:37:38 +00006486 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006487
6488 DECLARE_INSTRUCTION(MemoryBarrier);
6489
6490 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006491 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6492 static constexpr size_t kFieldBarrierKindSize =
6493 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6494 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6495 kFieldBarrierKind + kFieldBarrierKindSize;
6496 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6497 "Too many packed fields.");
6498 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006499
6500 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6501};
6502
Vladimir Markofcb503c2016-05-18 12:48:17 +01006503class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006504 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006506 kEnter,
6507 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006508 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006509 };
6510
6511 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006512 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6514 dex_pc) {
6515 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006516 SetRawInputAt(0, object);
6517 }
6518
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006519 // Instruction may go into runtime, so we need an environment.
6520 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006521
6522 bool CanThrow() const OVERRIDE {
6523 // Verifier guarantees that monitor-exit cannot throw.
6524 // This is important because it allows the HGraphBuilder to remove
6525 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6526 return IsEnter();
6527 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006528
Vladimir Markoa1de9182016-02-25 11:37:38 +00006529 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6530 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006531
6532 DECLARE_INSTRUCTION(MonitorOperation);
6533
Calin Juravle52c48962014-12-16 17:02:57 +00006534 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006535 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6536 static constexpr size_t kFieldOperationKindSize =
6537 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6538 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6539 kFieldOperationKind + kFieldOperationKindSize;
6540 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6541 "Too many packed fields.");
6542 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006543
6544 private:
6545 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6546};
6547
Vladimir Markofcb503c2016-05-18 12:48:17 +01006548class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006549 public:
6550 HSelect(HInstruction* condition,
6551 HInstruction* true_value,
6552 HInstruction* false_value,
6553 uint32_t dex_pc)
6554 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6555 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6556
6557 // First input must be `true_value` or `false_value` to allow codegens to
6558 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6559 // that architectures which implement HSelect as a conditional move also
6560 // will not need to invert the condition.
6561 SetRawInputAt(0, false_value);
6562 SetRawInputAt(1, true_value);
6563 SetRawInputAt(2, condition);
6564 }
6565
6566 HInstruction* GetFalseValue() const { return InputAt(0); }
6567 HInstruction* GetTrueValue() const { return InputAt(1); }
6568 HInstruction* GetCondition() const { return InputAt(2); }
6569
6570 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006571 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6572 return true;
6573 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006574
6575 bool CanBeNull() const OVERRIDE {
6576 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6577 }
6578
6579 DECLARE_INSTRUCTION(Select);
6580
6581 private:
6582 DISALLOW_COPY_AND_ASSIGN(HSelect);
6583};
6584
Vladimir Markof9f64412015-09-02 14:05:49 +01006585class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006586 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006587 MoveOperands(Location source,
6588 Location destination,
6589 Primitive::Type type,
6590 HInstruction* instruction)
6591 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006592
6593 Location GetSource() const { return source_; }
6594 Location GetDestination() const { return destination_; }
6595
6596 void SetSource(Location value) { source_ = value; }
6597 void SetDestination(Location value) { destination_ = value; }
6598
6599 // The parallel move resolver marks moves as "in-progress" by clearing the
6600 // destination (but not the source).
6601 Location MarkPending() {
6602 DCHECK(!IsPending());
6603 Location dest = destination_;
6604 destination_ = Location::NoLocation();
6605 return dest;
6606 }
6607
6608 void ClearPending(Location dest) {
6609 DCHECK(IsPending());
6610 destination_ = dest;
6611 }
6612
6613 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006614 DCHECK(source_.IsValid() || destination_.IsInvalid());
6615 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616 }
6617
6618 // True if this blocks a move from the given location.
6619 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006620 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006621 }
6622
6623 // A move is redundant if it's been eliminated, if its source and
6624 // destination are the same, or if its destination is unneeded.
6625 bool IsRedundant() const {
6626 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6627 }
6628
6629 // We clear both operands to indicate move that's been eliminated.
6630 void Eliminate() {
6631 source_ = destination_ = Location::NoLocation();
6632 }
6633
6634 bool IsEliminated() const {
6635 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6636 return source_.IsInvalid();
6637 }
6638
Alexey Frunze4dda3372015-06-01 18:31:49 -07006639 Primitive::Type GetType() const { return type_; }
6640
Nicolas Geoffray90218252015-04-15 11:56:51 +01006641 bool Is64BitMove() const {
6642 return Primitive::Is64BitType(type_);
6643 }
6644
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006645 HInstruction* GetInstruction() const { return instruction_; }
6646
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006647 private:
6648 Location source_;
6649 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006650 // The type this move is for.
6651 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006652 // The instruction this move is assocatied with. Null when this move is
6653 // for moving an input in the expected locations of user (including a phi user).
6654 // This is only used in debug mode, to ensure we do not connect interval siblings
6655 // in the same parallel move.
6656 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006657};
6658
Roland Levillainc9285912015-12-18 10:38:42 +00006659std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6660
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006661static constexpr size_t kDefaultNumberOfMoves = 4;
6662
Vladimir Markofcb503c2016-05-18 12:48:17 +01006663class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006665 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006666 : HTemplateInstruction(SideEffects::None(), dex_pc),
6667 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6668 moves_.reserve(kDefaultNumberOfMoves);
6669 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006670
Nicolas Geoffray90218252015-04-15 11:56:51 +01006671 void AddMove(Location source,
6672 Location destination,
6673 Primitive::Type type,
6674 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006675 DCHECK(source.IsValid());
6676 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006677 if (kIsDebugBuild) {
6678 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006679 for (const MoveOperands& move : moves_) {
6680 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006681 // Special case the situation where the move is for the spill slot
6682 // of the instruction.
6683 if ((GetPrevious() == instruction)
6684 || ((GetPrevious() == nullptr)
6685 && instruction->IsPhi()
6686 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006687 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006688 << "Doing parallel moves for the same instruction.";
6689 } else {
6690 DCHECK(false) << "Doing parallel moves for the same instruction.";
6691 }
6692 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006693 }
6694 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006695 for (const MoveOperands& move : moves_) {
6696 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006697 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006698 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006699 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006700 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006701 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006702 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006703 }
6704
Vladimir Marko225b6462015-09-28 12:17:40 +01006705 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006706 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006707 }
6708
Vladimir Marko225b6462015-09-28 12:17:40 +01006709 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006710
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006711 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006712
6713 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006714 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006715
6716 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6717};
6718
Mark Mendell0616ae02015-04-17 12:49:27 -04006719} // namespace art
6720
Aart Bikf8f5a162017-02-06 15:35:29 -08006721#include "nodes_vector.h"
6722
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006723#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6724#include "nodes_shared.h"
6725#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006726#ifdef ART_ENABLE_CODEGEN_arm
6727#include "nodes_arm.h"
6728#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006729#ifdef ART_ENABLE_CODEGEN_mips
6730#include "nodes_mips.h"
6731#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006732#ifdef ART_ENABLE_CODEGEN_x86
6733#include "nodes_x86.h"
6734#endif
6735
6736namespace art {
6737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006738class HGraphVisitor : public ValueObject {
6739 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006740 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6741 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006742
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006743 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006744 virtual void VisitBasicBlock(HBasicBlock* block);
6745
Roland Levillain633021e2014-10-01 14:12:25 +01006746 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006747 void VisitInsertionOrder();
6748
Roland Levillain633021e2014-10-01 14:12:25 +01006749 // Visit the graph following dominator tree reverse post-order.
6750 void VisitReversePostOrder();
6751
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006752 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006753
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006754 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006755#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006756 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6757
6758 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6759
6760#undef DECLARE_VISIT_INSTRUCTION
6761
6762 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006763 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006764
6765 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6766};
6767
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006768class HGraphDelegateVisitor : public HGraphVisitor {
6769 public:
6770 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6771 virtual ~HGraphDelegateVisitor() {}
6772
6773 // Visit functions that delegate to to super class.
6774#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006775 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006776
6777 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6778
6779#undef DECLARE_VISIT_INSTRUCTION
6780
6781 private:
6782 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6783};
6784
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006785// Iterator over the blocks that art part of the loop. Includes blocks part
6786// of an inner loop. The order in which the blocks are iterated is on their
6787// block id.
6788class HBlocksInLoopIterator : public ValueObject {
6789 public:
6790 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6791 : blocks_in_loop_(info.GetBlocks()),
6792 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6793 index_(0) {
6794 if (!blocks_in_loop_.IsBitSet(index_)) {
6795 Advance();
6796 }
6797 }
6798
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006799 bool Done() const { return index_ == blocks_.size(); }
6800 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006801 void Advance() {
6802 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006803 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006804 if (blocks_in_loop_.IsBitSet(index_)) {
6805 break;
6806 }
6807 }
6808 }
6809
6810 private:
6811 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006812 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006813 size_t index_;
6814
6815 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6816};
6817
Mingyao Yang3584bce2015-05-19 16:01:59 -07006818// Iterator over the blocks that art part of the loop. Includes blocks part
6819// of an inner loop. The order in which the blocks are iterated is reverse
6820// post order.
6821class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6822 public:
6823 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6824 : blocks_in_loop_(info.GetBlocks()),
6825 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6826 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006827 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006828 Advance();
6829 }
6830 }
6831
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006832 bool Done() const { return index_ == blocks_.size(); }
6833 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006834 void Advance() {
6835 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006836 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6837 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006838 break;
6839 }
6840 }
6841 }
6842
6843 private:
6844 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006845 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006846 size_t index_;
6847
6848 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6849};
6850
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006851inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006852 if (constant->IsIntConstant()) {
6853 return constant->AsIntConstant()->GetValue();
6854 } else if (constant->IsLongConstant()) {
6855 return constant->AsLongConstant()->GetValue();
6856 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006857 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006858 return 0;
6859 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006860}
6861
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006862#define INSTRUCTION_TYPE_CHECK(type, super) \
6863 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6864 inline const H##type* HInstruction::As##type() const { \
6865 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6866 } \
6867 inline H##type* HInstruction::As##type() { \
6868 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6869 }
6870
6871 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6872#undef INSTRUCTION_TYPE_CHECK
6873
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006874// Create space in `blocks` for adding `number_of_new_blocks` entries
6875// starting at location `at`. Blocks after `at` are moved accordingly.
6876inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6877 size_t number_of_new_blocks,
6878 size_t after) {
6879 DCHECK_LT(after, blocks->size());
6880 size_t old_size = blocks->size();
6881 size_t new_size = old_size + number_of_new_blocks;
6882 blocks->resize(new_size);
6883 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6884}
6885
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006886/*
6887 * Hunt "under the hood" of array lengths (leading to array references),
6888 * null checks (also leading to array references), and new arrays
6889 * (leading to the actual length). This makes it more likely related
6890 * instructions become actually comparable.
6891 */
6892inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6893 while (instruction->IsArrayLength() ||
6894 instruction->IsNullCheck() ||
6895 instruction->IsNewArray()) {
6896 instruction = instruction->IsNewArray()
6897 ? instruction->AsNewArray()->GetLength()
6898 : instruction->InputAt(0);
6899 }
6900 return instruction;
6901}
6902
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006903} // namespace art
6904
6905#endif // ART_COMPILER_OPTIMIZING_NODES_H_