blob: 6881d8f6aee1ac9a8ac58429bada385ace38dd1a [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 Geoffrayb331feb2016-02-05 16:51:53 +0000344 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700345 osr_(osr),
346 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100347 blocks_.reserve(kDefaultNumberOfBlocks);
348 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000349
David Brazdilbadd8262016-02-02 16:28:56 +0000350 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700351 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000352
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000353 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100354 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
355
David Brazdil69ba7b72015-06-23 18:27:30 +0100356 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000357 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100358
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000359 HBasicBlock* GetEntryBlock() const { return entry_block_; }
360 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100361 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000362
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000363 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
364 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000365
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000366 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100367
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100368 void ComputeDominanceInformation();
369 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000370 void ClearLoopInformation();
371 void FindBackEdges(ArenaBitVector* visited);
372 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100373 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100374 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000375
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // Analyze all natural loops in this graph. Returns a code specifying that it
377 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000378 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000379 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100380
David Brazdilffee3d32015-07-06 11:48:53 +0100381 // Iterate over blocks to compute try block membership. Needs reverse post
382 // order and loop information.
383 void ComputeTryBlockInformation();
384
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000385 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000386 // Returns the instruction to replace the invoke expression or null if the
387 // invoke is for a void method. Note that the caller is responsible for replacing
388 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000389 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000390
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000391 // Update the loop and try membership of `block`, which was spawned from `reference`.
392 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
393 // should be the new back edge.
394 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
395 HBasicBlock* reference,
396 bool replace_if_back_edge);
397
Mingyao Yang3584bce2015-05-19 16:01:59 -0700398 // Need to add a couple of blocks to test if the loop body is entered and
399 // put deoptimization instructions, etc.
400 void TransformLoopHeaderForBCE(HBasicBlock* header);
401
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000402 // Removes `block` from the graph. Assumes `block` has been disconnected from
403 // other blocks and has no instructions or phis.
404 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000405
David Brazdilfc6a86a2015-06-26 10:33:45 +0000406 // Splits the edge between `block` and `successor` while preserving the
407 // indices in the predecessor/successor lists. If there are multiple edges
408 // between the blocks, the lowest indices are used.
409 // Returns the new block which is empty and has the same dex pc as `successor`.
410 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
411
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100412 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
413 void SimplifyLoop(HBasicBlock* header);
414
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000415 int32_t GetNextInstructionId() {
416 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000417 return current_instruction_id_++;
418 }
419
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 int32_t GetCurrentInstructionId() const {
421 return current_instruction_id_;
422 }
423
424 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000425 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000426 current_instruction_id_ = id;
427 }
428
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100429 uint16_t GetMaximumNumberOfOutVRegs() const {
430 return maximum_number_of_out_vregs_;
431 }
432
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
434 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100435 }
436
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100437 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
438 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
439 }
440
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000441 void UpdateTemporariesVRegSlots(size_t slots) {
442 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100443 }
444
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100446 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000447 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 }
449
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100450 void SetNumberOfVRegs(uint16_t number_of_vregs) {
451 number_of_vregs_ = number_of_vregs;
452 }
453
454 uint16_t GetNumberOfVRegs() const {
455 return number_of_vregs_;
456 }
457
458 void SetNumberOfInVRegs(uint16_t value) {
459 number_of_in_vregs_ = value;
460 }
461
David Brazdildee58d62016-04-07 09:54:26 +0000462 uint16_t GetNumberOfInVRegs() const {
463 return number_of_in_vregs_;
464 }
465
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100467 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100468 return number_of_vregs_ - number_of_in_vregs_;
469 }
470
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100471 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100472 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100473 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100474
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100475 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
476 DCHECK(GetReversePostOrder()[0] == entry_block_);
477 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
478 }
479
480 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
481 return ReverseRange(GetReversePostOrder());
482 }
483
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100484 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100485 return linear_order_;
486 }
487
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100488 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
489 return ReverseRange(GetLinearOrder());
490 }
491
Mark Mendell1152c922015-04-24 17:06:35 -0400492 bool HasBoundsChecks() const {
493 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800494 }
495
Mark Mendell1152c922015-04-24 17:06:35 -0400496 void SetHasBoundsChecks(bool value) {
497 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800498 }
499
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100500 bool ShouldGenerateConstructorBarrier() const {
501 return should_generate_constructor_barrier_;
502 }
503
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000504 bool IsDebuggable() const { return debuggable_; }
505
David Brazdil8d5b8b22015-03-24 10:51:52 +0000506 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000507 // already, it is created and inserted into the graph. This method is only for
508 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600509 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000510
511 // TODO: This is problematic for the consistency of reference type propagation
512 // because it can be created anytime after the pass and thus it will be left
513 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000515
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600516 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
517 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000518 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
520 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000521 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600522 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
523 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000524 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600525 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
526 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000528
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100529 HCurrentMethod* GetCurrentMethod();
530
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100531 const DexFile& GetDexFile() const {
532 return dex_file_;
533 }
534
535 uint32_t GetMethodIdx() const {
536 return method_idx_;
537 }
538
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100539 InvokeType GetInvokeType() const {
540 return invoke_type_;
541 }
542
Mark Mendellc4701932015-04-10 13:18:51 -0400543 InstructionSet GetInstructionSet() const {
544 return instruction_set_;
545 }
546
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000547 bool IsCompilingOsr() const { return osr_; }
548
Mingyao Yang063fc772016-08-02 11:02:54 -0700549 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
550 return cha_single_implementation_list_;
551 }
552
553 void AddCHASingleImplementationDependency(ArtMethod* method) {
554 cha_single_implementation_list_.insert(method);
555 }
556
557 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800558 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700559 }
560
David Brazdil77a48ae2015-09-15 12:34:04 +0000561 bool HasTryCatch() const { return has_try_catch_; }
562 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100563
Aart Bikb13c65b2017-03-21 20:14:07 -0700564 bool HasSIMD() const { return has_simd_; }
565 void SetHasSIMD(bool value) { has_simd_ = value; }
566
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800567 bool HasLoops() const { return has_loops_; }
568 void SetHasLoops(bool value) { has_loops_ = value; }
569
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000570 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
571 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
572
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100573 ArtMethod* GetArtMethod() const { return art_method_; }
574 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
575
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100576 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500577 // The instruction has been inserted into the graph, either as a constant, or
578 // before cursor.
579 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
580
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000581 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
582
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800583 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
584 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
585 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
586
David Brazdil2d7352b2015-04-20 14:52:42 +0100587 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000588 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100589 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000590
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 template <class InstructionType, typename ValueType>
592 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600593 ArenaSafeMap<ValueType, InstructionType*>* cache,
594 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000595 // Try to find an existing constant of the given value.
596 InstructionType* constant = nullptr;
597 auto cached_constant = cache->find(value);
598 if (cached_constant != cache->end()) {
599 constant = cached_constant->second;
600 }
601
602 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100603 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000604 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600605 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 cache->Overwrite(value, constant);
607 InsertConstant(constant);
608 }
609 return constant;
610 }
611
David Brazdil8d5b8b22015-03-24 10:51:52 +0000612 void InsertConstant(HConstant* instruction);
613
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000614 // Cache a float constant into the graph. This method should only be
615 // called by the SsaBuilder when creating "equivalent" instructions.
616 void CacheFloatConstant(HFloatConstant* constant);
617
618 // See CacheFloatConstant comment.
619 void CacheDoubleConstant(HDoubleConstant* constant);
620
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000622
623 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100624 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000625
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100626 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100627 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000628
Aart Bik281c6812016-08-26 11:31:48 -0700629 // List of blocks to perform a linear order tree traversal. Unlike the reverse
630 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100631 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100632
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000633 HBasicBlock* entry_block_;
634 HBasicBlock* exit_block_;
635
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100636 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100637 uint16_t maximum_number_of_out_vregs_;
638
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100639 // The number of virtual registers in this method. Contains the parameters.
640 uint16_t number_of_vregs_;
641
642 // The number of virtual registers used by parameters of this method.
643 uint16_t number_of_in_vregs_;
644
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000645 // Number of vreg size slots that the temporaries use (used in baseline compiler).
646 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100647
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800648 // Flag whether there are bounds checks in the graph. We can skip
649 // BCE if it's false. It's only best effort to keep it up to date in
650 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400651 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800652
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800653 // Flag whether there are try/catch blocks in the graph. We will skip
654 // try/catch-related passes if it's false. It's only best effort to keep
655 // it up to date in the presence of code elimination so there might be
656 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000657 bool has_try_catch_;
658
Aart Bikb13c65b2017-03-21 20:14:07 -0700659 // Flag whether SIMD instructions appear in the graph. If true, the
660 // code generators may have to be more careful spilling the wider
661 // contents of SIMD registers.
662 bool has_simd_;
663
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800664 // Flag whether there are any loops in the graph. We can skip loop
665 // optimization if it's false. It's only best effort to keep it up
666 // to date in the presence of code elimination so there might be false
667 // positives.
668 bool has_loops_;
669
670 // Flag whether there are any irreducible loops in the graph. It's only
671 // best effort to keep it up to date in the presence of code elimination
672 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000673 bool has_irreducible_loops_;
674
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000675 // Indicates whether the graph should be compiled in a way that
676 // ensures full debuggability. If false, we can apply more
677 // aggressive optimizations that may limit the level of debugging.
678 const bool debuggable_;
679
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000680 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000681 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000682
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100683 // The dex file from which the method is from.
684 const DexFile& dex_file_;
685
686 // The method index in the dex file.
687 const uint32_t method_idx_;
688
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100689 // If inlined, this encodes how the callee is being invoked.
690 const InvokeType invoke_type_;
691
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100692 // Whether the graph has been transformed to SSA form. Only used
693 // in debug mode to ensure we are not using properties only valid
694 // for non-SSA form (like the number of temporaries).
695 bool in_ssa_form_;
696
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100697 const bool should_generate_constructor_barrier_;
698
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800699 // Number of CHA guards in the graph. Used to short-circuit the
700 // CHA guard optimization pass when there is no CHA guard left.
701 uint32_t number_of_cha_guards_;
702
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703 const InstructionSet instruction_set_;
704
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000705 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000706 HNullConstant* cached_null_constant_;
707 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000708 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000709 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000710 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000711
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100712 HCurrentMethod* cached_current_method_;
713
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100714 // The ArtMethod this graph is for. Note that for AOT, it may be null,
715 // for example for methods whose declaring class could not be resolved
716 // (such as when the superclass could not be found).
717 ArtMethod* art_method_;
718
David Brazdil4833f5a2015-12-16 10:37:39 +0000719 // Keep the RTI of inexact Object to avoid having to pass stack handle
720 // collection pointer to passes which may create NullConstant.
721 ReferenceTypeInfo inexact_object_rti_;
722
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000723 // Whether we are compiling this graph for on stack replacement: this will
724 // make all loops seen as irreducible and emit special stack maps to mark
725 // compiled code entries which the interpreter can directly jump to.
726 const bool osr_;
727
Mingyao Yang063fc772016-08-02 11:02:54 -0700728 // List of methods that are assumed to have single implementation.
729 ArenaSet<ArtMethod*> cha_single_implementation_list_;
730
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000731 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100732 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000733 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000734 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000735 DISALLOW_COPY_AND_ASSIGN(HGraph);
736};
737
Vladimir Markof9f64412015-09-02 14:05:49 +0100738class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000739 public:
740 HLoopInformation(HBasicBlock* header, HGraph* graph)
741 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100742 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000743 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100744 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100745 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100746 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000747 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 back_edges_.reserve(kDefaultNumberOfBackEdges);
749 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100750
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000751 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100752 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000753
754 void Dump(std::ostream& os);
755
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100756 HBasicBlock* GetHeader() const {
757 return header_;
758 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000760 void SetHeader(HBasicBlock* block) {
761 header_ = block;
762 }
763
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100764 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
765 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
766 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
767
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000768 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100769 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000770 }
771
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100772 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100773 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100774 }
775
David Brazdil46e2a392015-03-16 17:31:52 +0000776 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 }
779
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000780 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100781 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000782 }
783
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100784 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100785
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100788 }
789
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100790 // Returns the lifetime position of the back edge that has the
791 // greatest lifetime position.
792 size_t GetLifetimeEnd() const;
793
794 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100795 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100796 }
797
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000798 // Finds blocks that are part of this loop.
799 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100800
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100801 // Returns whether this loop information contains `block`.
802 // Note that this loop information *must* be populated before entering this function.
803 bool Contains(const HBasicBlock& block) const;
804
805 // Returns whether this loop information is an inner loop of `other`.
806 // Note that `other` *must* be populated before entering this function.
807 bool IsIn(const HLoopInformation& other) const;
808
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800809 // Returns true if instruction is not defined within this loop.
810 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700811
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100812 const ArenaBitVector& GetBlocks() const { return blocks_; }
813
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000814 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000815 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000816
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000817 void ClearAllBlocks() {
818 blocks_.ClearAllBits();
819 }
820
David Brazdil3f4a5222016-05-06 12:46:21 +0100821 bool HasBackEdgeNotDominatedByHeader() const;
822
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100823 bool IsPopulated() const {
824 return blocks_.GetHighestBitSet() != -1;
825 }
826
Anton Shaminf89381f2016-05-16 16:44:13 +0600827 bool DominatesAllBackEdges(HBasicBlock* block);
828
David Sehrc757dec2016-11-04 15:48:34 -0700829 bool HasExitEdge() const;
830
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000831 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 // Internal recursive implementation of `Populate`.
833 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100834 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100835
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000836 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100837 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000838 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100839 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100840 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100841 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000842
843 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
844};
845
David Brazdilec16f792015-08-19 15:04:01 +0100846// Stores try/catch information for basic blocks.
847// Note that HGraph is constructed so that catch blocks cannot simultaneously
848// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100849class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100850 public:
851 // Try block information constructor.
852 explicit TryCatchInformation(const HTryBoundary& try_entry)
853 : try_entry_(&try_entry),
854 catch_dex_file_(nullptr),
855 catch_type_index_(DexFile::kDexNoIndex16) {
856 DCHECK(try_entry_ != nullptr);
857 }
858
859 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800860 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100861 : try_entry_(nullptr),
862 catch_dex_file_(&dex_file),
863 catch_type_index_(catch_type_index) {}
864
865 bool IsTryBlock() const { return try_entry_ != nullptr; }
866
867 const HTryBoundary& GetTryEntry() const {
868 DCHECK(IsTryBlock());
869 return *try_entry_;
870 }
871
872 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
873
874 bool IsCatchAllTypeIndex() const {
875 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800876 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100877 }
878
Andreas Gampea5b09a62016-11-17 15:21:22 -0800879 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100880 DCHECK(IsCatchBlock());
881 return catch_type_index_;
882 }
883
884 const DexFile& GetCatchDexFile() const {
885 DCHECK(IsCatchBlock());
886 return *catch_dex_file_;
887 }
888
889 private:
890 // One of possibly several TryBoundary instructions entering the block's try.
891 // Only set for try blocks.
892 const HTryBoundary* try_entry_;
893
894 // Exception type information. Only set for catch blocks.
895 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800896 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100897};
898
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100899static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100900static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100901
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902// A block in a method. Contains the list of instructions represented
903// as a double linked list. Each block knows its predecessors and
904// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100905
Vladimir Markof9f64412015-09-02 14:05:49 +0100906class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000907 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700908 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000910 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
911 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000912 loop_information_(nullptr),
913 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000914 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100915 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100916 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100917 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000918 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000919 try_catch_information_(nullptr) {
920 predecessors_.reserve(kDefaultNumberOfPredecessors);
921 successors_.reserve(kDefaultNumberOfSuccessors);
922 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
923 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000924
Vladimir Marko60584552015-09-03 13:35:12 +0000925 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100926 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000927 }
928
Vladimir Marko60584552015-09-03 13:35:12 +0000929 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100930 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931 }
932
David Brazdild26a4112015-11-10 11:07:31 +0000933 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
934 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
935
Vladimir Marko60584552015-09-03 13:35:12 +0000936 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
937 return ContainsElement(successors_, block, start_from);
938 }
939
940 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100941 return dominated_blocks_;
942 }
943
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100944 bool IsEntryBlock() const {
945 return graph_->GetEntryBlock() == this;
946 }
947
948 bool IsExitBlock() const {
949 return graph_->GetExitBlock() == this;
950 }
951
David Brazdil46e2a392015-03-16 17:31:52 +0000952 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000953 bool IsSingleTryBoundary() const;
954
955 // Returns true if this block emits nothing but a jump.
956 bool IsSingleJump() const {
957 HLoopInformation* loop_info = GetLoopInformation();
958 return (IsSingleGoto() || IsSingleTryBoundary())
959 // Back edges generate a suspend check.
960 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
961 }
David Brazdil46e2a392015-03-16 17:31:52 +0000962
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000963 void AddBackEdge(HBasicBlock* back_edge) {
964 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000965 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100967 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000968 loop_information_->AddBackEdge(back_edge);
969 }
970
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000971 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000972 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000973
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100974 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000975 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600976 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000978 HBasicBlock* GetDominator() const { return dominator_; }
979 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000980 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
981
982 void RemoveDominatedBlock(HBasicBlock* block) {
983 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100984 }
Vladimir Marko60584552015-09-03 13:35:12 +0000985
986 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
987 ReplaceElement(dominated_blocks_, existing, new_block);
988 }
989
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100990 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000991
992 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100993 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000994 }
995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100996 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
997 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100998 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100999 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001000 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1001 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001002
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001003 HInstruction* GetFirstInstructionDisregardMoves() const;
1004
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001005 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001006 successors_.push_back(block);
1007 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001008 }
1009
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001010 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1011 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001012 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001013 new_block->predecessors_.push_back(this);
1014 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001015 }
1016
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1018 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001019 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001020 new_block->successors_.push_back(this);
1021 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001022 }
1023
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001024 // Insert `this` between `predecessor` and `successor. This method
1025 // preserves the indicies, and will update the first edge found between
1026 // `predecessor` and `successor`.
1027 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1028 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001029 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001030 successor->predecessors_[predecessor_index] = this;
1031 predecessor->successors_[successor_index] = this;
1032 successors_.push_back(successor);
1033 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001034 }
1035
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001036 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001037 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001038 }
1039
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001040 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001041 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001042 }
1043
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001044 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001045 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001046 }
1047
1048 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001049 predecessors_.push_back(block);
1050 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 }
1052
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001053 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001054 DCHECK_EQ(predecessors_.size(), 2u);
1055 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001056 }
1057
David Brazdil769c9e52015-04-27 13:54:09 +01001058 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 DCHECK_EQ(successors_.size(), 2u);
1060 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001061 }
1062
David Brazdilfc6a86a2015-06-26 10:33:45 +00001063 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001064 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001065 }
1066
David Brazdilfc6a86a2015-06-26 10:33:45 +00001067 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001068 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001069 }
1070
David Brazdilfc6a86a2015-06-26 10:33:45 +00001071 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001072 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001073 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 }
1075
1076 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001077 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001078 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001079 }
1080
1081 // Returns whether the first occurrence of `predecessor` in the list of
1082 // predecessors is at index `idx`.
1083 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001084 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001085 return GetPredecessorIndexOf(predecessor) == idx;
1086 }
1087
David Brazdild7558da2015-09-22 13:04:14 +01001088 // Create a new block between this block and its predecessors. The new block
1089 // is added to the graph, all predecessor edges are relinked to it and an edge
1090 // is created to `this`. Returns the new empty block. Reverse post order or
1091 // loop and try/catch information are not updated.
1092 HBasicBlock* CreateImmediateDominator();
1093
David Brazdilfc6a86a2015-06-26 10:33:45 +00001094 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001095 // created, latter block. Note that this method will add the block to the
1096 // graph, create a Goto at the end of the former block and will create an edge
1097 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001098 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001099 HBasicBlock* SplitBefore(HInstruction* cursor);
1100
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001101 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001102 // created block. Note that this method just updates raw block information,
1103 // like predecessors, successors, dominators, and instruction list. It does not
1104 // update the graph, reverse post order, loop information, nor make sure the
1105 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001106 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1107
1108 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1109 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001110
1111 // Merge `other` at the end of `this`. Successors and dominated blocks of
1112 // `other` are changed to be successors and dominated blocks of `this`. Note
1113 // that this method does not update the graph, reverse post order, loop
1114 // information, nor make sure the blocks are consistent (for example ending
1115 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001116 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117
1118 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1119 // of `this` are moved to `other`.
1120 // Note that this method does not update the graph, reverse post order, loop
1121 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001122 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001123 void ReplaceWith(HBasicBlock* other);
1124
Aart Bik6b69e0a2017-01-11 10:20:43 -08001125 // Merges the instructions of `other` at the end of `this`.
1126 void MergeInstructionsWith(HBasicBlock* other);
1127
David Brazdil2d7352b2015-04-20 14:52:42 +01001128 // Merge `other` at the end of `this`. This method updates loops, reverse post
1129 // order, links to predecessors, successors, dominators and deletes the block
1130 // from the graph. The two blocks must be successive, i.e. `this` the only
1131 // predecessor of `other` and vice versa.
1132 void MergeWith(HBasicBlock* other);
1133
1134 // Disconnects `this` from all its predecessors, successors and dominator,
1135 // removes it from all loops it is included in and eventually from the graph.
1136 // The block must not dominate any other block. Predecessors and successors
1137 // are safely updated.
1138 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001139
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001140 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001141 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001142 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001143 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001144 // Replace instruction `initial` with `replacement` within this block.
1145 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1146 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001147 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001148 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001149 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1150 // instruction list. With 'ensure_safety' set to true, it verifies that the
1151 // instruction is not in use and removes it from the use lists of its inputs.
1152 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1153 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001154 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001155
1156 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001157 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001158 }
1159
Roland Levillain6b879dd2014-09-22 17:13:44 +01001160 bool IsLoopPreHeaderFirstPredecessor() const {
1161 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001162 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001163 }
1164
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001165 bool IsFirstPredecessorBackEdge() const {
1166 DCHECK(IsLoopHeader());
1167 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1168 }
1169
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001170 HLoopInformation* GetLoopInformation() const {
1171 return loop_information_;
1172 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001173
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001174 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001175 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001176 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001177 void SetInLoop(HLoopInformation* info) {
1178 if (IsLoopHeader()) {
1179 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001180 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001181 loop_information_ = info;
1182 } else if (loop_information_->Contains(*info->GetHeader())) {
1183 // Block is currently part of an outer loop. Make it part of this inner loop.
1184 // Note that a non loop header having a loop information means this loop information
1185 // has already been populated
1186 loop_information_ = info;
1187 } else {
1188 // Block is part of an inner loop. Do not update the loop information.
1189 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1190 // at this point, because this method is being called while populating `info`.
1191 }
1192 }
1193
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001194 // Raw update of the loop information.
1195 void SetLoopInformation(HLoopInformation* info) {
1196 loop_information_ = info;
1197 }
1198
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001199 bool IsInLoop() const { return loop_information_ != nullptr; }
1200
David Brazdilec16f792015-08-19 15:04:01 +01001201 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1202
1203 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1204 try_catch_information_ = try_catch_information;
1205 }
1206
1207 bool IsTryBlock() const {
1208 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1209 }
1210
1211 bool IsCatchBlock() const {
1212 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1213 }
David Brazdilffee3d32015-07-06 11:48:53 +01001214
1215 // Returns the try entry that this block's successors should have. They will
1216 // be in the same try, unless the block ends in a try boundary. In that case,
1217 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001218 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001219
David Brazdild7558da2015-09-22 13:04:14 +01001220 bool HasThrowingInstructions() const;
1221
David Brazdila4b8c212015-05-07 09:59:30 +01001222 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001223 bool Dominates(HBasicBlock* block) const;
1224
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001225 size_t GetLifetimeStart() const { return lifetime_start_; }
1226 size_t GetLifetimeEnd() const { return lifetime_end_; }
1227
1228 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1229 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1230
David Brazdil8d5b8b22015-03-24 10:51:52 +00001231 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001232 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001233 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001234 bool HasSinglePhi() const;
1235
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001236 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001237 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001238 ArenaVector<HBasicBlock*> predecessors_;
1239 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001240 HInstructionList instructions_;
1241 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001242 HLoopInformation* loop_information_;
1243 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001244 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001245 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001246 // The dex program counter of the first instruction of this block.
1247 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001248 size_t lifetime_start_;
1249 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001250 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001251
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001252 friend class HGraph;
1253 friend class HInstruction;
1254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001255 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1256};
1257
David Brazdilb2bd1c52015-03-25 11:17:37 +00001258// Iterates over the LoopInformation of all loops which contain 'block'
1259// from the innermost to the outermost.
1260class HLoopInformationOutwardIterator : public ValueObject {
1261 public:
1262 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1263 : current_(block.GetLoopInformation()) {}
1264
1265 bool Done() const { return current_ == nullptr; }
1266
1267 void Advance() {
1268 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001269 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001270 }
1271
1272 HLoopInformation* Current() const {
1273 DCHECK(!Done());
1274 return current_;
1275 }
1276
1277 private:
1278 HLoopInformation* current_;
1279
1280 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1281};
1282
Alexandre Ramesef20f712015-06-09 10:29:30 +01001283#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001284 M(Above, Condition) \
1285 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001287 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001288 M(ArrayGet, Instruction) \
1289 M(ArrayLength, Instruction) \
1290 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001291 M(Below, Condition) \
1292 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001293 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001294 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001295 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001296 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001297 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001298 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001299 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001300 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001301 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001302 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001303 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001304 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001305 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001306 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001307 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Exit, Instruction) \
1309 M(FloatConstant, Constant) \
1310 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001311 M(GreaterThan, Condition) \
1312 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001313 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(InstanceFieldGet, Instruction) \
1315 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001316 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001318 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001319 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001320 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001322 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001323 M(LessThan, Condition) \
1324 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001325 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001326 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001327 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001328 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001329 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001330 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001331 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001332 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001333 M(Neg, UnaryOperation) \
1334 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001335 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001336 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001337 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001338 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001339 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001340 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001341 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001342 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001343 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001344 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001345 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001346 M(Return, Instruction) \
1347 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001348 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001349 M(Shl, BinaryOperation) \
1350 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001351 M(StaticFieldGet, Instruction) \
1352 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001353 M(UnresolvedInstanceFieldGet, Instruction) \
1354 M(UnresolvedInstanceFieldSet, Instruction) \
1355 M(UnresolvedStaticFieldGet, Instruction) \
1356 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001357 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001359 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001360 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001361 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001362 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001363 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001364 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001365
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001366/*
1367 * Instructions, shared across several (not all) architectures.
1368 */
1369#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1370#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1371#else
1372#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001373 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001374 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001375 M(MultiplyAccumulate, Instruction) \
1376 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001377#endif
1378
Vladimir Markob4536b72015-11-24 13:45:23 +00001379#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001380#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001381#else
1382#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1383 M(ArmDexCacheArraysBase, Instruction)
1384#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001385
1386#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1387
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001388#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001389#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001390#else
1391#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1392 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001393 M(MipsDexCacheArraysBase, Instruction) \
1394 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001395#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001396
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001397#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1398
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001399#ifndef ART_ENABLE_CODEGEN_x86
1400#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1401#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001402#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1403 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001404 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001405 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001406 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001407#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001408
1409#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1410
1411#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1412 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001413 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001414 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1415 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001416 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001417 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001418 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1419 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1420
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001421#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1422 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001424 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001425 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001426 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001427
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001428#define FOR_EACH_INSTRUCTION(M) \
1429 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1430 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1431
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001432#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001433FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1434#undef FORWARD_DECLARATION
1435
Vladimir Marko372f10e2016-05-17 16:30:10 +01001436#define DECLARE_INSTRUCTION(type) \
1437 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1438 const char* DebugName() const OVERRIDE { return #type; } \
1439 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1440 return other->Is##type(); \
1441 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001442 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001443
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001444#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1445 bool Is##type() const { return As##type() != nullptr; } \
1446 const H##type* As##type() const { return this; } \
1447 H##type* As##type() { return this; }
1448
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001449template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001450class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001451 public:
David Brazdiled596192015-01-23 10:39:45 +00001452 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001453 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001454 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001455
Vladimir Marko46817b82016-03-29 12:21:58 +01001456 // Hook for the IntrusiveForwardList<>.
1457 // TODO: Hide this better.
1458 IntrusiveForwardListHook hook;
1459
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001460 private:
David Brazdiled596192015-01-23 10:39:45 +00001461 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001462 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001463
1464 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001465 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001466
Vladimir Marko46817b82016-03-29 12:21:58 +01001467 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001468
1469 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1470};
1471
David Brazdiled596192015-01-23 10:39:45 +00001472template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001473using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001474
David Brazdil1abb4192015-02-17 18:33:36 +00001475// This class is used by HEnvironment and HInstruction classes to record the
1476// instructions they use and pointers to the corresponding HUseListNodes kept
1477// by the used instructions.
1478template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001479class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001480 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001481 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1482 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001483
Vladimir Marko46817b82016-03-29 12:21:58 +01001484 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1485 : HUserRecord(old_record.instruction_, before_use_node) {}
1486 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1487 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001488 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001489 }
1490
1491 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001492 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1493 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001494
1495 private:
1496 // Instruction used by the user.
1497 HInstruction* instruction_;
1498
Vladimir Marko46817b82016-03-29 12:21:58 +01001499 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1500 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001501};
1502
Vladimir Markoe9004912016-06-16 16:50:52 +01001503// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1504// This is used for HInstruction::GetInputs() to return a container wrapper providing
1505// HInstruction* values even though the underlying container has HUserRecord<>s.
1506struct HInputExtractor {
1507 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1508 return record.GetInstruction();
1509 }
1510 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1511 return record.GetInstruction();
1512 }
1513};
1514
1515using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1516using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1517
Aart Bik854a02b2015-07-14 16:07:00 -07001518/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001519 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001520 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001521 * For write/read dependences on fields/arrays, the dependence analysis uses
1522 * type disambiguation (e.g. a float field write cannot modify the value of an
1523 * integer field read) and the access type (e.g. a reference array write cannot
1524 * modify the value of a reference field read [although it may modify the
1525 * reference fetch prior to reading the field, which is represented by its own
1526 * write/read dependence]). The analysis makes conservative points-to
1527 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1528 * the same, and any reference read depends on any reference read without
1529 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001530 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001531 * The internal representation uses 38-bit and is described in the table below.
1532 * The first line indicates the side effect, and for field/array accesses the
1533 * second line indicates the type of the access (in the order of the
1534 * Primitive::Type enum).
1535 * The two numbered lines below indicate the bit position in the bitfield (read
1536 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001537 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001538 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1539 * +-------------+---------+---------+--------------+---------+---------+
1540 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1541 * | 3 |333333322|222222221| 1 |111111110|000000000|
1542 * | 7 |654321098|765432109| 8 |765432109|876543210|
1543 *
1544 * Note that, to ease the implementation, 'changes' bits are least significant
1545 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001546 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001547class SideEffects : public ValueObject {
1548 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001549 SideEffects() : flags_(0) {}
1550
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001551 static SideEffects None() {
1552 return SideEffects(0);
1553 }
1554
1555 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001556 return SideEffects(kAllChangeBits | kAllDependOnBits);
1557 }
1558
1559 static SideEffects AllChanges() {
1560 return SideEffects(kAllChangeBits);
1561 }
1562
1563 static SideEffects AllDependencies() {
1564 return SideEffects(kAllDependOnBits);
1565 }
1566
1567 static SideEffects AllExceptGCDependency() {
1568 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1569 }
1570
1571 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001572 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001573 }
1574
Aart Bik34c3ba92015-07-20 14:08:59 -07001575 static SideEffects AllWrites() {
1576 return SideEffects(kAllWrites);
1577 }
1578
1579 static SideEffects AllReads() {
1580 return SideEffects(kAllReads);
1581 }
1582
1583 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1584 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001585 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001586 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001587 }
1588
Aart Bik854a02b2015-07-14 16:07:00 -07001589 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001590 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001591 }
1592
Aart Bik34c3ba92015-07-20 14:08:59 -07001593 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1594 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001596 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001597 }
1598
1599 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001600 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001601 }
1602
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 static SideEffects CanTriggerGC() {
1604 return SideEffects(1ULL << kCanTriggerGCBit);
1605 }
1606
1607 static SideEffects DependsOnGC() {
1608 return SideEffects(1ULL << kDependsOnGCBit);
1609 }
1610
Aart Bik854a02b2015-07-14 16:07:00 -07001611 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001612 SideEffects Union(SideEffects other) const {
1613 return SideEffects(flags_ | other.flags_);
1614 }
1615
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001616 SideEffects Exclusion(SideEffects other) const {
1617 return SideEffects(flags_ & ~other.flags_);
1618 }
1619
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001620 void Add(SideEffects other) {
1621 flags_ |= other.flags_;
1622 }
1623
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 bool Includes(SideEffects other) const {
1625 return (other.flags_ & flags_) == other.flags_;
1626 }
1627
1628 bool HasSideEffects() const {
1629 return (flags_ & kAllChangeBits);
1630 }
1631
1632 bool HasDependencies() const {
1633 return (flags_ & kAllDependOnBits);
1634 }
1635
1636 // Returns true if there are no side effects or dependencies.
1637 bool DoesNothing() const {
1638 return flags_ == 0;
1639 }
1640
Aart Bik854a02b2015-07-14 16:07:00 -07001641 // Returns true if something is written.
1642 bool DoesAnyWrite() const {
1643 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001644 }
1645
Aart Bik854a02b2015-07-14 16:07:00 -07001646 // Returns true if something is read.
1647 bool DoesAnyRead() const {
1648 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001649 }
1650
Aart Bik854a02b2015-07-14 16:07:00 -07001651 // Returns true if potentially everything is written and read
1652 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001653 bool DoesAllReadWrite() const {
1654 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1655 }
1656
Aart Bik854a02b2015-07-14 16:07:00 -07001657 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001659 }
1660
Roland Levillain0d5a2812015-11-13 10:07:31 +00001661 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001662 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1664 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001665 }
1666
1667 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001668 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001669 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001670 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001671 for (int s = kLastBit; s >= 0; s--) {
1672 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1673 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1674 // This is a bit for the GC side effect.
1675 if (current_bit_is_set) {
1676 flags += "GC";
1677 }
Aart Bik854a02b2015-07-14 16:07:00 -07001678 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001679 } else {
1680 // This is a bit for the array/field analysis.
1681 // The underscore character stands for the 'can trigger GC' bit.
1682 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1683 if (current_bit_is_set) {
1684 flags += kDebug[s];
1685 }
1686 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1687 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1688 flags += "|";
1689 }
1690 }
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001693 }
1694
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001696
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 private:
1698 static constexpr int kFieldArrayAnalysisBits = 9;
1699
1700 static constexpr int kFieldWriteOffset = 0;
1701 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1702 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1703 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1704
1705 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1706
1707 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1708 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1709 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1710 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1711
1712 static constexpr int kLastBit = kDependsOnGCBit;
1713 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1714
1715 // Aliases.
1716
1717 static_assert(kChangeBits == kDependOnBits,
1718 "the 'change' bits should match the 'depend on' bits.");
1719
1720 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1721 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1722 static constexpr uint64_t kAllWrites =
1723 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1724 static constexpr uint64_t kAllReads =
1725 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001726
Aart Bik854a02b2015-07-14 16:07:00 -07001727 // Translates type to bit flag.
1728 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1729 CHECK_NE(type, Primitive::kPrimVoid);
1730 const uint64_t one = 1;
1731 const int shift = type; // 0-based consecutive enum
1732 DCHECK_LE(kFieldWriteOffset, shift);
1733 DCHECK_LT(shift, kArrayWriteOffset);
1734 return one << (type + offset);
1735 }
1736
1737 // Private constructor on direct flags value.
1738 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1739
1740 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001741};
1742
David Brazdiled596192015-01-23 10:39:45 +00001743// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001744class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001745 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001746 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1747 size_t number_of_vregs,
1748 ArtMethod* method,
1749 uint32_t dex_pc,
1750 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001751 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1752 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001753 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001754 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001755 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001756 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 }
1758
Mingyao Yang01b47b02017-02-03 12:09:57 -08001759 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001760 : HEnvironment(arena,
1761 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001762 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001763 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001764 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001765
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001767 if (parent_ != nullptr) {
1768 parent_->SetAndCopyParentChain(allocator, parent);
1769 } else {
1770 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1771 parent_->CopyFrom(parent);
1772 if (parent->GetParent() != nullptr) {
1773 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1774 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001775 }
David Brazdiled596192015-01-23 10:39:45 +00001776 }
1777
Vladimir Marko71bf8092015-09-15 15:33:14 +01001778 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001779 void CopyFrom(HEnvironment* environment);
1780
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001781 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1782 // input to the loop phi instead. This is for inserting instructions that
1783 // require an environment (like HDeoptimization) in the loop pre-header.
1784 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001785
1786 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001787 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001788 }
1789
1790 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001791 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001792 }
1793
David Brazdil1abb4192015-02-17 18:33:36 +00001794 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001795
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001797
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001798 HEnvironment* GetParent() const { return parent_; }
1799
1800 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001801 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001802 }
1803
1804 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001805 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001806 }
1807
1808 uint32_t GetDexPc() const {
1809 return dex_pc_;
1810 }
1811
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001812 ArtMethod* GetMethod() const {
1813 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001814 }
1815
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001816 HInstruction* GetHolder() const {
1817 return holder_;
1818 }
1819
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001820
1821 bool IsFromInlinedInvoke() const {
1822 return GetParent() != nullptr;
1823 }
1824
David Brazdiled596192015-01-23 10:39:45 +00001825 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001826 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1827 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001828 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001829 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001830 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001831
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001832 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001833 HInstruction* const holder_;
1834
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001835 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001836
1837 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1838};
1839
Vladimir Markof9f64412015-09-02 14:05:49 +01001840class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001841 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001842 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843 : previous_(nullptr),
1844 next_(nullptr),
1845 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001846 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001848 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001849 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001850 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001851 locations_(nullptr),
1852 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001853 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001854 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001855 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1856 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1857 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001858
Dave Allison20dfc792014-06-16 20:44:29 -07001859 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001861#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001862 enum InstructionKind {
1863 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1864 };
1865#undef DECLARE_KIND
1866
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001867 HInstruction* GetNext() const { return next_; }
1868 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869
Calin Juravle77520bc2015-01-12 18:45:46 +00001870 HInstruction* GetNextDisregardingMoves() const;
1871 HInstruction* GetPreviousDisregardingMoves() const;
1872
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001873 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001874 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001876 bool IsInBlock() const { return block_ != nullptr; }
1877 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001878 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1879 bool IsIrreducibleLoopHeaderPhi() const {
1880 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1881 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001882
Vladimir Marko372f10e2016-05-17 16:30:10 +01001883 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1884
1885 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1886 // One virtual method is enough, just const_cast<> and then re-add the const.
1887 return ArrayRef<const HUserRecord<HInstruction*>>(
1888 const_cast<HInstruction*>(this)->GetInputRecords());
1889 }
1890
Vladimir Markoe9004912016-06-16 16:50:52 +01001891 HInputsRef GetInputs() {
1892 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001893 }
1894
Vladimir Markoe9004912016-06-16 16:50:52 +01001895 HConstInputsRef GetInputs() const {
1896 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001897 }
1898
1899 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001900 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001901
Aart Bik2767f4b2016-10-28 15:03:53 -07001902 bool HasInput(HInstruction* input) const {
1903 for (const HInstruction* i : GetInputs()) {
1904 if (i == input) {
1905 return true;
1906 }
1907 }
1908 return false;
1909 }
1910
Vladimir Marko372f10e2016-05-17 16:30:10 +01001911 void SetRawInputAt(size_t index, HInstruction* input) {
1912 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1913 }
1914
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001915 virtual void Accept(HGraphVisitor* visitor) = 0;
1916 virtual const char* DebugName() const = 0;
1917
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1919
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001920 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001921
1922 uint32_t GetDexPc() const { return dex_pc_; }
1923
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001924 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001925
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001926 // Can the instruction throw?
1927 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1928 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001929 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001930 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001931
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001932 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001933 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001934
Calin Juravle10e244f2015-01-26 18:54:32 +00001935 // Does not apply for all instructions, but having this at top level greatly
1936 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001937 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001938 virtual bool CanBeNull() const {
1939 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1940 return true;
1941 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001942
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001943 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001944 return false;
1945 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001946
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001947 virtual bool IsActualObject() const {
1948 return GetType() == Primitive::kPrimNot;
1949 }
1950
Calin Juravle2e768302015-07-28 14:41:11 +00001951 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001952
Calin Juravle61d544b2015-02-23 16:46:57 +00001953 ReferenceTypeInfo GetReferenceTypeInfo() const {
1954 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001955 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001956 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001957 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001960 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001961 // Note: fixup_end remains valid across push_front().
1962 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1963 HUseListNode<HInstruction*>* new_node =
1964 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1965 uses_.push_front(*new_node);
1966 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001967 }
1968
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001969 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001970 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001971 // Note: env_fixup_end remains valid across push_front().
1972 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1973 HUseListNode<HEnvironment*>* new_node =
1974 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1975 env_uses_.push_front(*new_node);
1976 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001977 }
1978
David Brazdil1abb4192015-02-17 18:33:36 +00001979 void RemoveAsUserOfInput(size_t input) {
1980 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001981 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1982 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1983 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001984 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001985
Vladimir Marko372f10e2016-05-17 16:30:10 +01001986 void RemoveAsUserOfAllInputs() {
1987 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1988 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1989 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1990 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1991 }
1992 }
1993
David Brazdil1abb4192015-02-17 18:33:36 +00001994 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1995 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001996
Vladimir Marko46817b82016-03-29 12:21:58 +01001997 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1998 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1999 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002000 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002001 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002002 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002003
Aart Bikcc42be02016-10-20 16:14:16 -07002004 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002005 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002006 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002007 !CanThrow() &&
2008 !IsSuspendCheck() &&
2009 !IsControlFlow() &&
2010 !IsNativeDebugInfo() &&
2011 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002012 // If we added an explicit barrier then we should keep it.
2013 !IsMemoryBarrier();
2014 }
2015
Aart Bikcc42be02016-10-20 16:14:16 -07002016 bool IsDeadAndRemovable() const {
2017 return IsRemovable() && !HasUses();
2018 }
2019
Roland Levillain6c82d402014-10-13 16:10:27 +01002020 // Does this instruction strictly dominate `other_instruction`?
2021 // Returns false if this instruction and `other_instruction` are the same.
2022 // Aborts if this instruction and `other_instruction` are both phis.
2023 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002024
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002025 int GetId() const { return id_; }
2026 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002027
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002028 int GetSsaIndex() const { return ssa_index_; }
2029 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2030 bool HasSsaIndex() const { return ssa_index_ != -1; }
2031
2032 bool HasEnvironment() const { return environment_ != nullptr; }
2033 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002034 // Set the `environment_` field. Raw because this method does not
2035 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002036 void SetRawEnvironment(HEnvironment* environment) {
2037 DCHECK(environment_ == nullptr);
2038 DCHECK_EQ(environment->GetHolder(), this);
2039 environment_ = environment;
2040 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002041
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002042 void InsertRawEnvironment(HEnvironment* environment) {
2043 DCHECK(environment_ != nullptr);
2044 DCHECK_EQ(environment->GetHolder(), this);
2045 DCHECK(environment->GetParent() == nullptr);
2046 environment->parent_ = environment_;
2047 environment_ = environment;
2048 }
2049
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002050 void RemoveEnvironment();
2051
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002052 // Set the environment of this instruction, copying it from `environment`. While
2053 // copying, the uses lists are being updated.
2054 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002055 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002056 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002057 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002058 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002059 if (environment->GetParent() != nullptr) {
2060 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2061 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002062 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002063
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002064 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2065 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002066 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002067 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002068 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002069 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002070 if (environment->GetParent() != nullptr) {
2071 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2072 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002073 }
2074
Nicolas Geoffray39468442014-09-02 15:17:15 +01002075 // Returns the number of entries in the environment. Typically, that is the
2076 // number of dex registers in a method. It could be more in case of inlining.
2077 size_t EnvironmentSize() const;
2078
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002079 LocationSummary* GetLocations() const { return locations_; }
2080 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002081
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002082 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002083 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002084
Alexandre Rames188d4312015-04-09 18:30:21 +01002085 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2086 // uses of this instruction by `other` are *not* updated.
2087 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2088 ReplaceWith(other);
2089 other->ReplaceInput(this, use_index);
2090 }
2091
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002092 // Move `this` instruction before `cursor`
2093 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002094
Vladimir Markofb337ea2015-11-25 15:25:10 +00002095 // Move `this` before its first user and out of any loops. If there is no
2096 // out-of-loop user that dominates all other users, move the instruction
2097 // to the end of the out-of-loop common dominator of the user's blocks.
2098 //
2099 // This can be used only on non-throwing instructions with no side effects that
2100 // have at least one use but no environment uses.
2101 void MoveBeforeFirstUserAndOutOfLoops();
2102
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002103#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 bool Is##type() const; \
2105 const H##type* As##type() const; \
2106 H##type* As##type();
2107
2108 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2109#undef INSTRUCTION_TYPE_CHECK
2110
2111#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002112 bool Is##type() const { return (As##type() != nullptr); } \
2113 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002114 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002115 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002116#undef INSTRUCTION_TYPE_CHECK
2117
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002118 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002119 // TODO: this method is used by LICM and GVN with possibly different
2120 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002121 virtual bool CanBeMoved() const { return false; }
2122
2123 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002124 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002125 return false;
2126 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127
2128 // Returns whether any data encoded in the two instructions is equal.
2129 // This method does not look at the inputs. Both instructions must be
2130 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002131 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002132 return false;
2133 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002134
2135 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002136 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002137 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002138 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002139
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2141 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2142 // the virtual function because the __attribute__((__pure__)) doesn't really
2143 // apply the strong requirement for virtual functions, preventing optimizations.
2144 InstructionKind GetKind() const PURE;
2145 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002146
2147 virtual size_t ComputeHashCode() const {
2148 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002149 for (const HInstruction* input : GetInputs()) {
2150 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002151 }
2152 return result;
2153 }
2154
2155 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002156 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002157 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002158
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002159 size_t GetLifetimePosition() const { return lifetime_position_; }
2160 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2161 LiveInterval* GetLiveInterval() const { return live_interval_; }
2162 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2163 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2164
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002165 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2166
2167 // Returns whether the code generation of the instruction will require to have access
2168 // to the current method. Such instructions are:
2169 // (1): Instructions that require an environment, as calling the runtime requires
2170 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002171 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2172 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002173 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002174 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002175 }
2176
Vladimir Markodc151b22015-10-15 18:02:30 +01002177 // Returns whether the code generation of the instruction will require to have access
2178 // to the dex cache of the current method's declaring class via the current method.
2179 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002180
Mark Mendellc4701932015-04-10 13:18:51 -04002181 // Does this instruction have any use in an environment before
2182 // control flow hits 'other'?
2183 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2184
2185 // Remove all references to environment uses of this instruction.
2186 // The caller must ensure that this is safe to do.
2187 void RemoveEnvironmentUsers();
2188
Vladimir Markoa1de9182016-02-25 11:37:38 +00002189 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2190 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002191
David Brazdil1abb4192015-02-17 18:33:36 +00002192 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002193 // If set, the machine code for this instruction is assumed to be generated by
2194 // its users. Used by liveness analysis to compute use positions accordingly.
2195 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2196 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2197 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2198 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2199
Vladimir Marko372f10e2016-05-17 16:30:10 +01002200 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2201 return GetInputRecords()[i];
2202 }
2203
2204 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2205 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2206 input_records[index] = input;
2207 }
David Brazdil1abb4192015-02-17 18:33:36 +00002208
Vladimir Markoa1de9182016-02-25 11:37:38 +00002209 uint32_t GetPackedFields() const {
2210 return packed_fields_;
2211 }
2212
2213 template <size_t flag>
2214 bool GetPackedFlag() const {
2215 return (packed_fields_ & (1u << flag)) != 0u;
2216 }
2217
2218 template <size_t flag>
2219 void SetPackedFlag(bool value = true) {
2220 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2221 }
2222
2223 template <typename BitFieldType>
2224 typename BitFieldType::value_type GetPackedField() const {
2225 return BitFieldType::Decode(packed_fields_);
2226 }
2227
2228 template <typename BitFieldType>
2229 void SetPackedField(typename BitFieldType::value_type value) {
2230 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2231 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2232 }
2233
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002234 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002235 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2236 auto before_use_node = uses_.before_begin();
2237 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2238 HInstruction* user = use_node->GetUser();
2239 size_t input_index = use_node->GetIndex();
2240 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2241 before_use_node = use_node;
2242 }
2243 }
2244
2245 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2246 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2247 if (next != uses_.end()) {
2248 HInstruction* next_user = next->GetUser();
2249 size_t next_index = next->GetIndex();
2250 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2251 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2252 }
2253 }
2254
2255 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2256 auto before_env_use_node = env_uses_.before_begin();
2257 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2258 HEnvironment* user = env_use_node->GetUser();
2259 size_t input_index = env_use_node->GetIndex();
2260 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2261 before_env_use_node = env_use_node;
2262 }
2263 }
2264
2265 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2266 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2267 if (next != env_uses_.end()) {
2268 HEnvironment* next_user = next->GetUser();
2269 size_t next_index = next->GetIndex();
2270 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2271 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2272 }
2273 }
David Brazdil1abb4192015-02-17 18:33:36 +00002274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275 HInstruction* previous_;
2276 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002277 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002278 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002279
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280 // An instruction gets an id when it is added to the graph.
2281 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002282 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002283 int id_;
2284
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002285 // When doing liveness analysis, instructions that have uses get an SSA index.
2286 int ssa_index_;
2287
Vladimir Markoa1de9182016-02-25 11:37:38 +00002288 // Packed fields.
2289 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002290
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002291 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002292 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002293
2294 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002295 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002296
Nicolas Geoffray39468442014-09-02 15:17:15 +01002297 // The environment associated with this instruction. Not null if the instruction
2298 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002299 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002300
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002301 // Set by the code generator.
2302 LocationSummary* locations_;
2303
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002304 // Set by the liveness analysis.
2305 LiveInterval* live_interval_;
2306
2307 // Set by the liveness analysis, this is the position in a linear
2308 // order of blocks where this instruction's live interval start.
2309 size_t lifetime_position_;
2310
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002311 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002312
Vladimir Markoa1de9182016-02-25 11:37:38 +00002313 // The reference handle part of the reference type info.
2314 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002315 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002316 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002317
David Brazdil1abb4192015-02-17 18:33:36 +00002318 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002320 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002321 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323
2324 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2325};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002326std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002328// Iterates over the instructions, while preserving the next instruction
2329// in case the current instruction gets removed from the list by the user
2330// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331class HInstructionIterator : public ValueObject {
2332 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002333 explicit HInstructionIterator(const HInstructionList& instructions)
2334 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002335 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002336 }
2337
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002338 bool Done() const { return instruction_ == nullptr; }
2339 HInstruction* Current() const { return instruction_; }
2340 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002341 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002342 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343 }
2344
2345 private:
2346 HInstruction* instruction_;
2347 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002348
2349 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002350};
2351
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002352// Iterates over the instructions without saving the next instruction,
2353// therefore handling changes in the graph potentially made by the user
2354// of this iterator.
2355class HInstructionIteratorHandleChanges : public ValueObject {
2356 public:
2357 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2358 : instruction_(instructions.first_instruction_) {
2359 }
2360
2361 bool Done() const { return instruction_ == nullptr; }
2362 HInstruction* Current() const { return instruction_; }
2363 void Advance() {
2364 instruction_ = instruction_->GetNext();
2365 }
2366
2367 private:
2368 HInstruction* instruction_;
2369
2370 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2371};
2372
2373
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002374class HBackwardInstructionIterator : public ValueObject {
2375 public:
2376 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2377 : instruction_(instructions.last_instruction_) {
2378 next_ = Done() ? nullptr : instruction_->GetPrevious();
2379 }
2380
2381 bool Done() const { return instruction_ == nullptr; }
2382 HInstruction* Current() const { return instruction_; }
2383 void Advance() {
2384 instruction_ = next_;
2385 next_ = Done() ? nullptr : instruction_->GetPrevious();
2386 }
2387
2388 private:
2389 HInstruction* instruction_;
2390 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002391
2392 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002393};
2394
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002395class HVariableInputSizeInstruction : public HInstruction {
2396 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002397 using HInstruction::GetInputRecords; // Keep the const version visible.
2398 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2399 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2400 }
2401
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002402 void AddInput(HInstruction* input);
2403 void InsertInputAt(size_t index, HInstruction* input);
2404 void RemoveInputAt(size_t index);
2405
2406 protected:
2407 HVariableInputSizeInstruction(SideEffects side_effects,
2408 uint32_t dex_pc,
2409 ArenaAllocator* arena,
2410 size_t number_of_inputs,
2411 ArenaAllocKind kind)
2412 : HInstruction(side_effects, dex_pc),
2413 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2414
2415 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2416
2417 private:
2418 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2419};
2420
Vladimir Markof9f64412015-09-02 14:05:49 +01002421template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002422class HTemplateInstruction: public HInstruction {
2423 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002424 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002425 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002426 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002427
Vladimir Marko372f10e2016-05-17 16:30:10 +01002428 using HInstruction::GetInputRecords; // Keep the const version visible.
2429 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2430 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002431 }
2432
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002433 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002434 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002435
2436 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002437};
2438
Vladimir Markof9f64412015-09-02 14:05:49 +01002439// HTemplateInstruction specialization for N=0.
2440template<>
2441class HTemplateInstruction<0>: public HInstruction {
2442 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002443 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002444 : HInstruction(side_effects, dex_pc) {}
2445
Vladimir Markof9f64412015-09-02 14:05:49 +01002446 virtual ~HTemplateInstruction() {}
2447
Vladimir Marko372f10e2016-05-17 16:30:10 +01002448 using HInstruction::GetInputRecords; // Keep the const version visible.
2449 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2450 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002451 }
2452
2453 private:
2454 friend class SsaBuilder;
2455};
2456
Dave Allison20dfc792014-06-16 20:44:29 -07002457template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002458class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002459 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002460 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002461 : HTemplateInstruction<N>(side_effects, dex_pc) {
2462 this->template SetPackedField<TypeField>(type);
2463 }
Dave Allison20dfc792014-06-16 20:44:29 -07002464 virtual ~HExpression() {}
2465
Vladimir Markoa1de9182016-02-25 11:37:38 +00002466 Primitive::Type GetType() const OVERRIDE {
2467 return TypeField::Decode(this->GetPackedFields());
2468 }
Dave Allison20dfc792014-06-16 20:44:29 -07002469
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002470 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002471 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2472 static constexpr size_t kFieldTypeSize =
2473 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2474 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2475 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2476 "Too many packed fields.");
2477 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002478};
2479
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2481// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002482class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002484 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2485 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002487 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002488
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002489 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002490
2491 private:
2492 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2493};
2494
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002495// Represents dex's RETURN opcodes. A HReturn is a control flow
2496// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002497class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002498 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002499 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2500 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002501 SetRawInputAt(0, value);
2502 }
2503
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002504 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002505
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002506 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002507
2508 private:
2509 DISALLOW_COPY_AND_ASSIGN(HReturn);
2510};
2511
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002512class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002513 public:
2514 HPhi(ArenaAllocator* arena,
2515 uint32_t reg_number,
2516 size_t number_of_inputs,
2517 Primitive::Type type,
2518 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002519 : HVariableInputSizeInstruction(
2520 SideEffects::None(),
2521 dex_pc,
2522 arena,
2523 number_of_inputs,
2524 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002525 reg_number_(reg_number) {
2526 SetPackedField<TypeField>(ToPhiType(type));
2527 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2528 // Phis are constructed live and marked dead if conflicting or unused.
2529 // Individual steps of SsaBuilder should assume that if a phi has been
2530 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2531 SetPackedFlag<kFlagIsLive>(true);
2532 SetPackedFlag<kFlagCanBeNull>(true);
2533 }
2534
2535 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2536 static Primitive::Type ToPhiType(Primitive::Type type) {
2537 return Primitive::PrimitiveKind(type);
2538 }
2539
2540 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2541
David Brazdildee58d62016-04-07 09:54:26 +00002542 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2543 void SetType(Primitive::Type new_type) {
2544 // Make sure that only valid type changes occur. The following are allowed:
2545 // (1) int -> float/ref (primitive type propagation),
2546 // (2) long -> double (primitive type propagation).
2547 DCHECK(GetType() == new_type ||
2548 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2549 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2550 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2551 SetPackedField<TypeField>(new_type);
2552 }
2553
2554 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2555 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2556
2557 uint32_t GetRegNumber() const { return reg_number_; }
2558
2559 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2560 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2561 bool IsDead() const { return !IsLive(); }
2562 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2563
Vladimir Markoe9004912016-06-16 16:50:52 +01002564 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002565 return other != nullptr
2566 && other->IsPhi()
2567 && other->AsPhi()->GetBlock() == GetBlock()
2568 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2569 }
2570
2571 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2572 // An equivalent phi is a phi having the same dex register and type.
2573 // It assumes that phis with the same dex register are adjacent.
2574 HPhi* GetNextEquivalentPhiWithSameType() {
2575 HInstruction* next = GetNext();
2576 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2577 if (next->GetType() == GetType()) {
2578 return next->AsPhi();
2579 }
2580 next = next->GetNext();
2581 }
2582 return nullptr;
2583 }
2584
2585 DECLARE_INSTRUCTION(Phi);
2586
David Brazdildee58d62016-04-07 09:54:26 +00002587 private:
2588 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2589 static constexpr size_t kFieldTypeSize =
2590 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2591 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2592 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2593 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2594 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2595 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2596
David Brazdildee58d62016-04-07 09:54:26 +00002597 const uint32_t reg_number_;
2598
2599 DISALLOW_COPY_AND_ASSIGN(HPhi);
2600};
2601
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002603// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002605class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002606 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002607 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002608
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002609 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002611 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002612
2613 private:
2614 DISALLOW_COPY_AND_ASSIGN(HExit);
2615};
2616
2617// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002618class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002619 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002620 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002621
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002622 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002623
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002624 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002625 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002626 }
2627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002628 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002629
2630 private:
2631 DISALLOW_COPY_AND_ASSIGN(HGoto);
2632};
2633
Roland Levillain9867bc72015-08-05 10:21:34 +01002634class HConstant : public HExpression<0> {
2635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2637 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002638
2639 bool CanBeMoved() const OVERRIDE { return true; }
2640
Roland Levillain1a653882016-03-18 18:05:57 +00002641 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002642 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002643 // Is this constant 0 in the arithmetic sense?
2644 virtual bool IsArithmeticZero() const { return false; }
2645 // Is this constant a 0-bit pattern?
2646 virtual bool IsZeroBitPattern() const { return false; }
2647 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002648 virtual bool IsOne() const { return false; }
2649
David Brazdil77a48ae2015-09-15 12:34:04 +00002650 virtual uint64_t GetValueAsUint64() const = 0;
2651
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002652 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002653
2654 private:
2655 DISALLOW_COPY_AND_ASSIGN(HConstant);
2656};
2657
Vladimir Markofcb503c2016-05-18 12:48:17 +01002658class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002660 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002661 return true;
2662 }
2663
David Brazdil77a48ae2015-09-15 12:34:04 +00002664 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2665
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 size_t ComputeHashCode() const OVERRIDE { return 0; }
2667
Roland Levillain1a653882016-03-18 18:05:57 +00002668 // The null constant representation is a 0-bit pattern.
2669 virtual bool IsZeroBitPattern() const { return true; }
2670
Roland Levillain9867bc72015-08-05 10:21:34 +01002671 DECLARE_INSTRUCTION(NullConstant);
2672
2673 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002674 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002675
2676 friend class HGraph;
2677 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2678};
2679
2680// Constants of the type int. Those can be from Dex instructions, or
2681// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002682class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 public:
2684 int32_t GetValue() const { return value_; }
2685
David Brazdil9f389d42015-10-01 14:32:56 +01002686 uint64_t GetValueAsUint64() const OVERRIDE {
2687 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2688 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002689
Vladimir Marko372f10e2016-05-17 16:30:10 +01002690 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002691 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 return other->AsIntConstant()->value_ == value_;
2693 }
2694
2695 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2696
2697 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002698 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2699 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002700 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2701
Roland Levillain1a653882016-03-18 18:05:57 +00002702 // Integer constants are used to encode Boolean values as well,
2703 // where 1 means true and 0 means false.
2704 bool IsTrue() const { return GetValue() == 1; }
2705 bool IsFalse() const { return GetValue() == 0; }
2706
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 DECLARE_INSTRUCTION(IntConstant);
2708
2709 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002710 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2711 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2712 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2713 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002714
2715 const int32_t value_;
2716
2717 friend class HGraph;
2718 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2719 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2720 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2721};
2722
Vladimir Markofcb503c2016-05-18 12:48:17 +01002723class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 public:
2725 int64_t GetValue() const { return value_; }
2726
David Brazdil77a48ae2015-09-15 12:34:04 +00002727 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2728
Vladimir Marko372f10e2016-05-17 16:30:10 +01002729 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002730 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 return other->AsLongConstant()->value_ == value_;
2732 }
2733
2734 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2735
2736 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002737 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2738 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002739 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2740
2741 DECLARE_INSTRUCTION(LongConstant);
2742
2743 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002744 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2745 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002746
2747 const int64_t value_;
2748
2749 friend class HGraph;
2750 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2751};
Dave Allison20dfc792014-06-16 20:44:29 -07002752
Vladimir Markofcb503c2016-05-18 12:48:17 +01002753class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002754 public:
2755 float GetValue() const { return value_; }
2756
2757 uint64_t GetValueAsUint64() const OVERRIDE {
2758 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2759 }
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->IsFloatConstant()) << other->DebugName();
2763 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2764 }
2765
2766 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2767
2768 bool IsMinusOne() const OVERRIDE {
2769 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2770 }
Roland Levillain1a653882016-03-18 18:05:57 +00002771 bool IsArithmeticZero() const OVERRIDE {
2772 return std::fpclassify(value_) == FP_ZERO;
2773 }
2774 bool IsArithmeticPositiveZero() const {
2775 return IsArithmeticZero() && !std::signbit(value_);
2776 }
2777 bool IsArithmeticNegativeZero() const {
2778 return IsArithmeticZero() && std::signbit(value_);
2779 }
2780 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002781 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002782 }
2783 bool IsOne() const OVERRIDE {
2784 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2785 }
2786 bool IsNaN() const {
2787 return std::isnan(value_);
2788 }
2789
2790 DECLARE_INSTRUCTION(FloatConstant);
2791
2792 private:
2793 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2794 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2795 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2796 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2797
2798 const float value_;
2799
2800 // Only the SsaBuilder and HGraph can create floating-point constants.
2801 friend class SsaBuilder;
2802 friend class HGraph;
2803 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2804};
2805
Vladimir Markofcb503c2016-05-18 12:48:17 +01002806class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002807 public:
2808 double GetValue() const { return value_; }
2809
2810 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2811
Vladimir Marko372f10e2016-05-17 16:30:10 +01002812 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002813 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2814 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2815 }
2816
2817 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2818
2819 bool IsMinusOne() const OVERRIDE {
2820 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2821 }
Roland Levillain1a653882016-03-18 18:05:57 +00002822 bool IsArithmeticZero() const OVERRIDE {
2823 return std::fpclassify(value_) == FP_ZERO;
2824 }
2825 bool IsArithmeticPositiveZero() const {
2826 return IsArithmeticZero() && !std::signbit(value_);
2827 }
2828 bool IsArithmeticNegativeZero() const {
2829 return IsArithmeticZero() && std::signbit(value_);
2830 }
2831 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002832 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002833 }
2834 bool IsOne() const OVERRIDE {
2835 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2836 }
2837 bool IsNaN() const {
2838 return std::isnan(value_);
2839 }
2840
2841 DECLARE_INSTRUCTION(DoubleConstant);
2842
2843 private:
2844 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2845 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2846 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2847 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2848
2849 const double value_;
2850
2851 // Only the SsaBuilder and HGraph can create floating-point constants.
2852 friend class SsaBuilder;
2853 friend class HGraph;
2854 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2855};
2856
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002857// Conditional branch. A block ending with an HIf instruction must have
2858// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002859class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002860 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002861 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2862 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002863 SetRawInputAt(0, input);
2864 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002865
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002866 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002867
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002868 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002869 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002870 }
2871
2872 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002873 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002874 }
2875
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002876 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002877
2878 private:
2879 DISALLOW_COPY_AND_ASSIGN(HIf);
2880};
2881
David Brazdilfc6a86a2015-06-26 10:33:45 +00002882
2883// Abstract instruction which marks the beginning and/or end of a try block and
2884// links it to the respective exception handlers. Behaves the same as a Goto in
2885// non-exceptional control flow.
2886// Normal-flow successor is stored at index zero, exception handlers under
2887// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002888class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002889 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002890 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002891 kEntry,
2892 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002893 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002894 };
2895
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002896 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002897 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2898 SetPackedField<BoundaryKindField>(kind);
2899 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002900
2901 bool IsControlFlow() const OVERRIDE { return true; }
2902
2903 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002904 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002905
David Brazdild26a4112015-11-10 11:07:31 +00002906 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2907 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2908 }
2909
David Brazdilfc6a86a2015-06-26 10:33:45 +00002910 // Returns whether `handler` is among its exception handlers (non-zero index
2911 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002912 bool HasExceptionHandler(const HBasicBlock& handler) const {
2913 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002914 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002915 }
2916
2917 // If not present already, adds `handler` to its block's list of exception
2918 // handlers.
2919 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002920 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002921 GetBlock()->AddSuccessor(handler);
2922 }
2923 }
2924
Vladimir Markoa1de9182016-02-25 11:37:38 +00002925 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2926 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002927
David Brazdilffee3d32015-07-06 11:48:53 +01002928 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2929
David Brazdilfc6a86a2015-06-26 10:33:45 +00002930 DECLARE_INSTRUCTION(TryBoundary);
2931
2932 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002933 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2934 static constexpr size_t kFieldBoundaryKindSize =
2935 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2936 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2937 kFieldBoundaryKind + kFieldBoundaryKindSize;
2938 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2939 "Too many packed fields.");
2940 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002941
2942 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2943};
2944
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002945// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002946class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002947 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002948 // We set CanTriggerGC to prevent any intermediate address to be live
2949 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002950 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002951 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002952 SetRawInputAt(0, cond);
2953 }
2954
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002955 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002956 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002957 return true;
2958 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002959 bool NeedsEnvironment() const OVERRIDE { return true; }
2960 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002961
2962 DECLARE_INSTRUCTION(Deoptimize);
2963
2964 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002965 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2966};
2967
Mingyao Yang063fc772016-08-02 11:02:54 -07002968// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2969// The compiled code checks this flag value in a guard before devirtualized call and
2970// if it's true, starts to do deoptimization.
2971// It has a 4-byte slot on stack.
2972// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002973class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002974 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002975 // CHA guards are only optimized in a separate pass and it has no side effects
2976 // with regard to other passes.
2977 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2978 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002979 }
2980
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002981 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2982
2983 // We do all CHA guard elimination/motion in a single pass, after which there is no
2984 // further guard elimination/motion since a guard might have been used for justification
2985 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2986 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002987 bool CanBeMoved() const OVERRIDE { return false; }
2988
2989 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2990
2991 private:
2992 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2993};
2994
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002995// Represents the ArtMethod that was passed as a first argument to
2996// the method. It is used by instructions that depend on it, like
2997// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002998class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002999 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003000 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3001 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003002
3003 DECLARE_INSTRUCTION(CurrentMethod);
3004
3005 private:
3006 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3007};
3008
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003009// Fetches an ArtMethod from the virtual table or the interface method table
3010// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003011class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003012 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003013 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003014 kVTable,
3015 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003016 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003017 };
3018 HClassTableGet(HInstruction* cls,
3019 Primitive::Type type,
3020 TableKind kind,
3021 size_t index,
3022 uint32_t dex_pc)
3023 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003024 index_(index) {
3025 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003026 SetRawInputAt(0, cls);
3027 }
3028
3029 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003030 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003031 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003032 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003033 }
3034
Vladimir Markoa1de9182016-02-25 11:37:38 +00003035 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003036 size_t GetIndex() const { return index_; }
3037
3038 DECLARE_INSTRUCTION(ClassTableGet);
3039
3040 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003041 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3042 static constexpr size_t kFieldTableKindSize =
3043 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3044 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3045 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3046 "Too many packed fields.");
3047 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3048
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003049 // The index of the ArtMethod in the table.
3050 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003051
3052 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3053};
3054
Mark Mendellfe57faa2015-09-18 09:26:15 -04003055// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3056// have one successor for each entry in the switch table, and the final successor
3057// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003058class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003059 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003060 HPackedSwitch(int32_t start_value,
3061 uint32_t num_entries,
3062 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003063 uint32_t dex_pc = kNoDexPc)
3064 : HTemplateInstruction(SideEffects::None(), dex_pc),
3065 start_value_(start_value),
3066 num_entries_(num_entries) {
3067 SetRawInputAt(0, input);
3068 }
3069
3070 bool IsControlFlow() const OVERRIDE { return true; }
3071
3072 int32_t GetStartValue() const { return start_value_; }
3073
Vladimir Marko211c2112015-09-24 16:52:33 +01003074 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003075
3076 HBasicBlock* GetDefaultBlock() const {
3077 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003078 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003079 }
3080 DECLARE_INSTRUCTION(PackedSwitch);
3081
3082 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003083 const int32_t start_value_;
3084 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003085
3086 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3087};
3088
Roland Levillain88cb1752014-10-20 16:36:47 +01003089class HUnaryOperation : public HExpression<1> {
3090 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003091 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3092 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003093 SetRawInputAt(0, input);
3094 }
3095
3096 HInstruction* GetInput() const { return InputAt(0); }
3097 Primitive::Type GetResultType() const { return GetType(); }
3098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003100 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003101 return true;
3102 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003103
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 // Try to statically evaluate `this` and return a HConstant
3105 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003106 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003107 HConstant* TryStaticEvaluation() const;
3108
3109 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003110 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3111 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003112 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3113 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003114
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003115 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003116
3117 private:
3118 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3119};
3120
Dave Allison20dfc792014-06-16 20:44:29 -07003121class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003122 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003123 HBinaryOperation(Primitive::Type result_type,
3124 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003125 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003126 SideEffects side_effects = SideEffects::None(),
3127 uint32_t dex_pc = kNoDexPc)
3128 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003129 SetRawInputAt(0, left);
3130 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003131 }
3132
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003133 HInstruction* GetLeft() const { return InputAt(0); }
3134 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003135 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003136
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003137 virtual bool IsCommutative() const { return false; }
3138
3139 // Put constant on the right.
3140 // Returns whether order is changed.
3141 bool OrderInputsWithConstantOnTheRight() {
3142 HInstruction* left = InputAt(0);
3143 HInstruction* right = InputAt(1);
3144 if (left->IsConstant() && !right->IsConstant()) {
3145 ReplaceInput(right, 0);
3146 ReplaceInput(left, 1);
3147 return true;
3148 }
3149 return false;
3150 }
3151
3152 // Order inputs by instruction id, but favor constant on the right side.
3153 // This helps GVN for commutative ops.
3154 void OrderInputs() {
3155 DCHECK(IsCommutative());
3156 HInstruction* left = InputAt(0);
3157 HInstruction* right = InputAt(1);
3158 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3159 return;
3160 }
3161 if (OrderInputsWithConstantOnTheRight()) {
3162 return;
3163 }
3164 // Order according to instruction id.
3165 if (left->GetId() > right->GetId()) {
3166 ReplaceInput(right, 0);
3167 ReplaceInput(left, 1);
3168 }
3169 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003170
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003171 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003172 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003173 return true;
3174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003175
Roland Levillain31dd3d62016-02-16 12:21:02 +00003176 // Try to statically evaluate `this` and return a HConstant
3177 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003178 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003179 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003180
3181 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3183 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003184 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3185 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003186 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003187 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3188 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003189 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3190 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003191 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3192 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003193 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003194 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3195 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003196
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003197 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003198 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003199 HConstant* GetConstantRight() const;
3200
3201 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003202 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003203 HInstruction* GetLeastConstantLeft() const;
3204
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003205 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003206
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003207 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003208 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3209};
3210
Mark Mendellc4701932015-04-10 13:18:51 -04003211// The comparison bias applies for floating point operations and indicates how NaN
3212// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003213enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003214 kNoBias, // bias is not applicable (i.e. for long operation)
3215 kGtBias, // return 1 for NaN comparisons
3216 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003217 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003218};
3219
Roland Levillain31dd3d62016-02-16 12:21:02 +00003220std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3221
Dave Allison20dfc792014-06-16 20:44:29 -07003222class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003223 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003224 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003225 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3226 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3227 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003228
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003229 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003230 // `instruction`, and disregard moves in between.
3231 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003232
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003233 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003234
3235 virtual IfCondition GetCondition() const = 0;
3236
Mark Mendellc4701932015-04-10 13:18:51 -04003237 virtual IfCondition GetOppositeCondition() const = 0;
3238
Vladimir Markoa1de9182016-02-25 11:37:38 +00003239 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003240 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3241
Vladimir Markoa1de9182016-02-25 11:37:38 +00003242 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3243 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003244
Vladimir Marko372f10e2016-05-17 16:30:10 +01003245 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003246 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003247 }
3248
Roland Levillain4fa13f62015-07-06 18:11:54 +01003249 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003250 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003251 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003252 if (if_cond == kCondNE) {
3253 return true;
3254 } else if (if_cond == kCondEQ) {
3255 return false;
3256 }
3257 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003258 }
3259
3260 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003262 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003263 if (if_cond == kCondEQ) {
3264 return true;
3265 } else if (if_cond == kCondNE) {
3266 return false;
3267 }
3268 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003269 }
3270
Roland Levillain31dd3d62016-02-16 12:21:02 +00003271 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003272 // Needed if we merge a HCompare into a HCondition.
3273 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3274 static constexpr size_t kFieldComparisonBiasSize =
3275 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3276 static constexpr size_t kNumberOfConditionPackedBits =
3277 kFieldComparisonBias + kFieldComparisonBiasSize;
3278 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3279 using ComparisonBiasField =
3280 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3281
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 template <typename T>
3283 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3284
3285 template <typename T>
3286 int32_t CompareFP(T x, T y) const {
3287 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3288 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3289 // Handle the bias.
3290 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3291 }
3292
3293 // Return an integer constant containing the result of a condition evaluated at compile time.
3294 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3295 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3296 }
3297
Dave Allison20dfc792014-06-16 20:44:29 -07003298 private:
3299 DISALLOW_COPY_AND_ASSIGN(HCondition);
3300};
3301
3302// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003303class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003304 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003305 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3306 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003307
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003308 bool IsCommutative() const OVERRIDE { return true; }
3309
Vladimir Marko9e23df52015-11-10 17:14:35 +00003310 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3311 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003312 return MakeConstantCondition(true, GetDexPc());
3313 }
3314 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3315 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3316 }
3317 // In the following Evaluate methods, a HCompare instruction has
3318 // been merged into this HEqual instruction; evaluate it as
3319 // `Compare(x, y) == 0`.
3320 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3321 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3322 GetDexPc());
3323 }
3324 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3325 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3326 }
3327 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3328 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003329 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003330
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003331 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003332
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003333 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003334 return kCondEQ;
3335 }
3336
Mark Mendellc4701932015-04-10 13:18:51 -04003337 IfCondition GetOppositeCondition() const OVERRIDE {
3338 return kCondNE;
3339 }
3340
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003341 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003342 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003343
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003344 DISALLOW_COPY_AND_ASSIGN(HEqual);
3345};
3346
Vladimir Markofcb503c2016-05-18 12:48:17 +01003347class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003348 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003349 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3350 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003351
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003352 bool IsCommutative() const OVERRIDE { return true; }
3353
Vladimir Marko9e23df52015-11-10 17:14:35 +00003354 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3355 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003356 return MakeConstantCondition(false, GetDexPc());
3357 }
3358 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3359 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3360 }
3361 // In the following Evaluate methods, a HCompare instruction has
3362 // been merged into this HNotEqual instruction; evaluate it as
3363 // `Compare(x, y) != 0`.
3364 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3365 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3366 }
3367 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3368 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3369 }
3370 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3371 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003372 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003373
Dave Allison20dfc792014-06-16 20:44:29 -07003374 DECLARE_INSTRUCTION(NotEqual);
3375
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003376 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003377 return kCondNE;
3378 }
3379
Mark Mendellc4701932015-04-10 13:18:51 -04003380 IfCondition GetOppositeCondition() const OVERRIDE {
3381 return kCondEQ;
3382 }
3383
Dave Allison20dfc792014-06-16 20:44:29 -07003384 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003385 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003386
Dave Allison20dfc792014-06-16 20:44:29 -07003387 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3388};
3389
Vladimir Markofcb503c2016-05-18 12:48:17 +01003390class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003391 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003392 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3393 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003394
Roland Levillain9867bc72015-08-05 10:21:34 +01003395 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003396 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003397 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003398 // In the following Evaluate methods, a HCompare instruction has
3399 // been merged into this HLessThan instruction; evaluate it as
3400 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003401 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003402 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3403 }
3404 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3405 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3406 }
3407 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3408 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003409 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003410
Dave Allison20dfc792014-06-16 20:44:29 -07003411 DECLARE_INSTRUCTION(LessThan);
3412
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003413 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003414 return kCondLT;
3415 }
3416
Mark Mendellc4701932015-04-10 13:18:51 -04003417 IfCondition GetOppositeCondition() const OVERRIDE {
3418 return kCondGE;
3419 }
3420
Dave Allison20dfc792014-06-16 20:44:29 -07003421 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003422 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003423
Dave Allison20dfc792014-06-16 20:44:29 -07003424 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3425};
3426
Vladimir Markofcb503c2016-05-18 12:48:17 +01003427class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003428 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003429 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3430 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003431
Roland Levillain9867bc72015-08-05 10:21:34 +01003432 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003433 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003434 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003435 // In the following Evaluate methods, a HCompare instruction has
3436 // been merged into this HLessThanOrEqual instruction; evaluate it as
3437 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003438 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003439 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3440 }
3441 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3442 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3443 }
3444 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3445 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003446 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003447
Dave Allison20dfc792014-06-16 20:44:29 -07003448 DECLARE_INSTRUCTION(LessThanOrEqual);
3449
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003450 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003451 return kCondLE;
3452 }
3453
Mark Mendellc4701932015-04-10 13:18:51 -04003454 IfCondition GetOppositeCondition() const OVERRIDE {
3455 return kCondGT;
3456 }
3457
Dave Allison20dfc792014-06-16 20:44:29 -07003458 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003459 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003460
Dave Allison20dfc792014-06-16 20:44:29 -07003461 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3462};
3463
Vladimir Markofcb503c2016-05-18 12:48:17 +01003464class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003465 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003466 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3467 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003468
Roland Levillain9867bc72015-08-05 10:21:34 +01003469 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003470 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003471 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 // In the following Evaluate methods, a HCompare instruction has
3473 // been merged into this HGreaterThan instruction; evaluate it as
3474 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003475 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003476 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3477 }
3478 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3479 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3480 }
3481 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3482 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003483 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003484
Dave Allison20dfc792014-06-16 20:44:29 -07003485 DECLARE_INSTRUCTION(GreaterThan);
3486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003487 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003488 return kCondGT;
3489 }
3490
Mark Mendellc4701932015-04-10 13:18:51 -04003491 IfCondition GetOppositeCondition() const OVERRIDE {
3492 return kCondLE;
3493 }
3494
Dave Allison20dfc792014-06-16 20:44:29 -07003495 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003496 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003497
Dave Allison20dfc792014-06-16 20:44:29 -07003498 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3499};
3500
Vladimir Markofcb503c2016-05-18 12:48:17 +01003501class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003502 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003503 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3504 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003505
Roland Levillain9867bc72015-08-05 10:21:34 +01003506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003507 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003508 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003509 // In the following Evaluate methods, a HCompare instruction has
3510 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3511 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003512 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3514 }
3515 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3516 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3517 }
3518 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3519 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003520 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003521
Dave Allison20dfc792014-06-16 20:44:29 -07003522 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3523
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003524 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003525 return kCondGE;
3526 }
3527
Mark Mendellc4701932015-04-10 13:18:51 -04003528 IfCondition GetOppositeCondition() const OVERRIDE {
3529 return kCondLT;
3530 }
3531
Dave Allison20dfc792014-06-16 20:44:29 -07003532 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003533 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003534
Dave Allison20dfc792014-06-16 20:44:29 -07003535 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3536};
3537
Vladimir Markofcb503c2016-05-18 12:48:17 +01003538class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003539 public:
3540 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3541 : HCondition(first, second, dex_pc) {}
3542
3543 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003544 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003545 }
3546 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003547 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3548 }
3549 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3550 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3551 LOG(FATAL) << DebugName() << " is not defined for float values";
3552 UNREACHABLE();
3553 }
3554 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3555 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3556 LOG(FATAL) << DebugName() << " is not defined for double values";
3557 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003558 }
3559
3560 DECLARE_INSTRUCTION(Below);
3561
3562 IfCondition GetCondition() const OVERRIDE {
3563 return kCondB;
3564 }
3565
3566 IfCondition GetOppositeCondition() const OVERRIDE {
3567 return kCondAE;
3568 }
3569
3570 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003571 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeUnsigned(x) < MakeUnsigned(y);
3573 }
Aart Bike9f37602015-10-09 11:15:55 -07003574
3575 DISALLOW_COPY_AND_ASSIGN(HBelow);
3576};
3577
Vladimir Markofcb503c2016-05-18 12:48:17 +01003578class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003579 public:
3580 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3581 : HCondition(first, second, dex_pc) {}
3582
3583 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003584 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003585 }
3586 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3588 }
3589 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3590 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3591 LOG(FATAL) << DebugName() << " is not defined for float values";
3592 UNREACHABLE();
3593 }
3594 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3595 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3596 LOG(FATAL) << DebugName() << " is not defined for double values";
3597 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003598 }
3599
3600 DECLARE_INSTRUCTION(BelowOrEqual);
3601
3602 IfCondition GetCondition() const OVERRIDE {
3603 return kCondBE;
3604 }
3605
3606 IfCondition GetOppositeCondition() const OVERRIDE {
3607 return kCondA;
3608 }
3609
3610 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003611 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 return MakeUnsigned(x) <= MakeUnsigned(y);
3613 }
Aart Bike9f37602015-10-09 11:15:55 -07003614
3615 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3616};
3617
Vladimir Markofcb503c2016-05-18 12:48:17 +01003618class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003619 public:
3620 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3621 : HCondition(first, second, dex_pc) {}
3622
3623 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003625 }
3626 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003627 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3628 }
3629 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3630 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3631 LOG(FATAL) << DebugName() << " is not defined for float values";
3632 UNREACHABLE();
3633 }
3634 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3635 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3636 LOG(FATAL) << DebugName() << " is not defined for double values";
3637 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003638 }
3639
3640 DECLARE_INSTRUCTION(Above);
3641
3642 IfCondition GetCondition() const OVERRIDE {
3643 return kCondA;
3644 }
3645
3646 IfCondition GetOppositeCondition() const OVERRIDE {
3647 return kCondBE;
3648 }
3649
3650 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003651 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 return MakeUnsigned(x) > MakeUnsigned(y);
3653 }
Aart Bike9f37602015-10-09 11:15:55 -07003654
3655 DISALLOW_COPY_AND_ASSIGN(HAbove);
3656};
3657
Vladimir Markofcb503c2016-05-18 12:48:17 +01003658class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003659 public:
3660 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3661 : HCondition(first, second, dex_pc) {}
3662
3663 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003664 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003665 }
3666 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003667 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3668 }
3669 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3670 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3671 LOG(FATAL) << DebugName() << " is not defined for float values";
3672 UNREACHABLE();
3673 }
3674 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3675 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3676 LOG(FATAL) << DebugName() << " is not defined for double values";
3677 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003678 }
3679
3680 DECLARE_INSTRUCTION(AboveOrEqual);
3681
3682 IfCondition GetCondition() const OVERRIDE {
3683 return kCondAE;
3684 }
3685
3686 IfCondition GetOppositeCondition() const OVERRIDE {
3687 return kCondB;
3688 }
3689
3690 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003691 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003692 return MakeUnsigned(x) >= MakeUnsigned(y);
3693 }
Aart Bike9f37602015-10-09 11:15:55 -07003694
3695 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3696};
Dave Allison20dfc792014-06-16 20:44:29 -07003697
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003698// Instruction to check how two inputs compare to each other.
3699// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003700class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003701 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003702 // Note that `comparison_type` is the type of comparison performed
3703 // between the comparison's inputs, not the type of the instantiated
3704 // HCompare instruction (which is always Primitive::kPrimInt).
3705 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003706 HInstruction* first,
3707 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003708 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003709 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003710 : HBinaryOperation(Primitive::kPrimInt,
3711 first,
3712 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003713 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003714 dex_pc) {
3715 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003716 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3717 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003718 }
3719
Roland Levillain9867bc72015-08-05 10:21:34 +01003720 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3722
3723 template <typename T>
3724 int32_t ComputeFP(T x, T y) const {
3725 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3726 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3727 // Handle the bias.
3728 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3729 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003730
Roland Levillain9867bc72015-08-05 10:21:34 +01003731 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3733 // reach this code path when processing a freshly built HIR
3734 // graph. However HCompare integer instructions can be synthesized
3735 // by the instruction simplifier to implement IntegerCompare and
3736 // IntegerSignum intrinsics, so we have to handle this case.
3737 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003738 }
3739 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003740 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3741 }
3742 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3743 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3744 }
3745 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3746 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003747 }
3748
Vladimir Marko372f10e2016-05-17 16:30:10 +01003749 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003750 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003751 }
3752
Vladimir Markoa1de9182016-02-25 11:37:38 +00003753 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003754
Roland Levillain31dd3d62016-02-16 12:21:02 +00003755 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003756 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 bool IsGtBias() const {
3758 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003759 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003761
Roland Levillain1693a1f2016-03-15 14:57:31 +00003762 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3763 // Comparisons do not require a runtime call in any back end.
3764 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003765 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003766
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003767 DECLARE_INSTRUCTION(Compare);
3768
Roland Levillain31dd3d62016-02-16 12:21:02 +00003769 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003770 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3771 static constexpr size_t kFieldComparisonBiasSize =
3772 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3773 static constexpr size_t kNumberOfComparePackedBits =
3774 kFieldComparisonBias + kFieldComparisonBiasSize;
3775 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3776 using ComparisonBiasField =
3777 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3778
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 // Return an integer constant containing the result of a comparison evaluated at compile time.
3780 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3781 DCHECK(value == -1 || value == 0 || value == 1) << value;
3782 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3783 }
3784
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003785 private:
3786 DISALLOW_COPY_AND_ASSIGN(HCompare);
3787};
3788
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003789class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003790 public:
3791 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003792 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003793 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003794 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003795 bool finalizable,
3796 QuickEntrypointEnum entrypoint)
3797 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3798 type_index_(type_index),
3799 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003800 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003801 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003802 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003803 }
3804
Andreas Gampea5b09a62016-11-17 15:21:22 -08003805 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003806 const DexFile& GetDexFile() const { return dex_file_; }
3807
3808 // Calls runtime so needs an environment.
3809 bool NeedsEnvironment() const OVERRIDE { return true; }
3810
Mingyao Yang062157f2016-03-02 10:15:36 -08003811 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3812 bool CanThrow() const OVERRIDE { return true; }
3813
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003814 bool NeedsChecks() const {
3815 return entrypoint_ == kQuickAllocObjectWithChecks;
3816 }
David Brazdil6de19382016-01-08 17:37:10 +00003817
Vladimir Markoa1de9182016-02-25 11:37:38 +00003818 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003819
3820 bool CanBeNull() const OVERRIDE { return false; }
3821
3822 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3823
3824 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3825 entrypoint_ = entrypoint;
3826 }
3827
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003828 HLoadClass* GetLoadClass() const {
3829 HInstruction* input = InputAt(0);
3830 if (input->IsClinitCheck()) {
3831 input = input->InputAt(0);
3832 }
3833 DCHECK(input->IsLoadClass());
3834 return input->AsLoadClass();
3835 }
3836
David Brazdil6de19382016-01-08 17:37:10 +00003837 bool IsStringAlloc() const;
3838
3839 DECLARE_INSTRUCTION(NewInstance);
3840
3841 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003842 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003843 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3844 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3845 "Too many packed fields.");
3846
Andreas Gampea5b09a62016-11-17 15:21:22 -08003847 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003848 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003849 QuickEntrypointEnum entrypoint_;
3850
3851 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3852};
3853
Agi Csaki05f20562015-08-19 14:58:14 -07003854enum IntrinsicNeedsEnvironmentOrCache {
3855 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3856 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003857};
3858
Aart Bik5d75afe2015-12-14 11:57:01 -08003859enum IntrinsicSideEffects {
3860 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3861 kReadSideEffects, // Intrinsic may read heap memory.
3862 kWriteSideEffects, // Intrinsic may write heap memory.
3863 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3864};
3865
3866enum IntrinsicExceptions {
3867 kNoThrow, // Intrinsic does not throw any exceptions.
3868 kCanThrow // Intrinsic may throw exceptions.
3869};
3870
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003871class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003872 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003873 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003874
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003875 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003876 SetRawInputAt(index, argument);
3877 }
3878
Roland Levillain3e3d7332015-04-28 11:00:54 +01003879 // Return the number of arguments. This number can be lower than
3880 // the number of inputs returned by InputCount(), as some invoke
3881 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3882 // inputs at the end of their list of inputs.
3883 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3884
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003886
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003887 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3888
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003889 InvokeType GetInvokeType() const {
3890 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003891 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003892
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003893 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003894 return intrinsic_;
3895 }
3896
Aart Bik5d75afe2015-12-14 11:57:01 -08003897 void SetIntrinsic(Intrinsics intrinsic,
3898 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3899 IntrinsicSideEffects side_effects,
3900 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003901
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003902 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003903 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003904 }
3905
Aart Bikff7d89c2016-11-07 08:49:28 -08003906 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3907
Vladimir Markoa1de9182016-02-25 11:37:38 +00003908 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003909
Aart Bik71bf7b42016-11-16 10:17:46 -08003910 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003911
Vladimir Marko372f10e2016-05-17 16:30:10 +01003912 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003913 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3914 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003915
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003916 uint32_t* GetIntrinsicOptimizations() {
3917 return &intrinsic_optimizations_;
3918 }
3919
3920 const uint32_t* GetIntrinsicOptimizations() const {
3921 return &intrinsic_optimizations_;
3922 }
3923
3924 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3925
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003926 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00003927 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003928
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003929 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003930
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003931 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003932 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3933 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003934 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3935 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003936 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003937 static constexpr size_t kFieldReturnTypeSize =
3938 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3939 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3940 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3941 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003942 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003943 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3944
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003945 HInvoke(ArenaAllocator* arena,
3946 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003947 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003948 Primitive::Type return_type,
3949 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003950 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003951 ArtMethod* resolved_method,
3952 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003953 : HVariableInputSizeInstruction(
3954 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3955 dex_pc,
3956 arena,
3957 number_of_arguments + number_of_other_inputs,
3958 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003959 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003960 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003961 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003962 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003963 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003964 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003965 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003966 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003967 }
3968
Roland Levillain3e3d7332015-04-28 11:00:54 +01003969 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00003970 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003971 const uint32_t dex_method_index_;
3972 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003973
3974 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3975 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003976
3977 private:
3978 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3979};
3980
Vladimir Markofcb503c2016-05-18 12:48:17 +01003981class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003982 public:
3983 HInvokeUnresolved(ArenaAllocator* arena,
3984 uint32_t number_of_arguments,
3985 Primitive::Type return_type,
3986 uint32_t dex_pc,
3987 uint32_t dex_method_index,
3988 InvokeType invoke_type)
3989 : HInvoke(arena,
3990 number_of_arguments,
3991 0u /* number_of_other_inputs */,
3992 return_type,
3993 dex_pc,
3994 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003995 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003996 invoke_type) {
3997 }
3998
3999 DECLARE_INSTRUCTION(InvokeUnresolved);
4000
4001 private:
4002 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4003};
4004
Orion Hodsonac141392017-01-13 11:53:47 +00004005class HInvokePolymorphic FINAL : public HInvoke {
4006 public:
4007 HInvokePolymorphic(ArenaAllocator* arena,
4008 uint32_t number_of_arguments,
4009 Primitive::Type return_type,
4010 uint32_t dex_pc,
4011 uint32_t dex_method_index)
4012 : HInvoke(arena,
4013 number_of_arguments,
4014 0u /* number_of_other_inputs */,
4015 return_type,
4016 dex_pc,
4017 dex_method_index,
4018 nullptr,
4019 kVirtual) {}
4020
4021 DECLARE_INSTRUCTION(InvokePolymorphic);
4022
4023 private:
4024 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4025};
4026
Vladimir Markofcb503c2016-05-18 12:48:17 +01004027class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004028 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004029 // Requirements of this method call regarding the class
4030 // initialization (clinit) check of its declaring class.
4031 enum class ClinitCheckRequirement {
4032 kNone, // Class already initialized.
4033 kExplicit, // Static call having explicit clinit check as last input.
4034 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004036 };
4037
Vladimir Marko58155012015-08-19 12:49:41 +00004038 // Determines how to load the target ArtMethod*.
4039 enum class MethodLoadKind {
4040 // Use a String init ArtMethod* loaded from Thread entrypoints.
4041 kStringInit,
4042
4043 // Use the method's own ArtMethod* loaded by the register allocator.
4044 kRecursive,
4045
4046 // Use ArtMethod* at a known address, embed the direct address in the code.
4047 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4048 kDirectAddress,
4049
Vladimir Markoa1de9182016-02-25 11:37:38 +00004050 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004051 // Used when we need to use the dex cache, for example for invoke-static that
4052 // may cause class initialization (the entry may point to a resolution method),
4053 // and we know that we can access the dex cache arrays using a PC-relative load.
4054 kDexCachePcRelative,
4055
4056 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4057 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4058 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4059 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4060 kDexCacheViaMethod,
4061 };
4062
4063 // Determines the location of the code pointer.
4064 enum class CodePtrLocation {
4065 // Recursive call, use local PC-relative call instruction.
4066 kCallSelf,
4067
Vladimir Marko58155012015-08-19 12:49:41 +00004068 // Use code pointer from the ArtMethod*.
4069 // Used when we don't know the target code. This is also the last-resort-kind used when
4070 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4071 kCallArtMethod,
4072 };
4073
4074 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004075 MethodLoadKind method_load_kind;
4076 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004077 // The method load data holds
4078 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4079 // Note that there are multiple string init methods, each having its own offset.
4080 // - the method address for kDirectAddress
4081 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004082 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004083 };
4084
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004085 HInvokeStaticOrDirect(ArenaAllocator* arena,
4086 uint32_t number_of_arguments,
4087 Primitive::Type return_type,
4088 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004089 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004090 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004091 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004092 InvokeType invoke_type,
4093 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004094 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004095 : HInvoke(arena,
4096 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004097 // There is potentially one extra argument for the HCurrentMethod node, and
4098 // potentially one other if the clinit check is explicit, and potentially
4099 // one other if the method is a string factory.
4100 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004101 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004102 return_type,
4103 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004104 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004105 resolved_method,
4106 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004107 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004108 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4110 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004111
Vladimir Markodc151b22015-10-15 18:02:30 +01004112 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004113 bool had_current_method_input = HasCurrentMethodInput();
4114 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4115
4116 // Using the current method is the default and once we find a better
4117 // method load kind, we should not go back to using the current method.
4118 DCHECK(had_current_method_input || !needs_current_method_input);
4119
4120 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004121 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4122 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004123 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004124 dispatch_info_ = dispatch_info;
4125 }
4126
Vladimir Markoc53c0792015-11-19 15:48:33 +00004127 void AddSpecialInput(HInstruction* input) {
4128 // We allow only one special input.
4129 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4130 DCHECK(InputCount() == GetSpecialInputIndex() ||
4131 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4132 InsertInputAt(GetSpecialInputIndex(), input);
4133 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004134
Vladimir Marko372f10e2016-05-17 16:30:10 +01004135 using HInstruction::GetInputRecords; // Keep the const version visible.
4136 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4137 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4138 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4139 DCHECK(!input_records.empty());
4140 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4141 HInstruction* last_input = input_records.back().GetInstruction();
4142 // Note: `last_input` may be null during arguments setup.
4143 if (last_input != nullptr) {
4144 // `last_input` is the last input of a static invoke marked as having
4145 // an explicit clinit check. It must either be:
4146 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4147 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4148 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4149 }
4150 }
4151 return input_records;
4152 }
4153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004154 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004155 // We access the method via the dex cache so we can't do an implicit null check.
4156 // TODO: for intrinsics we can generate implicit null checks.
4157 return false;
4158 }
4159
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004160 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004161 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004162 }
4163
Vladimir Markoc53c0792015-11-19 15:48:33 +00004164 // Get the index of the special input, if any.
4165 //
David Brazdil6de19382016-01-08 17:37:10 +00004166 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4167 // method pointer; otherwise there may be one platform-specific special input,
4168 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004169 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004170 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004171
Vladimir Marko58155012015-08-19 12:49:41 +00004172 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4173 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4174 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004175 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004176 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004177 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004178 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004179 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4180 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004181 bool HasCurrentMethodInput() const {
4182 // This function can be called only after the invoke has been fully initialized by the builder.
4183 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004184 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004185 return true;
4186 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004187 DCHECK(InputCount() == GetSpecialInputIndex() ||
4188 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004189 return false;
4190 }
4191 }
Vladimir Marko58155012015-08-19 12:49:41 +00004192
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004193 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004194 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004195 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004196 }
4197
4198 uint64_t GetMethodAddress() const {
4199 DCHECK(HasMethodAddress());
4200 return dispatch_info_.method_load_data;
4201 }
4202
4203 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004204 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004205 return dispatch_info_.method_load_data;
4206 }
4207
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004208 const DexFile& GetDexFileForPcRelativeDexCache() const;
4209
Vladimir Markoa1de9182016-02-25 11:37:38 +00004210 ClinitCheckRequirement GetClinitCheckRequirement() const {
4211 return GetPackedField<ClinitCheckRequirementField>();
4212 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004213
Roland Levillain4c0eb422015-04-24 16:43:49 +01004214 // Is this instruction a call to a static method?
4215 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004216 return GetInvokeType() == kStatic;
4217 }
4218
4219 MethodReference GetTargetMethod() const {
4220 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004221 }
4222
Vladimir Markofbb184a2015-11-13 14:47:00 +00004223 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4224 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4225 // instruction; only relevant for static calls with explicit clinit check.
4226 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004227 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004228 size_t last_input_index = inputs_.size() - 1u;
4229 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004230 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004231 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004232 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004233 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004234 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004235 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004236 }
4237
4238 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004239 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004240 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004241 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004242 }
4243
4244 // Is this a call to a static method whose declaring class has an
4245 // implicit intialization check requirement?
4246 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004247 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004248 }
4249
Vladimir Markob554b5a2015-11-06 12:57:55 +00004250 // Does this method load kind need the current method as an input?
4251 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4252 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4253 }
4254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004255 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004256
4257 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004258 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004259 static constexpr size_t kFieldClinitCheckRequirementSize =
4260 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4261 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4262 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4263 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4264 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004265 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4266 kFieldClinitCheckRequirement,
4267 kFieldClinitCheckRequirementSize>;
4268
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004269 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004270 MethodReference target_method_;
4271 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004272
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004273 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004274};
Vladimir Markof64242a2015-12-01 14:58:23 +00004275std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004276std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004277
Vladimir Markofcb503c2016-05-18 12:48:17 +01004278class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004279 public:
4280 HInvokeVirtual(ArenaAllocator* arena,
4281 uint32_t number_of_arguments,
4282 Primitive::Type return_type,
4283 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004284 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004285 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004286 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004287 : HInvoke(arena,
4288 number_of_arguments,
4289 0u,
4290 return_type,
4291 dex_pc,
4292 dex_method_index,
4293 resolved_method,
4294 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004295 vtable_index_(vtable_index) {}
4296
Aart Bik71bf7b42016-11-16 10:17:46 -08004297 bool CanBeNull() const OVERRIDE {
4298 switch (GetIntrinsic()) {
4299 case Intrinsics::kThreadCurrentThread:
4300 case Intrinsics::kStringBufferAppend:
4301 case Intrinsics::kStringBufferToString:
4302 case Intrinsics::kStringBuilderAppend:
4303 case Intrinsics::kStringBuilderToString:
4304 return false;
4305 default:
4306 return HInvoke::CanBeNull();
4307 }
4308 }
4309
Calin Juravle641547a2015-04-21 22:08:51 +01004310 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004311 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004312 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004313 }
4314
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004315 uint32_t GetVTableIndex() const { return vtable_index_; }
4316
4317 DECLARE_INSTRUCTION(InvokeVirtual);
4318
4319 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004320 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004321 const uint32_t vtable_index_;
4322
4323 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4324};
4325
Vladimir Markofcb503c2016-05-18 12:48:17 +01004326class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004327 public:
4328 HInvokeInterface(ArenaAllocator* arena,
4329 uint32_t number_of_arguments,
4330 Primitive::Type return_type,
4331 uint32_t dex_pc,
4332 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004333 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004334 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004335 : HInvoke(arena,
4336 number_of_arguments,
4337 0u,
4338 return_type,
4339 dex_pc,
4340 dex_method_index,
4341 resolved_method,
4342 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004343 imt_index_(imt_index) {}
4344
Calin Juravle641547a2015-04-21 22:08:51 +01004345 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004346 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004347 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004348 }
4349
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004350 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4351 // The assembly stub currently needs it.
4352 return true;
4353 }
4354
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004355 uint32_t GetImtIndex() const { return imt_index_; }
4356 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4357
4358 DECLARE_INSTRUCTION(InvokeInterface);
4359
4360 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004361 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004362 const uint32_t imt_index_;
4363
4364 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4365};
4366
Vladimir Markofcb503c2016-05-18 12:48:17 +01004367class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004368 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004369 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004370 : HUnaryOperation(result_type, input, dex_pc) {
4371 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4372 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004373
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004374 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004375
4376 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004377 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004378 }
4379 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004380 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004381 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004382 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4383 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4384 }
4385 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4386 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4387 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004388
Roland Levillain88cb1752014-10-20 16:36:47 +01004389 DECLARE_INSTRUCTION(Neg);
4390
4391 private:
4392 DISALLOW_COPY_AND_ASSIGN(HNeg);
4393};
4394
Vladimir Markofcb503c2016-05-18 12:48:17 +01004395class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004396 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004397 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4398 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4399 SetRawInputAt(0, cls);
4400 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004401 }
4402
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004403 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004404 bool NeedsEnvironment() const OVERRIDE { return true; }
4405
Mingyao Yang0c365e62015-03-31 15:09:29 -07004406 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4407 bool CanThrow() const OVERRIDE { return true; }
4408
Calin Juravle10e244f2015-01-26 18:54:32 +00004409 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004410
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004411 HLoadClass* GetLoadClass() const {
4412 DCHECK(InputAt(0)->IsLoadClass());
4413 return InputAt(0)->AsLoadClass();
4414 }
4415
4416 HInstruction* GetLength() const {
4417 return InputAt(1);
4418 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004419
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004420 DECLARE_INSTRUCTION(NewArray);
4421
4422 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004423 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4424};
4425
Vladimir Markofcb503c2016-05-18 12:48:17 +01004426class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004428 HAdd(Primitive::Type result_type,
4429 HInstruction* left,
4430 HInstruction* right,
4431 uint32_t dex_pc = kNoDexPc)
4432 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004434 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004435
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004436 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004437
4438 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004439 return GetBlock()->GetGraph()->GetIntConstant(
4440 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004441 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004442 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004443 return GetBlock()->GetGraph()->GetLongConstant(
4444 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004445 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004446 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4447 return GetBlock()->GetGraph()->GetFloatConstant(
4448 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4449 }
4450 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4451 return GetBlock()->GetGraph()->GetDoubleConstant(
4452 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4453 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004454
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004455 DECLARE_INSTRUCTION(Add);
4456
4457 private:
4458 DISALLOW_COPY_AND_ASSIGN(HAdd);
4459};
4460
Vladimir Markofcb503c2016-05-18 12:48:17 +01004461class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004462 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 HSub(Primitive::Type result_type,
4464 HInstruction* left,
4465 HInstruction* right,
4466 uint32_t dex_pc = kNoDexPc)
4467 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004468
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004469 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004470
4471 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004472 return GetBlock()->GetGraph()->GetIntConstant(
4473 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004474 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004476 return GetBlock()->GetGraph()->GetLongConstant(
4477 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004478 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004479 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4480 return GetBlock()->GetGraph()->GetFloatConstant(
4481 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4482 }
4483 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4484 return GetBlock()->GetGraph()->GetDoubleConstant(
4485 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4486 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004487
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004488 DECLARE_INSTRUCTION(Sub);
4489
4490 private:
4491 DISALLOW_COPY_AND_ASSIGN(HSub);
4492};
4493
Vladimir Markofcb503c2016-05-18 12:48:17 +01004494class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004495 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004496 HMul(Primitive::Type result_type,
4497 HInstruction* left,
4498 HInstruction* right,
4499 uint32_t dex_pc = kNoDexPc)
4500 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004501
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004502 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004503
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004504 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004505
4506 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004507 return GetBlock()->GetGraph()->GetIntConstant(
4508 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004509 }
4510 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004511 return GetBlock()->GetGraph()->GetLongConstant(
4512 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004513 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004514 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4515 return GetBlock()->GetGraph()->GetFloatConstant(
4516 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4517 }
4518 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4519 return GetBlock()->GetGraph()->GetDoubleConstant(
4520 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4521 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004522
4523 DECLARE_INSTRUCTION(Mul);
4524
4525 private:
4526 DISALLOW_COPY_AND_ASSIGN(HMul);
4527};
4528
Vladimir Markofcb503c2016-05-18 12:48:17 +01004529class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004530 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004531 HDiv(Primitive::Type result_type,
4532 HInstruction* left,
4533 HInstruction* right,
4534 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004535 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004536
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004538 T ComputeIntegral(T x, T y) const {
4539 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 // Our graph structure ensures we never have 0 for `y` during
4541 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004542 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004543 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004544 return (y == -1) ? -x : x / y;
4545 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004546
Roland Levillain31dd3d62016-02-16 12:21:02 +00004547 template <typename T>
4548 T ComputeFP(T x, T y) const {
4549 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4550 return x / y;
4551 }
4552
Roland Levillain9867bc72015-08-05 10:21:34 +01004553 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004554 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004555 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004556 }
4557 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004559 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4560 }
4561 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4562 return GetBlock()->GetGraph()->GetFloatConstant(
4563 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4564 }
4565 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4566 return GetBlock()->GetGraph()->GetDoubleConstant(
4567 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004568 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004569
4570 DECLARE_INSTRUCTION(Div);
4571
4572 private:
4573 DISALLOW_COPY_AND_ASSIGN(HDiv);
4574};
4575
Vladimir Markofcb503c2016-05-18 12:48:17 +01004576class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004577 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 HRem(Primitive::Type result_type,
4579 HInstruction* left,
4580 HInstruction* right,
4581 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004582 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004583
Roland Levillain9867bc72015-08-05 10:21:34 +01004584 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004585 T ComputeIntegral(T x, T y) const {
4586 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004587 // Our graph structure ensures we never have 0 for `y` during
4588 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004589 DCHECK_NE(y, 0);
4590 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4591 return (y == -1) ? 0 : x % y;
4592 }
4593
Roland Levillain31dd3d62016-02-16 12:21:02 +00004594 template <typename T>
4595 T ComputeFP(T x, T y) const {
4596 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4597 return std::fmod(x, y);
4598 }
4599
Roland Levillain9867bc72015-08-05 10:21:34 +01004600 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004601 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004602 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 }
4604 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004605 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004606 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004607 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004608 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4609 return GetBlock()->GetGraph()->GetFloatConstant(
4610 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4611 }
4612 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4613 return GetBlock()->GetGraph()->GetDoubleConstant(
4614 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4615 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004616
4617 DECLARE_INSTRUCTION(Rem);
4618
4619 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004620 DISALLOW_COPY_AND_ASSIGN(HRem);
4621};
4622
Vladimir Markofcb503c2016-05-18 12:48:17 +01004623class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004624 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004625 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4626 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004627 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004628 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004629 SetRawInputAt(0, value);
4630 }
4631
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004632 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4633
Calin Juravled0d48522014-11-04 16:40:20 +00004634 bool CanBeMoved() const OVERRIDE { return true; }
4635
Vladimir Marko372f10e2016-05-17 16:30:10 +01004636 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004637 return true;
4638 }
4639
4640 bool NeedsEnvironment() const OVERRIDE { return true; }
4641 bool CanThrow() const OVERRIDE { return true; }
4642
Calin Juravled0d48522014-11-04 16:40:20 +00004643 DECLARE_INSTRUCTION(DivZeroCheck);
4644
4645 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004646 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4647};
4648
Vladimir Markofcb503c2016-05-18 12:48:17 +01004649class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004650 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004651 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004652 HInstruction* value,
4653 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004655 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4656 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4657 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 }
4659
Roland Levillain5b5b9312016-03-22 14:57:31 +00004660 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004661 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004662 return value << (distance & max_shift_distance);
4663 }
4664
4665 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004666 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004667 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004668 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004669 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004670 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004671 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004673 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4674 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4675 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4676 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004677 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004678 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4679 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004680 LOG(FATAL) << DebugName() << " is not defined for float values";
4681 UNREACHABLE();
4682 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004683 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4684 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 LOG(FATAL) << DebugName() << " is not defined for double values";
4686 UNREACHABLE();
4687 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004688
4689 DECLARE_INSTRUCTION(Shl);
4690
4691 private:
4692 DISALLOW_COPY_AND_ASSIGN(HShl);
4693};
4694
Vladimir Markofcb503c2016-05-18 12:48:17 +01004695class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004697 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004698 HInstruction* value,
4699 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004700 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004701 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4702 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4703 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004704 }
4705
Roland Levillain5b5b9312016-03-22 14:57:31 +00004706 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004707 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 return value >> (distance & max_shift_distance);
4709 }
4710
4711 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004712 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004713 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004714 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004715 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004716 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004717 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004718 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004719 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4720 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4721 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4722 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004723 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004724 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4725 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004726 LOG(FATAL) << DebugName() << " is not defined for float values";
4727 UNREACHABLE();
4728 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004729 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4730 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004731 LOG(FATAL) << DebugName() << " is not defined for double values";
4732 UNREACHABLE();
4733 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004734
4735 DECLARE_INSTRUCTION(Shr);
4736
4737 private:
4738 DISALLOW_COPY_AND_ASSIGN(HShr);
4739};
4740
Vladimir Markofcb503c2016-05-18 12:48:17 +01004741class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004742 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004743 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 HInstruction* value,
4745 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4748 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4749 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004750 }
4751
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004753 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 typedef typename std::make_unsigned<T>::type V;
4755 V ux = static_cast<V>(value);
4756 return static_cast<T>(ux >> (distance & max_shift_distance));
4757 }
4758
4759 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004760 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004761 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004762 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004764 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004765 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004766 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4768 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4769 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4770 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004771 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4773 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004774 LOG(FATAL) << DebugName() << " is not defined for float values";
4775 UNREACHABLE();
4776 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4778 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004779 LOG(FATAL) << DebugName() << " is not defined for double values";
4780 UNREACHABLE();
4781 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004782
4783 DECLARE_INSTRUCTION(UShr);
4784
4785 private:
4786 DISALLOW_COPY_AND_ASSIGN(HUShr);
4787};
4788
Vladimir Markofcb503c2016-05-18 12:48:17 +01004789class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004790 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004791 HAnd(Primitive::Type result_type,
4792 HInstruction* left,
4793 HInstruction* right,
4794 uint32_t dex_pc = kNoDexPc)
4795 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004796
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004797 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004798
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004799 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004800
4801 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 return GetBlock()->GetGraph()->GetIntConstant(
4803 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004806 return GetBlock()->GetGraph()->GetLongConstant(
4807 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004808 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004809 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4810 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4811 LOG(FATAL) << DebugName() << " is not defined for float values";
4812 UNREACHABLE();
4813 }
4814 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4815 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4816 LOG(FATAL) << DebugName() << " is not defined for double values";
4817 UNREACHABLE();
4818 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004819
4820 DECLARE_INSTRUCTION(And);
4821
4822 private:
4823 DISALLOW_COPY_AND_ASSIGN(HAnd);
4824};
4825
Vladimir Markofcb503c2016-05-18 12:48:17 +01004826class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004827 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004828 HOr(Primitive::Type result_type,
4829 HInstruction* left,
4830 HInstruction* right,
4831 uint32_t dex_pc = kNoDexPc)
4832 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004833
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004834 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004835
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004836 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004837
4838 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004839 return GetBlock()->GetGraph()->GetIntConstant(
4840 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004841 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 return GetBlock()->GetGraph()->GetLongConstant(
4844 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004845 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004846 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4847 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4848 LOG(FATAL) << DebugName() << " is not defined for float values";
4849 UNREACHABLE();
4850 }
4851 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4852 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4853 LOG(FATAL) << DebugName() << " is not defined for double values";
4854 UNREACHABLE();
4855 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004856
4857 DECLARE_INSTRUCTION(Or);
4858
4859 private:
4860 DISALLOW_COPY_AND_ASSIGN(HOr);
4861};
4862
Vladimir Markofcb503c2016-05-18 12:48:17 +01004863class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004864 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 HXor(Primitive::Type result_type,
4866 HInstruction* left,
4867 HInstruction* right,
4868 uint32_t dex_pc = kNoDexPc)
4869 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004870
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004871 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004872
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004873 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004874
4875 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004876 return GetBlock()->GetGraph()->GetIntConstant(
4877 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004878 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004880 return GetBlock()->GetGraph()->GetLongConstant(
4881 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004882 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004883 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4884 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4885 LOG(FATAL) << DebugName() << " is not defined for float values";
4886 UNREACHABLE();
4887 }
4888 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4889 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4890 LOG(FATAL) << DebugName() << " is not defined for double values";
4891 UNREACHABLE();
4892 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004893
4894 DECLARE_INSTRUCTION(Xor);
4895
4896 private:
4897 DISALLOW_COPY_AND_ASSIGN(HXor);
4898};
4899
Vladimir Markofcb503c2016-05-18 12:48:17 +01004900class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004901 public:
4902 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004903 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004904 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4905 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004906 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004907
Roland Levillain5b5b9312016-03-22 14:57:31 +00004908 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004909 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004910 typedef typename std::make_unsigned<T>::type V;
4911 V ux = static_cast<V>(value);
4912 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004913 return static_cast<T>(ux);
4914 } else {
4915 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004916 return static_cast<T>(ux >> (distance & max_shift_value)) |
4917 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004918 }
4919 }
4920
Roland Levillain5b5b9312016-03-22 14:57:31 +00004921 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004922 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004923 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004924 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004925 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004926 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004927 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004928 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004929 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4930 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4931 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4932 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004933 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004934 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4935 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004936 LOG(FATAL) << DebugName() << " is not defined for float values";
4937 UNREACHABLE();
4938 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004939 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4940 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004941 LOG(FATAL) << DebugName() << " is not defined for double values";
4942 UNREACHABLE();
4943 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004944
4945 DECLARE_INSTRUCTION(Ror);
4946
4947 private:
4948 DISALLOW_COPY_AND_ASSIGN(HRor);
4949};
4950
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004951// The value of a parameter in this method. Its location depends on
4952// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004953class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004954 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004955 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004956 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004957 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004958 Primitive::Type parameter_type,
4959 bool is_this = false)
4960 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004961 dex_file_(dex_file),
4962 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004963 index_(index) {
4964 SetPackedFlag<kFlagIsThis>(is_this);
4965 SetPackedFlag<kFlagCanBeNull>(!is_this);
4966 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004967
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004968 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004969 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004970 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004971 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004972
Vladimir Markoa1de9182016-02-25 11:37:38 +00004973 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4974 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004975
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004976 DECLARE_INSTRUCTION(ParameterValue);
4977
4978 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004979 // Whether or not the parameter value corresponds to 'this' argument.
4980 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4981 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4982 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4983 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4984 "Too many packed fields.");
4985
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004986 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004987 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004988 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004989 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004990 const uint8_t index_;
4991
4992 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4993};
4994
Vladimir Markofcb503c2016-05-18 12:48:17 +01004995class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004996 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4998 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004999
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005000 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005001 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005002 return true;
5003 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005004
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005005 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005006
5007 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005008 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005009 }
5010 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005011 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005012 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005013 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5014 LOG(FATAL) << DebugName() << " is not defined for float values";
5015 UNREACHABLE();
5016 }
5017 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5018 LOG(FATAL) << DebugName() << " is not defined for double values";
5019 UNREACHABLE();
5020 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005021
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005022 DECLARE_INSTRUCTION(Not);
5023
5024 private:
5025 DISALLOW_COPY_AND_ASSIGN(HNot);
5026};
5027
Vladimir Markofcb503c2016-05-18 12:48:17 +01005028class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005029 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5031 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005032
5033 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005034 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005035 return true;
5036 }
5037
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005038 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005039 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005040 return !x;
5041 }
5042
Roland Levillain9867bc72015-08-05 10:21:34 +01005043 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005044 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005045 }
5046 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5047 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005048 UNREACHABLE();
5049 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005050 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5051 LOG(FATAL) << DebugName() << " is not defined for float values";
5052 UNREACHABLE();
5053 }
5054 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5055 LOG(FATAL) << DebugName() << " is not defined for double values";
5056 UNREACHABLE();
5057 }
David Brazdil66d126e2015-04-03 16:02:44 +01005058
5059 DECLARE_INSTRUCTION(BooleanNot);
5060
5061 private:
5062 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5063};
5064
Vladimir Markofcb503c2016-05-18 12:48:17 +01005065class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005066 public:
5067 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005068 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005069 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005070 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005071 // Invariant: We should never generate a conversion to a Boolean value.
5072 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005073 }
5074
5075 HInstruction* GetInput() const { return InputAt(0); }
5076 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5077 Primitive::Type GetResultType() const { return GetType(); }
5078
5079 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005080 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5081 return true;
5082 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005083
Mark Mendelle82549b2015-05-06 10:55:34 -04005084 // Try to statically evaluate the conversion and return a HConstant
5085 // containing the result. If the input cannot be converted, return nullptr.
5086 HConstant* TryStaticEvaluation() const;
5087
Roland Levillaindff1f282014-11-05 14:15:05 +00005088 DECLARE_INSTRUCTION(TypeConversion);
5089
5090 private:
5091 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5092};
5093
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005094static constexpr uint32_t kNoRegNumber = -1;
5095
Vladimir Markofcb503c2016-05-18 12:48:17 +01005096class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005098 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5099 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005100 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005101 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102 SetRawInputAt(0, value);
5103 }
5104
Calin Juravle10e244f2015-01-26 18:54:32 +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
Calin Juravle10e244f2015-01-26 18:54:32 +00005110 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111
Calin Juravle10e244f2015-01-26 18:54:32 +00005112 bool CanThrow() const OVERRIDE { return true; }
5113
5114 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005115
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005116
5117 DECLARE_INSTRUCTION(NullCheck);
5118
5119 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005120 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5121};
5122
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005123// Embeds an ArtField and all the information required by the compiler. We cache
5124// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125class FieldInfo : public ValueObject {
5126 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005127 FieldInfo(ArtField* field,
5128 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005129 Primitive::Type field_type,
5130 bool is_volatile,
5131 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005132 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005133 const DexFile& dex_file)
5134 : field_(field),
5135 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005136 field_type_(field_type),
5137 is_volatile_(is_volatile),
5138 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005139 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005140 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005141
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005142 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005143 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005144 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005145 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005146 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005147 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005148 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005149
5150 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005151 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005152 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005153 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005154 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005155 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005156 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005157 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158};
5159
Vladimir Markofcb503c2016-05-18 12:48:17 +01005160class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 public:
5162 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005163 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005165 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005166 bool is_volatile,
5167 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005168 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005169 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005170 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005171 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005172 field_info_(field,
5173 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005174 field_type,
5175 is_volatile,
5176 field_idx,
5177 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005178 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179 SetRawInputAt(0, value);
5180 }
5181
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005182 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005183
Vladimir Marko372f10e2016-05-17 16:30:10 +01005184 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5185 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005186 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005187 }
5188
Calin Juravle641547a2015-04-21 22:08:51 +01005189 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005190 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005191 }
5192
5193 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005194 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5195 }
5196
Calin Juravle52c48962014-12-16 17:02:57 +00005197 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005198 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005199 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005200 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201
5202 DECLARE_INSTRUCTION(InstanceFieldGet);
5203
5204 private:
5205 const FieldInfo field_info_;
5206
5207 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5208};
5209
Vladimir Markofcb503c2016-05-18 12:48:17 +01005210class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 public:
5212 HInstanceFieldSet(HInstruction* object,
5213 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005214 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005215 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005216 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005217 bool is_volatile,
5218 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005219 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005220 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005221 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005222 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005223 field_info_(field,
5224 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005225 field_type,
5226 is_volatile,
5227 field_idx,
5228 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005229 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005231 SetRawInputAt(0, object);
5232 SetRawInputAt(1, value);
5233 }
5234
Calin Juravle641547a2015-04-21 22:08:51 +01005235 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005236 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 }
5238
Calin Juravle52c48962014-12-16 17:02:57 +00005239 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005240 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005241 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005242 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005244 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5245 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005246
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005247 DECLARE_INSTRUCTION(InstanceFieldSet);
5248
5249 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005250 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5251 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5252 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5253 "Too many packed fields.");
5254
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005255 const FieldInfo field_info_;
5256
5257 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5258};
5259
Vladimir Markofcb503c2016-05-18 12:48:17 +01005260class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005262 HArrayGet(HInstruction* array,
5263 HInstruction* index,
5264 Primitive::Type type,
5265 uint32_t dex_pc,
5266 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005267 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005268 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 SetRawInputAt(0, array);
5270 SetRawInputAt(1, index);
5271 }
5272
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005273 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005274 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005275 return true;
5276 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005277 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005278 // TODO: We can be smarter here.
5279 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5280 // which generates the implicit null check. There are cases when these can be removed
5281 // to produce better code. If we ever add optimizations to do so we should allow an
5282 // implicit check here (as long as the address falls in the first page).
5283 return false;
5284 }
5285
David Brazdil4833f5a2015-12-16 10:37:39 +00005286 bool IsEquivalentOf(HArrayGet* other) const {
5287 bool result = (GetDexPc() == other->GetDexPc());
5288 if (kIsDebugBuild && result) {
5289 DCHECK_EQ(GetBlock(), other->GetBlock());
5290 DCHECK_EQ(GetArray(), other->GetArray());
5291 DCHECK_EQ(GetIndex(), other->GetIndex());
5292 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005293 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005294 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005295 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5296 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005297 }
5298 }
5299 return result;
5300 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005301
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005302 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5303
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005304 HInstruction* GetArray() const { return InputAt(0); }
5305 HInstruction* GetIndex() const { return InputAt(1); }
5306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 DECLARE_INSTRUCTION(ArrayGet);
5308
5309 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005310 // We treat a String as an array, creating the HArrayGet from String.charAt()
5311 // intrinsic in the instruction simplifier. We can always determine whether
5312 // a particular HArrayGet is actually a String.charAt() by looking at the type
5313 // of the input but that requires holding the mutator lock, so we prefer to use
5314 // a flag, so that code generators don't need to do the locking.
5315 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5316 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5317 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5318 "Too many packed fields.");
5319
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5321};
5322
Vladimir Markofcb503c2016-05-18 12:48:17 +01005323class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005324 public:
5325 HArraySet(HInstruction* array,
5326 HInstruction* index,
5327 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005328 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005329 uint32_t dex_pc)
5330 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5332 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5333 SetPackedFlag<kFlagValueCanBeNull>(true);
5334 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 SetRawInputAt(0, array);
5336 SetRawInputAt(1, index);
5337 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005338 // Make a best guess now, may be refined during SSA building.
5339 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 }
5341
Calin Juravle77520bc2015-01-12 18:45:46 +00005342 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005343 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005344 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 }
5346
Mingyao Yang81014cb2015-06-02 03:16:27 -07005347 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005348 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005350 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005351 // TODO: Same as for ArrayGet.
5352 return false;
5353 }
5354
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005355 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005356 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005357 }
5358
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005359 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005360 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005361 }
5362
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005363 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005364 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005365 }
5366
Vladimir Markoa1de9182016-02-25 11:37:38 +00005367 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5368 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5369 bool StaticTypeOfArrayIsObjectArray() const {
5370 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5371 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005372
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005373 HInstruction* GetArray() const { return InputAt(0); }
5374 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005375 HInstruction* GetValue() const { return InputAt(2); }
5376
5377 Primitive::Type GetComponentType() const {
5378 // The Dex format does not type floating point index operations. Since the
5379 // `expected_component_type_` is set during building and can therefore not
5380 // be correct, we also check what is the value type. If it is a floating
5381 // point type, we must use that type.
5382 Primitive::Type value_type = GetValue()->GetType();
5383 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5384 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005385 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005386 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005387
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005388 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005389 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005390 }
5391
Aart Bik18b36ab2016-04-13 16:41:35 -07005392 void ComputeSideEffects() {
5393 Primitive::Type type = GetComponentType();
5394 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5395 SideEffectsForArchRuntimeCalls(type)));
5396 }
5397
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005398 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5399 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5400 }
5401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 DECLARE_INSTRUCTION(ArraySet);
5403
5404 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005405 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5406 static constexpr size_t kFieldExpectedComponentTypeSize =
5407 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5408 static constexpr size_t kFlagNeedsTypeCheck =
5409 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5410 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 // Cached information for the reference_type_info_ so that codegen
5412 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005413 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5414 static constexpr size_t kNumberOfArraySetPackedBits =
5415 kFlagStaticTypeOfArrayIsObjectArray + 1;
5416 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5417 using ExpectedComponentTypeField =
5418 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419
5420 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5421};
5422
Vladimir Markofcb503c2016-05-18 12:48:17 +01005423class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005425 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005426 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005427 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005428 // Note that arrays do not change length, so the instruction does not
5429 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 SetRawInputAt(0, array);
5431 }
5432
Calin Juravle77520bc2015-01-12 18:45:46 +00005433 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005434 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005435 return true;
5436 }
Calin Juravle641547a2015-04-21 22:08:51 +01005437 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5438 return obj == InputAt(0);
5439 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005440
Vladimir Markodce016e2016-04-28 13:10:02 +01005441 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 DECLARE_INSTRUCTION(ArrayLength);
5444
5445 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005446 // We treat a String as an array, creating the HArrayLength from String.length()
5447 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5448 // determine whether a particular HArrayLength is actually a String.length() by
5449 // looking at the type of the input but that requires holding the mutator lock, so
5450 // we prefer to use a flag, so that code generators don't need to do the locking.
5451 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5452 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5453 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5454 "Too many packed fields.");
5455
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5457};
5458
Vladimir Markofcb503c2016-05-18 12:48:17 +01005459class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005461 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5462 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005463 HBoundsCheck(HInstruction* index,
5464 HInstruction* length,
5465 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005466 bool string_char_at = false)
5467 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005468 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005469 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470 SetRawInputAt(0, index);
5471 SetRawInputAt(1, length);
5472 }
5473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005474 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005475 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005476 return true;
5477 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005479 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005480
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005481 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005482
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005483 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005484
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005485 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005486
5487 DECLARE_INSTRUCTION(BoundsCheck);
5488
5489 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005490 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005491
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005492 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5493};
5494
Vladimir Markofcb503c2016-05-18 12:48:17 +01005495class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005496 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005497 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005498 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005499
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005500 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005501 return true;
5502 }
5503
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005504 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5505 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005506
5507 DECLARE_INSTRUCTION(SuspendCheck);
5508
5509 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005510 // Only used for code generation, in order to share the same slow path between back edges
5511 // of a same loop.
5512 SlowPathCode* slow_path_;
5513
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005514 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5515};
5516
David Srbecky0cf44932015-12-09 14:09:59 +00005517// Pseudo-instruction which provides the native debugger with mapping information.
5518// It ensures that we can generate line number and local variables at this point.
5519class HNativeDebugInfo : public HTemplateInstruction<0> {
5520 public:
5521 explicit HNativeDebugInfo(uint32_t dex_pc)
5522 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5523
5524 bool NeedsEnvironment() const OVERRIDE {
5525 return true;
5526 }
5527
5528 DECLARE_INSTRUCTION(NativeDebugInfo);
5529
5530 private:
5531 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5532};
5533
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005534/**
5535 * Instruction to load a Class object.
5536 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005537class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005538 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005539 // Determines how to load the Class.
5540 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005541 // We cannot load this class. See HSharpening::SharpenLoadClass.
5542 kInvalid = -1,
5543
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005544 // Use the Class* from the method's own ArtMethod*.
5545 kReferrersClass,
5546
5547 // Use boot image Class* address that will be known at link time.
5548 // Used for boot image classes referenced by boot image code in non-PIC mode.
5549 kBootImageLinkTimeAddress,
5550
5551 // Use PC-relative boot image Class* address that will be known at link time.
5552 // Used for boot image classes referenced by boot image code in PIC mode.
5553 kBootImageLinkTimePcRelative,
5554
5555 // Use a known boot image Class* address, embedded in the code by the codegen.
5556 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557 kBootImageAddress,
5558
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005559 // Load from an entry in the .bss section using a PC-relative load.
5560 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5561 kBssEntry,
5562
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005563 // Load from the root table associated with the JIT compiled method.
5564 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005566 // Load from resolved types array accessed through the class loaded from
5567 // the compiled method's own ArtMethod*. This is the default access type when
5568 // all other types are unavailable.
5569 kDexCacheViaMethod,
5570
5571 kLast = kDexCacheViaMethod
5572 };
5573
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005574 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005575 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005576 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005577 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005578 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005579 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005580 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005581 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5582 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005583 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005584 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005585 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005586 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005587 // Referrers class should not need access check. We never inline unverified
5588 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005589 DCHECK(!is_referrers_class || !needs_access_check);
5590
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005591 SetPackedField<LoadKindField>(
5592 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005593 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005594 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005595 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005596 }
5597
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005598 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005599
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005600 LoadKind GetLoadKind() const {
5601 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005602 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005603
5604 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005605
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005606 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607
Andreas Gampea5b09a62016-11-17 15:21:22 -08005608 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005610 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005612 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005613 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005614 }
5615
Calin Juravle0ba218d2015-05-19 18:46:01 +01005616 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005617 // The entrypoint the code generator is going to call does not do
5618 // clinit of the class.
5619 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005620 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005621 }
5622
5623 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005624 return NeedsAccessCheck() ||
5625 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005626 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5627 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005628 }
5629
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005630 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005631 return NeedsAccessCheck() ||
5632 MustGenerateClinitCheck() ||
5633 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5634 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5635 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005636 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5637 GetLoadKind() == LoadKind::kBssEntry) &&
5638 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005639 }
5640
Calin Juravleacf735c2015-02-12 15:25:22 +00005641 ReferenceTypeInfo GetLoadedClassRTI() {
5642 return loaded_class_rti_;
5643 }
5644
5645 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5646 // Make sure we only set exact types (the loaded class should never be merged).
5647 DCHECK(rti.IsExact());
5648 loaded_class_rti_ = rti;
5649 }
5650
Andreas Gampea5b09a62016-11-17 15:21:22 -08005651 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005652 const DexFile& GetDexFile() const { return dex_file_; }
5653
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005654 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005655 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005656 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005657
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005658 static SideEffects SideEffectsForArchRuntimeCalls() {
5659 return SideEffects::CanTriggerGC();
5660 }
5661
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005663 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005664 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005665 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005666
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005667 void MarkInBootImage() {
5668 SetPackedFlag<kFlagIsInBootImage>(true);
5669 }
5670
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005671 void AddSpecialInput(HInstruction* special_input);
5672
5673 using HInstruction::GetInputRecords; // Keep the const version visible.
5674 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5675 return ArrayRef<HUserRecord<HInstruction*>>(
5676 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5677 }
5678
5679 Primitive::Type GetType() const OVERRIDE {
5680 return Primitive::kPrimNot;
5681 }
5682
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005683 Handle<mirror::Class> GetClass() const {
5684 return klass_;
5685 }
5686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687 DECLARE_INSTRUCTION(LoadClass);
5688
5689 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005691 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005692 // Whether this instruction must generate the initialization check.
5693 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005694 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005695 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5696 static constexpr size_t kFieldLoadKindSize =
5697 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5698 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005699 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5701
5702 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005703 return load_kind == LoadKind::kReferrersClass ||
5704 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005706 load_kind == LoadKind::kBssEntry ||
5707 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708 }
5709
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005710 void SetLoadKindInternal(LoadKind load_kind);
5711
5712 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5713 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005714 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005716
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005717 // A type index and dex file where the class can be accessed. The dex file can be:
5718 // - The compiling method's dex file if the class is defined there too.
5719 // - The compiling method's dex file if the class is referenced there.
5720 // - The dex file where the class is defined. When the load kind can only be
5721 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005722 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005723 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005724
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005725 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005726
Calin Juravleacf735c2015-02-12 15:25:22 +00005727 ReferenceTypeInfo loaded_class_rti_;
5728
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005729 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5730};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005731std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5732
5733// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005734inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005735 // The special input is used for PC-relative loads on some architectures,
5736 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005737 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005738 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005739 GetLoadKind() == LoadKind::kBootImageAddress ||
5740 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005741 DCHECK(special_input_.GetInstruction() == nullptr);
5742 special_input_ = HUserRecord<HInstruction*>(special_input);
5743 special_input->AddUseAt(this, 0);
5744}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005745
Vladimir Marko372f10e2016-05-17 16:30:10 +01005746class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005747 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005748 // Determines how to load the String.
5749 enum class LoadKind {
5750 // Use boot image String* address that will be known at link time.
5751 // Used for boot image strings referenced by boot image code in non-PIC mode.
5752 kBootImageLinkTimeAddress,
5753
5754 // Use PC-relative boot image String* address that will be known at link time.
5755 // Used for boot image strings referenced by boot image code in PIC mode.
5756 kBootImageLinkTimePcRelative,
5757
5758 // Use a known boot image String* address, embedded in the code by the codegen.
5759 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005760 kBootImageAddress,
5761
Vladimir Markoaad75c62016-10-03 08:46:48 +00005762 // Load from an entry in the .bss section using a PC-relative load.
5763 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5764 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005765
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005766 // Load from the root table associated with the JIT compiled method.
5767 kJitTableAddress,
5768
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005769 // Load from resolved strings array accessed through the class loaded from
5770 // the compiled method's own ArtMethod*. This is the default access type when
5771 // all other types are unavailable.
5772 kDexCacheViaMethod,
5773
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005774 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775 };
5776
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005777 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005778 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 const DexFile& dex_file,
5780 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005781 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5782 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005783 string_index_(string_index),
5784 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005786 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005787
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005788 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005789
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 LoadKind GetLoadKind() const {
5791 return GetPackedField<LoadKindField>();
5792 }
5793
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005794 const DexFile& GetDexFile() const {
5795 return dex_file_;
5796 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005798 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005799 return string_index_;
5800 }
5801
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005802 Handle<mirror::String> GetString() const {
5803 return string_;
5804 }
5805
5806 void SetString(Handle<mirror::String> str) {
5807 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808 }
5809
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005810 bool CanBeMoved() const OVERRIDE { return true; }
5811
Vladimir Marko372f10e2016-05-17 16:30:10 +01005812 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005813
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005814 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005815
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816 // Will call the runtime if we need to load the string through
5817 // the dex cache and the string is not guaranteed to be there yet.
5818 bool NeedsEnvironment() const OVERRIDE {
5819 LoadKind load_kind = GetLoadKind();
5820 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5821 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005822 load_kind == LoadKind::kBootImageAddress ||
5823 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005824 return false;
5825 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005826 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005827 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005828
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5830 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5831 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005832
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005833 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005835
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005836 static SideEffects SideEffectsForArchRuntimeCalls() {
5837 return SideEffects::CanTriggerGC();
5838 }
5839
Vladimir Marko372f10e2016-05-17 16:30:10 +01005840 void AddSpecialInput(HInstruction* special_input);
5841
5842 using HInstruction::GetInputRecords; // Keep the const version visible.
5843 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5844 return ArrayRef<HUserRecord<HInstruction*>>(
5845 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 }
5847
Vladimir Marko372f10e2016-05-17 16:30:10 +01005848 Primitive::Type GetType() const OVERRIDE {
5849 return Primitive::kPrimNot;
5850 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005851
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005852 DECLARE_INSTRUCTION(LoadString);
5853
5854 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005855 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 static constexpr size_t kFieldLoadKindSize =
5857 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5858 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005861
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862 void SetLoadKindInternal(LoadKind load_kind);
5863
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005864 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5865 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005866 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005867 HUserRecord<HInstruction*> special_input_;
5868
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005869 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005870 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005871
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005872 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005873
5874 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5875};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5877
5878// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005880 // The special input is used for PC-relative loads on some architectures,
5881 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005883 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005884 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5885 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 // HLoadString::GetInputRecords() returns an empty array at this point,
5887 // so use the GetInputRecords() from the base class to set the input record.
5888 DCHECK(special_input_.GetInstruction() == nullptr);
5889 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 special_input->AddUseAt(this, 0);
5891}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005893/**
5894 * Performs an initialization check on its Class object input.
5895 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005896class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005898 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005899 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005900 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005901 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5902 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 SetRawInputAt(0, constant);
5904 }
5905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005906 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005907 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908 return true;
5909 }
5910
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005911 bool NeedsEnvironment() const OVERRIDE {
5912 // May call runtime to initialize the class.
5913 return true;
5914 }
5915
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005916 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005918 HLoadClass* GetLoadClass() const {
5919 DCHECK(InputAt(0)->IsLoadClass());
5920 return InputAt(0)->AsLoadClass();
5921 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005922
5923 DECLARE_INSTRUCTION(ClinitCheck);
5924
5925 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5927};
5928
Vladimir Markofcb503c2016-05-18 12:48:17 +01005929class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005930 public:
5931 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005932 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005933 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005934 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005935 bool is_volatile,
5936 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005937 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005938 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005939 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005940 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005941 field_info_(field,
5942 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005943 field_type,
5944 is_volatile,
5945 field_idx,
5946 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005947 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 SetRawInputAt(0, cls);
5949 }
5950
Calin Juravle52c48962014-12-16 17:02:57 +00005951
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005952 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005953
Vladimir Marko372f10e2016-05-17 16:30:10 +01005954 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5955 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005956 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005957 }
5958
5959 size_t ComputeHashCode() const OVERRIDE {
5960 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5961 }
5962
Calin Juravle52c48962014-12-16 17:02:57 +00005963 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5965 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005966 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005967
5968 DECLARE_INSTRUCTION(StaticFieldGet);
5969
5970 private:
5971 const FieldInfo field_info_;
5972
5973 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5974};
5975
Vladimir Markofcb503c2016-05-18 12:48:17 +01005976class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005977 public:
5978 HStaticFieldSet(HInstruction* cls,
5979 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005980 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005981 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005982 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005983 bool is_volatile,
5984 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005985 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005986 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005987 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005988 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005989 field_info_(field,
5990 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005991 field_type,
5992 is_volatile,
5993 field_idx,
5994 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005995 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005996 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997 SetRawInputAt(0, cls);
5998 SetRawInputAt(1, value);
5999 }
6000
Calin Juravle52c48962014-12-16 17:02:57 +00006001 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006002 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6003 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006004 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006006 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006007 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6008 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006009
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006010 DECLARE_INSTRUCTION(StaticFieldSet);
6011
6012 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006013 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6014 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6015 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6016 "Too many packed fields.");
6017
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018 const FieldInfo field_info_;
6019
6020 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6021};
6022
Vladimir Markofcb503c2016-05-18 12:48:17 +01006023class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006024 public:
6025 HUnresolvedInstanceFieldGet(HInstruction* obj,
6026 Primitive::Type field_type,
6027 uint32_t field_index,
6028 uint32_t dex_pc)
6029 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6030 field_index_(field_index) {
6031 SetRawInputAt(0, obj);
6032 }
6033
6034 bool NeedsEnvironment() const OVERRIDE { return true; }
6035 bool CanThrow() const OVERRIDE { return true; }
6036
6037 Primitive::Type GetFieldType() const { return GetType(); }
6038 uint32_t GetFieldIndex() const { return field_index_; }
6039
6040 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6041
6042 private:
6043 const uint32_t field_index_;
6044
6045 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6046};
6047
Vladimir Markofcb503c2016-05-18 12:48:17 +01006048class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006049 public:
6050 HUnresolvedInstanceFieldSet(HInstruction* obj,
6051 HInstruction* value,
6052 Primitive::Type field_type,
6053 uint32_t field_index,
6054 uint32_t dex_pc)
6055 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006056 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006058 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006059 SetRawInputAt(0, obj);
6060 SetRawInputAt(1, value);
6061 }
6062
6063 bool NeedsEnvironment() const OVERRIDE { return true; }
6064 bool CanThrow() const OVERRIDE { return true; }
6065
Vladimir Markoa1de9182016-02-25 11:37:38 +00006066 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006067 uint32_t GetFieldIndex() const { return field_index_; }
6068
6069 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6070
6071 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006072 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6073 static constexpr size_t kFieldFieldTypeSize =
6074 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6075 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6076 kFieldFieldType + kFieldFieldTypeSize;
6077 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6078 "Too many packed fields.");
6079 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6080
Calin Juravlee460d1d2015-09-29 04:52:17 +01006081 const uint32_t field_index_;
6082
6083 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6084};
6085
Vladimir Markofcb503c2016-05-18 12:48:17 +01006086class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006087 public:
6088 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6089 uint32_t field_index,
6090 uint32_t dex_pc)
6091 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6092 field_index_(field_index) {
6093 }
6094
6095 bool NeedsEnvironment() const OVERRIDE { return true; }
6096 bool CanThrow() const OVERRIDE { return true; }
6097
6098 Primitive::Type GetFieldType() const { return GetType(); }
6099 uint32_t GetFieldIndex() const { return field_index_; }
6100
6101 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6102
6103 private:
6104 const uint32_t field_index_;
6105
6106 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6107};
6108
Vladimir Markofcb503c2016-05-18 12:48:17 +01006109class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006110 public:
6111 HUnresolvedStaticFieldSet(HInstruction* value,
6112 Primitive::Type field_type,
6113 uint32_t field_index,
6114 uint32_t dex_pc)
6115 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006116 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006117 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006118 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006119 SetRawInputAt(0, value);
6120 }
6121
6122 bool NeedsEnvironment() const OVERRIDE { return true; }
6123 bool CanThrow() const OVERRIDE { return true; }
6124
Vladimir Markoa1de9182016-02-25 11:37:38 +00006125 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006126 uint32_t GetFieldIndex() const { return field_index_; }
6127
6128 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6129
6130 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006131 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6132 static constexpr size_t kFieldFieldTypeSize =
6133 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6134 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6135 kFieldFieldType + kFieldFieldTypeSize;
6136 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6137 "Too many packed fields.");
6138 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6139
Calin Juravlee460d1d2015-09-29 04:52:17 +01006140 const uint32_t field_index_;
6141
6142 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6143};
6144
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006145// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006146class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006148 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6149 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006150
David Brazdilbbd733e2015-08-18 17:48:17 +01006151 bool CanBeNull() const OVERRIDE { return false; }
6152
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006153 DECLARE_INSTRUCTION(LoadException);
6154
6155 private:
6156 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6157};
6158
David Brazdilcb1c0552015-08-04 16:22:25 +01006159// Implicit part of move-exception which clears thread-local exception storage.
6160// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006161class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006162 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006163 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6164 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006165
6166 DECLARE_INSTRUCTION(ClearException);
6167
6168 private:
6169 DISALLOW_COPY_AND_ASSIGN(HClearException);
6170};
6171
Vladimir Markofcb503c2016-05-18 12:48:17 +01006172class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006173 public:
6174 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006175 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006176 SetRawInputAt(0, exception);
6177 }
6178
6179 bool IsControlFlow() const OVERRIDE { return true; }
6180
6181 bool NeedsEnvironment() const OVERRIDE { return true; }
6182
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006183 bool CanThrow() const OVERRIDE { return true; }
6184
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006185
6186 DECLARE_INSTRUCTION(Throw);
6187
6188 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006189 DISALLOW_COPY_AND_ASSIGN(HThrow);
6190};
6191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192/**
6193 * Implementation strategies for the code generator of a HInstanceOf
6194 * or `HCheckCast`.
6195 */
6196enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006197 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 kExactCheck, // Can do a single class compare.
6199 kClassHierarchyCheck, // Can just walk the super class chain.
6200 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6201 kInterfaceCheck, // No optimization yet when checking against an interface.
6202 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006203 kArrayCheck, // No optimization yet when checking against a generic array.
6204 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205};
6206
Roland Levillain86503782016-02-11 19:07:30 +00006207std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6208
Vladimir Markofcb503c2016-05-18 12:48:17 +01006209class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006211 HInstanceOf(HInstruction* object,
6212 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006214 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006215 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006217 dex_pc) {
6218 SetPackedField<TypeCheckKindField>(check_kind);
6219 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006220 SetRawInputAt(0, object);
6221 SetRawInputAt(1, constant);
6222 }
6223
6224 bool CanBeMoved() const OVERRIDE { return true; }
6225
Vladimir Marko372f10e2016-05-17 16:30:10 +01006226 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006227 return true;
6228 }
6229
6230 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006232 }
6233
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006234 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006235 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6236 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6237 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6238 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006239
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006240 static bool CanCallRuntime(TypeCheckKind check_kind) {
6241 // Mips currently does runtime calls for any other checks.
6242 return check_kind != TypeCheckKind::kExactCheck;
6243 }
6244
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006246 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006247 }
6248
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006249 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006250
6251 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006252 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6253 static constexpr size_t kFieldTypeCheckKindSize =
6254 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6255 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6256 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6257 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6258 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006259
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006260 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6261};
6262
Vladimir Markofcb503c2016-05-18 12:48:17 +01006263class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006264 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006265 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006266 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006267 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6268 SetPackedFlag<kFlagUpperCanBeNull>(true);
6269 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006270 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006271 SetRawInputAt(0, input);
6272 }
6273
David Brazdilf5552582015-12-27 13:36:12 +00006274 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006275 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006277 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006278
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006279 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 DCHECK(GetUpperCanBeNull() || !can_be_null);
6281 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006282 }
6283
Vladimir Markoa1de9182016-02-25 11:37:38 +00006284 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006285
Calin Juravleb1498f62015-02-16 13:13:29 +00006286 DECLARE_INSTRUCTION(BoundType);
6287
6288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006289 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6290 // is false then CanBeNull() cannot be true).
6291 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6292 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6293 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6294 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6295
Calin Juravleb1498f62015-02-16 13:13:29 +00006296 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006297 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6298 // It is used to bound the type in cases like:
6299 // if (x instanceof ClassX) {
6300 // // uper_bound_ will be ClassX
6301 // }
David Brazdilf5552582015-12-27 13:36:12 +00006302 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006303
6304 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6305};
6306
Vladimir Markofcb503c2016-05-18 12:48:17 +01006307class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308 public:
6309 HCheckCast(HInstruction* object,
6310 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006311 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006313 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6314 SetPackedField<TypeCheckKindField>(check_kind);
6315 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006316 SetRawInputAt(0, object);
6317 SetRawInputAt(1, constant);
6318 }
6319
6320 bool CanBeMoved() const OVERRIDE { return true; }
6321
Vladimir Marko372f10e2016-05-17 16:30:10 +01006322 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006323 return true;
6324 }
6325
6326 bool NeedsEnvironment() const OVERRIDE {
6327 // Instruction may throw a CheckCastError.
6328 return true;
6329 }
6330
6331 bool CanThrow() const OVERRIDE { return true; }
6332
Vladimir Markoa1de9182016-02-25 11:37:38 +00006333 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6334 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6335 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6336 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006337
6338 DECLARE_INSTRUCTION(CheckCast);
6339
6340 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006341 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6342 static constexpr size_t kFieldTypeCheckKindSize =
6343 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6344 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6345 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6346 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6347 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006348
6349 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006350};
6351
Andreas Gampe26de38b2016-07-27 17:53:11 -07006352/**
6353 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6354 * @details We define the combined barrier types that are actually required
6355 * by the Java Memory Model, rather than using exactly the terminology from
6356 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6357 * primitives. Note that the JSR-133 cookbook generally does not deal with
6358 * store atomicity issues, and the recipes there are not always entirely sufficient.
6359 * The current recipe is as follows:
6360 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6361 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6362 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6363 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6364 * class has final fields.
6365 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6366 * store-to-memory instructions. Only generated together with non-temporal stores.
6367 */
6368enum MemBarrierKind {
6369 kAnyStore,
6370 kLoadAny,
6371 kStoreStore,
6372 kAnyAny,
6373 kNTStoreStore,
6374 kLastBarrierKind = kNTStoreStore
6375};
6376std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6377
Vladimir Markofcb503c2016-05-18 12:48:17 +01006378class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006379 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006380 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006381 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6383 SetPackedField<BarrierKindField>(barrier_kind);
6384 }
Calin Juravle27df7582015-04-17 19:12:31 +01006385
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006387
6388 DECLARE_INSTRUCTION(MemoryBarrier);
6389
6390 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006391 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6392 static constexpr size_t kFieldBarrierKindSize =
6393 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6394 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6395 kFieldBarrierKind + kFieldBarrierKindSize;
6396 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6397 "Too many packed fields.");
6398 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006399
6400 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6401};
6402
Vladimir Markofcb503c2016-05-18 12:48:17 +01006403class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006404 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006406 kEnter,
6407 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006408 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006409 };
6410
6411 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006412 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006413 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6414 dex_pc) {
6415 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006416 SetRawInputAt(0, object);
6417 }
6418
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006419 // Instruction may go into runtime, so we need an environment.
6420 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006421
6422 bool CanThrow() const OVERRIDE {
6423 // Verifier guarantees that monitor-exit cannot throw.
6424 // This is important because it allows the HGraphBuilder to remove
6425 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6426 return IsEnter();
6427 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006428
Vladimir Markoa1de9182016-02-25 11:37:38 +00006429 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6430 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006431
6432 DECLARE_INSTRUCTION(MonitorOperation);
6433
Calin Juravle52c48962014-12-16 17:02:57 +00006434 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006435 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6436 static constexpr size_t kFieldOperationKindSize =
6437 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6438 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6439 kFieldOperationKind + kFieldOperationKindSize;
6440 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6441 "Too many packed fields.");
6442 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006443
6444 private:
6445 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6446};
6447
Vladimir Markofcb503c2016-05-18 12:48:17 +01006448class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006449 public:
6450 HSelect(HInstruction* condition,
6451 HInstruction* true_value,
6452 HInstruction* false_value,
6453 uint32_t dex_pc)
6454 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6455 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6456
6457 // First input must be `true_value` or `false_value` to allow codegens to
6458 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6459 // that architectures which implement HSelect as a conditional move also
6460 // will not need to invert the condition.
6461 SetRawInputAt(0, false_value);
6462 SetRawInputAt(1, true_value);
6463 SetRawInputAt(2, condition);
6464 }
6465
6466 HInstruction* GetFalseValue() const { return InputAt(0); }
6467 HInstruction* GetTrueValue() const { return InputAt(1); }
6468 HInstruction* GetCondition() const { return InputAt(2); }
6469
6470 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006471 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6472 return true;
6473 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006474
6475 bool CanBeNull() const OVERRIDE {
6476 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6477 }
6478
6479 DECLARE_INSTRUCTION(Select);
6480
6481 private:
6482 DISALLOW_COPY_AND_ASSIGN(HSelect);
6483};
6484
Vladimir Markof9f64412015-09-02 14:05:49 +01006485class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006486 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006487 MoveOperands(Location source,
6488 Location destination,
6489 Primitive::Type type,
6490 HInstruction* instruction)
6491 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006492
6493 Location GetSource() const { return source_; }
6494 Location GetDestination() const { return destination_; }
6495
6496 void SetSource(Location value) { source_ = value; }
6497 void SetDestination(Location value) { destination_ = value; }
6498
6499 // The parallel move resolver marks moves as "in-progress" by clearing the
6500 // destination (but not the source).
6501 Location MarkPending() {
6502 DCHECK(!IsPending());
6503 Location dest = destination_;
6504 destination_ = Location::NoLocation();
6505 return dest;
6506 }
6507
6508 void ClearPending(Location dest) {
6509 DCHECK(IsPending());
6510 destination_ = dest;
6511 }
6512
6513 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006514 DCHECK(source_.IsValid() || destination_.IsInvalid());
6515 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006516 }
6517
6518 // True if this blocks a move from the given location.
6519 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006520 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006521 }
6522
6523 // A move is redundant if it's been eliminated, if its source and
6524 // destination are the same, or if its destination is unneeded.
6525 bool IsRedundant() const {
6526 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6527 }
6528
6529 // We clear both operands to indicate move that's been eliminated.
6530 void Eliminate() {
6531 source_ = destination_ = Location::NoLocation();
6532 }
6533
6534 bool IsEliminated() const {
6535 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6536 return source_.IsInvalid();
6537 }
6538
Alexey Frunze4dda3372015-06-01 18:31:49 -07006539 Primitive::Type GetType() const { return type_; }
6540
Nicolas Geoffray90218252015-04-15 11:56:51 +01006541 bool Is64BitMove() const {
6542 return Primitive::Is64BitType(type_);
6543 }
6544
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006545 HInstruction* GetInstruction() const { return instruction_; }
6546
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006547 private:
6548 Location source_;
6549 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006550 // The type this move is for.
6551 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006552 // The instruction this move is assocatied with. Null when this move is
6553 // for moving an input in the expected locations of user (including a phi user).
6554 // This is only used in debug mode, to ensure we do not connect interval siblings
6555 // in the same parallel move.
6556 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006557};
6558
Roland Levillainc9285912015-12-18 10:38:42 +00006559std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6560
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561static constexpr size_t kDefaultNumberOfMoves = 4;
6562
Vladimir Markofcb503c2016-05-18 12:48:17 +01006563class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006565 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006566 : HTemplateInstruction(SideEffects::None(), dex_pc),
6567 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6568 moves_.reserve(kDefaultNumberOfMoves);
6569 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570
Nicolas Geoffray90218252015-04-15 11:56:51 +01006571 void AddMove(Location source,
6572 Location destination,
6573 Primitive::Type type,
6574 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006575 DCHECK(source.IsValid());
6576 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006577 if (kIsDebugBuild) {
6578 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006579 for (const MoveOperands& move : moves_) {
6580 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006581 // Special case the situation where the move is for the spill slot
6582 // of the instruction.
6583 if ((GetPrevious() == instruction)
6584 || ((GetPrevious() == nullptr)
6585 && instruction->IsPhi()
6586 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006587 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006588 << "Doing parallel moves for the same instruction.";
6589 } else {
6590 DCHECK(false) << "Doing parallel moves for the same instruction.";
6591 }
6592 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006593 }
6594 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006595 for (const MoveOperands& move : moves_) {
6596 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006597 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006598 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006599 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006600 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006601 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006602 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006603 }
6604
Vladimir Marko225b6462015-09-28 12:17:40 +01006605 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006607 }
6608
Vladimir Marko225b6462015-09-28 12:17:40 +01006609 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006610
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006611 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006612
6613 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006614 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006615
6616 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6617};
6618
Mark Mendell0616ae02015-04-17 12:49:27 -04006619} // namespace art
6620
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006621#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6622#include "nodes_shared.h"
6623#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006624#ifdef ART_ENABLE_CODEGEN_arm
6625#include "nodes_arm.h"
6626#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006627#ifdef ART_ENABLE_CODEGEN_mips
6628#include "nodes_mips.h"
6629#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006630#ifdef ART_ENABLE_CODEGEN_x86
6631#include "nodes_x86.h"
6632#endif
6633
6634namespace art {
6635
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006636class HGraphVisitor : public ValueObject {
6637 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006638 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6639 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006640
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006641 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006642 virtual void VisitBasicBlock(HBasicBlock* block);
6643
Roland Levillain633021e2014-10-01 14:12:25 +01006644 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006645 void VisitInsertionOrder();
6646
Roland Levillain633021e2014-10-01 14:12:25 +01006647 // Visit the graph following dominator tree reverse post-order.
6648 void VisitReversePostOrder();
6649
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006650 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006651
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006653#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006654 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6655
6656 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6657
6658#undef DECLARE_VISIT_INSTRUCTION
6659
6660 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006661 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006662
6663 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6664};
6665
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006666class HGraphDelegateVisitor : public HGraphVisitor {
6667 public:
6668 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6669 virtual ~HGraphDelegateVisitor() {}
6670
6671 // Visit functions that delegate to to super class.
6672#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006673 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006674
6675 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6676
6677#undef DECLARE_VISIT_INSTRUCTION
6678
6679 private:
6680 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6681};
6682
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006683// Iterator over the blocks that art part of the loop. Includes blocks part
6684// of an inner loop. The order in which the blocks are iterated is on their
6685// block id.
6686class HBlocksInLoopIterator : public ValueObject {
6687 public:
6688 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6689 : blocks_in_loop_(info.GetBlocks()),
6690 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6691 index_(0) {
6692 if (!blocks_in_loop_.IsBitSet(index_)) {
6693 Advance();
6694 }
6695 }
6696
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006697 bool Done() const { return index_ == blocks_.size(); }
6698 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006699 void Advance() {
6700 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006701 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006702 if (blocks_in_loop_.IsBitSet(index_)) {
6703 break;
6704 }
6705 }
6706 }
6707
6708 private:
6709 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006710 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006711 size_t index_;
6712
6713 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6714};
6715
Mingyao Yang3584bce2015-05-19 16:01:59 -07006716// Iterator over the blocks that art part of the loop. Includes blocks part
6717// of an inner loop. The order in which the blocks are iterated is reverse
6718// post order.
6719class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6720 public:
6721 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6722 : blocks_in_loop_(info.GetBlocks()),
6723 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6724 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006725 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006726 Advance();
6727 }
6728 }
6729
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006730 bool Done() const { return index_ == blocks_.size(); }
6731 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006732 void Advance() {
6733 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006734 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6735 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006736 break;
6737 }
6738 }
6739 }
6740
6741 private:
6742 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006743 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006744 size_t index_;
6745
6746 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6747};
6748
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006749inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006750 if (constant->IsIntConstant()) {
6751 return constant->AsIntConstant()->GetValue();
6752 } else if (constant->IsLongConstant()) {
6753 return constant->AsLongConstant()->GetValue();
6754 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006755 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006756 return 0;
6757 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006758}
6759
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006760#define INSTRUCTION_TYPE_CHECK(type, super) \
6761 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6762 inline const H##type* HInstruction::As##type() const { \
6763 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6764 } \
6765 inline H##type* HInstruction::As##type() { \
6766 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6767 }
6768
6769 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6770#undef INSTRUCTION_TYPE_CHECK
6771
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006772// Create space in `blocks` for adding `number_of_new_blocks` entries
6773// starting at location `at`. Blocks after `at` are moved accordingly.
6774inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6775 size_t number_of_new_blocks,
6776 size_t after) {
6777 DCHECK_LT(after, blocks->size());
6778 size_t old_size = blocks->size();
6779 size_t new_size = old_size + number_of_new_blocks;
6780 blocks->resize(new_size);
6781 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6782}
6783
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006784/*
6785 * Hunt "under the hood" of array lengths (leading to array references),
6786 * null checks (also leading to array references), and new arrays
6787 * (leading to the actual length). This makes it more likely related
6788 * instructions become actually comparable.
6789 */
6790inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6791 while (instruction->IsArrayLength() ||
6792 instruction->IsNullCheck() ||
6793 instruction->IsNewArray()) {
6794 instruction = instruction->IsNewArray()
6795 ? instruction->AsNewArray()->GetLength()
6796 : instruction->InputAt(0);
6797 }
6798 return instruction;
6799}
6800
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006801} // namespace art
6802
6803#endif // ART_COMPILER_OPTIMIZING_NODES_H_