blob: 671f950aa688c4a083a015ca403b136bfc5a09f2 [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) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001377 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001378 M(VecNot, VecUnaryOperation) \
1379 M(VecAdd, VecBinaryOperation) \
1380 M(VecSub, VecBinaryOperation) \
1381 M(VecMul, VecBinaryOperation) \
1382 M(VecDiv, VecBinaryOperation) \
1383 M(VecAnd, VecBinaryOperation) \
1384 M(VecAndNot, VecBinaryOperation) \
1385 M(VecOr, VecBinaryOperation) \
1386 M(VecXor, VecBinaryOperation) \
1387 M(VecShl, VecBinaryOperation) \
1388 M(VecShr, VecBinaryOperation) \
1389 M(VecUShr, VecBinaryOperation) \
1390 M(VecLoad, VecMemoryOperation) \
1391 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001392
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001393/*
1394 * Instructions, shared across several (not all) architectures.
1395 */
1396#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1397#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1398#else
1399#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001400 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001401 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001402 M(MultiplyAccumulate, Instruction) \
1403 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001404#endif
1405
Vladimir Markob4536b72015-11-24 13:45:23 +00001406#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001407#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001408#else
1409#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1410 M(ArmDexCacheArraysBase, Instruction)
1411#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001412
1413#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1414
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001415#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001416#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001417#else
1418#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1419 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001420 M(MipsDexCacheArraysBase, Instruction) \
1421 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001422#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001423
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001424#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1425
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001426#ifndef ART_ENABLE_CODEGEN_x86
1427#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1428#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001429#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1430 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001431 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001432 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001433 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001434#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001435
1436#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1437
1438#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1439 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001440 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001441 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1442 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001444 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001445 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1446 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1447
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001448#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1449 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001450 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001451 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001452 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001453 M(Invoke, Instruction) \
1454 M(VecOperation, Instruction) \
1455 M(VecUnaryOperation, VecOperation) \
1456 M(VecBinaryOperation, VecOperation) \
1457 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001458
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001459#define FOR_EACH_INSTRUCTION(M) \
1460 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1461 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1462
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001463#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001464FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1465#undef FORWARD_DECLARATION
1466
Vladimir Marko372f10e2016-05-17 16:30:10 +01001467#define DECLARE_INSTRUCTION(type) \
1468 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1469 const char* DebugName() const OVERRIDE { return #type; } \
1470 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1471 return other->Is##type(); \
1472 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001473 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001474
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001475#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1476 bool Is##type() const { return As##type() != nullptr; } \
1477 const H##type* As##type() const { return this; } \
1478 H##type* As##type() { return this; }
1479
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001480template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001481class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001482 public:
David Brazdiled596192015-01-23 10:39:45 +00001483 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001484 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001485 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001486
Vladimir Marko46817b82016-03-29 12:21:58 +01001487 // Hook for the IntrusiveForwardList<>.
1488 // TODO: Hide this better.
1489 IntrusiveForwardListHook hook;
1490
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491 private:
David Brazdiled596192015-01-23 10:39:45 +00001492 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001493 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001494
1495 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001496 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001497
Vladimir Marko46817b82016-03-29 12:21:58 +01001498 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001499
1500 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1501};
1502
David Brazdiled596192015-01-23 10:39:45 +00001503template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001504using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001505
David Brazdil1abb4192015-02-17 18:33:36 +00001506// This class is used by HEnvironment and HInstruction classes to record the
1507// instructions they use and pointers to the corresponding HUseListNodes kept
1508// by the used instructions.
1509template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001510class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001511 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001512 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1513 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001514
Vladimir Marko46817b82016-03-29 12:21:58 +01001515 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1516 : HUserRecord(old_record.instruction_, before_use_node) {}
1517 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1518 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001519 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001520 }
1521
1522 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001523 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1524 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001525
1526 private:
1527 // Instruction used by the user.
1528 HInstruction* instruction_;
1529
Vladimir Marko46817b82016-03-29 12:21:58 +01001530 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1531 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001532};
1533
Vladimir Markoe9004912016-06-16 16:50:52 +01001534// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1535// This is used for HInstruction::GetInputs() to return a container wrapper providing
1536// HInstruction* values even though the underlying container has HUserRecord<>s.
1537struct HInputExtractor {
1538 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1539 return record.GetInstruction();
1540 }
1541 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1542 return record.GetInstruction();
1543 }
1544};
1545
1546using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1547using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1548
Aart Bik854a02b2015-07-14 16:07:00 -07001549/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001550 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001551 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 * For write/read dependences on fields/arrays, the dependence analysis uses
1553 * type disambiguation (e.g. a float field write cannot modify the value of an
1554 * integer field read) and the access type (e.g. a reference array write cannot
1555 * modify the value of a reference field read [although it may modify the
1556 * reference fetch prior to reading the field, which is represented by its own
1557 * write/read dependence]). The analysis makes conservative points-to
1558 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1559 * the same, and any reference read depends on any reference read without
1560 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001561 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 * The internal representation uses 38-bit and is described in the table below.
1563 * The first line indicates the side effect, and for field/array accesses the
1564 * second line indicates the type of the access (in the order of the
1565 * Primitive::Type enum).
1566 * The two numbered lines below indicate the bit position in the bitfield (read
1567 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001568 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001569 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1570 * +-------------+---------+---------+--------------+---------+---------+
1571 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1572 * | 3 |333333322|222222221| 1 |111111110|000000000|
1573 * | 7 |654321098|765432109| 8 |765432109|876543210|
1574 *
1575 * Note that, to ease the implementation, 'changes' bits are least significant
1576 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001577 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578class SideEffects : public ValueObject {
1579 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001580 SideEffects() : flags_(0) {}
1581
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582 static SideEffects None() {
1583 return SideEffects(0);
1584 }
1585
1586 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001587 return SideEffects(kAllChangeBits | kAllDependOnBits);
1588 }
1589
1590 static SideEffects AllChanges() {
1591 return SideEffects(kAllChangeBits);
1592 }
1593
1594 static SideEffects AllDependencies() {
1595 return SideEffects(kAllDependOnBits);
1596 }
1597
1598 static SideEffects AllExceptGCDependency() {
1599 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1600 }
1601
1602 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001603 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001604 }
1605
Aart Bik34c3ba92015-07-20 14:08:59 -07001606 static SideEffects AllWrites() {
1607 return SideEffects(kAllWrites);
1608 }
1609
1610 static SideEffects AllReads() {
1611 return SideEffects(kAllReads);
1612 }
1613
1614 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1615 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001617 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001621 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001622 }
1623
Aart Bik34c3ba92015-07-20 14:08:59 -07001624 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1625 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001627 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001628 }
1629
1630 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001631 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001632 }
1633
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 static SideEffects CanTriggerGC() {
1635 return SideEffects(1ULL << kCanTriggerGCBit);
1636 }
1637
1638 static SideEffects DependsOnGC() {
1639 return SideEffects(1ULL << kDependsOnGCBit);
1640 }
1641
Aart Bik854a02b2015-07-14 16:07:00 -07001642 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001643 SideEffects Union(SideEffects other) const {
1644 return SideEffects(flags_ | other.flags_);
1645 }
1646
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 SideEffects Exclusion(SideEffects other) const {
1648 return SideEffects(flags_ & ~other.flags_);
1649 }
1650
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001651 void Add(SideEffects other) {
1652 flags_ |= other.flags_;
1653 }
1654
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 bool Includes(SideEffects other) const {
1656 return (other.flags_ & flags_) == other.flags_;
1657 }
1658
1659 bool HasSideEffects() const {
1660 return (flags_ & kAllChangeBits);
1661 }
1662
1663 bool HasDependencies() const {
1664 return (flags_ & kAllDependOnBits);
1665 }
1666
1667 // Returns true if there are no side effects or dependencies.
1668 bool DoesNothing() const {
1669 return flags_ == 0;
1670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Returns true if something is written.
1673 bool DoesAnyWrite() const {
1674 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001675 }
1676
Aart Bik854a02b2015-07-14 16:07:00 -07001677 // Returns true if something is read.
1678 bool DoesAnyRead() const {
1679 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001680 }
1681
Aart Bik854a02b2015-07-14 16:07:00 -07001682 // Returns true if potentially everything is written and read
1683 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001684 bool DoesAllReadWrite() const {
1685 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1686 }
1687
Aart Bik854a02b2015-07-14 16:07:00 -07001688 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001689 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001690 }
1691
Roland Levillain0d5a2812015-11-13 10:07:31 +00001692 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001693 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1695 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001696 }
1697
1698 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001700 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001701 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 for (int s = kLastBit; s >= 0; s--) {
1703 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1704 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1705 // This is a bit for the GC side effect.
1706 if (current_bit_is_set) {
1707 flags += "GC";
1708 }
Aart Bik854a02b2015-07-14 16:07:00 -07001709 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 } else {
1711 // This is a bit for the array/field analysis.
1712 // The underscore character stands for the 'can trigger GC' bit.
1713 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1714 if (current_bit_is_set) {
1715 flags += kDebug[s];
1716 }
1717 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1718 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1719 flags += "|";
1720 }
1721 }
Aart Bik854a02b2015-07-14 16:07:00 -07001722 }
1723 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001724 }
1725
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001726 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001727
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001728 private:
1729 static constexpr int kFieldArrayAnalysisBits = 9;
1730
1731 static constexpr int kFieldWriteOffset = 0;
1732 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1733 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1734 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1735
1736 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1737
1738 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1739 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1740 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1741 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1742
1743 static constexpr int kLastBit = kDependsOnGCBit;
1744 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1745
1746 // Aliases.
1747
1748 static_assert(kChangeBits == kDependOnBits,
1749 "the 'change' bits should match the 'depend on' bits.");
1750
1751 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1752 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1753 static constexpr uint64_t kAllWrites =
1754 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1755 static constexpr uint64_t kAllReads =
1756 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001757
Aart Bik854a02b2015-07-14 16:07:00 -07001758 // Translates type to bit flag.
1759 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1760 CHECK_NE(type, Primitive::kPrimVoid);
1761 const uint64_t one = 1;
1762 const int shift = type; // 0-based consecutive enum
1763 DCHECK_LE(kFieldWriteOffset, shift);
1764 DCHECK_LT(shift, kArrayWriteOffset);
1765 return one << (type + offset);
1766 }
1767
1768 // Private constructor on direct flags value.
1769 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1770
1771 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001772};
1773
David Brazdiled596192015-01-23 10:39:45 +00001774// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001775class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001776 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001777 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1778 size_t number_of_vregs,
1779 ArtMethod* method,
1780 uint32_t dex_pc,
1781 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1783 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001785 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001786 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001787 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001788 }
1789
Mingyao Yang01b47b02017-02-03 12:09:57 -08001790 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001791 : HEnvironment(arena,
1792 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001793 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001794 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001795 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001796
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001798 if (parent_ != nullptr) {
1799 parent_->SetAndCopyParentChain(allocator, parent);
1800 } else {
1801 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1802 parent_->CopyFrom(parent);
1803 if (parent->GetParent() != nullptr) {
1804 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1805 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 }
David Brazdiled596192015-01-23 10:39:45 +00001807 }
1808
Vladimir Marko71bf8092015-09-15 15:33:14 +01001809 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001810 void CopyFrom(HEnvironment* environment);
1811
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001812 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1813 // input to the loop phi instead. This is for inserting instructions that
1814 // require an environment (like HDeoptimization) in the loop pre-header.
1815 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001816
1817 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001818 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001819 }
1820
1821 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001822 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001823 }
1824
David Brazdil1abb4192015-02-17 18:33:36 +00001825 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001826
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001828
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001829 HEnvironment* GetParent() const { return parent_; }
1830
1831 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001832 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001833 }
1834
1835 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001836 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 }
1838
1839 uint32_t GetDexPc() const {
1840 return dex_pc_;
1841 }
1842
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001843 ArtMethod* GetMethod() const {
1844 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001845 }
1846
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001847 HInstruction* GetHolder() const {
1848 return holder_;
1849 }
1850
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001851
1852 bool IsFromInlinedInvoke() const {
1853 return GetParent() != nullptr;
1854 }
1855
David Brazdiled596192015-01-23 10:39:45 +00001856 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001857 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1858 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001859 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001860 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001861 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001862
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001863 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001864 HInstruction* const holder_;
1865
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001866 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001867
1868 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1869};
1870
Vladimir Markof9f64412015-09-02 14:05:49 +01001871class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001872 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001873 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 : previous_(nullptr),
1875 next_(nullptr),
1876 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001877 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001878 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001879 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001880 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001881 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001882 locations_(nullptr),
1883 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001884 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001885 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001886 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1887 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1888 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001889
Dave Allison20dfc792014-06-16 20:44:29 -07001890 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001891
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001892#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001893 enum InstructionKind {
1894 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1895 };
1896#undef DECLARE_KIND
1897
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 HInstruction* GetNext() const { return next_; }
1899 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001900
Calin Juravle77520bc2015-01-12 18:45:46 +00001901 HInstruction* GetNextDisregardingMoves() const;
1902 HInstruction* GetPreviousDisregardingMoves() const;
1903
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001904 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001905 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001907 bool IsInBlock() const { return block_ != nullptr; }
1908 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001909 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1910 bool IsIrreducibleLoopHeaderPhi() const {
1911 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1912 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001913
Vladimir Marko372f10e2016-05-17 16:30:10 +01001914 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1915
1916 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1917 // One virtual method is enough, just const_cast<> and then re-add the const.
1918 return ArrayRef<const HUserRecord<HInstruction*>>(
1919 const_cast<HInstruction*>(this)->GetInputRecords());
1920 }
1921
Vladimir Markoe9004912016-06-16 16:50:52 +01001922 HInputsRef GetInputs() {
1923 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001924 }
1925
Vladimir Markoe9004912016-06-16 16:50:52 +01001926 HConstInputsRef GetInputs() const {
1927 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001928 }
1929
1930 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001931 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001932
Aart Bik2767f4b2016-10-28 15:03:53 -07001933 bool HasInput(HInstruction* input) const {
1934 for (const HInstruction* i : GetInputs()) {
1935 if (i == input) {
1936 return true;
1937 }
1938 }
1939 return false;
1940 }
1941
Vladimir Marko372f10e2016-05-17 16:30:10 +01001942 void SetRawInputAt(size_t index, HInstruction* input) {
1943 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1944 }
1945
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001946 virtual void Accept(HGraphVisitor* visitor) = 0;
1947 virtual const char* DebugName() const = 0;
1948
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001949 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1950
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001952
1953 uint32_t GetDexPc() const { return dex_pc_; }
1954
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001955 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001956
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001957 // Can the instruction throw?
1958 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1959 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001960 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001961 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001962
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001963 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001964 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
Calin Juravle10e244f2015-01-26 18:54:32 +00001966 // Does not apply for all instructions, but having this at top level greatly
1967 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001968 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001969 virtual bool CanBeNull() const {
1970 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1971 return true;
1972 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001973
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001974 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001975 return false;
1976 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001977
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001978 virtual bool IsActualObject() const {
1979 return GetType() == Primitive::kPrimNot;
1980 }
1981
Calin Juravle2e768302015-07-28 14:41:11 +00001982 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001983
Calin Juravle61d544b2015-02-23 16:46:57 +00001984 ReferenceTypeInfo GetReferenceTypeInfo() const {
1985 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001986 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001987 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001988 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001989
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001990 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001991 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001992 // Note: fixup_end remains valid across push_front().
1993 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1994 HUseListNode<HInstruction*>* new_node =
1995 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1996 uses_.push_front(*new_node);
1997 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001998 }
1999
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002001 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002002 // Note: env_fixup_end remains valid across push_front().
2003 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2004 HUseListNode<HEnvironment*>* new_node =
2005 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2006 env_uses_.push_front(*new_node);
2007 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008 }
2009
David Brazdil1abb4192015-02-17 18:33:36 +00002010 void RemoveAsUserOfInput(size_t input) {
2011 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002012 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2013 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2014 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002015 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016
Vladimir Marko372f10e2016-05-17 16:30:10 +01002017 void RemoveAsUserOfAllInputs() {
2018 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2019 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2020 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2021 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2022 }
2023 }
2024
David Brazdil1abb4192015-02-17 18:33:36 +00002025 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2026 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027
Vladimir Marko46817b82016-03-29 12:21:58 +01002028 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2029 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2030 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002031 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002032 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002033 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002034
Aart Bikcc42be02016-10-20 16:14:16 -07002035 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002036 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002037 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002038 !CanThrow() &&
2039 !IsSuspendCheck() &&
2040 !IsControlFlow() &&
2041 !IsNativeDebugInfo() &&
2042 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002043 // If we added an explicit barrier then we should keep it.
2044 !IsMemoryBarrier();
2045 }
2046
Aart Bikcc42be02016-10-20 16:14:16 -07002047 bool IsDeadAndRemovable() const {
2048 return IsRemovable() && !HasUses();
2049 }
2050
Roland Levillain6c82d402014-10-13 16:10:27 +01002051 // Does this instruction strictly dominate `other_instruction`?
2052 // Returns false if this instruction and `other_instruction` are the same.
2053 // Aborts if this instruction and `other_instruction` are both phis.
2054 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002055
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002056 int GetId() const { return id_; }
2057 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002058
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002059 int GetSsaIndex() const { return ssa_index_; }
2060 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2061 bool HasSsaIndex() const { return ssa_index_ != -1; }
2062
2063 bool HasEnvironment() const { return environment_ != nullptr; }
2064 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002065 // Set the `environment_` field. Raw because this method does not
2066 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002067 void SetRawEnvironment(HEnvironment* environment) {
2068 DCHECK(environment_ == nullptr);
2069 DCHECK_EQ(environment->GetHolder(), this);
2070 environment_ = environment;
2071 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002072
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002073 void InsertRawEnvironment(HEnvironment* environment) {
2074 DCHECK(environment_ != nullptr);
2075 DCHECK_EQ(environment->GetHolder(), this);
2076 DCHECK(environment->GetParent() == nullptr);
2077 environment->parent_ = environment_;
2078 environment_ = environment;
2079 }
2080
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002081 void RemoveEnvironment();
2082
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002083 // Set the environment of this instruction, copying it from `environment`. While
2084 // copying, the uses lists are being updated.
2085 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002086 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002087 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002088 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002089 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002090 if (environment->GetParent() != nullptr) {
2091 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2092 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002093 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002094
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002095 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2096 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002097 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002098 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002099 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002100 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002101 if (environment->GetParent() != nullptr) {
2102 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2103 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002104 }
2105
Nicolas Geoffray39468442014-09-02 15:17:15 +01002106 // Returns the number of entries in the environment. Typically, that is the
2107 // number of dex registers in a method. It could be more in case of inlining.
2108 size_t EnvironmentSize() const;
2109
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 LocationSummary* GetLocations() const { return locations_; }
2111 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002112
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002114 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002115 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002116
Alexandre Rames188d4312015-04-09 18:30:21 +01002117 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2118 // uses of this instruction by `other` are *not* updated.
2119 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2120 ReplaceWith(other);
2121 other->ReplaceInput(this, use_index);
2122 }
2123
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002124 // Move `this` instruction before `cursor`
2125 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002126
Vladimir Markofb337ea2015-11-25 15:25:10 +00002127 // Move `this` before its first user and out of any loops. If there is no
2128 // out-of-loop user that dominates all other users, move the instruction
2129 // to the end of the out-of-loop common dominator of the user's blocks.
2130 //
2131 // This can be used only on non-throwing instructions with no side effects that
2132 // have at least one use but no environment uses.
2133 void MoveBeforeFirstUserAndOutOfLoops();
2134
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002135#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 bool Is##type() const; \
2137 const H##type* As##type() const; \
2138 H##type* As##type();
2139
2140 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2141#undef INSTRUCTION_TYPE_CHECK
2142
2143#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002144 bool Is##type() const { return (As##type() != nullptr); } \
2145 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002146 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002147 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002148#undef INSTRUCTION_TYPE_CHECK
2149
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002150 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002151 // TODO: this method is used by LICM and GVN with possibly different
2152 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002153 virtual bool CanBeMoved() const { return false; }
2154
2155 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002156 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002157 return false;
2158 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159
2160 // Returns whether any data encoded in the two instructions is equal.
2161 // This method does not look at the inputs. Both instructions must be
2162 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002163 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002164 return false;
2165 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
2167 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002168 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002169 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002170 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002171
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002172 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2173 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2174 // the virtual function because the __attribute__((__pure__)) doesn't really
2175 // apply the strong requirement for virtual functions, preventing optimizations.
2176 InstructionKind GetKind() const PURE;
2177 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002178
2179 virtual size_t ComputeHashCode() const {
2180 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002181 for (const HInstruction* input : GetInputs()) {
2182 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002183 }
2184 return result;
2185 }
2186
2187 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002188 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002189 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002190
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002191 size_t GetLifetimePosition() const { return lifetime_position_; }
2192 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2193 LiveInterval* GetLiveInterval() const { return live_interval_; }
2194 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2195 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2196
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002197 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2198
2199 // Returns whether the code generation of the instruction will require to have access
2200 // to the current method. Such instructions are:
2201 // (1): Instructions that require an environment, as calling the runtime requires
2202 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002203 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2204 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002205 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002206 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002207 }
2208
Vladimir Markodc151b22015-10-15 18:02:30 +01002209 // Returns whether the code generation of the instruction will require to have access
2210 // to the dex cache of the current method's declaring class via the current method.
2211 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002212
Mark Mendellc4701932015-04-10 13:18:51 -04002213 // Does this instruction have any use in an environment before
2214 // control flow hits 'other'?
2215 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2216
2217 // Remove all references to environment uses of this instruction.
2218 // The caller must ensure that this is safe to do.
2219 void RemoveEnvironmentUsers();
2220
Vladimir Markoa1de9182016-02-25 11:37:38 +00002221 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2222 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002223
David Brazdil1abb4192015-02-17 18:33:36 +00002224 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002225 // If set, the machine code for this instruction is assumed to be generated by
2226 // its users. Used by liveness analysis to compute use positions accordingly.
2227 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2228 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2229 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2230 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2231
Vladimir Marko372f10e2016-05-17 16:30:10 +01002232 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2233 return GetInputRecords()[i];
2234 }
2235
2236 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2237 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2238 input_records[index] = input;
2239 }
David Brazdil1abb4192015-02-17 18:33:36 +00002240
Vladimir Markoa1de9182016-02-25 11:37:38 +00002241 uint32_t GetPackedFields() const {
2242 return packed_fields_;
2243 }
2244
2245 template <size_t flag>
2246 bool GetPackedFlag() const {
2247 return (packed_fields_ & (1u << flag)) != 0u;
2248 }
2249
2250 template <size_t flag>
2251 void SetPackedFlag(bool value = true) {
2252 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2253 }
2254
2255 template <typename BitFieldType>
2256 typename BitFieldType::value_type GetPackedField() const {
2257 return BitFieldType::Decode(packed_fields_);
2258 }
2259
2260 template <typename BitFieldType>
2261 void SetPackedField(typename BitFieldType::value_type value) {
2262 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2263 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2264 }
2265
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002266 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002267 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2268 auto before_use_node = uses_.before_begin();
2269 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2270 HInstruction* user = use_node->GetUser();
2271 size_t input_index = use_node->GetIndex();
2272 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2273 before_use_node = use_node;
2274 }
2275 }
2276
2277 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2278 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2279 if (next != uses_.end()) {
2280 HInstruction* next_user = next->GetUser();
2281 size_t next_index = next->GetIndex();
2282 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2283 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2284 }
2285 }
2286
2287 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2288 auto before_env_use_node = env_uses_.before_begin();
2289 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2290 HEnvironment* user = env_use_node->GetUser();
2291 size_t input_index = env_use_node->GetIndex();
2292 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2293 before_env_use_node = env_use_node;
2294 }
2295 }
2296
2297 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2298 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2299 if (next != env_uses_.end()) {
2300 HEnvironment* next_user = next->GetUser();
2301 size_t next_index = next->GetIndex();
2302 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2303 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2304 }
2305 }
David Brazdil1abb4192015-02-17 18:33:36 +00002306
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 HInstruction* previous_;
2308 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002309 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002310 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312 // An instruction gets an id when it is added to the graph.
2313 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002314 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002315 int id_;
2316
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002317 // When doing liveness analysis, instructions that have uses get an SSA index.
2318 int ssa_index_;
2319
Vladimir Markoa1de9182016-02-25 11:37:38 +00002320 // Packed fields.
2321 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002322
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002323 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002324 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002325
2326 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002327 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002328
Nicolas Geoffray39468442014-09-02 15:17:15 +01002329 // The environment associated with this instruction. Not null if the instruction
2330 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002331 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002332
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002333 // Set by the code generator.
2334 LocationSummary* locations_;
2335
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002336 // Set by the liveness analysis.
2337 LiveInterval* live_interval_;
2338
2339 // Set by the liveness analysis, this is the position in a linear
2340 // order of blocks where this instruction's live interval start.
2341 size_t lifetime_position_;
2342
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002343 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002344
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 // The reference handle part of the reference type info.
2346 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002347 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002348 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002349
David Brazdil1abb4192015-02-17 18:33:36 +00002350 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002352 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002353 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002354 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355
2356 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2357};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002358std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002359
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002360// Iterates over the instructions, while preserving the next instruction
2361// in case the current instruction gets removed from the list by the user
2362// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002363class HInstructionIterator : public ValueObject {
2364 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002365 explicit HInstructionIterator(const HInstructionList& instructions)
2366 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002367 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002368 }
2369
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002370 bool Done() const { return instruction_ == nullptr; }
2371 HInstruction* Current() const { return instruction_; }
2372 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002373 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002374 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002375 }
2376
2377 private:
2378 HInstruction* instruction_;
2379 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002380
2381 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382};
2383
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002384// Iterates over the instructions without saving the next instruction,
2385// therefore handling changes in the graph potentially made by the user
2386// of this iterator.
2387class HInstructionIteratorHandleChanges : public ValueObject {
2388 public:
2389 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2390 : instruction_(instructions.first_instruction_) {
2391 }
2392
2393 bool Done() const { return instruction_ == nullptr; }
2394 HInstruction* Current() const { return instruction_; }
2395 void Advance() {
2396 instruction_ = instruction_->GetNext();
2397 }
2398
2399 private:
2400 HInstruction* instruction_;
2401
2402 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2403};
2404
2405
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002406class HBackwardInstructionIterator : public ValueObject {
2407 public:
2408 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2409 : instruction_(instructions.last_instruction_) {
2410 next_ = Done() ? nullptr : instruction_->GetPrevious();
2411 }
2412
2413 bool Done() const { return instruction_ == nullptr; }
2414 HInstruction* Current() const { return instruction_; }
2415 void Advance() {
2416 instruction_ = next_;
2417 next_ = Done() ? nullptr : instruction_->GetPrevious();
2418 }
2419
2420 private:
2421 HInstruction* instruction_;
2422 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002423
2424 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002425};
2426
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002427class HVariableInputSizeInstruction : public HInstruction {
2428 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002429 using HInstruction::GetInputRecords; // Keep the const version visible.
2430 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2431 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2432 }
2433
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002434 void AddInput(HInstruction* input);
2435 void InsertInputAt(size_t index, HInstruction* input);
2436 void RemoveInputAt(size_t index);
2437
2438 protected:
2439 HVariableInputSizeInstruction(SideEffects side_effects,
2440 uint32_t dex_pc,
2441 ArenaAllocator* arena,
2442 size_t number_of_inputs,
2443 ArenaAllocKind kind)
2444 : HInstruction(side_effects, dex_pc),
2445 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2446
2447 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2448
2449 private:
2450 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2451};
2452
Vladimir Markof9f64412015-09-02 14:05:49 +01002453template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454class HTemplateInstruction: public HInstruction {
2455 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002456 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002457 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002458 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002459
Vladimir Marko372f10e2016-05-17 16:30:10 +01002460 using HInstruction::GetInputRecords; // Keep the const version visible.
2461 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2462 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002463 }
2464
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002466 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002467
2468 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002469};
2470
Vladimir Markof9f64412015-09-02 14:05:49 +01002471// HTemplateInstruction specialization for N=0.
2472template<>
2473class HTemplateInstruction<0>: public HInstruction {
2474 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002475 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002476 : HInstruction(side_effects, dex_pc) {}
2477
Vladimir Markof9f64412015-09-02 14:05:49 +01002478 virtual ~HTemplateInstruction() {}
2479
Vladimir Marko372f10e2016-05-17 16:30:10 +01002480 using HInstruction::GetInputRecords; // Keep the const version visible.
2481 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2482 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002483 }
2484
2485 private:
2486 friend class SsaBuilder;
2487};
2488
Dave Allison20dfc792014-06-16 20:44:29 -07002489template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002490class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002491 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002492 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002493 : HTemplateInstruction<N>(side_effects, dex_pc) {
2494 this->template SetPackedField<TypeField>(type);
2495 }
Dave Allison20dfc792014-06-16 20:44:29 -07002496 virtual ~HExpression() {}
2497
Vladimir Markoa1de9182016-02-25 11:37:38 +00002498 Primitive::Type GetType() const OVERRIDE {
2499 return TypeField::Decode(this->GetPackedFields());
2500 }
Dave Allison20dfc792014-06-16 20:44:29 -07002501
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002502 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002503 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2504 static constexpr size_t kFieldTypeSize =
2505 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2506 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2507 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2508 "Too many packed fields.");
2509 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002510};
2511
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2513// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002514class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002516 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2517 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002518
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002519 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002520
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002521 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002522
2523 private:
2524 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2525};
2526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002527// Represents dex's RETURN opcodes. A HReturn is a control flow
2528// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002529class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002530 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002531 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2532 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002533 SetRawInputAt(0, value);
2534 }
2535
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002536 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002537
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002538 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539
2540 private:
2541 DISALLOW_COPY_AND_ASSIGN(HReturn);
2542};
2543
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002544class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002545 public:
2546 HPhi(ArenaAllocator* arena,
2547 uint32_t reg_number,
2548 size_t number_of_inputs,
2549 Primitive::Type type,
2550 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002551 : HVariableInputSizeInstruction(
2552 SideEffects::None(),
2553 dex_pc,
2554 arena,
2555 number_of_inputs,
2556 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002557 reg_number_(reg_number) {
2558 SetPackedField<TypeField>(ToPhiType(type));
2559 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2560 // Phis are constructed live and marked dead if conflicting or unused.
2561 // Individual steps of SsaBuilder should assume that if a phi has been
2562 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2563 SetPackedFlag<kFlagIsLive>(true);
2564 SetPackedFlag<kFlagCanBeNull>(true);
2565 }
2566
2567 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2568 static Primitive::Type ToPhiType(Primitive::Type type) {
2569 return Primitive::PrimitiveKind(type);
2570 }
2571
2572 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2573
David Brazdildee58d62016-04-07 09:54:26 +00002574 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2575 void SetType(Primitive::Type new_type) {
2576 // Make sure that only valid type changes occur. The following are allowed:
2577 // (1) int -> float/ref (primitive type propagation),
2578 // (2) long -> double (primitive type propagation).
2579 DCHECK(GetType() == new_type ||
2580 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2581 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2582 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2583 SetPackedField<TypeField>(new_type);
2584 }
2585
2586 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2587 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2588
2589 uint32_t GetRegNumber() const { return reg_number_; }
2590
2591 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2592 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2593 bool IsDead() const { return !IsLive(); }
2594 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2595
Vladimir Markoe9004912016-06-16 16:50:52 +01002596 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002597 return other != nullptr
2598 && other->IsPhi()
2599 && other->AsPhi()->GetBlock() == GetBlock()
2600 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2601 }
2602
2603 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2604 // An equivalent phi is a phi having the same dex register and type.
2605 // It assumes that phis with the same dex register are adjacent.
2606 HPhi* GetNextEquivalentPhiWithSameType() {
2607 HInstruction* next = GetNext();
2608 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2609 if (next->GetType() == GetType()) {
2610 return next->AsPhi();
2611 }
2612 next = next->GetNext();
2613 }
2614 return nullptr;
2615 }
2616
2617 DECLARE_INSTRUCTION(Phi);
2618
David Brazdildee58d62016-04-07 09:54:26 +00002619 private:
2620 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2621 static constexpr size_t kFieldTypeSize =
2622 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2623 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2624 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2625 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2626 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2627 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2628
David Brazdildee58d62016-04-07 09:54:26 +00002629 const uint32_t reg_number_;
2630
2631 DISALLOW_COPY_AND_ASSIGN(HPhi);
2632};
2633
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002634// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002635// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002636// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002637class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002638 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002639 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002640
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002641 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002642
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002643 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002644
2645 private:
2646 DISALLOW_COPY_AND_ASSIGN(HExit);
2647};
2648
2649// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002650class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002652 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002653
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002654 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002655
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002656 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002657 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002658 }
2659
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002660 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002661
2662 private:
2663 DISALLOW_COPY_AND_ASSIGN(HGoto);
2664};
2665
Roland Levillain9867bc72015-08-05 10:21:34 +01002666class HConstant : public HExpression<0> {
2667 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002668 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2669 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002670
2671 bool CanBeMoved() const OVERRIDE { return true; }
2672
Roland Levillain1a653882016-03-18 18:05:57 +00002673 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002674 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002675 // Is this constant 0 in the arithmetic sense?
2676 virtual bool IsArithmeticZero() const { return false; }
2677 // Is this constant a 0-bit pattern?
2678 virtual bool IsZeroBitPattern() const { return false; }
2679 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002680 virtual bool IsOne() const { return false; }
2681
David Brazdil77a48ae2015-09-15 12:34:04 +00002682 virtual uint64_t GetValueAsUint64() const = 0;
2683
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002684 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002685
2686 private:
2687 DISALLOW_COPY_AND_ASSIGN(HConstant);
2688};
2689
Vladimir Markofcb503c2016-05-18 12:48:17 +01002690class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002691 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002692 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 return true;
2694 }
2695
David Brazdil77a48ae2015-09-15 12:34:04 +00002696 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2697
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 size_t ComputeHashCode() const OVERRIDE { return 0; }
2699
Roland Levillain1a653882016-03-18 18:05:57 +00002700 // The null constant representation is a 0-bit pattern.
2701 virtual bool IsZeroBitPattern() const { return true; }
2702
Roland Levillain9867bc72015-08-05 10:21:34 +01002703 DECLARE_INSTRUCTION(NullConstant);
2704
2705 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002706 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002707
2708 friend class HGraph;
2709 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2710};
2711
2712// Constants of the type int. Those can be from Dex instructions, or
2713// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002714class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002715 public:
2716 int32_t GetValue() const { return value_; }
2717
David Brazdil9f389d42015-10-01 14:32:56 +01002718 uint64_t GetValueAsUint64() const OVERRIDE {
2719 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2720 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002721
Vladimir Marko372f10e2016-05-17 16:30:10 +01002722 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002723 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 return other->AsIntConstant()->value_ == value_;
2725 }
2726
2727 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2728
2729 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002730 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2731 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002732 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2733
Roland Levillain1a653882016-03-18 18:05:57 +00002734 // Integer constants are used to encode Boolean values as well,
2735 // where 1 means true and 0 means false.
2736 bool IsTrue() const { return GetValue() == 1; }
2737 bool IsFalse() const { return GetValue() == 0; }
2738
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 DECLARE_INSTRUCTION(IntConstant);
2740
2741 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002742 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2743 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2744 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2745 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002746
2747 const int32_t value_;
2748
2749 friend class HGraph;
2750 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2751 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2752 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2753};
2754
Vladimir Markofcb503c2016-05-18 12:48:17 +01002755class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002756 public:
2757 int64_t GetValue() const { return value_; }
2758
David Brazdil77a48ae2015-09-15 12:34:04 +00002759 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2760
Vladimir Marko372f10e2016-05-17 16:30:10 +01002761 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002762 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 return other->AsLongConstant()->value_ == value_;
2764 }
2765
2766 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2767
2768 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002769 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2770 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002771 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2772
2773 DECLARE_INSTRUCTION(LongConstant);
2774
2775 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002776 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2777 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002778
2779 const int64_t value_;
2780
2781 friend class HGraph;
2782 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2783};
Dave Allison20dfc792014-06-16 20:44:29 -07002784
Vladimir Markofcb503c2016-05-18 12:48:17 +01002785class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002786 public:
2787 float GetValue() const { return value_; }
2788
2789 uint64_t GetValueAsUint64() const OVERRIDE {
2790 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2791 }
2792
Vladimir Marko372f10e2016-05-17 16:30:10 +01002793 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002794 DCHECK(other->IsFloatConstant()) << other->DebugName();
2795 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2796 }
2797
2798 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2799
2800 bool IsMinusOne() const OVERRIDE {
2801 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2802 }
Roland Levillain1a653882016-03-18 18:05:57 +00002803 bool IsArithmeticZero() const OVERRIDE {
2804 return std::fpclassify(value_) == FP_ZERO;
2805 }
2806 bool IsArithmeticPositiveZero() const {
2807 return IsArithmeticZero() && !std::signbit(value_);
2808 }
2809 bool IsArithmeticNegativeZero() const {
2810 return IsArithmeticZero() && std::signbit(value_);
2811 }
2812 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002813 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002814 }
2815 bool IsOne() const OVERRIDE {
2816 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2817 }
2818 bool IsNaN() const {
2819 return std::isnan(value_);
2820 }
2821
2822 DECLARE_INSTRUCTION(FloatConstant);
2823
2824 private:
2825 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2826 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2827 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2828 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2829
2830 const float value_;
2831
2832 // Only the SsaBuilder and HGraph can create floating-point constants.
2833 friend class SsaBuilder;
2834 friend class HGraph;
2835 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2836};
2837
Vladimir Markofcb503c2016-05-18 12:48:17 +01002838class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002839 public:
2840 double GetValue() const { return value_; }
2841
2842 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2843
Vladimir Marko372f10e2016-05-17 16:30:10 +01002844 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002845 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2846 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2847 }
2848
2849 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2850
2851 bool IsMinusOne() const OVERRIDE {
2852 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2853 }
Roland Levillain1a653882016-03-18 18:05:57 +00002854 bool IsArithmeticZero() const OVERRIDE {
2855 return std::fpclassify(value_) == FP_ZERO;
2856 }
2857 bool IsArithmeticPositiveZero() const {
2858 return IsArithmeticZero() && !std::signbit(value_);
2859 }
2860 bool IsArithmeticNegativeZero() const {
2861 return IsArithmeticZero() && std::signbit(value_);
2862 }
2863 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002864 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002865 }
2866 bool IsOne() const OVERRIDE {
2867 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2868 }
2869 bool IsNaN() const {
2870 return std::isnan(value_);
2871 }
2872
2873 DECLARE_INSTRUCTION(DoubleConstant);
2874
2875 private:
2876 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2877 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2878 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2879 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2880
2881 const double value_;
2882
2883 // Only the SsaBuilder and HGraph can create floating-point constants.
2884 friend class SsaBuilder;
2885 friend class HGraph;
2886 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2887};
2888
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002889// Conditional branch. A block ending with an HIf instruction must have
2890// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002891class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002893 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2894 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002895 SetRawInputAt(0, input);
2896 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002897
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002898 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002899
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002900 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002901 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002902 }
2903
2904 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002905 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002906 }
2907
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002908 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002909
2910 private:
2911 DISALLOW_COPY_AND_ASSIGN(HIf);
2912};
2913
David Brazdilfc6a86a2015-06-26 10:33:45 +00002914
2915// Abstract instruction which marks the beginning and/or end of a try block and
2916// links it to the respective exception handlers. Behaves the same as a Goto in
2917// non-exceptional control flow.
2918// Normal-flow successor is stored at index zero, exception handlers under
2919// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002920class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002921 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002922 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002923 kEntry,
2924 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002925 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002926 };
2927
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002929 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2930 SetPackedField<BoundaryKindField>(kind);
2931 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002932
2933 bool IsControlFlow() const OVERRIDE { return true; }
2934
2935 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002936 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002937
David Brazdild26a4112015-11-10 11:07:31 +00002938 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2939 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2940 }
2941
David Brazdilfc6a86a2015-06-26 10:33:45 +00002942 // Returns whether `handler` is among its exception handlers (non-zero index
2943 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002944 bool HasExceptionHandler(const HBasicBlock& handler) const {
2945 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002946 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002947 }
2948
2949 // If not present already, adds `handler` to its block's list of exception
2950 // handlers.
2951 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002952 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002953 GetBlock()->AddSuccessor(handler);
2954 }
2955 }
2956
Vladimir Markoa1de9182016-02-25 11:37:38 +00002957 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2958 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002959
David Brazdilffee3d32015-07-06 11:48:53 +01002960 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2961
David Brazdilfc6a86a2015-06-26 10:33:45 +00002962 DECLARE_INSTRUCTION(TryBoundary);
2963
2964 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002965 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2966 static constexpr size_t kFieldBoundaryKindSize =
2967 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2968 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2969 kFieldBoundaryKind + kFieldBoundaryKindSize;
2970 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2971 "Too many packed fields.");
2972 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002973
2974 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2975};
2976
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002977// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002978class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002979 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002980 enum class Kind {
2981 kBCE,
2982 kInline,
2983 kLast = kInline
2984 };
2985
2986 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2987 // across.
2988 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2989 : HVariableInputSizeInstruction(
2990 SideEffects::All(),
2991 dex_pc,
2992 arena,
2993 /* number_of_inputs */ 1,
2994 kArenaAllocMisc) {
2995 SetPackedFlag<kFieldCanBeMoved>(false);
2996 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002997 SetRawInputAt(0, cond);
2998 }
2999
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003000 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3001 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3002 // instead of `guard`.
3003 // We set CanTriggerGC to prevent any intermediate address to be live
3004 // at the point of the `HDeoptimize`.
3005 HDeoptimize(ArenaAllocator* arena,
3006 HInstruction* cond,
3007 HInstruction* guard,
3008 Kind kind,
3009 uint32_t dex_pc)
3010 : HVariableInputSizeInstruction(
3011 SideEffects::CanTriggerGC(),
3012 dex_pc,
3013 arena,
3014 /* number_of_inputs */ 2,
3015 kArenaAllocMisc) {
3016 SetPackedFlag<kFieldCanBeMoved>(true);
3017 SetPackedField<DeoptimizeKindField>(kind);
3018 SetRawInputAt(0, cond);
3019 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003020 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003021
3022 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3023
3024 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3025 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3026 }
3027
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003028 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003029
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003030 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003031
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003032 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3033
3034 Primitive::Type GetType() const OVERRIDE {
3035 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3036 }
3037
3038 bool GuardsAnInput() const {
3039 return InputCount() == 2;
3040 }
3041
3042 HInstruction* GuardedInput() const {
3043 DCHECK(GuardsAnInput());
3044 return InputAt(1);
3045 }
3046
3047 void RemoveGuard() {
3048 RemoveInputAt(1);
3049 }
3050
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003051 DECLARE_INSTRUCTION(Deoptimize);
3052
3053 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003054 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3055 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3056 static constexpr size_t kFieldDeoptimizeKindSize =
3057 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3058 static constexpr size_t kNumberOfDeoptimizePackedBits =
3059 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3060 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3061 "Too many packed fields.");
3062 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3063
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003064 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3065};
3066
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003067std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3068
Mingyao Yang063fc772016-08-02 11:02:54 -07003069// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3070// The compiled code checks this flag value in a guard before devirtualized call and
3071// if it's true, starts to do deoptimization.
3072// It has a 4-byte slot on stack.
3073// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003074class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003075 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003076 // CHA guards are only optimized in a separate pass and it has no side effects
3077 // with regard to other passes.
3078 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3079 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003080 }
3081
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003082 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3083
3084 // We do all CHA guard elimination/motion in a single pass, after which there is no
3085 // further guard elimination/motion since a guard might have been used for justification
3086 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3087 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003088 bool CanBeMoved() const OVERRIDE { return false; }
3089
3090 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3091
3092 private:
3093 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3094};
3095
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003096// Represents the ArtMethod that was passed as a first argument to
3097// the method. It is used by instructions that depend on it, like
3098// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003099class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003100 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003101 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3102 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003103
3104 DECLARE_INSTRUCTION(CurrentMethod);
3105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3108};
3109
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003110// Fetches an ArtMethod from the virtual table or the interface method table
3111// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003112class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003113 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003115 kVTable,
3116 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003118 };
3119 HClassTableGet(HInstruction* cls,
3120 Primitive::Type type,
3121 TableKind kind,
3122 size_t index,
3123 uint32_t dex_pc)
3124 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 index_(index) {
3126 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127 SetRawInputAt(0, cls);
3128 }
3129
3130 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003131 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003134 }
3135
Vladimir Markoa1de9182016-02-25 11:37:38 +00003136 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003137 size_t GetIndex() const { return index_; }
3138
3139 DECLARE_INSTRUCTION(ClassTableGet);
3140
3141 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003142 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3143 static constexpr size_t kFieldTableKindSize =
3144 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3145 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3146 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3147 "Too many packed fields.");
3148 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3149
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003150 // The index of the ArtMethod in the table.
3151 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003152
3153 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3154};
3155
Mark Mendellfe57faa2015-09-18 09:26:15 -04003156// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3157// have one successor for each entry in the switch table, and the final successor
3158// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003159class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003160 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003161 HPackedSwitch(int32_t start_value,
3162 uint32_t num_entries,
3163 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003164 uint32_t dex_pc = kNoDexPc)
3165 : HTemplateInstruction(SideEffects::None(), dex_pc),
3166 start_value_(start_value),
3167 num_entries_(num_entries) {
3168 SetRawInputAt(0, input);
3169 }
3170
3171 bool IsControlFlow() const OVERRIDE { return true; }
3172
3173 int32_t GetStartValue() const { return start_value_; }
3174
Vladimir Marko211c2112015-09-24 16:52:33 +01003175 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003176
3177 HBasicBlock* GetDefaultBlock() const {
3178 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003179 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003180 }
3181 DECLARE_INSTRUCTION(PackedSwitch);
3182
3183 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003184 const int32_t start_value_;
3185 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003186
3187 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3188};
3189
Roland Levillain88cb1752014-10-20 16:36:47 +01003190class HUnaryOperation : public HExpression<1> {
3191 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003192 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3193 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003194 SetRawInputAt(0, input);
3195 }
3196
3197 HInstruction* GetInput() const { return InputAt(0); }
3198 Primitive::Type GetResultType() const { return GetType(); }
3199
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003200 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003201 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003202 return true;
3203 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003204
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 // Try to statically evaluate `this` and return a HConstant
3206 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003207 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003208 HConstant* TryStaticEvaluation() const;
3209
3210 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003211 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3212 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003213 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3214 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003215
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003216 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003217
3218 private:
3219 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3220};
3221
Dave Allison20dfc792014-06-16 20:44:29 -07003222class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003223 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003224 HBinaryOperation(Primitive::Type result_type,
3225 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003226 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003227 SideEffects side_effects = SideEffects::None(),
3228 uint32_t dex_pc = kNoDexPc)
3229 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003230 SetRawInputAt(0, left);
3231 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003232 }
3233
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003234 HInstruction* GetLeft() const { return InputAt(0); }
3235 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003236 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003237
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003238 virtual bool IsCommutative() const { return false; }
3239
3240 // Put constant on the right.
3241 // Returns whether order is changed.
3242 bool OrderInputsWithConstantOnTheRight() {
3243 HInstruction* left = InputAt(0);
3244 HInstruction* right = InputAt(1);
3245 if (left->IsConstant() && !right->IsConstant()) {
3246 ReplaceInput(right, 0);
3247 ReplaceInput(left, 1);
3248 return true;
3249 }
3250 return false;
3251 }
3252
3253 // Order inputs by instruction id, but favor constant on the right side.
3254 // This helps GVN for commutative ops.
3255 void OrderInputs() {
3256 DCHECK(IsCommutative());
3257 HInstruction* left = InputAt(0);
3258 HInstruction* right = InputAt(1);
3259 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3260 return;
3261 }
3262 if (OrderInputsWithConstantOnTheRight()) {
3263 return;
3264 }
3265 // Order according to instruction id.
3266 if (left->GetId() > right->GetId()) {
3267 ReplaceInput(right, 0);
3268 ReplaceInput(left, 1);
3269 }
3270 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003271
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003272 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003273 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003274 return true;
3275 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003276
Roland Levillain31dd3d62016-02-16 12:21:02 +00003277 // Try to statically evaluate `this` and return a HConstant
3278 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003279 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003280 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003281
3282 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3284 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003285 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3286 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003287 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003288 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3289 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003290 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3291 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003292 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3293 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003294 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003295 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3296 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003297
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003298 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003299 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003300 HConstant* GetConstantRight() const;
3301
3302 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003303 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003304 HInstruction* GetLeastConstantLeft() const;
3305
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003306 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003307
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003308 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003309 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3310};
3311
Mark Mendellc4701932015-04-10 13:18:51 -04003312// The comparison bias applies for floating point operations and indicates how NaN
3313// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003314enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003315 kNoBias, // bias is not applicable (i.e. for long operation)
3316 kGtBias, // return 1 for NaN comparisons
3317 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003318 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003319};
3320
Roland Levillain31dd3d62016-02-16 12:21:02 +00003321std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3322
Dave Allison20dfc792014-06-16 20:44:29 -07003323class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003324 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003325 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003326 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3327 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3328 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003329
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003330 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003331 // `instruction`, and disregard moves in between.
3332 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003333
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003334 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003335
3336 virtual IfCondition GetCondition() const = 0;
3337
Mark Mendellc4701932015-04-10 13:18:51 -04003338 virtual IfCondition GetOppositeCondition() const = 0;
3339
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003341 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3342
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3344 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003345
Vladimir Marko372f10e2016-05-17 16:30:10 +01003346 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003347 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003348 }
3349
Roland Levillain4fa13f62015-07-06 18:11:54 +01003350 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003352 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003353 if (if_cond == kCondNE) {
3354 return true;
3355 } else if (if_cond == kCondEQ) {
3356 return false;
3357 }
3358 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003359 }
3360
3361 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003362 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003363 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003364 if (if_cond == kCondEQ) {
3365 return true;
3366 } else if (if_cond == kCondNE) {
3367 return false;
3368 }
3369 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003370 }
3371
Roland Levillain31dd3d62016-02-16 12:21:02 +00003372 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003373 // Needed if we merge a HCompare into a HCondition.
3374 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3375 static constexpr size_t kFieldComparisonBiasSize =
3376 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3377 static constexpr size_t kNumberOfConditionPackedBits =
3378 kFieldComparisonBias + kFieldComparisonBiasSize;
3379 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3380 using ComparisonBiasField =
3381 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3382
Roland Levillain31dd3d62016-02-16 12:21:02 +00003383 template <typename T>
3384 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3385
3386 template <typename T>
3387 int32_t CompareFP(T x, T y) const {
3388 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3389 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3390 // Handle the bias.
3391 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3392 }
3393
3394 // Return an integer constant containing the result of a condition evaluated at compile time.
3395 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3396 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3397 }
3398
Dave Allison20dfc792014-06-16 20:44:29 -07003399 private:
3400 DISALLOW_COPY_AND_ASSIGN(HCondition);
3401};
3402
3403// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003404class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003405 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003406 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3407 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003408
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003409 bool IsCommutative() const OVERRIDE { return true; }
3410
Vladimir Marko9e23df52015-11-10 17:14:35 +00003411 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3412 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003413 return MakeConstantCondition(true, GetDexPc());
3414 }
3415 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3416 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3417 }
3418 // In the following Evaluate methods, a HCompare instruction has
3419 // been merged into this HEqual instruction; evaluate it as
3420 // `Compare(x, y) == 0`.
3421 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3422 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3423 GetDexPc());
3424 }
3425 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3426 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3427 }
3428 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3429 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003430 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003431
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003432 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003434 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003435 return kCondEQ;
3436 }
3437
Mark Mendellc4701932015-04-10 13:18:51 -04003438 IfCondition GetOppositeCondition() const OVERRIDE {
3439 return kCondNE;
3440 }
3441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003442 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003443 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003444
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003445 DISALLOW_COPY_AND_ASSIGN(HEqual);
3446};
3447
Vladimir Markofcb503c2016-05-18 12:48:17 +01003448class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003450 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3451 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003452
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003453 bool IsCommutative() const OVERRIDE { return true; }
3454
Vladimir Marko9e23df52015-11-10 17:14:35 +00003455 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3456 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003457 return MakeConstantCondition(false, GetDexPc());
3458 }
3459 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3460 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3461 }
3462 // In the following Evaluate methods, a HCompare instruction has
3463 // been merged into this HNotEqual instruction; evaluate it as
3464 // `Compare(x, y) != 0`.
3465 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3466 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3467 }
3468 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3469 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3470 }
3471 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3472 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003473 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003474
Dave Allison20dfc792014-06-16 20:44:29 -07003475 DECLARE_INSTRUCTION(NotEqual);
3476
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003477 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003478 return kCondNE;
3479 }
3480
Mark Mendellc4701932015-04-10 13:18:51 -04003481 IfCondition GetOppositeCondition() const OVERRIDE {
3482 return kCondEQ;
3483 }
3484
Dave Allison20dfc792014-06-16 20:44:29 -07003485 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003486 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003487
Dave Allison20dfc792014-06-16 20:44:29 -07003488 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3489};
3490
Vladimir Markofcb503c2016-05-18 12:48:17 +01003491class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003492 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003493 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3494 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003495
Roland Levillain9867bc72015-08-05 10:21:34 +01003496 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003498 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003499 // In the following Evaluate methods, a HCompare instruction has
3500 // been merged into this HLessThan instruction; evaluate it as
3501 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003502 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003503 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3504 }
3505 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3506 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3507 }
3508 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3509 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003510 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003511
Dave Allison20dfc792014-06-16 20:44:29 -07003512 DECLARE_INSTRUCTION(LessThan);
3513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003514 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003515 return kCondLT;
3516 }
3517
Mark Mendellc4701932015-04-10 13:18:51 -04003518 IfCondition GetOppositeCondition() const OVERRIDE {
3519 return kCondGE;
3520 }
3521
Dave Allison20dfc792014-06-16 20:44:29 -07003522 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003523 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003524
Dave Allison20dfc792014-06-16 20:44:29 -07003525 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3526};
3527
Vladimir Markofcb503c2016-05-18 12:48:17 +01003528class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003530 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3531 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003532
Roland Levillain9867bc72015-08-05 10:21:34 +01003533 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003534 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003535 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 // In the following Evaluate methods, a HCompare instruction has
3537 // been merged into this HLessThanOrEqual instruction; evaluate it as
3538 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003539 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003540 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3541 }
3542 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3543 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3544 }
3545 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3546 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003547 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003548
Dave Allison20dfc792014-06-16 20:44:29 -07003549 DECLARE_INSTRUCTION(LessThanOrEqual);
3550
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003551 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003552 return kCondLE;
3553 }
3554
Mark Mendellc4701932015-04-10 13:18:51 -04003555 IfCondition GetOppositeCondition() const OVERRIDE {
3556 return kCondGT;
3557 }
3558
Dave Allison20dfc792014-06-16 20:44:29 -07003559 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003560 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003561
Dave Allison20dfc792014-06-16 20:44:29 -07003562 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3563};
3564
Vladimir Markofcb503c2016-05-18 12:48:17 +01003565class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003566 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003567 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3568 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003569
Roland Levillain9867bc72015-08-05 10:21:34 +01003570 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003571 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003572 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 // In the following Evaluate methods, a HCompare instruction has
3574 // been merged into this HGreaterThan instruction; evaluate it as
3575 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003576 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003577 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3578 }
3579 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3580 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3581 }
3582 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3583 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003584 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003585
Dave Allison20dfc792014-06-16 20:44:29 -07003586 DECLARE_INSTRUCTION(GreaterThan);
3587
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003588 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003589 return kCondGT;
3590 }
3591
Mark Mendellc4701932015-04-10 13:18:51 -04003592 IfCondition GetOppositeCondition() const OVERRIDE {
3593 return kCondLE;
3594 }
3595
Dave Allison20dfc792014-06-16 20:44:29 -07003596 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003597 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003598
Dave Allison20dfc792014-06-16 20:44:29 -07003599 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3600};
3601
Vladimir Markofcb503c2016-05-18 12:48:17 +01003602class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003603 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003604 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3605 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003606
Roland Levillain9867bc72015-08-05 10:21:34 +01003607 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003608 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003609 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 // In the following Evaluate methods, a HCompare instruction has
3611 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3612 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003613 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003614 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3615 }
3616 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3617 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3618 }
3619 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3620 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003621 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003622
Dave Allison20dfc792014-06-16 20:44:29 -07003623 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3624
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003625 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003626 return kCondGE;
3627 }
3628
Mark Mendellc4701932015-04-10 13:18:51 -04003629 IfCondition GetOppositeCondition() const OVERRIDE {
3630 return kCondLT;
3631 }
3632
Dave Allison20dfc792014-06-16 20:44:29 -07003633 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003634 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003635
Dave Allison20dfc792014-06-16 20:44:29 -07003636 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3637};
3638
Vladimir Markofcb503c2016-05-18 12:48:17 +01003639class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003640 public:
3641 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3642 : HCondition(first, second, dex_pc) {}
3643
3644 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003646 }
3647 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003648 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3649 }
3650 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3651 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3652 LOG(FATAL) << DebugName() << " is not defined for float values";
3653 UNREACHABLE();
3654 }
3655 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3656 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3657 LOG(FATAL) << DebugName() << " is not defined for double values";
3658 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003659 }
3660
3661 DECLARE_INSTRUCTION(Below);
3662
3663 IfCondition GetCondition() const OVERRIDE {
3664 return kCondB;
3665 }
3666
3667 IfCondition GetOppositeCondition() const OVERRIDE {
3668 return kCondAE;
3669 }
3670
3671 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003672 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003673 return MakeUnsigned(x) < MakeUnsigned(y);
3674 }
Aart Bike9f37602015-10-09 11:15:55 -07003675
3676 DISALLOW_COPY_AND_ASSIGN(HBelow);
3677};
3678
Vladimir Markofcb503c2016-05-18 12:48:17 +01003679class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003680 public:
3681 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3682 : HCondition(first, second, dex_pc) {}
3683
3684 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003686 }
3687 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3689 }
3690 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3691 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3692 LOG(FATAL) << DebugName() << " is not defined for float values";
3693 UNREACHABLE();
3694 }
3695 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3696 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3697 LOG(FATAL) << DebugName() << " is not defined for double values";
3698 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003699 }
3700
3701 DECLARE_INSTRUCTION(BelowOrEqual);
3702
3703 IfCondition GetCondition() const OVERRIDE {
3704 return kCondBE;
3705 }
3706
3707 IfCondition GetOppositeCondition() const OVERRIDE {
3708 return kCondA;
3709 }
3710
3711 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003712 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003713 return MakeUnsigned(x) <= MakeUnsigned(y);
3714 }
Aart Bike9f37602015-10-09 11:15:55 -07003715
3716 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3717};
3718
Vladimir Markofcb503c2016-05-18 12:48:17 +01003719class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003720 public:
3721 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3722 : HCondition(first, second, dex_pc) {}
3723
3724 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003726 }
3727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3729 }
3730 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3731 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3732 LOG(FATAL) << DebugName() << " is not defined for float values";
3733 UNREACHABLE();
3734 }
3735 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3736 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3737 LOG(FATAL) << DebugName() << " is not defined for double values";
3738 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003739 }
3740
3741 DECLARE_INSTRUCTION(Above);
3742
3743 IfCondition GetCondition() const OVERRIDE {
3744 return kCondA;
3745 }
3746
3747 IfCondition GetOppositeCondition() const OVERRIDE {
3748 return kCondBE;
3749 }
3750
3751 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003752 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003753 return MakeUnsigned(x) > MakeUnsigned(y);
3754 }
Aart Bike9f37602015-10-09 11:15:55 -07003755
3756 DISALLOW_COPY_AND_ASSIGN(HAbove);
3757};
3758
Vladimir Markofcb503c2016-05-18 12:48:17 +01003759class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003760 public:
3761 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3762 : HCondition(first, second, dex_pc) {}
3763
3764 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003766 }
3767 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003768 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3769 }
3770 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3771 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3772 LOG(FATAL) << DebugName() << " is not defined for float values";
3773 UNREACHABLE();
3774 }
3775 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3776 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3777 LOG(FATAL) << DebugName() << " is not defined for double values";
3778 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003779 }
3780
3781 DECLARE_INSTRUCTION(AboveOrEqual);
3782
3783 IfCondition GetCondition() const OVERRIDE {
3784 return kCondAE;
3785 }
3786
3787 IfCondition GetOppositeCondition() const OVERRIDE {
3788 return kCondB;
3789 }
3790
3791 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003792 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003793 return MakeUnsigned(x) >= MakeUnsigned(y);
3794 }
Aart Bike9f37602015-10-09 11:15:55 -07003795
3796 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3797};
Dave Allison20dfc792014-06-16 20:44:29 -07003798
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003799// Instruction to check how two inputs compare to each other.
3800// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003801class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003802 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003803 // Note that `comparison_type` is the type of comparison performed
3804 // between the comparison's inputs, not the type of the instantiated
3805 // HCompare instruction (which is always Primitive::kPrimInt).
3806 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003807 HInstruction* first,
3808 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003809 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003810 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003811 : HBinaryOperation(Primitive::kPrimInt,
3812 first,
3813 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003814 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003815 dex_pc) {
3816 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003817 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3818 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003819 }
3820
Roland Levillain9867bc72015-08-05 10:21:34 +01003821 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003822 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3823
3824 template <typename T>
3825 int32_t ComputeFP(T x, T y) const {
3826 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3827 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3828 // Handle the bias.
3829 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3830 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003831
Roland Levillain9867bc72015-08-05 10:21:34 +01003832 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3834 // reach this code path when processing a freshly built HIR
3835 // graph. However HCompare integer instructions can be synthesized
3836 // by the instruction simplifier to implement IntegerCompare and
3837 // IntegerSignum intrinsics, so we have to handle this case.
3838 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003839 }
3840 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003841 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3842 }
3843 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3844 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3845 }
3846 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3847 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003848 }
3849
Vladimir Marko372f10e2016-05-17 16:30:10 +01003850 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003851 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003852 }
3853
Vladimir Markoa1de9182016-02-25 11:37:38 +00003854 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003855
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003857 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 bool IsGtBias() const {
3859 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003860 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003861 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003862
Roland Levillain1693a1f2016-03-15 14:57:31 +00003863 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3864 // Comparisons do not require a runtime call in any back end.
3865 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003866 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003867
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003868 DECLARE_INSTRUCTION(Compare);
3869
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003871 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3872 static constexpr size_t kFieldComparisonBiasSize =
3873 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3874 static constexpr size_t kNumberOfComparePackedBits =
3875 kFieldComparisonBias + kFieldComparisonBiasSize;
3876 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3877 using ComparisonBiasField =
3878 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3879
Roland Levillain31dd3d62016-02-16 12:21:02 +00003880 // Return an integer constant containing the result of a comparison evaluated at compile time.
3881 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3882 DCHECK(value == -1 || value == 0 || value == 1) << value;
3883 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3884 }
3885
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003886 private:
3887 DISALLOW_COPY_AND_ASSIGN(HCompare);
3888};
3889
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003890class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003891 public:
3892 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003893 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003894 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003895 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003896 bool finalizable,
3897 QuickEntrypointEnum entrypoint)
3898 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3899 type_index_(type_index),
3900 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003901 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003902 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003903 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003904 }
3905
Andreas Gampea5b09a62016-11-17 15:21:22 -08003906 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003907 const DexFile& GetDexFile() const { return dex_file_; }
3908
3909 // Calls runtime so needs an environment.
3910 bool NeedsEnvironment() const OVERRIDE { return true; }
3911
Mingyao Yang062157f2016-03-02 10:15:36 -08003912 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3913 bool CanThrow() const OVERRIDE { return true; }
3914
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003915 bool NeedsChecks() const {
3916 return entrypoint_ == kQuickAllocObjectWithChecks;
3917 }
David Brazdil6de19382016-01-08 17:37:10 +00003918
Vladimir Markoa1de9182016-02-25 11:37:38 +00003919 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003920
3921 bool CanBeNull() const OVERRIDE { return false; }
3922
3923 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3924
3925 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3926 entrypoint_ = entrypoint;
3927 }
3928
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003929 HLoadClass* GetLoadClass() const {
3930 HInstruction* input = InputAt(0);
3931 if (input->IsClinitCheck()) {
3932 input = input->InputAt(0);
3933 }
3934 DCHECK(input->IsLoadClass());
3935 return input->AsLoadClass();
3936 }
3937
David Brazdil6de19382016-01-08 17:37:10 +00003938 bool IsStringAlloc() const;
3939
3940 DECLARE_INSTRUCTION(NewInstance);
3941
3942 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003943 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003944 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3945 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3946 "Too many packed fields.");
3947
Andreas Gampea5b09a62016-11-17 15:21:22 -08003948 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003949 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003950 QuickEntrypointEnum entrypoint_;
3951
3952 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3953};
3954
Agi Csaki05f20562015-08-19 14:58:14 -07003955enum IntrinsicNeedsEnvironmentOrCache {
3956 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3957 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003958};
3959
Aart Bik5d75afe2015-12-14 11:57:01 -08003960enum IntrinsicSideEffects {
3961 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3962 kReadSideEffects, // Intrinsic may read heap memory.
3963 kWriteSideEffects, // Intrinsic may write heap memory.
3964 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3965};
3966
3967enum IntrinsicExceptions {
3968 kNoThrow, // Intrinsic does not throw any exceptions.
3969 kCanThrow // Intrinsic may throw exceptions.
3970};
3971
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003972class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003973 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003974 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003975
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003976 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003977 SetRawInputAt(index, argument);
3978 }
3979
Roland Levillain3e3d7332015-04-28 11:00:54 +01003980 // Return the number of arguments. This number can be lower than
3981 // the number of inputs returned by InputCount(), as some invoke
3982 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3983 // inputs at the end of their list of inputs.
3984 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3985
Vladimir Markoa1de9182016-02-25 11:37:38 +00003986 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003987
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003988 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3989
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003990 InvokeType GetInvokeType() const {
3991 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003992 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003993
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003994 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003995 return intrinsic_;
3996 }
3997
Aart Bik5d75afe2015-12-14 11:57:01 -08003998 void SetIntrinsic(Intrinsics intrinsic,
3999 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4000 IntrinsicSideEffects side_effects,
4001 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004002
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004003 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004004 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004005 }
4006
Aart Bikff7d89c2016-11-07 08:49:28 -08004007 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4008
Vladimir Markoa1de9182016-02-25 11:37:38 +00004009 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004010
Aart Bik71bf7b42016-11-16 10:17:46 -08004011 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004012
Vladimir Marko372f10e2016-05-17 16:30:10 +01004013 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004014 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4015 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004016
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004017 uint32_t* GetIntrinsicOptimizations() {
4018 return &intrinsic_optimizations_;
4019 }
4020
4021 const uint32_t* GetIntrinsicOptimizations() const {
4022 return &intrinsic_optimizations_;
4023 }
4024
4025 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4026
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004027 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004028 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004029
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004030 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004031
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004032 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004033 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4034 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4036 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004037 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004038 static constexpr size_t kFieldReturnTypeSize =
4039 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4040 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4041 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4042 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004043 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004044 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4045
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004046 HInvoke(ArenaAllocator* arena,
4047 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004048 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004049 Primitive::Type return_type,
4050 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004051 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004052 ArtMethod* resolved_method,
4053 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004054 : HVariableInputSizeInstruction(
4055 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4056 dex_pc,
4057 arena,
4058 number_of_arguments + number_of_other_inputs,
4059 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004060 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004061 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004062 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004063 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004064 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004065 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004067 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004068 }
4069
Roland Levillain3e3d7332015-04-28 11:00:54 +01004070 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004071 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004072 const uint32_t dex_method_index_;
4073 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004074
4075 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4076 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004077
4078 private:
4079 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4080};
4081
Vladimir Markofcb503c2016-05-18 12:48:17 +01004082class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004083 public:
4084 HInvokeUnresolved(ArenaAllocator* arena,
4085 uint32_t number_of_arguments,
4086 Primitive::Type return_type,
4087 uint32_t dex_pc,
4088 uint32_t dex_method_index,
4089 InvokeType invoke_type)
4090 : HInvoke(arena,
4091 number_of_arguments,
4092 0u /* number_of_other_inputs */,
4093 return_type,
4094 dex_pc,
4095 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004096 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004097 invoke_type) {
4098 }
4099
4100 DECLARE_INSTRUCTION(InvokeUnresolved);
4101
4102 private:
4103 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4104};
4105
Orion Hodsonac141392017-01-13 11:53:47 +00004106class HInvokePolymorphic FINAL : public HInvoke {
4107 public:
4108 HInvokePolymorphic(ArenaAllocator* arena,
4109 uint32_t number_of_arguments,
4110 Primitive::Type return_type,
4111 uint32_t dex_pc,
4112 uint32_t dex_method_index)
4113 : HInvoke(arena,
4114 number_of_arguments,
4115 0u /* number_of_other_inputs */,
4116 return_type,
4117 dex_pc,
4118 dex_method_index,
4119 nullptr,
4120 kVirtual) {}
4121
4122 DECLARE_INSTRUCTION(InvokePolymorphic);
4123
4124 private:
4125 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4126};
4127
Vladimir Markofcb503c2016-05-18 12:48:17 +01004128class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004129 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004130 // Requirements of this method call regarding the class
4131 // initialization (clinit) check of its declaring class.
4132 enum class ClinitCheckRequirement {
4133 kNone, // Class already initialized.
4134 kExplicit, // Static call having explicit clinit check as last input.
4135 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004136 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004137 };
4138
Vladimir Marko58155012015-08-19 12:49:41 +00004139 // Determines how to load the target ArtMethod*.
4140 enum class MethodLoadKind {
4141 // Use a String init ArtMethod* loaded from Thread entrypoints.
4142 kStringInit,
4143
4144 // Use the method's own ArtMethod* loaded by the register allocator.
4145 kRecursive,
4146
4147 // Use ArtMethod* at a known address, embed the direct address in the code.
4148 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4149 kDirectAddress,
4150
Vladimir Markoa1de9182016-02-25 11:37:38 +00004151 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004152 // Used when we need to use the dex cache, for example for invoke-static that
4153 // may cause class initialization (the entry may point to a resolution method),
4154 // and we know that we can access the dex cache arrays using a PC-relative load.
4155 kDexCachePcRelative,
4156
4157 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4158 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4159 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4160 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4161 kDexCacheViaMethod,
4162 };
4163
4164 // Determines the location of the code pointer.
4165 enum class CodePtrLocation {
4166 // Recursive call, use local PC-relative call instruction.
4167 kCallSelf,
4168
Vladimir Marko58155012015-08-19 12:49:41 +00004169 // Use code pointer from the ArtMethod*.
4170 // Used when we don't know the target code. This is also the last-resort-kind used when
4171 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4172 kCallArtMethod,
4173 };
4174
4175 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004176 MethodLoadKind method_load_kind;
4177 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004178 // The method load data holds
4179 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4180 // Note that there are multiple string init methods, each having its own offset.
4181 // - the method address for kDirectAddress
4182 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004183 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004184 };
4185
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004186 HInvokeStaticOrDirect(ArenaAllocator* arena,
4187 uint32_t number_of_arguments,
4188 Primitive::Type return_type,
4189 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004190 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004191 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004192 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004193 InvokeType invoke_type,
4194 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004195 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004196 : HInvoke(arena,
4197 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004198 // There is potentially one extra argument for the HCurrentMethod node, and
4199 // potentially one other if the clinit check is explicit, and potentially
4200 // one other if the method is a string factory.
4201 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004202 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004203 return_type,
4204 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004205 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004206 resolved_method,
4207 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004208 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004209 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004210 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4211 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004212
Vladimir Markodc151b22015-10-15 18:02:30 +01004213 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004214 bool had_current_method_input = HasCurrentMethodInput();
4215 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4216
4217 // Using the current method is the default and once we find a better
4218 // method load kind, we should not go back to using the current method.
4219 DCHECK(had_current_method_input || !needs_current_method_input);
4220
4221 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004222 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4223 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004224 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004225 dispatch_info_ = dispatch_info;
4226 }
4227
Aart Bik6daebeb2017-04-03 14:35:41 -07004228 DispatchInfo GetDispatchInfo() const {
4229 return dispatch_info_;
4230 }
4231
Vladimir Markoc53c0792015-11-19 15:48:33 +00004232 void AddSpecialInput(HInstruction* input) {
4233 // We allow only one special input.
4234 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4235 DCHECK(InputCount() == GetSpecialInputIndex() ||
4236 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4237 InsertInputAt(GetSpecialInputIndex(), input);
4238 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004239
Vladimir Marko372f10e2016-05-17 16:30:10 +01004240 using HInstruction::GetInputRecords; // Keep the const version visible.
4241 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4242 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4243 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4244 DCHECK(!input_records.empty());
4245 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4246 HInstruction* last_input = input_records.back().GetInstruction();
4247 // Note: `last_input` may be null during arguments setup.
4248 if (last_input != nullptr) {
4249 // `last_input` is the last input of a static invoke marked as having
4250 // an explicit clinit check. It must either be:
4251 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4252 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4253 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4254 }
4255 }
4256 return input_records;
4257 }
4258
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004259 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004260 // We access the method via the dex cache so we can't do an implicit null check.
4261 // TODO: for intrinsics we can generate implicit null checks.
4262 return false;
4263 }
4264
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004265 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004266 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004267 }
4268
Vladimir Markoc53c0792015-11-19 15:48:33 +00004269 // Get the index of the special input, if any.
4270 //
David Brazdil6de19382016-01-08 17:37:10 +00004271 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4272 // method pointer; otherwise there may be one platform-specific special input,
4273 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004274 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004275 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004276
Vladimir Marko58155012015-08-19 12:49:41 +00004277 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4278 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4279 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004280 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004281 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004282 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004283 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004284 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4285 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004286 bool HasCurrentMethodInput() const {
4287 // This function can be called only after the invoke has been fully initialized by the builder.
4288 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004289 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004290 return true;
4291 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004292 DCHECK(InputCount() == GetSpecialInputIndex() ||
4293 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004294 return false;
4295 }
4296 }
Vladimir Marko58155012015-08-19 12:49:41 +00004297
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004298 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004299 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004300 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004301 }
4302
4303 uint64_t GetMethodAddress() const {
4304 DCHECK(HasMethodAddress());
4305 return dispatch_info_.method_load_data;
4306 }
4307
4308 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004309 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004310 return dispatch_info_.method_load_data;
4311 }
4312
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004313 const DexFile& GetDexFileForPcRelativeDexCache() const;
4314
Vladimir Markoa1de9182016-02-25 11:37:38 +00004315 ClinitCheckRequirement GetClinitCheckRequirement() const {
4316 return GetPackedField<ClinitCheckRequirementField>();
4317 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004318
Roland Levillain4c0eb422015-04-24 16:43:49 +01004319 // Is this instruction a call to a static method?
4320 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004321 return GetInvokeType() == kStatic;
4322 }
4323
4324 MethodReference GetTargetMethod() const {
4325 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004326 }
4327
Vladimir Markofbb184a2015-11-13 14:47:00 +00004328 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4329 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4330 // instruction; only relevant for static calls with explicit clinit check.
4331 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004333 size_t last_input_index = inputs_.size() - 1u;
4334 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004335 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004336 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004337 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004338 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004339 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004340 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004341 }
4342
4343 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004344 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004346 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004347 }
4348
4349 // Is this a call to a static method whose declaring class has an
4350 // implicit intialization check requirement?
4351 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004352 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004353 }
4354
Vladimir Markob554b5a2015-11-06 12:57:55 +00004355 // Does this method load kind need the current method as an input?
4356 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4357 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4358 }
4359
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004360 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004361
4362 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004363 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004364 static constexpr size_t kFieldClinitCheckRequirementSize =
4365 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4366 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4367 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4368 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4369 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004370 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4371 kFieldClinitCheckRequirement,
4372 kFieldClinitCheckRequirementSize>;
4373
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004374 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004375 MethodReference target_method_;
4376 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004378 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379};
Vladimir Markof64242a2015-12-01 14:58:23 +00004380std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004381std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004382
Vladimir Markofcb503c2016-05-18 12:48:17 +01004383class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004384 public:
4385 HInvokeVirtual(ArenaAllocator* arena,
4386 uint32_t number_of_arguments,
4387 Primitive::Type return_type,
4388 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004389 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004390 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004391 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004392 : HInvoke(arena,
4393 number_of_arguments,
4394 0u,
4395 return_type,
4396 dex_pc,
4397 dex_method_index,
4398 resolved_method,
4399 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004400 vtable_index_(vtable_index) {}
4401
Aart Bik71bf7b42016-11-16 10:17:46 -08004402 bool CanBeNull() const OVERRIDE {
4403 switch (GetIntrinsic()) {
4404 case Intrinsics::kThreadCurrentThread:
4405 case Intrinsics::kStringBufferAppend:
4406 case Intrinsics::kStringBufferToString:
4407 case Intrinsics::kStringBuilderAppend:
4408 case Intrinsics::kStringBuilderToString:
4409 return false;
4410 default:
4411 return HInvoke::CanBeNull();
4412 }
4413 }
4414
Calin Juravle641547a2015-04-21 22:08:51 +01004415 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004416 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004417 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004418 }
4419
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004420 uint32_t GetVTableIndex() const { return vtable_index_; }
4421
4422 DECLARE_INSTRUCTION(InvokeVirtual);
4423
4424 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004425 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004426 const uint32_t vtable_index_;
4427
4428 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4429};
4430
Vladimir Markofcb503c2016-05-18 12:48:17 +01004431class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004432 public:
4433 HInvokeInterface(ArenaAllocator* arena,
4434 uint32_t number_of_arguments,
4435 Primitive::Type return_type,
4436 uint32_t dex_pc,
4437 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004438 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004439 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004440 : HInvoke(arena,
4441 number_of_arguments,
4442 0u,
4443 return_type,
4444 dex_pc,
4445 dex_method_index,
4446 resolved_method,
4447 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004448 imt_index_(imt_index) {}
4449
Calin Juravle641547a2015-04-21 22:08:51 +01004450 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004451 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004452 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004453 }
4454
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004455 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4456 // The assembly stub currently needs it.
4457 return true;
4458 }
4459
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004460 uint32_t GetImtIndex() const { return imt_index_; }
4461 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4462
4463 DECLARE_INSTRUCTION(InvokeInterface);
4464
4465 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004466 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004467 const uint32_t imt_index_;
4468
4469 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4470};
4471
Vladimir Markofcb503c2016-05-18 12:48:17 +01004472class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004473 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004474 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004475 : HUnaryOperation(result_type, input, dex_pc) {
4476 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4477 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004478
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004479 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004480
4481 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
4484 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004485 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004486 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004487 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4488 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4489 }
4490 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4491 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4492 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004493
Roland Levillain88cb1752014-10-20 16:36:47 +01004494 DECLARE_INSTRUCTION(Neg);
4495
4496 private:
4497 DISALLOW_COPY_AND_ASSIGN(HNeg);
4498};
4499
Vladimir Markofcb503c2016-05-18 12:48:17 +01004500class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004501 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004502 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4503 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4504 SetRawInputAt(0, cls);
4505 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004506 }
4507
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004508 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004509 bool NeedsEnvironment() const OVERRIDE { return true; }
4510
Mingyao Yang0c365e62015-03-31 15:09:29 -07004511 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4512 bool CanThrow() const OVERRIDE { return true; }
4513
Calin Juravle10e244f2015-01-26 18:54:32 +00004514 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004515
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004516 HLoadClass* GetLoadClass() const {
4517 DCHECK(InputAt(0)->IsLoadClass());
4518 return InputAt(0)->AsLoadClass();
4519 }
4520
4521 HInstruction* GetLength() const {
4522 return InputAt(1);
4523 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004524
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525 DECLARE_INSTRUCTION(NewArray);
4526
4527 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004528 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4529};
4530
Vladimir Markofcb503c2016-05-18 12:48:17 +01004531class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004532 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 HAdd(Primitive::Type result_type,
4534 HInstruction* left,
4535 HInstruction* right,
4536 uint32_t dex_pc = kNoDexPc)
4537 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004538
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004539 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004540
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004541 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004542
4543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 return GetBlock()->GetGraph()->GetIntConstant(
4545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004546 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004547 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004548 return GetBlock()->GetGraph()->GetLongConstant(
4549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004550 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004551 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4552 return GetBlock()->GetGraph()->GetFloatConstant(
4553 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4554 }
4555 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4556 return GetBlock()->GetGraph()->GetDoubleConstant(
4557 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4558 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004559
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004560 DECLARE_INSTRUCTION(Add);
4561
4562 private:
4563 DISALLOW_COPY_AND_ASSIGN(HAdd);
4564};
4565
Vladimir Markofcb503c2016-05-18 12:48:17 +01004566class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004567 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004568 HSub(Primitive::Type result_type,
4569 HInstruction* left,
4570 HInstruction* right,
4571 uint32_t dex_pc = kNoDexPc)
4572 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004573
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004574 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575
4576 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 return GetBlock()->GetGraph()->GetIntConstant(
4578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004579 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004580 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004581 return GetBlock()->GetGraph()->GetLongConstant(
4582 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004583 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004584 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4585 return GetBlock()->GetGraph()->GetFloatConstant(
4586 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4587 }
4588 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4589 return GetBlock()->GetGraph()->GetDoubleConstant(
4590 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4591 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004592
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004593 DECLARE_INSTRUCTION(Sub);
4594
4595 private:
4596 DISALLOW_COPY_AND_ASSIGN(HSub);
4597};
4598
Vladimir Markofcb503c2016-05-18 12:48:17 +01004599class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004600 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 HMul(Primitive::Type result_type,
4602 HInstruction* left,
4603 HInstruction* right,
4604 uint32_t dex_pc = kNoDexPc)
4605 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004606
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004607 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004608
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004609 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004610
4611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 return GetBlock()->GetGraph()->GetIntConstant(
4613 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
4615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004616 return GetBlock()->GetGraph()->GetLongConstant(
4617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004618 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004619 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4620 return GetBlock()->GetGraph()->GetFloatConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4622 }
4623 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4624 return GetBlock()->GetGraph()->GetDoubleConstant(
4625 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4626 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004627
4628 DECLARE_INSTRUCTION(Mul);
4629
4630 private:
4631 DISALLOW_COPY_AND_ASSIGN(HMul);
4632};
4633
Vladimir Markofcb503c2016-05-18 12:48:17 +01004634class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004636 HDiv(Primitive::Type result_type,
4637 HInstruction* left,
4638 HInstruction* right,
4639 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004640 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004641
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004643 T ComputeIntegral(T x, T y) const {
4644 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 // Our graph structure ensures we never have 0 for `y` during
4646 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004647 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004648 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004649 return (y == -1) ? -x : x / y;
4650 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004651
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 template <typename T>
4653 T ComputeFP(T x, T y) const {
4654 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4655 return x / y;
4656 }
4657
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004661 }
4662 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004663 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004664 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4665 }
4666 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4667 return GetBlock()->GetGraph()->GetFloatConstant(
4668 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4669 }
4670 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4671 return GetBlock()->GetGraph()->GetDoubleConstant(
4672 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004673 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004674
4675 DECLARE_INSTRUCTION(Div);
4676
4677 private:
4678 DISALLOW_COPY_AND_ASSIGN(HDiv);
4679};
4680
Vladimir Markofcb503c2016-05-18 12:48:17 +01004681class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 HRem(Primitive::Type result_type,
4684 HInstruction* left,
4685 HInstruction* right,
4686 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004687 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004688
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004690 T ComputeIntegral(T x, T y) const {
4691 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004692 // Our graph structure ensures we never have 0 for `y` during
4693 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004694 DCHECK_NE(y, 0);
4695 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4696 return (y == -1) ? 0 : x % y;
4697 }
4698
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 template <typename T>
4700 T ComputeFP(T x, T y) const {
4701 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4702 return std::fmod(x, y);
4703 }
4704
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004708 }
4709 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004710 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004712 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004713 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4714 return GetBlock()->GetGraph()->GetFloatConstant(
4715 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4716 }
4717 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4718 return GetBlock()->GetGraph()->GetDoubleConstant(
4719 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4720 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004721
4722 DECLARE_INSTRUCTION(Rem);
4723
4724 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004725 DISALLOW_COPY_AND_ASSIGN(HRem);
4726};
4727
Vladimir Markofcb503c2016-05-18 12:48:17 +01004728class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004729 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004730 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4731 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004732 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004733 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004734 SetRawInputAt(0, value);
4735 }
4736
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004737 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4738
Calin Juravled0d48522014-11-04 16:40:20 +00004739 bool CanBeMoved() const OVERRIDE { return true; }
4740
Vladimir Marko372f10e2016-05-17 16:30:10 +01004741 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004742 return true;
4743 }
4744
4745 bool NeedsEnvironment() const OVERRIDE { return true; }
4746 bool CanThrow() const OVERRIDE { return true; }
4747
Calin Juravled0d48522014-11-04 16:40:20 +00004748 DECLARE_INSTRUCTION(DivZeroCheck);
4749
4750 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004751 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4752};
4753
Vladimir Markofcb503c2016-05-18 12:48:17 +01004754class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004755 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 HInstruction* value,
4758 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004759 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4761 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4762 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 }
4764
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004766 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 return value << (distance & max_shift_distance);
4768 }
4769
4770 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004775 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004776 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4779 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4780 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4781 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4784 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for float values";
4786 UNREACHABLE();
4787 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4789 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004790 LOG(FATAL) << DebugName() << " is not defined for double values";
4791 UNREACHABLE();
4792 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004793
4794 DECLARE_INSTRUCTION(Shl);
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HShl);
4798};
4799
Vladimir Markofcb503c2016-05-18 12:48:17 +01004800class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 HInstruction* value,
4804 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004805 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4807 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4808 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 }
4810
Roland Levillain5b5b9312016-03-22 14:57:31 +00004811 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004812 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 return value >> (distance & max_shift_distance);
4814 }
4815
4816 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004821 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004822 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4825 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4826 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4827 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004829 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4830 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 LOG(FATAL) << DebugName() << " is not defined for float values";
4832 UNREACHABLE();
4833 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4835 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004836 LOG(FATAL) << DebugName() << " is not defined for double values";
4837 UNREACHABLE();
4838 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004839
4840 DECLARE_INSTRUCTION(Shr);
4841
4842 private:
4843 DISALLOW_COPY_AND_ASSIGN(HShr);
4844};
4845
Vladimir Markofcb503c2016-05-18 12:48:17 +01004846class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004847 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 HInstruction* value,
4850 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4853 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4854 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004855 }
4856
Roland Levillain5b5b9312016-03-22 14:57:31 +00004857 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004858 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 typedef typename std::make_unsigned<T>::type V;
4860 V ux = static_cast<V>(value);
4861 return static_cast<T>(ux >> (distance & max_shift_distance));
4862 }
4863
4864 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004868 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004870 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004871 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4873 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4874 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4875 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004876 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4878 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 LOG(FATAL) << DebugName() << " is not defined for float values";
4880 UNREACHABLE();
4881 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4883 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004884 LOG(FATAL) << DebugName() << " is not defined for double values";
4885 UNREACHABLE();
4886 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004887
4888 DECLARE_INSTRUCTION(UShr);
4889
4890 private:
4891 DISALLOW_COPY_AND_ASSIGN(HUShr);
4892};
4893
Vladimir Markofcb503c2016-05-18 12:48:17 +01004894class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004895 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004896 HAnd(Primitive::Type result_type,
4897 HInstruction* left,
4898 HInstruction* right,
4899 uint32_t dex_pc = kNoDexPc)
4900 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004901
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004902 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004903
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004904 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004905
4906 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 return GetBlock()->GetGraph()->GetIntConstant(
4908 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004909 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004911 return GetBlock()->GetGraph()->GetLongConstant(
4912 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004913 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004914 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4915 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for float values";
4917 UNREACHABLE();
4918 }
4919 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4920 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4921 LOG(FATAL) << DebugName() << " is not defined for double values";
4922 UNREACHABLE();
4923 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004924
4925 DECLARE_INSTRUCTION(And);
4926
4927 private:
4928 DISALLOW_COPY_AND_ASSIGN(HAnd);
4929};
4930
Vladimir Markofcb503c2016-05-18 12:48:17 +01004931class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004932 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004933 HOr(Primitive::Type result_type,
4934 HInstruction* left,
4935 HInstruction* right,
4936 uint32_t dex_pc = kNoDexPc)
4937 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004939 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004940
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004941 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004942
4943 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 return GetBlock()->GetGraph()->GetIntConstant(
4945 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004947 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 return GetBlock()->GetGraph()->GetLongConstant(
4949 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004950 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004951 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4952 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4953 LOG(FATAL) << DebugName() << " is not defined for float values";
4954 UNREACHABLE();
4955 }
4956 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4957 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4958 LOG(FATAL) << DebugName() << " is not defined for double values";
4959 UNREACHABLE();
4960 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004961
4962 DECLARE_INSTRUCTION(Or);
4963
4964 private:
4965 DISALLOW_COPY_AND_ASSIGN(HOr);
4966};
4967
Vladimir Markofcb503c2016-05-18 12:48:17 +01004968class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004970 HXor(Primitive::Type result_type,
4971 HInstruction* left,
4972 HInstruction* right,
4973 uint32_t dex_pc = kNoDexPc)
4974 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004975
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004976 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004977
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004978 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004979
4980 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetIntConstant(
4982 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 return GetBlock()->GetGraph()->GetLongConstant(
4986 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004987 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004988 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4989 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4990 LOG(FATAL) << DebugName() << " is not defined for float values";
4991 UNREACHABLE();
4992 }
4993 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4994 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4995 LOG(FATAL) << DebugName() << " is not defined for double values";
4996 UNREACHABLE();
4997 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004998
4999 DECLARE_INSTRUCTION(Xor);
5000
5001 private:
5002 DISALLOW_COPY_AND_ASSIGN(HXor);
5003};
5004
Vladimir Markofcb503c2016-05-18 12:48:17 +01005005class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005006 public:
5007 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005008 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005009 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5010 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005011 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005012
Roland Levillain5b5b9312016-03-22 14:57:31 +00005013 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005014 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005015 typedef typename std::make_unsigned<T>::type V;
5016 V ux = static_cast<V>(value);
5017 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005018 return static_cast<T>(ux);
5019 } else {
5020 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 return static_cast<T>(ux >> (distance & max_shift_value)) |
5022 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005023 }
5024 }
5025
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005027 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005029 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005030 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005031 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005032 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5035 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5036 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5037 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005038 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5040 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 LOG(FATAL) << DebugName() << " is not defined for float values";
5042 UNREACHABLE();
5043 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005044 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5045 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005046 LOG(FATAL) << DebugName() << " is not defined for double values";
5047 UNREACHABLE();
5048 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005049
5050 DECLARE_INSTRUCTION(Ror);
5051
5052 private:
5053 DISALLOW_COPY_AND_ASSIGN(HRor);
5054};
5055
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005056// The value of a parameter in this method. Its location depends on
5057// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005058class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005059 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005060 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005061 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005062 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005063 Primitive::Type parameter_type,
5064 bool is_this = false)
5065 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005066 dex_file_(dex_file),
5067 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005068 index_(index) {
5069 SetPackedFlag<kFlagIsThis>(is_this);
5070 SetPackedFlag<kFlagCanBeNull>(!is_this);
5071 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005073 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005074 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005075 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005076 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005077
Vladimir Markoa1de9182016-02-25 11:37:38 +00005078 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5079 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005080
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005081 DECLARE_INSTRUCTION(ParameterValue);
5082
5083 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005084 // Whether or not the parameter value corresponds to 'this' argument.
5085 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5086 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5087 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5088 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5089 "Too many packed fields.");
5090
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005091 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005092 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005093 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005094 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005095 const uint8_t index_;
5096
5097 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5098};
5099
Vladimir Markofcb503c2016-05-18 12:48:17 +01005100class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005101 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005102 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5103 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005104
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005105 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005106 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005107 return true;
5108 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005109
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005110 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005111
5112 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 }
5115 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005117 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005118 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5119 LOG(FATAL) << DebugName() << " is not defined for float values";
5120 UNREACHABLE();
5121 }
5122 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5123 LOG(FATAL) << DebugName() << " is not defined for double values";
5124 UNREACHABLE();
5125 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005126
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005127 DECLARE_INSTRUCTION(Not);
5128
5129 private:
5130 DISALLOW_COPY_AND_ASSIGN(HNot);
5131};
5132
Vladimir Markofcb503c2016-05-18 12:48:17 +01005133class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005134 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005135 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5136 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005137
5138 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005139 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005140 return true;
5141 }
5142
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005143 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005144 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005145 return !x;
5146 }
5147
Roland Levillain9867bc72015-08-05 10:21:34 +01005148 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005150 }
5151 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5152 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005153 UNREACHABLE();
5154 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005155 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5156 LOG(FATAL) << DebugName() << " is not defined for float values";
5157 UNREACHABLE();
5158 }
5159 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5160 LOG(FATAL) << DebugName() << " is not defined for double values";
5161 UNREACHABLE();
5162 }
David Brazdil66d126e2015-04-03 16:02:44 +01005163
5164 DECLARE_INSTRUCTION(BooleanNot);
5165
5166 private:
5167 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5168};
5169
Vladimir Markofcb503c2016-05-18 12:48:17 +01005170class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005171 public:
5172 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005173 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005174 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005175 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005176 // Invariant: We should never generate a conversion to a Boolean value.
5177 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005178 }
5179
5180 HInstruction* GetInput() const { return InputAt(0); }
5181 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5182 Primitive::Type GetResultType() const { return GetType(); }
5183
5184 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005185 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5186 return true;
5187 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005188
Mark Mendelle82549b2015-05-06 10:55:34 -04005189 // Try to statically evaluate the conversion and return a HConstant
5190 // containing the result. If the input cannot be converted, return nullptr.
5191 HConstant* TryStaticEvaluation() const;
5192
Roland Levillaindff1f282014-11-05 14:15:05 +00005193 DECLARE_INSTRUCTION(TypeConversion);
5194
5195 private:
5196 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5197};
5198
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005199static constexpr uint32_t kNoRegNumber = -1;
5200
Vladimir Markofcb503c2016-05-18 12:48:17 +01005201class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005203 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5204 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005205 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005206 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 SetRawInputAt(0, value);
5208 }
5209
Calin Juravle10e244f2015-01-26 18:54:32 +00005210 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005211 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005212 return true;
5213 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005214
Calin Juravle10e244f2015-01-26 18:54:32 +00005215 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216
Calin Juravle10e244f2015-01-26 18:54:32 +00005217 bool CanThrow() const OVERRIDE { return true; }
5218
5219 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005220
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005221
5222 DECLARE_INSTRUCTION(NullCheck);
5223
5224 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5226};
5227
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005228// Embeds an ArtField and all the information required by the compiler. We cache
5229// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230class FieldInfo : public ValueObject {
5231 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005232 FieldInfo(ArtField* field,
5233 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005234 Primitive::Type field_type,
5235 bool is_volatile,
5236 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005237 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005238 const DexFile& dex_file)
5239 : field_(field),
5240 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005241 field_type_(field_type),
5242 is_volatile_(is_volatile),
5243 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005244 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005245 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005246
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005247 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005248 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005249 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005250 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005251 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005252 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005253 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005254
5255 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005256 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005257 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005258 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005259 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005260 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005261 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005262 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005263};
5264
Vladimir Markofcb503c2016-05-18 12:48:17 +01005265class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266 public:
5267 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005268 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005269 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005270 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005271 bool is_volatile,
5272 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005273 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005274 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005275 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005276 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005277 field_info_(field,
5278 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005279 field_type,
5280 is_volatile,
5281 field_idx,
5282 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005283 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005284 SetRawInputAt(0, value);
5285 }
5286
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005287 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005288
Vladimir Marko372f10e2016-05-17 16:30:10 +01005289 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5290 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005291 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005292 }
5293
Calin Juravle641547a2015-04-21 22:08:51 +01005294 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005295 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005296 }
5297
5298 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005299 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5300 }
5301
Calin Juravle52c48962014-12-16 17:02:57 +00005302 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005303 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005304 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005305 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005306
5307 DECLARE_INSTRUCTION(InstanceFieldGet);
5308
5309 private:
5310 const FieldInfo field_info_;
5311
5312 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5313};
5314
Vladimir Markofcb503c2016-05-18 12:48:17 +01005315class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005316 public:
5317 HInstanceFieldSet(HInstruction* object,
5318 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005319 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005320 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005321 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005322 bool is_volatile,
5323 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005324 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005325 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005326 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005327 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005328 field_info_(field,
5329 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005330 field_type,
5331 is_volatile,
5332 field_idx,
5333 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005334 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005336 SetRawInputAt(0, object);
5337 SetRawInputAt(1, value);
5338 }
5339
Calin Juravle641547a2015-04-21 22:08:51 +01005340 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005341 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 }
5343
Calin Juravle52c48962014-12-16 17:02:57 +00005344 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005345 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005346 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005347 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005348 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005349 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5350 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005352 DECLARE_INSTRUCTION(InstanceFieldSet);
5353
5354 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005355 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5356 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5357 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5358 "Too many packed fields.");
5359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005360 const FieldInfo field_info_;
5361
5362 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5363};
5364
Vladimir Markofcb503c2016-05-18 12:48:17 +01005365class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005367 HArrayGet(HInstruction* array,
5368 HInstruction* index,
5369 Primitive::Type type,
5370 uint32_t dex_pc,
5371 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005372 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005373 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 SetRawInputAt(0, array);
5375 SetRawInputAt(1, index);
5376 }
5377
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005378 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005379 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005380 return true;
5381 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005382 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005383 // TODO: We can be smarter here.
5384 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5385 // which generates the implicit null check. There are cases when these can be removed
5386 // to produce better code. If we ever add optimizations to do so we should allow an
5387 // implicit check here (as long as the address falls in the first page).
5388 return false;
5389 }
5390
David Brazdil4833f5a2015-12-16 10:37:39 +00005391 bool IsEquivalentOf(HArrayGet* other) const {
5392 bool result = (GetDexPc() == other->GetDexPc());
5393 if (kIsDebugBuild && result) {
5394 DCHECK_EQ(GetBlock(), other->GetBlock());
5395 DCHECK_EQ(GetArray(), other->GetArray());
5396 DCHECK_EQ(GetIndex(), other->GetIndex());
5397 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005398 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005399 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005400 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5401 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005402 }
5403 }
5404 return result;
5405 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005406
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005407 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5408
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005409 HInstruction* GetArray() const { return InputAt(0); }
5410 HInstruction* GetIndex() const { return InputAt(1); }
5411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412 DECLARE_INSTRUCTION(ArrayGet);
5413
5414 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005415 // We treat a String as an array, creating the HArrayGet from String.charAt()
5416 // intrinsic in the instruction simplifier. We can always determine whether
5417 // a particular HArrayGet is actually a String.charAt() by looking at the type
5418 // of the input but that requires holding the mutator lock, so we prefer to use
5419 // a flag, so that code generators don't need to do the locking.
5420 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5421 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5422 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5423 "Too many packed fields.");
5424
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5426};
5427
Vladimir Markofcb503c2016-05-18 12:48:17 +01005428class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 public:
5430 HArraySet(HInstruction* array,
5431 HInstruction* index,
5432 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005433 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005434 uint32_t dex_pc)
5435 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005436 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5437 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5438 SetPackedFlag<kFlagValueCanBeNull>(true);
5439 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 SetRawInputAt(0, array);
5441 SetRawInputAt(1, index);
5442 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005443 // Make a best guess now, may be refined during SSA building.
5444 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 }
5446
Calin Juravle77520bc2015-01-12 18:45:46 +00005447 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005448 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005449 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005450 }
5451
Mingyao Yang81014cb2015-06-02 03:16:27 -07005452 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005455 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005456 // TODO: Same as for ArrayGet.
5457 return false;
5458 }
5459
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005460 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005462 }
5463
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005464 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005465 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005466 }
5467
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005468 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005470 }
5471
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5473 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5474 bool StaticTypeOfArrayIsObjectArray() const {
5475 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5476 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005477
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005478 HInstruction* GetArray() const { return InputAt(0); }
5479 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005480 HInstruction* GetValue() const { return InputAt(2); }
5481
5482 Primitive::Type GetComponentType() const {
5483 // The Dex format does not type floating point index operations. Since the
5484 // `expected_component_type_` is set during building and can therefore not
5485 // be correct, we also check what is the value type. If it is a floating
5486 // point type, we must use that type.
5487 Primitive::Type value_type = GetValue()->GetType();
5488 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5489 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005490 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005491 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005492
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005493 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005494 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005495 }
5496
Aart Bik18b36ab2016-04-13 16:41:35 -07005497 void ComputeSideEffects() {
5498 Primitive::Type type = GetComponentType();
5499 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5500 SideEffectsForArchRuntimeCalls(type)));
5501 }
5502
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005503 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5504 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5505 }
5506
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005507 DECLARE_INSTRUCTION(ArraySet);
5508
5509 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5511 static constexpr size_t kFieldExpectedComponentTypeSize =
5512 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5513 static constexpr size_t kFlagNeedsTypeCheck =
5514 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5515 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005516 // Cached information for the reference_type_info_ so that codegen
5517 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005518 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5519 static constexpr size_t kNumberOfArraySetPackedBits =
5520 kFlagStaticTypeOfArrayIsObjectArray + 1;
5521 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5522 using ExpectedComponentTypeField =
5523 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524
5525 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5526};
5527
Vladimir Markofcb503c2016-05-18 12:48:17 +01005528class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005530 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005531 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005532 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005533 // Note that arrays do not change length, so the instruction does not
5534 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535 SetRawInputAt(0, array);
5536 }
5537
Calin Juravle77520bc2015-01-12 18:45:46 +00005538 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005539 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005540 return true;
5541 }
Calin Juravle641547a2015-04-21 22:08:51 +01005542 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5543 return obj == InputAt(0);
5544 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005545
Vladimir Markodce016e2016-04-28 13:10:02 +01005546 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5547
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 DECLARE_INSTRUCTION(ArrayLength);
5549
5550 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005551 // We treat a String as an array, creating the HArrayLength from String.length()
5552 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5553 // determine whether a particular HArrayLength is actually a String.length() by
5554 // looking at the type of the input but that requires holding the mutator lock, so
5555 // we prefer to use a flag, so that code generators don't need to do the locking.
5556 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5557 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5558 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5559 "Too many packed fields.");
5560
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5562};
5563
Vladimir Markofcb503c2016-05-18 12:48:17 +01005564class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005565 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005566 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5567 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005568 HBoundsCheck(HInstruction* index,
5569 HInstruction* length,
5570 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005571 bool string_char_at = false)
5572 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005573 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005574 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005575 SetRawInputAt(0, index);
5576 SetRawInputAt(1, length);
5577 }
5578
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005579 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005580 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005581 return true;
5582 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005584 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005586 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005587
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005588 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005589
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005590 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005591
5592 DECLARE_INSTRUCTION(BoundsCheck);
5593
5594 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005595 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005596
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005597 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5598};
5599
Vladimir Markofcb503c2016-05-18 12:48:17 +01005600class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005601 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005602 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005603 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005604
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005605 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005606 return true;
5607 }
5608
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005609 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5610 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005611
5612 DECLARE_INSTRUCTION(SuspendCheck);
5613
5614 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005615 // Only used for code generation, in order to share the same slow path between back edges
5616 // of a same loop.
5617 SlowPathCode* slow_path_;
5618
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005619 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5620};
5621
David Srbecky0cf44932015-12-09 14:09:59 +00005622// Pseudo-instruction which provides the native debugger with mapping information.
5623// It ensures that we can generate line number and local variables at this point.
5624class HNativeDebugInfo : public HTemplateInstruction<0> {
5625 public:
5626 explicit HNativeDebugInfo(uint32_t dex_pc)
5627 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5628
5629 bool NeedsEnvironment() const OVERRIDE {
5630 return true;
5631 }
5632
5633 DECLARE_INSTRUCTION(NativeDebugInfo);
5634
5635 private:
5636 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5637};
5638
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639/**
5640 * Instruction to load a Class object.
5641 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005642class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005643 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005644 // Determines how to load the Class.
5645 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005646 // We cannot load this class. See HSharpening::SharpenLoadClass.
5647 kInvalid = -1,
5648
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005649 // Use the Class* from the method's own ArtMethod*.
5650 kReferrersClass,
5651
5652 // Use boot image Class* address that will be known at link time.
5653 // Used for boot image classes referenced by boot image code in non-PIC mode.
5654 kBootImageLinkTimeAddress,
5655
5656 // Use PC-relative boot image Class* address that will be known at link time.
5657 // Used for boot image classes referenced by boot image code in PIC mode.
5658 kBootImageLinkTimePcRelative,
5659
5660 // Use a known boot image Class* address, embedded in the code by the codegen.
5661 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 kBootImageAddress,
5663
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005664 // Load from an entry in the .bss section using a PC-relative load.
5665 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5666 kBssEntry,
5667
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005668 // Load from the root table associated with the JIT compiled method.
5669 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005671 // Load from resolved types array accessed through the class loaded from
5672 // the compiled method's own ArtMethod*. This is the default access type when
5673 // all other types are unavailable.
5674 kDexCacheViaMethod,
5675
5676 kLast = kDexCacheViaMethod
5677 };
5678
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005679 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005680 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005681 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005682 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005683 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005684 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005685 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005686 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5687 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005688 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005689 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005690 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005691 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005692 // Referrers class should not need access check. We never inline unverified
5693 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005694 DCHECK(!is_referrers_class || !needs_access_check);
5695
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 SetPackedField<LoadKindField>(
5697 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005698 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005699 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005700 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005701 }
5702
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005703 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005704
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 LoadKind GetLoadKind() const {
5706 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005707 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005708
5709 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005710
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005711 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712
Andreas Gampea5b09a62016-11-17 15:21:22 -08005713 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005714
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005715 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005716
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005717 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005718 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719 }
5720
Calin Juravle0ba218d2015-05-19 18:46:01 +01005721 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005722 // The entrypoint the code generator is going to call does not do
5723 // clinit of the class.
5724 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005725 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005726 }
5727
5728 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005729 return NeedsAccessCheck() ||
5730 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005731 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5732 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005733 }
5734
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005735 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005736 return NeedsAccessCheck() ||
5737 MustGenerateClinitCheck() ||
5738 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5739 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5740 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005741 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5742 GetLoadKind() == LoadKind::kBssEntry) &&
5743 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005744 }
5745
Calin Juravleacf735c2015-02-12 15:25:22 +00005746 ReferenceTypeInfo GetLoadedClassRTI() {
5747 return loaded_class_rti_;
5748 }
5749
5750 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5751 // Make sure we only set exact types (the loaded class should never be merged).
5752 DCHECK(rti.IsExact());
5753 loaded_class_rti_ = rti;
5754 }
5755
Andreas Gampea5b09a62016-11-17 15:21:22 -08005756 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005757 const DexFile& GetDexFile() const { return dex_file_; }
5758
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005759 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005760 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005761 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005762
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005763 static SideEffects SideEffectsForArchRuntimeCalls() {
5764 return SideEffects::CanTriggerGC();
5765 }
5766
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005767 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005768 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005769 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005770 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005771
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005772 void MarkInBootImage() {
5773 SetPackedFlag<kFlagIsInBootImage>(true);
5774 }
5775
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005776 void AddSpecialInput(HInstruction* special_input);
5777
5778 using HInstruction::GetInputRecords; // Keep the const version visible.
5779 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5780 return ArrayRef<HUserRecord<HInstruction*>>(
5781 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5782 }
5783
5784 Primitive::Type GetType() const OVERRIDE {
5785 return Primitive::kPrimNot;
5786 }
5787
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005788 Handle<mirror::Class> GetClass() const {
5789 return klass_;
5790 }
5791
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005792 DECLARE_INSTRUCTION(LoadClass);
5793
5794 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005795 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005796 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005797 // Whether this instruction must generate the initialization check.
5798 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005799 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5801 static constexpr size_t kFieldLoadKindSize =
5802 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5803 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005804 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5806
5807 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005808 return load_kind == LoadKind::kReferrersClass ||
5809 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005810 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005811 load_kind == LoadKind::kBssEntry ||
5812 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005813 }
5814
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 void SetLoadKindInternal(LoadKind load_kind);
5816
5817 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5818 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005819 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005820 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005821
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005822 // A type index and dex file where the class can be accessed. The dex file can be:
5823 // - The compiling method's dex file if the class is defined there too.
5824 // - The compiling method's dex file if the class is referenced there.
5825 // - The dex file where the class is defined. When the load kind can only be
5826 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005827 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005828 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005829
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005830 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831
Calin Juravleacf735c2015-02-12 15:25:22 +00005832 ReferenceTypeInfo loaded_class_rti_;
5833
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005834 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5835};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005836std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5837
5838// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005839inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005840 // The special input is used for PC-relative loads on some architectures,
5841 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005842 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005843 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005844 GetLoadKind() == LoadKind::kBootImageAddress ||
5845 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005846 DCHECK(special_input_.GetInstruction() == nullptr);
5847 special_input_ = HUserRecord<HInstruction*>(special_input);
5848 special_input->AddUseAt(this, 0);
5849}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005850
Vladimir Marko372f10e2016-05-17 16:30:10 +01005851class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005852 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 // Determines how to load the String.
5854 enum class LoadKind {
5855 // Use boot image String* address that will be known at link time.
5856 // Used for boot image strings referenced by boot image code in non-PIC mode.
5857 kBootImageLinkTimeAddress,
5858
5859 // Use PC-relative boot image String* address that will be known at link time.
5860 // Used for boot image strings referenced by boot image code in PIC mode.
5861 kBootImageLinkTimePcRelative,
5862
5863 // Use a known boot image String* address, embedded in the code by the codegen.
5864 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865 kBootImageAddress,
5866
Vladimir Markoaad75c62016-10-03 08:46:48 +00005867 // Load from an entry in the .bss section using a PC-relative load.
5868 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5869 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005871 // Load from the root table associated with the JIT compiled method.
5872 kJitTableAddress,
5873
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 // Load from resolved strings array accessed through the class loaded from
5875 // the compiled method's own ArtMethod*. This is the default access type when
5876 // all other types are unavailable.
5877 kDexCacheViaMethod,
5878
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880 };
5881
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005882 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005883 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884 const DexFile& dex_file,
5885 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5887 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005888 string_index_(string_index),
5889 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005891 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005892
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005893 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005895 LoadKind GetLoadKind() const {
5896 return GetPackedField<LoadKindField>();
5897 }
5898
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005899 const DexFile& GetDexFile() const {
5900 return dex_file_;
5901 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005903 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904 return string_index_;
5905 }
5906
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005907 Handle<mirror::String> GetString() const {
5908 return string_;
5909 }
5910
5911 void SetString(Handle<mirror::String> str) {
5912 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913 }
5914
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005915 bool CanBeMoved() const OVERRIDE { return true; }
5916
Vladimir Marko372f10e2016-05-17 16:30:10 +01005917 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005918
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005919 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005920
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 // Will call the runtime if we need to load the string through
5922 // the dex cache and the string is not guaranteed to be there yet.
5923 bool NeedsEnvironment() const OVERRIDE {
5924 LoadKind load_kind = GetLoadKind();
5925 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5926 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005927 load_kind == LoadKind::kBootImageAddress ||
5928 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 return false;
5930 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005931 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005933
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5935 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5936 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005937
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005938 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005939 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005940
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005941 static SideEffects SideEffectsForArchRuntimeCalls() {
5942 return SideEffects::CanTriggerGC();
5943 }
5944
Vladimir Marko372f10e2016-05-17 16:30:10 +01005945 void AddSpecialInput(HInstruction* special_input);
5946
5947 using HInstruction::GetInputRecords; // Keep the const version visible.
5948 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5949 return ArrayRef<HUserRecord<HInstruction*>>(
5950 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005951 }
5952
Vladimir Marko372f10e2016-05-17 16:30:10 +01005953 Primitive::Type GetType() const OVERRIDE {
5954 return Primitive::kPrimNot;
5955 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005957 DECLARE_INSTRUCTION(LoadString);
5958
5959 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005960 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961 static constexpr size_t kFieldLoadKindSize =
5962 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5963 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005964 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005965 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005966
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 void SetLoadKindInternal(LoadKind load_kind);
5968
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005969 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5970 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005971 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005972 HUserRecord<HInstruction*> special_input_;
5973
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005974 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005975 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005977 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005978
5979 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5980};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5982
5983// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005984inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005985 // The special input is used for PC-relative loads on some architectures,
5986 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005988 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005989 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5990 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005991 // HLoadString::GetInputRecords() returns an empty array at this point,
5992 // so use the GetInputRecords() from the base class to set the input record.
5993 DCHECK(special_input_.GetInstruction() == nullptr);
5994 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995 special_input->AddUseAt(this, 0);
5996}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005997
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005998/**
5999 * Performs an initialization check on its Class object input.
6000 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006001class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006002 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006003 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006004 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006005 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006006 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6007 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008 SetRawInputAt(0, constant);
6009 }
6010
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006011 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006012 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006013 return true;
6014 }
6015
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006016 bool NeedsEnvironment() const OVERRIDE {
6017 // May call runtime to initialize the class.
6018 return true;
6019 }
6020
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006021 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006023 HLoadClass* GetLoadClass() const {
6024 DCHECK(InputAt(0)->IsLoadClass());
6025 return InputAt(0)->AsLoadClass();
6026 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027
6028 DECLARE_INSTRUCTION(ClinitCheck);
6029
6030 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006031 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6032};
6033
Vladimir Markofcb503c2016-05-18 12:48:17 +01006034class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006035 public:
6036 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006037 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006039 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006040 bool is_volatile,
6041 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006042 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006043 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006044 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006045 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006046 field_info_(field,
6047 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006048 field_type,
6049 is_volatile,
6050 field_idx,
6051 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006052 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006053 SetRawInputAt(0, cls);
6054 }
6055
Calin Juravle52c48962014-12-16 17:02:57 +00006056
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006057 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006058
Vladimir Marko372f10e2016-05-17 16:30:10 +01006059 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6060 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006061 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006062 }
6063
6064 size_t ComputeHashCode() const OVERRIDE {
6065 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6066 }
6067
Calin Juravle52c48962014-12-16 17:02:57 +00006068 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006069 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6070 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006071 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006072
6073 DECLARE_INSTRUCTION(StaticFieldGet);
6074
6075 private:
6076 const FieldInfo field_info_;
6077
6078 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6079};
6080
Vladimir Markofcb503c2016-05-18 12:48:17 +01006081class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006082 public:
6083 HStaticFieldSet(HInstruction* cls,
6084 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006085 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006086 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006087 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006088 bool is_volatile,
6089 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006090 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006091 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006092 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006093 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006094 field_info_(field,
6095 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006096 field_type,
6097 is_volatile,
6098 field_idx,
6099 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006100 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006101 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 SetRawInputAt(0, cls);
6103 SetRawInputAt(1, value);
6104 }
6105
Calin Juravle52c48962014-12-16 17:02:57 +00006106 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006107 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6108 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006109 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006111 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006112 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6113 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006115 DECLARE_INSTRUCTION(StaticFieldSet);
6116
6117 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006118 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6119 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6120 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6121 "Too many packed fields.");
6122
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006123 const FieldInfo field_info_;
6124
6125 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6126};
6127
Vladimir Markofcb503c2016-05-18 12:48:17 +01006128class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006129 public:
6130 HUnresolvedInstanceFieldGet(HInstruction* obj,
6131 Primitive::Type field_type,
6132 uint32_t field_index,
6133 uint32_t dex_pc)
6134 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6135 field_index_(field_index) {
6136 SetRawInputAt(0, obj);
6137 }
6138
6139 bool NeedsEnvironment() const OVERRIDE { return true; }
6140 bool CanThrow() const OVERRIDE { return true; }
6141
6142 Primitive::Type GetFieldType() const { return GetType(); }
6143 uint32_t GetFieldIndex() const { return field_index_; }
6144
6145 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6146
6147 private:
6148 const uint32_t field_index_;
6149
6150 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6151};
6152
Vladimir Markofcb503c2016-05-18 12:48:17 +01006153class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006154 public:
6155 HUnresolvedInstanceFieldSet(HInstruction* obj,
6156 HInstruction* value,
6157 Primitive::Type field_type,
6158 uint32_t field_index,
6159 uint32_t dex_pc)
6160 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006161 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006162 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006163 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 SetRawInputAt(0, obj);
6165 SetRawInputAt(1, value);
6166 }
6167
6168 bool NeedsEnvironment() const OVERRIDE { return true; }
6169 bool CanThrow() const OVERRIDE { return true; }
6170
Vladimir Markoa1de9182016-02-25 11:37:38 +00006171 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006172 uint32_t GetFieldIndex() const { return field_index_; }
6173
6174 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6175
6176 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006177 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6178 static constexpr size_t kFieldFieldTypeSize =
6179 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6180 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6181 kFieldFieldType + kFieldFieldTypeSize;
6182 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6183 "Too many packed fields.");
6184 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6185
Calin Juravlee460d1d2015-09-29 04:52:17 +01006186 const uint32_t field_index_;
6187
6188 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6189};
6190
Vladimir Markofcb503c2016-05-18 12:48:17 +01006191class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006192 public:
6193 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6194 uint32_t field_index,
6195 uint32_t dex_pc)
6196 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6197 field_index_(field_index) {
6198 }
6199
6200 bool NeedsEnvironment() const OVERRIDE { return true; }
6201 bool CanThrow() const OVERRIDE { return true; }
6202
6203 Primitive::Type GetFieldType() const { return GetType(); }
6204 uint32_t GetFieldIndex() const { return field_index_; }
6205
6206 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6207
6208 private:
6209 const uint32_t field_index_;
6210
6211 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6212};
6213
Vladimir Markofcb503c2016-05-18 12:48:17 +01006214class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006215 public:
6216 HUnresolvedStaticFieldSet(HInstruction* value,
6217 Primitive::Type field_type,
6218 uint32_t field_index,
6219 uint32_t dex_pc)
6220 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006221 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006223 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006224 SetRawInputAt(0, value);
6225 }
6226
6227 bool NeedsEnvironment() const OVERRIDE { return true; }
6228 bool CanThrow() const OVERRIDE { return true; }
6229
Vladimir Markoa1de9182016-02-25 11:37:38 +00006230 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006231 uint32_t GetFieldIndex() const { return field_index_; }
6232
6233 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6234
6235 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006236 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6237 static constexpr size_t kFieldFieldTypeSize =
6238 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6239 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6240 kFieldFieldType + kFieldFieldTypeSize;
6241 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6242 "Too many packed fields.");
6243 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6244
Calin Juravlee460d1d2015-09-29 04:52:17 +01006245 const uint32_t field_index_;
6246
6247 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6248};
6249
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006251class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006252 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006253 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6254 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006255
David Brazdilbbd733e2015-08-18 17:48:17 +01006256 bool CanBeNull() const OVERRIDE { return false; }
6257
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006258 DECLARE_INSTRUCTION(LoadException);
6259
6260 private:
6261 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6262};
6263
David Brazdilcb1c0552015-08-04 16:22:25 +01006264// Implicit part of move-exception which clears thread-local exception storage.
6265// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006266class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006267 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006268 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6269 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006270
6271 DECLARE_INSTRUCTION(ClearException);
6272
6273 private:
6274 DISALLOW_COPY_AND_ASSIGN(HClearException);
6275};
6276
Vladimir Markofcb503c2016-05-18 12:48:17 +01006277class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006278 public:
6279 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006280 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006281 SetRawInputAt(0, exception);
6282 }
6283
6284 bool IsControlFlow() const OVERRIDE { return true; }
6285
6286 bool NeedsEnvironment() const OVERRIDE { return true; }
6287
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006288 bool CanThrow() const OVERRIDE { return true; }
6289
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006290
6291 DECLARE_INSTRUCTION(Throw);
6292
6293 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006294 DISALLOW_COPY_AND_ASSIGN(HThrow);
6295};
6296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006297/**
6298 * Implementation strategies for the code generator of a HInstanceOf
6299 * or `HCheckCast`.
6300 */
6301enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006302 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006303 kExactCheck, // Can do a single class compare.
6304 kClassHierarchyCheck, // Can just walk the super class chain.
6305 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6306 kInterfaceCheck, // No optimization yet when checking against an interface.
6307 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006308 kArrayCheck, // No optimization yet when checking against a generic array.
6309 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310};
6311
Roland Levillain86503782016-02-11 19:07:30 +00006312std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6313
Vladimir Markofcb503c2016-05-18 12:48:17 +01006314class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006315 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006316 HInstanceOf(HInstruction* object,
6317 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006319 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006320 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006322 dex_pc) {
6323 SetPackedField<TypeCheckKindField>(check_kind);
6324 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006325 SetRawInputAt(0, object);
6326 SetRawInputAt(1, constant);
6327 }
6328
6329 bool CanBeMoved() const OVERRIDE { return true; }
6330
Vladimir Marko372f10e2016-05-17 16:30:10 +01006331 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006332 return true;
6333 }
6334
6335 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006337 }
6338
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006339 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006340 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6341 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6342 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6343 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006344
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006345 static bool CanCallRuntime(TypeCheckKind check_kind) {
6346 // Mips currently does runtime calls for any other checks.
6347 return check_kind != TypeCheckKind::kExactCheck;
6348 }
6349
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006350 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006351 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006352 }
6353
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006354 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006355
6356 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006357 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6358 static constexpr size_t kFieldTypeCheckKindSize =
6359 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6360 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6361 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6362 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6363 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006364
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006365 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6366};
6367
Vladimir Markofcb503c2016-05-18 12:48:17 +01006368class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006369 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006370 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006371 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006372 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6373 SetPackedFlag<kFlagUpperCanBeNull>(true);
6374 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006375 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006376 SetRawInputAt(0, input);
6377 }
6378
David Brazdilf5552582015-12-27 13:36:12 +00006379 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006380 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006382 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006383
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006384 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006385 DCHECK(GetUpperCanBeNull() || !can_be_null);
6386 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006387 }
6388
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006390
Calin Juravleb1498f62015-02-16 13:13:29 +00006391 DECLARE_INSTRUCTION(BoundType);
6392
6393 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006394 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6395 // is false then CanBeNull() cannot be true).
6396 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6397 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6398 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6399 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6400
Calin Juravleb1498f62015-02-16 13:13:29 +00006401 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006402 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6403 // It is used to bound the type in cases like:
6404 // if (x instanceof ClassX) {
6405 // // uper_bound_ will be ClassX
6406 // }
David Brazdilf5552582015-12-27 13:36:12 +00006407 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006408
6409 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6410};
6411
Vladimir Markofcb503c2016-05-18 12:48:17 +01006412class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 public:
6414 HCheckCast(HInstruction* object,
6415 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006417 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006418 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6419 SetPackedField<TypeCheckKindField>(check_kind);
6420 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006421 SetRawInputAt(0, object);
6422 SetRawInputAt(1, constant);
6423 }
6424
6425 bool CanBeMoved() const OVERRIDE { return true; }
6426
Vladimir Marko372f10e2016-05-17 16:30:10 +01006427 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006428 return true;
6429 }
6430
6431 bool NeedsEnvironment() const OVERRIDE {
6432 // Instruction may throw a CheckCastError.
6433 return true;
6434 }
6435
6436 bool CanThrow() const OVERRIDE { return true; }
6437
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6439 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6440 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6441 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006442
6443 DECLARE_INSTRUCTION(CheckCast);
6444
6445 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006446 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6447 static constexpr size_t kFieldTypeCheckKindSize =
6448 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6449 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6450 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6451 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6452 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006453
6454 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006455};
6456
Andreas Gampe26de38b2016-07-27 17:53:11 -07006457/**
6458 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6459 * @details We define the combined barrier types that are actually required
6460 * by the Java Memory Model, rather than using exactly the terminology from
6461 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6462 * primitives. Note that the JSR-133 cookbook generally does not deal with
6463 * store atomicity issues, and the recipes there are not always entirely sufficient.
6464 * The current recipe is as follows:
6465 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6466 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6467 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6468 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6469 * class has final fields.
6470 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6471 * store-to-memory instructions. Only generated together with non-temporal stores.
6472 */
6473enum MemBarrierKind {
6474 kAnyStore,
6475 kLoadAny,
6476 kStoreStore,
6477 kAnyAny,
6478 kNTStoreStore,
6479 kLastBarrierKind = kNTStoreStore
6480};
6481std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6482
Vladimir Markofcb503c2016-05-18 12:48:17 +01006483class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006484 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006485 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006486 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006487 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6488 SetPackedField<BarrierKindField>(barrier_kind);
6489 }
Calin Juravle27df7582015-04-17 19:12:31 +01006490
Vladimir Markoa1de9182016-02-25 11:37:38 +00006491 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006492
6493 DECLARE_INSTRUCTION(MemoryBarrier);
6494
6495 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006496 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6497 static constexpr size_t kFieldBarrierKindSize =
6498 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6499 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6500 kFieldBarrierKind + kFieldBarrierKindSize;
6501 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6502 "Too many packed fields.");
6503 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006504
6505 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6506};
6507
Vladimir Markofcb503c2016-05-18 12:48:17 +01006508class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006509 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006510 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006511 kEnter,
6512 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006514 };
6515
6516 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006517 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006518 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6519 dex_pc) {
6520 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006521 SetRawInputAt(0, object);
6522 }
6523
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006524 // Instruction may go into runtime, so we need an environment.
6525 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006526
6527 bool CanThrow() const OVERRIDE {
6528 // Verifier guarantees that monitor-exit cannot throw.
6529 // This is important because it allows the HGraphBuilder to remove
6530 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6531 return IsEnter();
6532 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006533
Vladimir Markoa1de9182016-02-25 11:37:38 +00006534 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6535 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006536
6537 DECLARE_INSTRUCTION(MonitorOperation);
6538
Calin Juravle52c48962014-12-16 17:02:57 +00006539 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006540 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6541 static constexpr size_t kFieldOperationKindSize =
6542 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6543 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6544 kFieldOperationKind + kFieldOperationKindSize;
6545 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6546 "Too many packed fields.");
6547 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006548
6549 private:
6550 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6551};
6552
Vladimir Markofcb503c2016-05-18 12:48:17 +01006553class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006554 public:
6555 HSelect(HInstruction* condition,
6556 HInstruction* true_value,
6557 HInstruction* false_value,
6558 uint32_t dex_pc)
6559 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6560 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6561
6562 // First input must be `true_value` or `false_value` to allow codegens to
6563 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6564 // that architectures which implement HSelect as a conditional move also
6565 // will not need to invert the condition.
6566 SetRawInputAt(0, false_value);
6567 SetRawInputAt(1, true_value);
6568 SetRawInputAt(2, condition);
6569 }
6570
6571 HInstruction* GetFalseValue() const { return InputAt(0); }
6572 HInstruction* GetTrueValue() const { return InputAt(1); }
6573 HInstruction* GetCondition() const { return InputAt(2); }
6574
6575 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006576 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6577 return true;
6578 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006579
6580 bool CanBeNull() const OVERRIDE {
6581 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6582 }
6583
6584 DECLARE_INSTRUCTION(Select);
6585
6586 private:
6587 DISALLOW_COPY_AND_ASSIGN(HSelect);
6588};
6589
Vladimir Markof9f64412015-09-02 14:05:49 +01006590class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006591 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006592 MoveOperands(Location source,
6593 Location destination,
6594 Primitive::Type type,
6595 HInstruction* instruction)
6596 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006597
6598 Location GetSource() const { return source_; }
6599 Location GetDestination() const { return destination_; }
6600
6601 void SetSource(Location value) { source_ = value; }
6602 void SetDestination(Location value) { destination_ = value; }
6603
6604 // The parallel move resolver marks moves as "in-progress" by clearing the
6605 // destination (but not the source).
6606 Location MarkPending() {
6607 DCHECK(!IsPending());
6608 Location dest = destination_;
6609 destination_ = Location::NoLocation();
6610 return dest;
6611 }
6612
6613 void ClearPending(Location dest) {
6614 DCHECK(IsPending());
6615 destination_ = dest;
6616 }
6617
6618 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006619 DCHECK(source_.IsValid() || destination_.IsInvalid());
6620 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006621 }
6622
6623 // True if this blocks a move from the given location.
6624 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006625 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006626 }
6627
6628 // A move is redundant if it's been eliminated, if its source and
6629 // destination are the same, or if its destination is unneeded.
6630 bool IsRedundant() const {
6631 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6632 }
6633
6634 // We clear both operands to indicate move that's been eliminated.
6635 void Eliminate() {
6636 source_ = destination_ = Location::NoLocation();
6637 }
6638
6639 bool IsEliminated() const {
6640 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6641 return source_.IsInvalid();
6642 }
6643
Alexey Frunze4dda3372015-06-01 18:31:49 -07006644 Primitive::Type GetType() const { return type_; }
6645
Nicolas Geoffray90218252015-04-15 11:56:51 +01006646 bool Is64BitMove() const {
6647 return Primitive::Is64BitType(type_);
6648 }
6649
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006650 HInstruction* GetInstruction() const { return instruction_; }
6651
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006652 private:
6653 Location source_;
6654 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006655 // The type this move is for.
6656 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006657 // The instruction this move is assocatied with. Null when this move is
6658 // for moving an input in the expected locations of user (including a phi user).
6659 // This is only used in debug mode, to ensure we do not connect interval siblings
6660 // in the same parallel move.
6661 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006662};
6663
Roland Levillainc9285912015-12-18 10:38:42 +00006664std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6665
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006666static constexpr size_t kDefaultNumberOfMoves = 4;
6667
Vladimir Markofcb503c2016-05-18 12:48:17 +01006668class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006669 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006670 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006671 : HTemplateInstruction(SideEffects::None(), dex_pc),
6672 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6673 moves_.reserve(kDefaultNumberOfMoves);
6674 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006675
Nicolas Geoffray90218252015-04-15 11:56:51 +01006676 void AddMove(Location source,
6677 Location destination,
6678 Primitive::Type type,
6679 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006680 DCHECK(source.IsValid());
6681 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006682 if (kIsDebugBuild) {
6683 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006684 for (const MoveOperands& move : moves_) {
6685 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006686 // Special case the situation where the move is for the spill slot
6687 // of the instruction.
6688 if ((GetPrevious() == instruction)
6689 || ((GetPrevious() == nullptr)
6690 && instruction->IsPhi()
6691 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006692 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006693 << "Doing parallel moves for the same instruction.";
6694 } else {
6695 DCHECK(false) << "Doing parallel moves for the same instruction.";
6696 }
6697 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006698 }
6699 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006700 for (const MoveOperands& move : moves_) {
6701 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006702 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006703 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006704 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006705 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006706 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006707 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006708 }
6709
Vladimir Marko225b6462015-09-28 12:17:40 +01006710 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006711 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006712 }
6713
Vladimir Marko225b6462015-09-28 12:17:40 +01006714 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006715
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006716 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006717
6718 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006719 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006720
6721 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6722};
6723
Mark Mendell0616ae02015-04-17 12:49:27 -04006724} // namespace art
6725
Aart Bikf8f5a162017-02-06 15:35:29 -08006726#include "nodes_vector.h"
6727
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006728#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6729#include "nodes_shared.h"
6730#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006731#ifdef ART_ENABLE_CODEGEN_arm
6732#include "nodes_arm.h"
6733#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006734#ifdef ART_ENABLE_CODEGEN_mips
6735#include "nodes_mips.h"
6736#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006737#ifdef ART_ENABLE_CODEGEN_x86
6738#include "nodes_x86.h"
6739#endif
6740
6741namespace art {
6742
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006743class HGraphVisitor : public ValueObject {
6744 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006745 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6746 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006747
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006748 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006749 virtual void VisitBasicBlock(HBasicBlock* block);
6750
Roland Levillain633021e2014-10-01 14:12:25 +01006751 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006752 void VisitInsertionOrder();
6753
Roland Levillain633021e2014-10-01 14:12:25 +01006754 // Visit the graph following dominator tree reverse post-order.
6755 void VisitReversePostOrder();
6756
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006757 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006758
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006759 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006760#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006761 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6762
6763 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6764
6765#undef DECLARE_VISIT_INSTRUCTION
6766
6767 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006768 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006769
6770 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6771};
6772
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006773class HGraphDelegateVisitor : public HGraphVisitor {
6774 public:
6775 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6776 virtual ~HGraphDelegateVisitor() {}
6777
6778 // Visit functions that delegate to to super class.
6779#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006780 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006781
6782 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6783
6784#undef DECLARE_VISIT_INSTRUCTION
6785
6786 private:
6787 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6788};
6789
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006790// Iterator over the blocks that art part of the loop. Includes blocks part
6791// of an inner loop. The order in which the blocks are iterated is on their
6792// block id.
6793class HBlocksInLoopIterator : public ValueObject {
6794 public:
6795 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6796 : blocks_in_loop_(info.GetBlocks()),
6797 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6798 index_(0) {
6799 if (!blocks_in_loop_.IsBitSet(index_)) {
6800 Advance();
6801 }
6802 }
6803
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006804 bool Done() const { return index_ == blocks_.size(); }
6805 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006806 void Advance() {
6807 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006808 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006809 if (blocks_in_loop_.IsBitSet(index_)) {
6810 break;
6811 }
6812 }
6813 }
6814
6815 private:
6816 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006817 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006818 size_t index_;
6819
6820 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6821};
6822
Mingyao Yang3584bce2015-05-19 16:01:59 -07006823// Iterator over the blocks that art part of the loop. Includes blocks part
6824// of an inner loop. The order in which the blocks are iterated is reverse
6825// post order.
6826class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6827 public:
6828 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6829 : blocks_in_loop_(info.GetBlocks()),
6830 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6831 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006832 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006833 Advance();
6834 }
6835 }
6836
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006837 bool Done() const { return index_ == blocks_.size(); }
6838 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006839 void Advance() {
6840 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006841 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6842 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006843 break;
6844 }
6845 }
6846 }
6847
6848 private:
6849 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006850 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006851 size_t index_;
6852
6853 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6854};
6855
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006856inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006857 if (constant->IsIntConstant()) {
6858 return constant->AsIntConstant()->GetValue();
6859 } else if (constant->IsLongConstant()) {
6860 return constant->AsLongConstant()->GetValue();
6861 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006862 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006863 return 0;
6864 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006865}
6866
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006867#define INSTRUCTION_TYPE_CHECK(type, super) \
6868 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6869 inline const H##type* HInstruction::As##type() const { \
6870 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6871 } \
6872 inline H##type* HInstruction::As##type() { \
6873 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6874 }
6875
6876 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6877#undef INSTRUCTION_TYPE_CHECK
6878
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006879// Create space in `blocks` for adding `number_of_new_blocks` entries
6880// starting at location `at`. Blocks after `at` are moved accordingly.
6881inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6882 size_t number_of_new_blocks,
6883 size_t after) {
6884 DCHECK_LT(after, blocks->size());
6885 size_t old_size = blocks->size();
6886 size_t new_size = old_size + number_of_new_blocks;
6887 blocks->resize(new_size);
6888 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6889}
6890
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006891/*
6892 * Hunt "under the hood" of array lengths (leading to array references),
6893 * null checks (also leading to array references), and new arrays
6894 * (leading to the actual length). This makes it more likely related
6895 * instructions become actually comparable.
6896 */
6897inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6898 while (instruction->IsArrayLength() ||
6899 instruction->IsNullCheck() ||
6900 instruction->IsNewArray()) {
6901 instruction = instruction->IsNewArray()
6902 ? instruction->AsNewArray()->GetLength()
6903 : instruction->InputAt(0);
6904 }
6905 return instruction;
6906}
6907
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006908} // namespace art
6909
6910#endif // ART_COMPILER_OPTIMIZING_NODES_H_