blob: 4a77bed44a46698b794958e0f672c5174582959c [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 Geoffray804d0932014-05-02 08:46:00 +0100174 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175
176 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
177};
178
David Brazdil4833f5a2015-12-16 10:37:39 +0000179class ReferenceTypeInfo : ValueObject {
180 public:
181 typedef Handle<mirror::Class> TypeHandle;
182
Vladimir Markoa1de9182016-02-25 11:37:38 +0000183 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
184
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700185 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100186 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
187 }
188
Vladimir Markoa1de9182016-02-25 11:37:38 +0000189 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000190 return ReferenceTypeInfo(type_handle, is_exact);
191 }
192
193 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
194
Vladimir Markof39745e2016-01-26 12:16:55 +0000195 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000196 return handle.GetReference() != nullptr;
197 }
198
Vladimir Marko456307a2016-04-19 14:12:13 +0000199 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000200 return IsValidHandle(type_handle_);
201 }
202
203 bool IsExact() const { return is_exact_; }
204
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700205 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000206 DCHECK(IsValid());
207 return GetTypeHandle()->IsObjectClass();
208 }
209
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700210 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000211 DCHECK(IsValid());
212 return GetTypeHandle()->IsStringClass();
213 }
214
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700215 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000216 DCHECK(IsValid());
217 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
218 }
219
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700220 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000221 DCHECK(IsValid());
222 return GetTypeHandle()->IsInterface();
223 }
224
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700225 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000226 DCHECK(IsValid());
227 return GetTypeHandle()->IsArrayClass();
228 }
229
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000231 DCHECK(IsValid());
232 return GetTypeHandle()->IsPrimitiveArray();
233 }
234
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700235 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000236 DCHECK(IsValid());
237 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
238 }
239
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700240 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000241 DCHECK(IsValid());
242 if (!IsExact()) return false;
243 if (!IsArrayClass()) return false;
244 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
245 }
246
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700247 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000248 DCHECK(IsValid());
249 if (!IsExact()) return false;
250 if (!IsArrayClass()) return false;
251 if (!rti.IsArrayClass()) return false;
252 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
253 rti.GetTypeHandle()->GetComponentType());
254 }
255
256 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
257
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700258 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000259 DCHECK(IsValid());
260 DCHECK(rti.IsValid());
261 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
262 }
263
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700264 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000265 DCHECK(IsValid());
266 DCHECK(rti.IsValid());
267 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
268 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
269 }
270
271 // Returns true if the type information provide the same amount of details.
272 // Note that it does not mean that the instructions have the same actual type
273 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000275 if (!IsValid() && !rti.IsValid()) {
276 // Invalid types are equal.
277 return true;
278 }
279 if (!IsValid() || !rti.IsValid()) {
280 // One is valid, the other not.
281 return false;
282 }
283 return IsExact() == rti.IsExact()
284 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
285 }
286
287 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000288 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
289 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
290 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000291
292 // The class of the object.
293 TypeHandle type_handle_;
294 // Whether or not the type is exact or a superclass of the actual type.
295 // Whether or not we have any information about this type.
296 bool is_exact_;
297};
298
299std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
300
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000301// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100302class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000303 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100304 HGraph(ArenaAllocator* arena,
305 const DexFile& dex_file,
306 uint32_t method_idx,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100307 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100309 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100310 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 blocks_(arena->Adapter(kArenaAllocBlockList)),
315 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
316 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700317 entry_block_(nullptr),
318 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320 number_of_vregs_(0),
321 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000322 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400323 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000324 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000325 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000326 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000327 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100328 dex_file_(dex_file),
329 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100330 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100331 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100332 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000334 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100335 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
336 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
337 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000339 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000340 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700341 osr_(osr),
342 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100343 blocks_.reserve(kDefaultNumberOfBlocks);
344 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000345
David Brazdilbadd8262016-02-02 16:28:56 +0000346 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700347 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000348
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000349 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100350 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
351
David Brazdil69ba7b72015-06-23 18:27:30 +0100352 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000353 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 HBasicBlock* GetEntryBlock() const { return entry_block_; }
356 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100357 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000358
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000359 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
360 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000362 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100363
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100364 void ComputeDominanceInformation();
365 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000366 void ClearLoopInformation();
367 void FindBackEdges(ArenaBitVector* visited);
368 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100369 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100370 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000371
David Brazdil4833f5a2015-12-16 10:37:39 +0000372 // Analyze all natural loops in this graph. Returns a code specifying that it
373 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000375 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100376
David Brazdilffee3d32015-07-06 11:48:53 +0100377 // Iterate over blocks to compute try block membership. Needs reverse post
378 // order and loop information.
379 void ComputeTryBlockInformation();
380
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000381 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000382 // Returns the instruction to replace the invoke expression or null if the
383 // invoke is for a void method. Note that the caller is responsible for replacing
384 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000385 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000387 // Update the loop and try membership of `block`, which was spawned from `reference`.
388 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
389 // should be the new back edge.
390 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
391 HBasicBlock* reference,
392 bool replace_if_back_edge);
393
Mingyao Yang3584bce2015-05-19 16:01:59 -0700394 // Need to add a couple of blocks to test if the loop body is entered and
395 // put deoptimization instructions, etc.
396 void TransformLoopHeaderForBCE(HBasicBlock* header);
397
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000398 // Removes `block` from the graph. Assumes `block` has been disconnected from
399 // other blocks and has no instructions or phis.
400 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000401
David Brazdilfc6a86a2015-06-26 10:33:45 +0000402 // Splits the edge between `block` and `successor` while preserving the
403 // indices in the predecessor/successor lists. If there are multiple edges
404 // between the blocks, the lowest indices are used.
405 // Returns the new block which is empty and has the same dex pc as `successor`.
406 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
407
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100408 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
409 void SimplifyLoop(HBasicBlock* header);
410
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000411 int32_t GetNextInstructionId() {
412 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000413 return current_instruction_id_++;
414 }
415
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000416 int32_t GetCurrentInstructionId() const {
417 return current_instruction_id_;
418 }
419
420 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000421 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000422 current_instruction_id_ = id;
423 }
424
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100425 uint16_t GetMaximumNumberOfOutVRegs() const {
426 return maximum_number_of_out_vregs_;
427 }
428
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000429 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
430 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100431 }
432
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100433 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
434 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
435 }
436
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000437 void UpdateTemporariesVRegSlots(size_t slots) {
438 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100439 }
440
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000441 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100444 }
445
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100446 void SetNumberOfVRegs(uint16_t number_of_vregs) {
447 number_of_vregs_ = number_of_vregs;
448 }
449
450 uint16_t GetNumberOfVRegs() const {
451 return number_of_vregs_;
452 }
453
454 void SetNumberOfInVRegs(uint16_t value) {
455 number_of_in_vregs_ = value;
456 }
457
David Brazdildee58d62016-04-07 09:54:26 +0000458 uint16_t GetNumberOfInVRegs() const {
459 return number_of_in_vregs_;
460 }
461
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100462 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100463 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 return number_of_vregs_ - number_of_in_vregs_;
465 }
466
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100467 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100468 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100469 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100470
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100471 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
472 DCHECK(GetReversePostOrder()[0] == entry_block_);
473 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
474 }
475
476 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
477 return ReverseRange(GetReversePostOrder());
478 }
479
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100480 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100481 return linear_order_;
482 }
483
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100484 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
485 return ReverseRange(GetLinearOrder());
486 }
487
Mark Mendell1152c922015-04-24 17:06:35 -0400488 bool HasBoundsChecks() const {
489 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800490 }
491
Mark Mendell1152c922015-04-24 17:06:35 -0400492 void SetHasBoundsChecks(bool value) {
493 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800494 }
495
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100496 bool ShouldGenerateConstructorBarrier() const {
497 return should_generate_constructor_barrier_;
498 }
499
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000500 bool IsDebuggable() const { return debuggable_; }
501
David Brazdil8d5b8b22015-03-24 10:51:52 +0000502 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000503 // already, it is created and inserted into the graph. This method is only for
504 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600505 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000506
507 // TODO: This is problematic for the consistency of reference type propagation
508 // because it can be created anytime after the pass and thus it will be left
509 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600510 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000511
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
513 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000514 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
516 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000517 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
519 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000520 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
522 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000523 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000524
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100525 HCurrentMethod* GetCurrentMethod();
526
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100527 const DexFile& GetDexFile() const {
528 return dex_file_;
529 }
530
531 uint32_t GetMethodIdx() const {
532 return method_idx_;
533 }
534
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100535 InvokeType GetInvokeType() const {
536 return invoke_type_;
537 }
538
Mark Mendellc4701932015-04-10 13:18:51 -0400539 InstructionSet GetInstructionSet() const {
540 return instruction_set_;
541 }
542
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000543 bool IsCompilingOsr() const { return osr_; }
544
Mingyao Yang063fc772016-08-02 11:02:54 -0700545 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
546 return cha_single_implementation_list_;
547 }
548
549 void AddCHASingleImplementationDependency(ArtMethod* method) {
550 cha_single_implementation_list_.insert(method);
551 }
552
553 bool HasShouldDeoptimizeFlag() const {
554 // TODO: if all CHA guards can be eliminated, there is no need for the flag
555 // even if cha_single_implementation_list_ is not empty.
556 return !cha_single_implementation_list_.empty();
557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Mark Mendellf6529172015-11-17 11:16:56 -0500568 // Returns an instruction with the opposite boolean value from 'cond'.
569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
David Brazdil2d7352b2015-04-20 14:52:42 +0100575 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000576 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100577 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000578
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000579 template <class InstructionType, typename ValueType>
580 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600581 ArenaSafeMap<ValueType, InstructionType*>* cache,
582 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 // Try to find an existing constant of the given value.
584 InstructionType* constant = nullptr;
585 auto cached_constant = cache->find(value);
586 if (cached_constant != cache->end()) {
587 constant = cached_constant->second;
588 }
589
590 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100591 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600593 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000594 cache->Overwrite(value, constant);
595 InsertConstant(constant);
596 }
597 return constant;
598 }
599
David Brazdil8d5b8b22015-03-24 10:51:52 +0000600 void InsertConstant(HConstant* instruction);
601
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 // Cache a float constant into the graph. This method should only be
603 // called by the SsaBuilder when creating "equivalent" instructions.
604 void CacheFloatConstant(HFloatConstant* constant);
605
606 // See CacheFloatConstant comment.
607 void CacheDoubleConstant(HDoubleConstant* constant);
608
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000609 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000610
611 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100612 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100614 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100615 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000616
Aart Bik281c6812016-08-26 11:31:48 -0700617 // List of blocks to perform a linear order tree traversal. Unlike the reverse
618 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100620
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000621 HBasicBlock* entry_block_;
622 HBasicBlock* exit_block_;
623
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100624 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100625 uint16_t maximum_number_of_out_vregs_;
626
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100627 // The number of virtual registers in this method. Contains the parameters.
628 uint16_t number_of_vregs_;
629
630 // The number of virtual registers used by parameters of this method.
631 uint16_t number_of_in_vregs_;
632
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000633 // Number of vreg size slots that the temporaries use (used in baseline compiler).
634 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100635
Mark Mendell1152c922015-04-24 17:06:35 -0400636 // Has bounds checks. We can totally skip BCE if it's false.
637 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800638
David Brazdil77a48ae2015-09-15 12:34:04 +0000639 // Flag whether there are any try/catch blocks in the graph. We will skip
640 // try/catch-related passes if false.
641 bool has_try_catch_;
642
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000643 // Flag whether there are any irreducible loops in the graph.
644 bool has_irreducible_loops_;
645
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000646 // Indicates whether the graph should be compiled in a way that
647 // ensures full debuggability. If false, we can apply more
648 // aggressive optimizations that may limit the level of debugging.
649 const bool debuggable_;
650
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000651 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000652 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000653
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100654 // The dex file from which the method is from.
655 const DexFile& dex_file_;
656
657 // The method index in the dex file.
658 const uint32_t method_idx_;
659
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100660 // If inlined, this encodes how the callee is being invoked.
661 const InvokeType invoke_type_;
662
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100663 // Whether the graph has been transformed to SSA form. Only used
664 // in debug mode to ensure we are not using properties only valid
665 // for non-SSA form (like the number of temporaries).
666 bool in_ssa_form_;
667
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100668 const bool should_generate_constructor_barrier_;
669
Mathieu Chartiere401d142015-04-22 13:56:20 -0700670 const InstructionSet instruction_set_;
671
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000672 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000673 HNullConstant* cached_null_constant_;
674 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000675 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000676 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000677 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000678
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100679 HCurrentMethod* cached_current_method_;
680
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100681 // The ArtMethod this graph is for. Note that for AOT, it may be null,
682 // for example for methods whose declaring class could not be resolved
683 // (such as when the superclass could not be found).
684 ArtMethod* art_method_;
685
David Brazdil4833f5a2015-12-16 10:37:39 +0000686 // Keep the RTI of inexact Object to avoid having to pass stack handle
687 // collection pointer to passes which may create NullConstant.
688 ReferenceTypeInfo inexact_object_rti_;
689
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000690 // Whether we are compiling this graph for on stack replacement: this will
691 // make all loops seen as irreducible and emit special stack maps to mark
692 // compiled code entries which the interpreter can directly jump to.
693 const bool osr_;
694
Mingyao Yang063fc772016-08-02 11:02:54 -0700695 // List of methods that are assumed to have single implementation.
696 ArenaSet<ArtMethod*> cha_single_implementation_list_;
697
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000698 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100699 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000700 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000701 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000702 DISALLOW_COPY_AND_ASSIGN(HGraph);
703};
704
Vladimir Markof9f64412015-09-02 14:05:49 +0100705class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000706 public:
707 HLoopInformation(HBasicBlock* header, HGraph* graph)
708 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100709 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000710 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100711 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100712 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100713 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000714 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100715 back_edges_.reserve(kDefaultNumberOfBackEdges);
716 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100717
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000720
721 void Dump(std::ostream& os);
722
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100723 HBasicBlock* GetHeader() const {
724 return header_;
725 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000726
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000727 void SetHeader(HBasicBlock* block) {
728 header_ = block;
729 }
730
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100731 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
732 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
733 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
734
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000735 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000737 }
738
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100739 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100740 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741 }
742
David Brazdil46e2a392015-03-16 17:31:52 +0000743 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100745 }
746
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000747 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000749 }
750
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100751 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100752
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100753 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100754 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100755 }
756
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100757 // Returns the lifetime position of the back edge that has the
758 // greatest lifetime position.
759 size_t GetLifetimeEnd() const;
760
761 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100762 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100763 }
764
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000765 // Finds blocks that are part of this loop.
766 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100767
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100768 // Returns whether this loop information contains `block`.
769 // Note that this loop information *must* be populated before entering this function.
770 bool Contains(const HBasicBlock& block) const;
771
772 // Returns whether this loop information is an inner loop of `other`.
773 // Note that `other` *must* be populated before entering this function.
774 bool IsIn(const HLoopInformation& other) const;
775
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800776 // Returns true if instruction is not defined within this loop.
777 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700778
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 const ArenaBitVector& GetBlocks() const { return blocks_; }
780
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000781 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000782 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000783
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000784 void ClearAllBlocks() {
785 blocks_.ClearAllBits();
786 }
787
David Brazdil3f4a5222016-05-06 12:46:21 +0100788 bool HasBackEdgeNotDominatedByHeader() const;
789
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100790 bool IsPopulated() const {
791 return blocks_.GetHighestBitSet() != -1;
792 }
793
Anton Shaminf89381f2016-05-16 16:44:13 +0600794 bool DominatesAllBackEdges(HBasicBlock* block);
795
David Sehrc757dec2016-11-04 15:48:34 -0700796 bool HasExitEdge() const;
797
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000798 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100799 // Internal recursive implementation of `Populate`.
800 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100801 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100802
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000803 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100804 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000805 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100806 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100807 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000809
810 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
811};
812
David Brazdilec16f792015-08-19 15:04:01 +0100813// Stores try/catch information for basic blocks.
814// Note that HGraph is constructed so that catch blocks cannot simultaneously
815// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100816class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100817 public:
818 // Try block information constructor.
819 explicit TryCatchInformation(const HTryBoundary& try_entry)
820 : try_entry_(&try_entry),
821 catch_dex_file_(nullptr),
822 catch_type_index_(DexFile::kDexNoIndex16) {
823 DCHECK(try_entry_ != nullptr);
824 }
825
826 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800827 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100828 : try_entry_(nullptr),
829 catch_dex_file_(&dex_file),
830 catch_type_index_(catch_type_index) {}
831
832 bool IsTryBlock() const { return try_entry_ != nullptr; }
833
834 const HTryBoundary& GetTryEntry() const {
835 DCHECK(IsTryBlock());
836 return *try_entry_;
837 }
838
839 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
840
841 bool IsCatchAllTypeIndex() const {
842 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800843 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100844 }
845
Andreas Gampea5b09a62016-11-17 15:21:22 -0800846 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100847 DCHECK(IsCatchBlock());
848 return catch_type_index_;
849 }
850
851 const DexFile& GetCatchDexFile() const {
852 DCHECK(IsCatchBlock());
853 return *catch_dex_file_;
854 }
855
856 private:
857 // One of possibly several TryBoundary instructions entering the block's try.
858 // Only set for try blocks.
859 const HTryBoundary* try_entry_;
860
861 // Exception type information. Only set for catch blocks.
862 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800863 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100864};
865
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100866static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100867static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100868
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000869// A block in a method. Contains the list of instructions represented
870// as a double linked list. Each block knows its predecessors and
871// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100872
Vladimir Markof9f64412015-09-02 14:05:49 +0100873class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000874 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700875 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000876 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000877 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
878 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000879 loop_information_(nullptr),
880 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000881 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100882 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100883 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100884 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000885 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000886 try_catch_information_(nullptr) {
887 predecessors_.reserve(kDefaultNumberOfPredecessors);
888 successors_.reserve(kDefaultNumberOfSuccessors);
889 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
890 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000891
Vladimir Marko60584552015-09-03 13:35:12 +0000892 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100893 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000894 }
895
Vladimir Marko60584552015-09-03 13:35:12 +0000896 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100897 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898 }
899
David Brazdild26a4112015-11-10 11:07:31 +0000900 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
901 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
902
Vladimir Marko60584552015-09-03 13:35:12 +0000903 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
904 return ContainsElement(successors_, block, start_from);
905 }
906
907 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100908 return dominated_blocks_;
909 }
910
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100911 bool IsEntryBlock() const {
912 return graph_->GetEntryBlock() == this;
913 }
914
915 bool IsExitBlock() const {
916 return graph_->GetExitBlock() == this;
917 }
918
David Brazdil46e2a392015-03-16 17:31:52 +0000919 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000920 bool IsSingleTryBoundary() const;
921
922 // Returns true if this block emits nothing but a jump.
923 bool IsSingleJump() const {
924 HLoopInformation* loop_info = GetLoopInformation();
925 return (IsSingleGoto() || IsSingleTryBoundary())
926 // Back edges generate a suspend check.
927 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
928 }
David Brazdil46e2a392015-03-16 17:31:52 +0000929
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000930 void AddBackEdge(HBasicBlock* back_edge) {
931 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000932 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000933 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100934 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000935 loop_information_->AddBackEdge(back_edge);
936 }
937
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000938 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000939 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000940
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100941 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000942 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600943 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000944
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000945 HBasicBlock* GetDominator() const { return dominator_; }
946 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000947 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
948
949 void RemoveDominatedBlock(HBasicBlock* block) {
950 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100951 }
Vladimir Marko60584552015-09-03 13:35:12 +0000952
953 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
954 ReplaceElement(dominated_blocks_, existing, new_block);
955 }
956
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100957 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000958
959 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100960 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000961 }
962
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100963 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
964 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100965 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100967 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
968 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000969
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000970 HInstruction* GetFirstInstructionDisregardMoves() const;
971
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000973 successors_.push_back(block);
974 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000975 }
976
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100977 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
978 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100979 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000980 new_block->predecessors_.push_back(this);
981 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982 }
983
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000984 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
985 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000986 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000987 new_block->successors_.push_back(this);
988 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000989 }
990
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100991 // Insert `this` between `predecessor` and `successor. This method
992 // preserves the indicies, and will update the first edge found between
993 // `predecessor` and `successor`.
994 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
995 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100996 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +0000997 successor->predecessors_[predecessor_index] = this;
998 predecessor->successors_[successor_index] = this;
999 successors_.push_back(successor);
1000 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001001 }
1002
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001003 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001004 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001005 }
1006
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001007 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001008 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001009 }
1010
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001011 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
1015 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 predecessors_.push_back(block);
1017 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001018 }
1019
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001020 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001021 DCHECK_EQ(predecessors_.size(), 2u);
1022 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001023 }
1024
David Brazdil769c9e52015-04-27 13:54:09 +01001025 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001026 DCHECK_EQ(successors_.size(), 2u);
1027 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001028 }
1029
David Brazdilfc6a86a2015-06-26 10:33:45 +00001030 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001031 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001032 }
1033
David Brazdilfc6a86a2015-06-26 10:33:45 +00001034 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001035 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001040 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001041 }
1042
1043 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001044 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001045 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 }
1047
1048 // Returns whether the first occurrence of `predecessor` in the list of
1049 // predecessors is at index `idx`.
1050 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001051 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001052 return GetPredecessorIndexOf(predecessor) == idx;
1053 }
1054
David Brazdild7558da2015-09-22 13:04:14 +01001055 // Create a new block between this block and its predecessors. The new block
1056 // is added to the graph, all predecessor edges are relinked to it and an edge
1057 // is created to `this`. Returns the new empty block. Reverse post order or
1058 // loop and try/catch information are not updated.
1059 HBasicBlock* CreateImmediateDominator();
1060
David Brazdilfc6a86a2015-06-26 10:33:45 +00001061 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001062 // created, latter block. Note that this method will add the block to the
1063 // graph, create a Goto at the end of the former block and will create an edge
1064 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001065 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001066 HBasicBlock* SplitBefore(HInstruction* cursor);
1067
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001068 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001069 // created block. Note that this method just updates raw block information,
1070 // like predecessors, successors, dominators, and instruction list. It does not
1071 // update the graph, reverse post order, loop information, nor make sure the
1072 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001073 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1074
1075 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1076 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077
1078 // Merge `other` at the end of `this`. Successors and dominated blocks of
1079 // `other` are changed to be successors and dominated blocks of `this`. Note
1080 // that this method does not update the graph, reverse post order, loop
1081 // information, nor make sure the blocks are consistent (for example ending
1082 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001083 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001084
1085 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1086 // of `this` are moved to `other`.
1087 // Note that this method does not update the graph, reverse post order, loop
1088 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001089 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001090 void ReplaceWith(HBasicBlock* other);
1091
David Brazdil2d7352b2015-04-20 14:52:42 +01001092 // Merge `other` at the end of `this`. This method updates loops, reverse post
1093 // order, links to predecessors, successors, dominators and deletes the block
1094 // from the graph. The two blocks must be successive, i.e. `this` the only
1095 // predecessor of `other` and vice versa.
1096 void MergeWith(HBasicBlock* other);
1097
1098 // Disconnects `this` from all its predecessors, successors and dominator,
1099 // removes it from all loops it is included in and eventually from the graph.
1100 // The block must not dominate any other block. Predecessors and successors
1101 // are safely updated.
1102 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001103
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001104 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001105 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001106 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001107 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001108 // Replace instruction `initial` with `replacement` within this block.
1109 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1110 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001111 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001112 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001113 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1114 // instruction list. With 'ensure_safety' set to true, it verifies that the
1115 // instruction is not in use and removes it from the use lists of its inputs.
1116 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1117 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001118 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001119
1120 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001121 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 }
1123
Roland Levillain6b879dd2014-09-22 17:13:44 +01001124 bool IsLoopPreHeaderFirstPredecessor() const {
1125 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001126 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001127 }
1128
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001129 bool IsFirstPredecessorBackEdge() const {
1130 DCHECK(IsLoopHeader());
1131 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1132 }
1133
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001134 HLoopInformation* GetLoopInformation() const {
1135 return loop_information_;
1136 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001137
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001138 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001139 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001140 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001141 void SetInLoop(HLoopInformation* info) {
1142 if (IsLoopHeader()) {
1143 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001144 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001145 loop_information_ = info;
1146 } else if (loop_information_->Contains(*info->GetHeader())) {
1147 // Block is currently part of an outer loop. Make it part of this inner loop.
1148 // Note that a non loop header having a loop information means this loop information
1149 // has already been populated
1150 loop_information_ = info;
1151 } else {
1152 // Block is part of an inner loop. Do not update the loop information.
1153 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1154 // at this point, because this method is being called while populating `info`.
1155 }
1156 }
1157
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001158 // Raw update of the loop information.
1159 void SetLoopInformation(HLoopInformation* info) {
1160 loop_information_ = info;
1161 }
1162
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001163 bool IsInLoop() const { return loop_information_ != nullptr; }
1164
David Brazdilec16f792015-08-19 15:04:01 +01001165 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1166
1167 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1168 try_catch_information_ = try_catch_information;
1169 }
1170
1171 bool IsTryBlock() const {
1172 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1173 }
1174
1175 bool IsCatchBlock() const {
1176 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1177 }
David Brazdilffee3d32015-07-06 11:48:53 +01001178
1179 // Returns the try entry that this block's successors should have. They will
1180 // be in the same try, unless the block ends in a try boundary. In that case,
1181 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001182 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001183
David Brazdild7558da2015-09-22 13:04:14 +01001184 bool HasThrowingInstructions() const;
1185
David Brazdila4b8c212015-05-07 09:59:30 +01001186 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001187 bool Dominates(HBasicBlock* block) const;
1188
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001189 size_t GetLifetimeStart() const { return lifetime_start_; }
1190 size_t GetLifetimeEnd() const { return lifetime_end_; }
1191
1192 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1193 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1194
David Brazdil8d5b8b22015-03-24 10:51:52 +00001195 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001196 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001197 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001198 bool HasSinglePhi() const;
1199
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001200 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001201 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001202 ArenaVector<HBasicBlock*> predecessors_;
1203 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001204 HInstructionList instructions_;
1205 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001206 HLoopInformation* loop_information_;
1207 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001208 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001209 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001210 // The dex program counter of the first instruction of this block.
1211 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001212 size_t lifetime_start_;
1213 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001214 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001215
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001216 friend class HGraph;
1217 friend class HInstruction;
1218
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001219 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1220};
1221
David Brazdilb2bd1c52015-03-25 11:17:37 +00001222// Iterates over the LoopInformation of all loops which contain 'block'
1223// from the innermost to the outermost.
1224class HLoopInformationOutwardIterator : public ValueObject {
1225 public:
1226 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1227 : current_(block.GetLoopInformation()) {}
1228
1229 bool Done() const { return current_ == nullptr; }
1230
1231 void Advance() {
1232 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001233 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001234 }
1235
1236 HLoopInformation* Current() const {
1237 DCHECK(!Done());
1238 return current_;
1239 }
1240
1241 private:
1242 HLoopInformation* current_;
1243
1244 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1245};
1246
Alexandre Ramesef20f712015-06-09 10:29:30 +01001247#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001248 M(Above, Condition) \
1249 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001250 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001251 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001252 M(ArrayGet, Instruction) \
1253 M(ArrayLength, Instruction) \
1254 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001255 M(Below, Condition) \
1256 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001257 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001258 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001259 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001260 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001261 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001262 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001263 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001264 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001265 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001266 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001267 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001268 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001269 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001270 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001271 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001272 M(Exit, Instruction) \
1273 M(FloatConstant, Constant) \
1274 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001275 M(GreaterThan, Condition) \
1276 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001278 M(InstanceFieldGet, Instruction) \
1279 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001280 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001281 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001282 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001283 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001284 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(InvokeVirtual, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001286 M(LessThan, Condition) \
1287 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001288 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001289 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001290 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001291 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001292 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001293 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001294 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001295 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001296 M(Neg, UnaryOperation) \
1297 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001299 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001300 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001301 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001302 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001303 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001304 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001305 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001306 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001307 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001308 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001309 M(Return, Instruction) \
1310 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001311 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001312 M(Shl, BinaryOperation) \
1313 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001314 M(StaticFieldGet, Instruction) \
1315 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001316 M(UnresolvedInstanceFieldGet, Instruction) \
1317 M(UnresolvedInstanceFieldSet, Instruction) \
1318 M(UnresolvedStaticFieldGet, Instruction) \
1319 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001320 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001322 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001323 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001324 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001325 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001326 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001327 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001328
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001329/*
1330 * Instructions, shared across several (not all) architectures.
1331 */
1332#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1333#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1334#else
1335#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001336 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001337 M(MultiplyAccumulate, Instruction) \
1338 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001339#endif
1340
Vladimir Markob4536b72015-11-24 13:45:23 +00001341#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001342#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001343#else
1344#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1345 M(ArmDexCacheArraysBase, Instruction)
1346#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001347
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001348#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001349#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001350#else
1351#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001352 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001353#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001354
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001355#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001357#else
1358#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1359 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001360 M(MipsDexCacheArraysBase, Instruction) \
1361 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001362#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001364#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1365
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001366#ifndef ART_ENABLE_CODEGEN_x86
1367#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1368#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001369#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1370 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001371 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001372 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001373 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001374#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001375
1376#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1377
1378#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1379 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001380 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001381 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1382 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001383 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001384 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001385 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1386 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1387
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001388#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1389 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001390 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001391 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001392 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001393 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001394
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001395#define FOR_EACH_INSTRUCTION(M) \
1396 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1397 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1398
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001400FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1401#undef FORWARD_DECLARATION
1402
Vladimir Marko372f10e2016-05-17 16:30:10 +01001403#define DECLARE_INSTRUCTION(type) \
1404 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1405 const char* DebugName() const OVERRIDE { return #type; } \
1406 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1407 return other->Is##type(); \
1408 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001409 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001410
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001411#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1412 bool Is##type() const { return As##type() != nullptr; } \
1413 const H##type* As##type() const { return this; } \
1414 H##type* As##type() { return this; }
1415
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001416template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001417class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001418 public:
David Brazdiled596192015-01-23 10:39:45 +00001419 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001420 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001421 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001422
Vladimir Marko46817b82016-03-29 12:21:58 +01001423 // Hook for the IntrusiveForwardList<>.
1424 // TODO: Hide this better.
1425 IntrusiveForwardListHook hook;
1426
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001427 private:
David Brazdiled596192015-01-23 10:39:45 +00001428 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001429 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001430
1431 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001432 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001433
Vladimir Marko46817b82016-03-29 12:21:58 +01001434 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001435
1436 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1437};
1438
David Brazdiled596192015-01-23 10:39:45 +00001439template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001440using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001441
David Brazdil1abb4192015-02-17 18:33:36 +00001442// This class is used by HEnvironment and HInstruction classes to record the
1443// instructions they use and pointers to the corresponding HUseListNodes kept
1444// by the used instructions.
1445template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001446class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001447 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001448 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1449 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001450
Vladimir Marko46817b82016-03-29 12:21:58 +01001451 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1452 : HUserRecord(old_record.instruction_, before_use_node) {}
1453 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1454 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001455 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001456 }
1457
1458 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001459 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1460 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001461
1462 private:
1463 // Instruction used by the user.
1464 HInstruction* instruction_;
1465
Vladimir Marko46817b82016-03-29 12:21:58 +01001466 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1467 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001468};
1469
Vladimir Markoe9004912016-06-16 16:50:52 +01001470// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1471// This is used for HInstruction::GetInputs() to return a container wrapper providing
1472// HInstruction* values even though the underlying container has HUserRecord<>s.
1473struct HInputExtractor {
1474 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1475 return record.GetInstruction();
1476 }
1477 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1478 return record.GetInstruction();
1479 }
1480};
1481
1482using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1483using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1484
Aart Bik854a02b2015-07-14 16:07:00 -07001485/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001486 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001487 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001488 * For write/read dependences on fields/arrays, the dependence analysis uses
1489 * type disambiguation (e.g. a float field write cannot modify the value of an
1490 * integer field read) and the access type (e.g. a reference array write cannot
1491 * modify the value of a reference field read [although it may modify the
1492 * reference fetch prior to reading the field, which is represented by its own
1493 * write/read dependence]). The analysis makes conservative points-to
1494 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1495 * the same, and any reference read depends on any reference read without
1496 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001497 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 * The internal representation uses 38-bit and is described in the table below.
1499 * The first line indicates the side effect, and for field/array accesses the
1500 * second line indicates the type of the access (in the order of the
1501 * Primitive::Type enum).
1502 * The two numbered lines below indicate the bit position in the bitfield (read
1503 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001504 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001505 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1506 * +-------------+---------+---------+--------------+---------+---------+
1507 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1508 * | 3 |333333322|222222221| 1 |111111110|000000000|
1509 * | 7 |654321098|765432109| 8 |765432109|876543210|
1510 *
1511 * Note that, to ease the implementation, 'changes' bits are least significant
1512 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001513 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001514class SideEffects : public ValueObject {
1515 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001516 SideEffects() : flags_(0) {}
1517
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001518 static SideEffects None() {
1519 return SideEffects(0);
1520 }
1521
1522 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001523 return SideEffects(kAllChangeBits | kAllDependOnBits);
1524 }
1525
1526 static SideEffects AllChanges() {
1527 return SideEffects(kAllChangeBits);
1528 }
1529
1530 static SideEffects AllDependencies() {
1531 return SideEffects(kAllDependOnBits);
1532 }
1533
1534 static SideEffects AllExceptGCDependency() {
1535 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1536 }
1537
1538 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001539 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001540 }
1541
Aart Bik34c3ba92015-07-20 14:08:59 -07001542 static SideEffects AllWrites() {
1543 return SideEffects(kAllWrites);
1544 }
1545
1546 static SideEffects AllReads() {
1547 return SideEffects(kAllReads);
1548 }
1549
1550 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1551 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001552 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001553 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001554 }
1555
Aart Bik854a02b2015-07-14 16:07:00 -07001556 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001557 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001558 }
1559
Aart Bik34c3ba92015-07-20 14:08:59 -07001560 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1561 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001562 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001563 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001564 }
1565
1566 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001567 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001568 }
1569
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 static SideEffects CanTriggerGC() {
1571 return SideEffects(1ULL << kCanTriggerGCBit);
1572 }
1573
1574 static SideEffects DependsOnGC() {
1575 return SideEffects(1ULL << kDependsOnGCBit);
1576 }
1577
Aart Bik854a02b2015-07-14 16:07:00 -07001578 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001579 SideEffects Union(SideEffects other) const {
1580 return SideEffects(flags_ | other.flags_);
1581 }
1582
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 SideEffects Exclusion(SideEffects other) const {
1584 return SideEffects(flags_ & ~other.flags_);
1585 }
1586
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001587 void Add(SideEffects other) {
1588 flags_ |= other.flags_;
1589 }
1590
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001591 bool Includes(SideEffects other) const {
1592 return (other.flags_ & flags_) == other.flags_;
1593 }
1594
1595 bool HasSideEffects() const {
1596 return (flags_ & kAllChangeBits);
1597 }
1598
1599 bool HasDependencies() const {
1600 return (flags_ & kAllDependOnBits);
1601 }
1602
1603 // Returns true if there are no side effects or dependencies.
1604 bool DoesNothing() const {
1605 return flags_ == 0;
1606 }
1607
Aart Bik854a02b2015-07-14 16:07:00 -07001608 // Returns true if something is written.
1609 bool DoesAnyWrite() const {
1610 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001611 }
1612
Aart Bik854a02b2015-07-14 16:07:00 -07001613 // Returns true if something is read.
1614 bool DoesAnyRead() const {
1615 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001616 }
1617
Aart Bik854a02b2015-07-14 16:07:00 -07001618 // Returns true if potentially everything is written and read
1619 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001620 bool DoesAllReadWrite() const {
1621 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1622 }
1623
Aart Bik854a02b2015-07-14 16:07:00 -07001624 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001625 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001626 }
1627
Roland Levillain0d5a2812015-11-13 10:07:31 +00001628 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001629 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1631 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001632 }
1633
1634 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001635 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001636 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001637 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001638 for (int s = kLastBit; s >= 0; s--) {
1639 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1640 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1641 // This is a bit for the GC side effect.
1642 if (current_bit_is_set) {
1643 flags += "GC";
1644 }
Aart Bik854a02b2015-07-14 16:07:00 -07001645 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001646 } else {
1647 // This is a bit for the array/field analysis.
1648 // The underscore character stands for the 'can trigger GC' bit.
1649 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1650 if (current_bit_is_set) {
1651 flags += kDebug[s];
1652 }
1653 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1654 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1655 flags += "|";
1656 }
1657 }
Aart Bik854a02b2015-07-14 16:07:00 -07001658 }
1659 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001660 }
1661
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001662 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001663
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001664 private:
1665 static constexpr int kFieldArrayAnalysisBits = 9;
1666
1667 static constexpr int kFieldWriteOffset = 0;
1668 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1669 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1670 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1671
1672 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1673
1674 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1675 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1676 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1677 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1678
1679 static constexpr int kLastBit = kDependsOnGCBit;
1680 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1681
1682 // Aliases.
1683
1684 static_assert(kChangeBits == kDependOnBits,
1685 "the 'change' bits should match the 'depend on' bits.");
1686
1687 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1688 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1689 static constexpr uint64_t kAllWrites =
1690 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1691 static constexpr uint64_t kAllReads =
1692 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001693
Aart Bik854a02b2015-07-14 16:07:00 -07001694 // Translates type to bit flag.
1695 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1696 CHECK_NE(type, Primitive::kPrimVoid);
1697 const uint64_t one = 1;
1698 const int shift = type; // 0-based consecutive enum
1699 DCHECK_LE(kFieldWriteOffset, shift);
1700 DCHECK_LT(shift, kArrayWriteOffset);
1701 return one << (type + offset);
1702 }
1703
1704 // Private constructor on direct flags value.
1705 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1706
1707 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001708};
1709
David Brazdiled596192015-01-23 10:39:45 +00001710// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001711class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001712 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001713 HEnvironment(ArenaAllocator* arena,
1714 size_t number_of_vregs,
1715 const DexFile& dex_file,
1716 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001717 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001718 InvokeType invoke_type,
1719 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001720 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1721 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001722 parent_(nullptr),
1723 dex_file_(dex_file),
1724 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001725 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001726 invoke_type_(invoke_type),
1727 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001728 }
1729
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001731 : HEnvironment(arena,
1732 to_copy.Size(),
1733 to_copy.GetDexFile(),
1734 to_copy.GetMethodIdx(),
1735 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001736 to_copy.GetInvokeType(),
1737 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001738
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001739 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001740 if (parent_ != nullptr) {
1741 parent_->SetAndCopyParentChain(allocator, parent);
1742 } else {
1743 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1744 parent_->CopyFrom(parent);
1745 if (parent->GetParent() != nullptr) {
1746 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1747 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 }
David Brazdiled596192015-01-23 10:39:45 +00001749 }
1750
Vladimir Marko71bf8092015-09-15 15:33:14 +01001751 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001752 void CopyFrom(HEnvironment* environment);
1753
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001754 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1755 // input to the loop phi instead. This is for inserting instructions that
1756 // require an environment (like HDeoptimization) in the loop pre-header.
1757 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001758
1759 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001760 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001761 }
1762
1763 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001764 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001765 }
1766
David Brazdil1abb4192015-02-17 18:33:36 +00001767 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001768
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001769 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001770
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001771 HEnvironment* GetParent() const { return parent_; }
1772
1773 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001774 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001775 }
1776
1777 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001779 }
1780
1781 uint32_t GetDexPc() const {
1782 return dex_pc_;
1783 }
1784
1785 uint32_t GetMethodIdx() const {
1786 return method_idx_;
1787 }
1788
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 InvokeType GetInvokeType() const {
1790 return invoke_type_;
1791 }
1792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 const DexFile& GetDexFile() const {
1794 return dex_file_;
1795 }
1796
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001797 HInstruction* GetHolder() const {
1798 return holder_;
1799 }
1800
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001801
1802 bool IsFromInlinedInvoke() const {
1803 return GetParent() != nullptr;
1804 }
1805
David Brazdiled596192015-01-23 10:39:45 +00001806 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001807 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1808 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001809 HEnvironment* parent_;
1810 const DexFile& dex_file_;
1811 const uint32_t method_idx_;
1812 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001813 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001814
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001815 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001816 HInstruction* const holder_;
1817
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001818 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001819
1820 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1821};
1822
Vladimir Markof9f64412015-09-02 14:05:49 +01001823class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001824 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001825 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826 : previous_(nullptr),
1827 next_(nullptr),
1828 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001829 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001830 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001831 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001832 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001833 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001834 locations_(nullptr),
1835 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001836 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001837 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001838 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1839 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1840 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001841
Dave Allison20dfc792014-06-16 20:44:29 -07001842 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001843
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001844#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001845 enum InstructionKind {
1846 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1847 };
1848#undef DECLARE_KIND
1849
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001850 HInstruction* GetNext() const { return next_; }
1851 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001852
Calin Juravle77520bc2015-01-12 18:45:46 +00001853 HInstruction* GetNextDisregardingMoves() const;
1854 HInstruction* GetPreviousDisregardingMoves() const;
1855
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001857 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001858 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001859 bool IsInBlock() const { return block_ != nullptr; }
1860 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001861 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1862 bool IsIrreducibleLoopHeaderPhi() const {
1863 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1864 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001865
Vladimir Marko372f10e2016-05-17 16:30:10 +01001866 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1867
1868 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1869 // One virtual method is enough, just const_cast<> and then re-add the const.
1870 return ArrayRef<const HUserRecord<HInstruction*>>(
1871 const_cast<HInstruction*>(this)->GetInputRecords());
1872 }
1873
Vladimir Markoe9004912016-06-16 16:50:52 +01001874 HInputsRef GetInputs() {
1875 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001876 }
1877
Vladimir Markoe9004912016-06-16 16:50:52 +01001878 HConstInputsRef GetInputs() const {
1879 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001880 }
1881
1882 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001883 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001884
Aart Bik2767f4b2016-10-28 15:03:53 -07001885 bool HasInput(HInstruction* input) const {
1886 for (const HInstruction* i : GetInputs()) {
1887 if (i == input) {
1888 return true;
1889 }
1890 }
1891 return false;
1892 }
1893
Vladimir Marko372f10e2016-05-17 16:30:10 +01001894 void SetRawInputAt(size_t index, HInstruction* input) {
1895 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1896 }
1897
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001898 virtual void Accept(HGraphVisitor* visitor) = 0;
1899 virtual const char* DebugName() const = 0;
1900
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1902
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001903 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001904
1905 uint32_t GetDexPc() const { return dex_pc_; }
1906
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001907 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001908
Roland Levillaine161a2a2014-10-03 12:45:18 +01001909 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001910 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001911
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001912 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001913 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001914
Calin Juravle10e244f2015-01-26 18:54:32 +00001915 // Does not apply for all instructions, but having this at top level greatly
1916 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001917 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001918 virtual bool CanBeNull() const {
1919 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1920 return true;
1921 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001922
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001923 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001924 return false;
1925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001926
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001927 virtual bool IsActualObject() const {
1928 return GetType() == Primitive::kPrimNot;
1929 }
1930
Calin Juravle2e768302015-07-28 14:41:11 +00001931 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001932
Calin Juravle61d544b2015-02-23 16:46:57 +00001933 ReferenceTypeInfo GetReferenceTypeInfo() const {
1934 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001935 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001936 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001937 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001938
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001939 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001940 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001941 // Note: fixup_end remains valid across push_front().
1942 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1943 HUseListNode<HInstruction*>* new_node =
1944 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1945 uses_.push_front(*new_node);
1946 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001947 }
1948
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001949 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001950 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001951 // Note: env_fixup_end remains valid across push_front().
1952 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1953 HUseListNode<HEnvironment*>* new_node =
1954 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1955 env_uses_.push_front(*new_node);
1956 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 }
1958
David Brazdil1abb4192015-02-17 18:33:36 +00001959 void RemoveAsUserOfInput(size_t input) {
1960 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001961 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1962 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1963 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001964 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965
Vladimir Marko372f10e2016-05-17 16:30:10 +01001966 void RemoveAsUserOfAllInputs() {
1967 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1968 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1969 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1970 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1971 }
1972 }
1973
David Brazdil1abb4192015-02-17 18:33:36 +00001974 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1975 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001976
Vladimir Marko46817b82016-03-29 12:21:58 +01001977 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1978 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1979 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001980 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001981 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001982 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983
Aart Bikcc42be02016-10-20 16:14:16 -07001984 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001985 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001986 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001987 !CanThrow() &&
1988 !IsSuspendCheck() &&
1989 !IsControlFlow() &&
1990 !IsNativeDebugInfo() &&
1991 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07001992 // If we added an explicit barrier then we should keep it.
1993 !IsMemoryBarrier();
1994 }
1995
Aart Bikcc42be02016-10-20 16:14:16 -07001996 bool IsDeadAndRemovable() const {
1997 return IsRemovable() && !HasUses();
1998 }
1999
Roland Levillain6c82d402014-10-13 16:10:27 +01002000 // Does this instruction strictly dominate `other_instruction`?
2001 // Returns false if this instruction and `other_instruction` are the same.
2002 // Aborts if this instruction and `other_instruction` are both phis.
2003 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002004
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002005 int GetId() const { return id_; }
2006 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002007
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002008 int GetSsaIndex() const { return ssa_index_; }
2009 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2010 bool HasSsaIndex() const { return ssa_index_ != -1; }
2011
2012 bool HasEnvironment() const { return environment_ != nullptr; }
2013 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002014 // Set the `environment_` field. Raw because this method does not
2015 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002016 void SetRawEnvironment(HEnvironment* environment) {
2017 DCHECK(environment_ == nullptr);
2018 DCHECK_EQ(environment->GetHolder(), this);
2019 environment_ = environment;
2020 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002021
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002022 void InsertRawEnvironment(HEnvironment* environment) {
2023 DCHECK(environment_ != nullptr);
2024 DCHECK_EQ(environment->GetHolder(), this);
2025 DCHECK(environment->GetParent() == nullptr);
2026 environment->parent_ = environment_;
2027 environment_ = environment;
2028 }
2029
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002030 void RemoveEnvironment();
2031
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002032 // Set the environment of this instruction, copying it from `environment`. While
2033 // copying, the uses lists are being updated.
2034 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002035 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002036 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002037 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002038 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002039 if (environment->GetParent() != nullptr) {
2040 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2041 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002042 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002043
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002044 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2045 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002046 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002047 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002048 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002049 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002050 if (environment->GetParent() != nullptr) {
2051 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2052 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002053 }
2054
Nicolas Geoffray39468442014-09-02 15:17:15 +01002055 // Returns the number of entries in the environment. Typically, that is the
2056 // number of dex registers in a method. It could be more in case of inlining.
2057 size_t EnvironmentSize() const;
2058
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002059 LocationSummary* GetLocations() const { return locations_; }
2060 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002061
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002062 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002063 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002064
Alexandre Rames188d4312015-04-09 18:30:21 +01002065 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2066 // uses of this instruction by `other` are *not* updated.
2067 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2068 ReplaceWith(other);
2069 other->ReplaceInput(this, use_index);
2070 }
2071
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002072 // Move `this` instruction before `cursor`.
2073 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002074
Vladimir Markofb337ea2015-11-25 15:25:10 +00002075 // Move `this` before its first user and out of any loops. If there is no
2076 // out-of-loop user that dominates all other users, move the instruction
2077 // to the end of the out-of-loop common dominator of the user's blocks.
2078 //
2079 // This can be used only on non-throwing instructions with no side effects that
2080 // have at least one use but no environment uses.
2081 void MoveBeforeFirstUserAndOutOfLoops();
2082
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002083#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 bool Is##type() const; \
2085 const H##type* As##type() const; \
2086 H##type* As##type();
2087
2088 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2089#undef INSTRUCTION_TYPE_CHECK
2090
2091#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002092 bool Is##type() const { return (As##type() != nullptr); } \
2093 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002095 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002096#undef INSTRUCTION_TYPE_CHECK
2097
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002098 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002099 // TODO: this method is used by LICM and GVN with possibly different
2100 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002101 virtual bool CanBeMoved() const { return false; }
2102
2103 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002104 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002105 return false;
2106 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002107
2108 // Returns whether any data encoded in the two instructions is equal.
2109 // This method does not look at the inputs. Both instructions must be
2110 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002111 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002112 return false;
2113 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002114
2115 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002116 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002117 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002118 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002120 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2121 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2122 // the virtual function because the __attribute__((__pure__)) doesn't really
2123 // apply the strong requirement for virtual functions, preventing optimizations.
2124 InstructionKind GetKind() const PURE;
2125 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002126
2127 virtual size_t ComputeHashCode() const {
2128 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002129 for (const HInstruction* input : GetInputs()) {
2130 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002131 }
2132 return result;
2133 }
2134
2135 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002136 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002137 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002138
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002139 size_t GetLifetimePosition() const { return lifetime_position_; }
2140 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2141 LiveInterval* GetLiveInterval() const { return live_interval_; }
2142 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2143 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2144
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002145 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2146
2147 // Returns whether the code generation of the instruction will require to have access
2148 // to the current method. Such instructions are:
2149 // (1): Instructions that require an environment, as calling the runtime requires
2150 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002151 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2152 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002153 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002154 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002155 }
2156
Vladimir Markodc151b22015-10-15 18:02:30 +01002157 // Returns whether the code generation of the instruction will require to have access
2158 // to the dex cache of the current method's declaring class via the current method.
2159 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002160
Mark Mendellc4701932015-04-10 13:18:51 -04002161 // Does this instruction have any use in an environment before
2162 // control flow hits 'other'?
2163 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2164
2165 // Remove all references to environment uses of this instruction.
2166 // The caller must ensure that this is safe to do.
2167 void RemoveEnvironmentUsers();
2168
Vladimir Markoa1de9182016-02-25 11:37:38 +00002169 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2170 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002171
David Brazdil1abb4192015-02-17 18:33:36 +00002172 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002173 // If set, the machine code for this instruction is assumed to be generated by
2174 // its users. Used by liveness analysis to compute use positions accordingly.
2175 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2176 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2177 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2178 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2179
Vladimir Marko372f10e2016-05-17 16:30:10 +01002180 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2181 return GetInputRecords()[i];
2182 }
2183
2184 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2185 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2186 input_records[index] = input;
2187 }
David Brazdil1abb4192015-02-17 18:33:36 +00002188
Vladimir Markoa1de9182016-02-25 11:37:38 +00002189 uint32_t GetPackedFields() const {
2190 return packed_fields_;
2191 }
2192
2193 template <size_t flag>
2194 bool GetPackedFlag() const {
2195 return (packed_fields_ & (1u << flag)) != 0u;
2196 }
2197
2198 template <size_t flag>
2199 void SetPackedFlag(bool value = true) {
2200 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2201 }
2202
2203 template <typename BitFieldType>
2204 typename BitFieldType::value_type GetPackedField() const {
2205 return BitFieldType::Decode(packed_fields_);
2206 }
2207
2208 template <typename BitFieldType>
2209 void SetPackedField(typename BitFieldType::value_type value) {
2210 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2211 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2212 }
2213
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002214 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002215 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2216 auto before_use_node = uses_.before_begin();
2217 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2218 HInstruction* user = use_node->GetUser();
2219 size_t input_index = use_node->GetIndex();
2220 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2221 before_use_node = use_node;
2222 }
2223 }
2224
2225 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2226 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2227 if (next != uses_.end()) {
2228 HInstruction* next_user = next->GetUser();
2229 size_t next_index = next->GetIndex();
2230 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2231 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2232 }
2233 }
2234
2235 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2236 auto before_env_use_node = env_uses_.before_begin();
2237 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2238 HEnvironment* user = env_use_node->GetUser();
2239 size_t input_index = env_use_node->GetIndex();
2240 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2241 before_env_use_node = env_use_node;
2242 }
2243 }
2244
2245 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2246 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2247 if (next != env_uses_.end()) {
2248 HEnvironment* next_user = next->GetUser();
2249 size_t next_index = next->GetIndex();
2250 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2251 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2252 }
2253 }
David Brazdil1abb4192015-02-17 18:33:36 +00002254
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002255 HInstruction* previous_;
2256 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002257 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002258 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002259
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002260 // An instruction gets an id when it is added to the graph.
2261 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002262 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002263 int id_;
2264
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002265 // When doing liveness analysis, instructions that have uses get an SSA index.
2266 int ssa_index_;
2267
Vladimir Markoa1de9182016-02-25 11:37:38 +00002268 // Packed fields.
2269 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002270
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002271 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002272 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002273
2274 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002275 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002276
Nicolas Geoffray39468442014-09-02 15:17:15 +01002277 // The environment associated with this instruction. Not null if the instruction
2278 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002279 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002280
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002281 // Set by the code generator.
2282 LocationSummary* locations_;
2283
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002284 // Set by the liveness analysis.
2285 LiveInterval* live_interval_;
2286
2287 // Set by the liveness analysis, this is the position in a linear
2288 // order of blocks where this instruction's live interval start.
2289 size_t lifetime_position_;
2290
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002291 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002292
Vladimir Markoa1de9182016-02-25 11:37:38 +00002293 // The reference handle part of the reference type info.
2294 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002295 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002296 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002297
David Brazdil1abb4192015-02-17 18:33:36 +00002298 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002300 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002301 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002302 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303
2304 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2305};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002306std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307
2308class HInstructionIterator : public ValueObject {
2309 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002310 explicit HInstructionIterator(const HInstructionList& instructions)
2311 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002312 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002313 }
2314
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002315 bool Done() const { return instruction_ == nullptr; }
2316 HInstruction* Current() const { return instruction_; }
2317 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002318 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002319 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320 }
2321
2322 private:
2323 HInstruction* instruction_;
2324 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002325
2326 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002327};
2328
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002329class HBackwardInstructionIterator : public ValueObject {
2330 public:
2331 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2332 : instruction_(instructions.last_instruction_) {
2333 next_ = Done() ? nullptr : instruction_->GetPrevious();
2334 }
2335
2336 bool Done() const { return instruction_ == nullptr; }
2337 HInstruction* Current() const { return instruction_; }
2338 void Advance() {
2339 instruction_ = next_;
2340 next_ = Done() ? nullptr : instruction_->GetPrevious();
2341 }
2342
2343 private:
2344 HInstruction* instruction_;
2345 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002346
2347 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002348};
2349
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002350class HVariableInputSizeInstruction : public HInstruction {
2351 public:
2352 void AddInput(HInstruction* input);
2353 void InsertInputAt(size_t index, HInstruction* input);
2354 void RemoveInputAt(size_t index);
2355
2356 protected:
2357 HVariableInputSizeInstruction(SideEffects side_effects,
2358 uint32_t dex_pc,
2359 ArenaAllocator* arena,
2360 size_t number_of_inputs,
2361 ArenaAllocKind kind)
2362 : HInstruction(side_effects, dex_pc),
2363 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2364
2365 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2366
2367 private:
2368 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2369};
2370
Vladimir Markof9f64412015-09-02 14:05:49 +01002371template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002372class HTemplateInstruction: public HInstruction {
2373 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002374 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002375 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002376 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002377
Vladimir Marko372f10e2016-05-17 16:30:10 +01002378 using HInstruction::GetInputRecords; // Keep the const version visible.
2379 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2380 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002381 }
2382
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002383 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002384 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002385
2386 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002387};
2388
Vladimir Markof9f64412015-09-02 14:05:49 +01002389// HTemplateInstruction specialization for N=0.
2390template<>
2391class HTemplateInstruction<0>: public HInstruction {
2392 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002393 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002394 : HInstruction(side_effects, dex_pc) {}
2395
Vladimir Markof9f64412015-09-02 14:05:49 +01002396 virtual ~HTemplateInstruction() {}
2397
Vladimir Marko372f10e2016-05-17 16:30:10 +01002398 using HInstruction::GetInputRecords; // Keep the const version visible.
2399 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2400 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002401 }
2402
2403 private:
2404 friend class SsaBuilder;
2405};
2406
Dave Allison20dfc792014-06-16 20:44:29 -07002407template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002408class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002409 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002410 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002411 : HTemplateInstruction<N>(side_effects, dex_pc) {
2412 this->template SetPackedField<TypeField>(type);
2413 }
Dave Allison20dfc792014-06-16 20:44:29 -07002414 virtual ~HExpression() {}
2415
Vladimir Markoa1de9182016-02-25 11:37:38 +00002416 Primitive::Type GetType() const OVERRIDE {
2417 return TypeField::Decode(this->GetPackedFields());
2418 }
Dave Allison20dfc792014-06-16 20:44:29 -07002419
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002420 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002421 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2422 static constexpr size_t kFieldTypeSize =
2423 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2424 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2425 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2426 "Too many packed fields.");
2427 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002428};
2429
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002430// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2431// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002432class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002433 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002434 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2435 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002436
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002437 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002438
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002439 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002440
2441 private:
2442 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2443};
2444
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002445// Represents dex's RETURN opcodes. A HReturn is a control flow
2446// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002447class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2450 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002451 SetRawInputAt(0, value);
2452 }
2453
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002454 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002455
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002456 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002457
2458 private:
2459 DISALLOW_COPY_AND_ASSIGN(HReturn);
2460};
2461
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002462class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002463 public:
2464 HPhi(ArenaAllocator* arena,
2465 uint32_t reg_number,
2466 size_t number_of_inputs,
2467 Primitive::Type type,
2468 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002469 : HVariableInputSizeInstruction(
2470 SideEffects::None(),
2471 dex_pc,
2472 arena,
2473 number_of_inputs,
2474 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002475 reg_number_(reg_number) {
2476 SetPackedField<TypeField>(ToPhiType(type));
2477 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2478 // Phis are constructed live and marked dead if conflicting or unused.
2479 // Individual steps of SsaBuilder should assume that if a phi has been
2480 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2481 SetPackedFlag<kFlagIsLive>(true);
2482 SetPackedFlag<kFlagCanBeNull>(true);
2483 }
2484
2485 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2486 static Primitive::Type ToPhiType(Primitive::Type type) {
2487 return Primitive::PrimitiveKind(type);
2488 }
2489
2490 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2491
Vladimir Marko372f10e2016-05-17 16:30:10 +01002492 using HInstruction::GetInputRecords; // Keep the const version visible.
2493 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2494 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2495 }
David Brazdildee58d62016-04-07 09:54:26 +00002496
David Brazdildee58d62016-04-07 09:54:26 +00002497 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2498 void SetType(Primitive::Type new_type) {
2499 // Make sure that only valid type changes occur. The following are allowed:
2500 // (1) int -> float/ref (primitive type propagation),
2501 // (2) long -> double (primitive type propagation).
2502 DCHECK(GetType() == new_type ||
2503 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2504 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2505 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2506 SetPackedField<TypeField>(new_type);
2507 }
2508
2509 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2510 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2511
2512 uint32_t GetRegNumber() const { return reg_number_; }
2513
2514 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2515 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2516 bool IsDead() const { return !IsLive(); }
2517 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2518
Vladimir Markoe9004912016-06-16 16:50:52 +01002519 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002520 return other != nullptr
2521 && other->IsPhi()
2522 && other->AsPhi()->GetBlock() == GetBlock()
2523 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2524 }
2525
2526 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2527 // An equivalent phi is a phi having the same dex register and type.
2528 // It assumes that phis with the same dex register are adjacent.
2529 HPhi* GetNextEquivalentPhiWithSameType() {
2530 HInstruction* next = GetNext();
2531 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2532 if (next->GetType() == GetType()) {
2533 return next->AsPhi();
2534 }
2535 next = next->GetNext();
2536 }
2537 return nullptr;
2538 }
2539
2540 DECLARE_INSTRUCTION(Phi);
2541
David Brazdildee58d62016-04-07 09:54:26 +00002542 private:
2543 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2544 static constexpr size_t kFieldTypeSize =
2545 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2546 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2547 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2548 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2549 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2550 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2551
David Brazdildee58d62016-04-07 09:54:26 +00002552 const uint32_t reg_number_;
2553
2554 DISALLOW_COPY_AND_ASSIGN(HPhi);
2555};
2556
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002558// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002559// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002560class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002562 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002563
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002564 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002565
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002566 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002567
2568 private:
2569 DISALLOW_COPY_AND_ASSIGN(HExit);
2570};
2571
2572// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002573class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002574 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002575 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002576
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002577 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002578
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002579 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002580 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002581 }
2582
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002583 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002584
2585 private:
2586 DISALLOW_COPY_AND_ASSIGN(HGoto);
2587};
2588
Roland Levillain9867bc72015-08-05 10:21:34 +01002589class HConstant : public HExpression<0> {
2590 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002591 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2592 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002593
2594 bool CanBeMoved() const OVERRIDE { return true; }
2595
Roland Levillain1a653882016-03-18 18:05:57 +00002596 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002597 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002598 // Is this constant 0 in the arithmetic sense?
2599 virtual bool IsArithmeticZero() const { return false; }
2600 // Is this constant a 0-bit pattern?
2601 virtual bool IsZeroBitPattern() const { return false; }
2602 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002603 virtual bool IsOne() const { return false; }
2604
David Brazdil77a48ae2015-09-15 12:34:04 +00002605 virtual uint64_t GetValueAsUint64() const = 0;
2606
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002607 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002608
2609 private:
2610 DISALLOW_COPY_AND_ASSIGN(HConstant);
2611};
2612
Vladimir Markofcb503c2016-05-18 12:48:17 +01002613class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002614 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002615 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002616 return true;
2617 }
2618
David Brazdil77a48ae2015-09-15 12:34:04 +00002619 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2620
Roland Levillain9867bc72015-08-05 10:21:34 +01002621 size_t ComputeHashCode() const OVERRIDE { return 0; }
2622
Roland Levillain1a653882016-03-18 18:05:57 +00002623 // The null constant representation is a 0-bit pattern.
2624 virtual bool IsZeroBitPattern() const { return true; }
2625
Roland Levillain9867bc72015-08-05 10:21:34 +01002626 DECLARE_INSTRUCTION(NullConstant);
2627
2628 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002629 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002630
2631 friend class HGraph;
2632 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2633};
2634
2635// Constants of the type int. Those can be from Dex instructions, or
2636// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002637class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002638 public:
2639 int32_t GetValue() const { return value_; }
2640
David Brazdil9f389d42015-10-01 14:32:56 +01002641 uint64_t GetValueAsUint64() const OVERRIDE {
2642 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2643 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002644
Vladimir Marko372f10e2016-05-17 16:30:10 +01002645 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002646 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 return other->AsIntConstant()->value_ == value_;
2648 }
2649
2650 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2651
2652 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002653 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2654 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002655 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2656
Roland Levillain1a653882016-03-18 18:05:57 +00002657 // Integer constants are used to encode Boolean values as well,
2658 // where 1 means true and 0 means false.
2659 bool IsTrue() const { return GetValue() == 1; }
2660 bool IsFalse() const { return GetValue() == 0; }
2661
Roland Levillain9867bc72015-08-05 10:21:34 +01002662 DECLARE_INSTRUCTION(IntConstant);
2663
2664 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2666 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2667 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2668 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002669
2670 const int32_t value_;
2671
2672 friend class HGraph;
2673 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2674 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2675 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2676};
2677
Vladimir Markofcb503c2016-05-18 12:48:17 +01002678class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002679 public:
2680 int64_t GetValue() const { return value_; }
2681
David Brazdil77a48ae2015-09-15 12:34:04 +00002682 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2683
Vladimir Marko372f10e2016-05-17 16:30:10 +01002684 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002685 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 return other->AsLongConstant()->value_ == value_;
2687 }
2688
2689 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2690
2691 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002692 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2693 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2695
2696 DECLARE_INSTRUCTION(LongConstant);
2697
2698 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002699 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2700 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002701
2702 const int64_t value_;
2703
2704 friend class HGraph;
2705 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2706};
Dave Allison20dfc792014-06-16 20:44:29 -07002707
Vladimir Markofcb503c2016-05-18 12:48:17 +01002708class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002709 public:
2710 float GetValue() const { return value_; }
2711
2712 uint64_t GetValueAsUint64() const OVERRIDE {
2713 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2714 }
2715
Vladimir Marko372f10e2016-05-17 16:30:10 +01002716 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002717 DCHECK(other->IsFloatConstant()) << other->DebugName();
2718 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2719 }
2720
2721 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2722
2723 bool IsMinusOne() const OVERRIDE {
2724 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2725 }
Roland Levillain1a653882016-03-18 18:05:57 +00002726 bool IsArithmeticZero() const OVERRIDE {
2727 return std::fpclassify(value_) == FP_ZERO;
2728 }
2729 bool IsArithmeticPositiveZero() const {
2730 return IsArithmeticZero() && !std::signbit(value_);
2731 }
2732 bool IsArithmeticNegativeZero() const {
2733 return IsArithmeticZero() && std::signbit(value_);
2734 }
2735 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002736 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002737 }
2738 bool IsOne() const OVERRIDE {
2739 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2740 }
2741 bool IsNaN() const {
2742 return std::isnan(value_);
2743 }
2744
2745 DECLARE_INSTRUCTION(FloatConstant);
2746
2747 private:
2748 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2749 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2750 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2751 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2752
2753 const float value_;
2754
2755 // Only the SsaBuilder and HGraph can create floating-point constants.
2756 friend class SsaBuilder;
2757 friend class HGraph;
2758 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2759};
2760
Vladimir Markofcb503c2016-05-18 12:48:17 +01002761class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002762 public:
2763 double GetValue() const { return value_; }
2764
2765 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2766
Vladimir Marko372f10e2016-05-17 16:30:10 +01002767 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002768 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2769 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2770 }
2771
2772 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2773
2774 bool IsMinusOne() const OVERRIDE {
2775 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2776 }
Roland Levillain1a653882016-03-18 18:05:57 +00002777 bool IsArithmeticZero() const OVERRIDE {
2778 return std::fpclassify(value_) == FP_ZERO;
2779 }
2780 bool IsArithmeticPositiveZero() const {
2781 return IsArithmeticZero() && !std::signbit(value_);
2782 }
2783 bool IsArithmeticNegativeZero() const {
2784 return IsArithmeticZero() && std::signbit(value_);
2785 }
2786 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002787 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002788 }
2789 bool IsOne() const OVERRIDE {
2790 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2791 }
2792 bool IsNaN() const {
2793 return std::isnan(value_);
2794 }
2795
2796 DECLARE_INSTRUCTION(DoubleConstant);
2797
2798 private:
2799 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2800 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2801 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2802 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2803
2804 const double value_;
2805
2806 // Only the SsaBuilder and HGraph can create floating-point constants.
2807 friend class SsaBuilder;
2808 friend class HGraph;
2809 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2810};
2811
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002812// Conditional branch. A block ending with an HIf instruction must have
2813// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002814class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002816 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2817 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002818 SetRawInputAt(0, input);
2819 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002820
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002821 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002822
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002823 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002824 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002825 }
2826
2827 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002828 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002829 }
2830
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002831 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002832
2833 private:
2834 DISALLOW_COPY_AND_ASSIGN(HIf);
2835};
2836
David Brazdilfc6a86a2015-06-26 10:33:45 +00002837
2838// Abstract instruction which marks the beginning and/or end of a try block and
2839// links it to the respective exception handlers. Behaves the same as a Goto in
2840// non-exceptional control flow.
2841// Normal-flow successor is stored at index zero, exception handlers under
2842// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002843class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002844 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002845 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002846 kEntry,
2847 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002848 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002849 };
2850
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002851 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002852 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2853 SetPackedField<BoundaryKindField>(kind);
2854 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002855
2856 bool IsControlFlow() const OVERRIDE { return true; }
2857
2858 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002859 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002860
David Brazdild26a4112015-11-10 11:07:31 +00002861 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2862 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2863 }
2864
David Brazdilfc6a86a2015-06-26 10:33:45 +00002865 // Returns whether `handler` is among its exception handlers (non-zero index
2866 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002867 bool HasExceptionHandler(const HBasicBlock& handler) const {
2868 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002869 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002870 }
2871
2872 // If not present already, adds `handler` to its block's list of exception
2873 // handlers.
2874 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002875 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002876 GetBlock()->AddSuccessor(handler);
2877 }
2878 }
2879
Vladimir Markoa1de9182016-02-25 11:37:38 +00002880 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2881 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002882
David Brazdilffee3d32015-07-06 11:48:53 +01002883 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2884
David Brazdilfc6a86a2015-06-26 10:33:45 +00002885 DECLARE_INSTRUCTION(TryBoundary);
2886
2887 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002888 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2889 static constexpr size_t kFieldBoundaryKindSize =
2890 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2891 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2892 kFieldBoundaryKind + kFieldBoundaryKindSize;
2893 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2894 "Too many packed fields.");
2895 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002896
2897 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2898};
2899
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002900// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002901class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002902 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002903 // We set CanTriggerGC to prevent any intermediate address to be live
2904 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002905 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002906 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002907 SetRawInputAt(0, cond);
2908 }
2909
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002910 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002911 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002912 return true;
2913 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002914 bool NeedsEnvironment() const OVERRIDE { return true; }
2915 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002916
2917 DECLARE_INSTRUCTION(Deoptimize);
2918
2919 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002920 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2921};
2922
Mingyao Yang063fc772016-08-02 11:02:54 -07002923// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2924// The compiled code checks this flag value in a guard before devirtualized call and
2925// if it's true, starts to do deoptimization.
2926// It has a 4-byte slot on stack.
2927// TODO: allocate a register for this flag.
2928class HShouldDeoptimizeFlag FINAL : public HExpression<0> {
2929 public:
2930 // TODO: use SideEffects to aid eliminating some CHA guards.
2931 explicit HShouldDeoptimizeFlag(uint32_t dex_pc)
2932 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
2933 }
2934
2935 // We don't eliminate CHA guards yet.
2936 bool CanBeMoved() const OVERRIDE { return false; }
2937
2938 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2939
2940 private:
2941 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2942};
2943
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002944// Represents the ArtMethod that was passed as a first argument to
2945// the method. It is used by instructions that depend on it, like
2946// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002947class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002948 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002949 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2950 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002951
2952 DECLARE_INSTRUCTION(CurrentMethod);
2953
2954 private:
2955 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2956};
2957
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002958// Fetches an ArtMethod from the virtual table or the interface method table
2959// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002960class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002961 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002962 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002963 kVTable,
2964 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002965 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002966 };
2967 HClassTableGet(HInstruction* cls,
2968 Primitive::Type type,
2969 TableKind kind,
2970 size_t index,
2971 uint32_t dex_pc)
2972 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002973 index_(index) {
2974 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002975 SetRawInputAt(0, cls);
2976 }
2977
2978 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002979 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002980 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002981 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002982 }
2983
Vladimir Markoa1de9182016-02-25 11:37:38 +00002984 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002985 size_t GetIndex() const { return index_; }
2986
2987 DECLARE_INSTRUCTION(ClassTableGet);
2988
2989 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002990 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2991 static constexpr size_t kFieldTableKindSize =
2992 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2993 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2994 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2995 "Too many packed fields.");
2996 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2997
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002998 // The index of the ArtMethod in the table.
2999 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003000
3001 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3002};
3003
Mark Mendellfe57faa2015-09-18 09:26:15 -04003004// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3005// have one successor for each entry in the switch table, and the final successor
3006// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003007class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003008 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003009 HPackedSwitch(int32_t start_value,
3010 uint32_t num_entries,
3011 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003012 uint32_t dex_pc = kNoDexPc)
3013 : HTemplateInstruction(SideEffects::None(), dex_pc),
3014 start_value_(start_value),
3015 num_entries_(num_entries) {
3016 SetRawInputAt(0, input);
3017 }
3018
3019 bool IsControlFlow() const OVERRIDE { return true; }
3020
3021 int32_t GetStartValue() const { return start_value_; }
3022
Vladimir Marko211c2112015-09-24 16:52:33 +01003023 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003024
3025 HBasicBlock* GetDefaultBlock() const {
3026 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003027 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003028 }
3029 DECLARE_INSTRUCTION(PackedSwitch);
3030
3031 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003032 const int32_t start_value_;
3033 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003034
3035 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3036};
3037
Roland Levillain88cb1752014-10-20 16:36:47 +01003038class HUnaryOperation : public HExpression<1> {
3039 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003040 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3041 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003042 SetRawInputAt(0, input);
3043 }
3044
3045 HInstruction* GetInput() const { return InputAt(0); }
3046 Primitive::Type GetResultType() const { return GetType(); }
3047
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003048 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003049 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003050 return true;
3051 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003052
Roland Levillain31dd3d62016-02-16 12:21:02 +00003053 // Try to statically evaluate `this` and return a HConstant
3054 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003055 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003056 HConstant* TryStaticEvaluation() const;
3057
3058 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003059 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3060 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003061 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3062 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003063
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003064 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003065
3066 private:
3067 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3068};
3069
Dave Allison20dfc792014-06-16 20:44:29 -07003070class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003071 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003072 HBinaryOperation(Primitive::Type result_type,
3073 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003074 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003075 SideEffects side_effects = SideEffects::None(),
3076 uint32_t dex_pc = kNoDexPc)
3077 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078 SetRawInputAt(0, left);
3079 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003080 }
3081
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003082 HInstruction* GetLeft() const { return InputAt(0); }
3083 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003084 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003085
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003086 virtual bool IsCommutative() const { return false; }
3087
3088 // Put constant on the right.
3089 // Returns whether order is changed.
3090 bool OrderInputsWithConstantOnTheRight() {
3091 HInstruction* left = InputAt(0);
3092 HInstruction* right = InputAt(1);
3093 if (left->IsConstant() && !right->IsConstant()) {
3094 ReplaceInput(right, 0);
3095 ReplaceInput(left, 1);
3096 return true;
3097 }
3098 return false;
3099 }
3100
3101 // Order inputs by instruction id, but favor constant on the right side.
3102 // This helps GVN for commutative ops.
3103 void OrderInputs() {
3104 DCHECK(IsCommutative());
3105 HInstruction* left = InputAt(0);
3106 HInstruction* right = InputAt(1);
3107 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3108 return;
3109 }
3110 if (OrderInputsWithConstantOnTheRight()) {
3111 return;
3112 }
3113 // Order according to instruction id.
3114 if (left->GetId() > right->GetId()) {
3115 ReplaceInput(right, 0);
3116 ReplaceInput(left, 1);
3117 }
3118 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003119
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003120 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003121 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003122 return true;
3123 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003124
Roland Levillain31dd3d62016-02-16 12:21:02 +00003125 // Try to statically evaluate `this` and return a HConstant
3126 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003127 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003128 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003129
3130 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003131 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3132 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003133 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3134 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003135 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003136 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3137 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003138 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3139 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003140 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3141 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003142 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003143 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3144 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003145
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003146 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003147 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003148 HConstant* GetConstantRight() const;
3149
3150 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003151 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003152 HInstruction* GetLeastConstantLeft() const;
3153
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003154 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003155
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003156 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003157 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3158};
3159
Mark Mendellc4701932015-04-10 13:18:51 -04003160// The comparison bias applies for floating point operations and indicates how NaN
3161// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003162enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003163 kNoBias, // bias is not applicable (i.e. for long operation)
3164 kGtBias, // return 1 for NaN comparisons
3165 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003166 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003167};
3168
Roland Levillain31dd3d62016-02-16 12:21:02 +00003169std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3170
Dave Allison20dfc792014-06-16 20:44:29 -07003171class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003172 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003173 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003174 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3175 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3176 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003177
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003178 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003179 // `instruction`, and disregard moves in between.
3180 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003181
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003182 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003183
3184 virtual IfCondition GetCondition() const = 0;
3185
Mark Mendellc4701932015-04-10 13:18:51 -04003186 virtual IfCondition GetOppositeCondition() const = 0;
3187
Vladimir Markoa1de9182016-02-25 11:37:38 +00003188 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003189 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3190
Vladimir Markoa1de9182016-02-25 11:37:38 +00003191 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3192 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003193
Vladimir Marko372f10e2016-05-17 16:30:10 +01003194 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003195 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003196 }
3197
Roland Levillain4fa13f62015-07-06 18:11:54 +01003198 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003200 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003201 if (if_cond == kCondNE) {
3202 return true;
3203 } else if (if_cond == kCondEQ) {
3204 return false;
3205 }
3206 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003207 }
3208
3209 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003211 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003212 if (if_cond == kCondEQ) {
3213 return true;
3214 } else if (if_cond == kCondNE) {
3215 return false;
3216 }
3217 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003218 }
3219
Roland Levillain31dd3d62016-02-16 12:21:02 +00003220 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003221 // Needed if we merge a HCompare into a HCondition.
3222 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3223 static constexpr size_t kFieldComparisonBiasSize =
3224 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3225 static constexpr size_t kNumberOfConditionPackedBits =
3226 kFieldComparisonBias + kFieldComparisonBiasSize;
3227 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3228 using ComparisonBiasField =
3229 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3230
Roland Levillain31dd3d62016-02-16 12:21:02 +00003231 template <typename T>
3232 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3233
3234 template <typename T>
3235 int32_t CompareFP(T x, T y) const {
3236 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3237 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3238 // Handle the bias.
3239 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3240 }
3241
3242 // Return an integer constant containing the result of a condition evaluated at compile time.
3243 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3244 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3245 }
3246
Dave Allison20dfc792014-06-16 20:44:29 -07003247 private:
3248 DISALLOW_COPY_AND_ASSIGN(HCondition);
3249};
3250
3251// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003252class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003253 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003254 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3255 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003256
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003257 bool IsCommutative() const OVERRIDE { return true; }
3258
Vladimir Marko9e23df52015-11-10 17:14:35 +00003259 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3260 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003261 return MakeConstantCondition(true, GetDexPc());
3262 }
3263 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3264 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3265 }
3266 // In the following Evaluate methods, a HCompare instruction has
3267 // been merged into this HEqual instruction; evaluate it as
3268 // `Compare(x, y) == 0`.
3269 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3270 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3271 GetDexPc());
3272 }
3273 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3274 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3275 }
3276 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3277 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003278 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003279
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003280 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003281
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003282 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003283 return kCondEQ;
3284 }
3285
Mark Mendellc4701932015-04-10 13:18:51 -04003286 IfCondition GetOppositeCondition() const OVERRIDE {
3287 return kCondNE;
3288 }
3289
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003290 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003291 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003292
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003293 DISALLOW_COPY_AND_ASSIGN(HEqual);
3294};
3295
Vladimir Markofcb503c2016-05-18 12:48:17 +01003296class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003298 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3299 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003300
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003301 bool IsCommutative() const OVERRIDE { return true; }
3302
Vladimir Marko9e23df52015-11-10 17:14:35 +00003303 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3304 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003305 return MakeConstantCondition(false, GetDexPc());
3306 }
3307 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3308 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3309 }
3310 // In the following Evaluate methods, a HCompare instruction has
3311 // been merged into this HNotEqual instruction; evaluate it as
3312 // `Compare(x, y) != 0`.
3313 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3314 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3315 }
3316 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3317 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3318 }
3319 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3320 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003321 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003322
Dave Allison20dfc792014-06-16 20:44:29 -07003323 DECLARE_INSTRUCTION(NotEqual);
3324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003325 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003326 return kCondNE;
3327 }
3328
Mark Mendellc4701932015-04-10 13:18:51 -04003329 IfCondition GetOppositeCondition() const OVERRIDE {
3330 return kCondEQ;
3331 }
3332
Dave Allison20dfc792014-06-16 20:44:29 -07003333 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003334 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003335
Dave Allison20dfc792014-06-16 20:44:29 -07003336 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3337};
3338
Vladimir Markofcb503c2016-05-18 12:48:17 +01003339class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003340 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003341 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3342 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003343
Roland Levillain9867bc72015-08-05 10:21:34 +01003344 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003345 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003346 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 // In the following Evaluate methods, a HCompare instruction has
3348 // been merged into this HLessThan instruction; evaluate it as
3349 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003350 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003351 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3352 }
3353 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3354 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3355 }
3356 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3357 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003358 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003359
Dave Allison20dfc792014-06-16 20:44:29 -07003360 DECLARE_INSTRUCTION(LessThan);
3361
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003362 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003363 return kCondLT;
3364 }
3365
Mark Mendellc4701932015-04-10 13:18:51 -04003366 IfCondition GetOppositeCondition() const OVERRIDE {
3367 return kCondGE;
3368 }
3369
Dave Allison20dfc792014-06-16 20:44:29 -07003370 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003371 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003372
Dave Allison20dfc792014-06-16 20:44:29 -07003373 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3374};
3375
Vladimir Markofcb503c2016-05-18 12:48:17 +01003376class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003377 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003378 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3379 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003380
Roland Levillain9867bc72015-08-05 10:21:34 +01003381 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003382 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003383 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 // In the following Evaluate methods, a HCompare instruction has
3385 // been merged into this HLessThanOrEqual instruction; evaluate it as
3386 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003387 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3389 }
3390 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3391 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3392 }
3393 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3394 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003395 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003396
Dave Allison20dfc792014-06-16 20:44:29 -07003397 DECLARE_INSTRUCTION(LessThanOrEqual);
3398
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003399 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003400 return kCondLE;
3401 }
3402
Mark Mendellc4701932015-04-10 13:18:51 -04003403 IfCondition GetOppositeCondition() const OVERRIDE {
3404 return kCondGT;
3405 }
3406
Dave Allison20dfc792014-06-16 20:44:29 -07003407 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003408 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003409
Dave Allison20dfc792014-06-16 20:44:29 -07003410 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3411};
3412
Vladimir Markofcb503c2016-05-18 12:48:17 +01003413class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003414 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003415 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3416 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003417
Roland Levillain9867bc72015-08-05 10:21:34 +01003418 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003419 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003420 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 // In the following Evaluate methods, a HCompare instruction has
3422 // been merged into this HGreaterThan instruction; evaluate it as
3423 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003425 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3426 }
3427 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3428 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3429 }
3430 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3431 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003432 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003433
Dave Allison20dfc792014-06-16 20:44:29 -07003434 DECLARE_INSTRUCTION(GreaterThan);
3435
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003436 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003437 return kCondGT;
3438 }
3439
Mark Mendellc4701932015-04-10 13:18:51 -04003440 IfCondition GetOppositeCondition() const OVERRIDE {
3441 return kCondLE;
3442 }
3443
Dave Allison20dfc792014-06-16 20:44:29 -07003444 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003445 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003446
Dave Allison20dfc792014-06-16 20:44:29 -07003447 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3448};
3449
Vladimir Markofcb503c2016-05-18 12:48:17 +01003450class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003451 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003452 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3453 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003454
Roland Levillain9867bc72015-08-05 10:21:34 +01003455 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003456 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003457 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 // In the following Evaluate methods, a HCompare instruction has
3459 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3460 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3463 }
3464 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3465 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3466 }
3467 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3468 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003469 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003470
Dave Allison20dfc792014-06-16 20:44:29 -07003471 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003473 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003474 return kCondGE;
3475 }
3476
Mark Mendellc4701932015-04-10 13:18:51 -04003477 IfCondition GetOppositeCondition() const OVERRIDE {
3478 return kCondLT;
3479 }
3480
Dave Allison20dfc792014-06-16 20:44:29 -07003481 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003482 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3485};
3486
Vladimir Markofcb503c2016-05-18 12:48:17 +01003487class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003488 public:
3489 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3490 : HCondition(first, second, dex_pc) {}
3491
3492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003494 }
3495 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3497 }
3498 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3499 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3500 LOG(FATAL) << DebugName() << " is not defined for float values";
3501 UNREACHABLE();
3502 }
3503 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3504 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3505 LOG(FATAL) << DebugName() << " is not defined for double values";
3506 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003507 }
3508
3509 DECLARE_INSTRUCTION(Below);
3510
3511 IfCondition GetCondition() const OVERRIDE {
3512 return kCondB;
3513 }
3514
3515 IfCondition GetOppositeCondition() const OVERRIDE {
3516 return kCondAE;
3517 }
3518
3519 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003520 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003521 return MakeUnsigned(x) < MakeUnsigned(y);
3522 }
Aart Bike9f37602015-10-09 11:15:55 -07003523
3524 DISALLOW_COPY_AND_ASSIGN(HBelow);
3525};
3526
Vladimir Markofcb503c2016-05-18 12:48:17 +01003527class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003528 public:
3529 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3530 : HCondition(first, second, dex_pc) {}
3531
3532 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003534 }
3535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3537 }
3538 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3539 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3540 LOG(FATAL) << DebugName() << " is not defined for float values";
3541 UNREACHABLE();
3542 }
3543 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3544 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3545 LOG(FATAL) << DebugName() << " is not defined for double values";
3546 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003547 }
3548
3549 DECLARE_INSTRUCTION(BelowOrEqual);
3550
3551 IfCondition GetCondition() const OVERRIDE {
3552 return kCondBE;
3553 }
3554
3555 IfCondition GetOppositeCondition() const OVERRIDE {
3556 return kCondA;
3557 }
3558
3559 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003560 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003561 return MakeUnsigned(x) <= MakeUnsigned(y);
3562 }
Aart Bike9f37602015-10-09 11:15:55 -07003563
3564 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3565};
3566
Vladimir Markofcb503c2016-05-18 12:48:17 +01003567class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003568 public:
3569 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3570 : HCondition(first, second, dex_pc) {}
3571
3572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003574 }
3575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3577 }
3578 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3579 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3580 LOG(FATAL) << DebugName() << " is not defined for float values";
3581 UNREACHABLE();
3582 }
3583 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3584 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3585 LOG(FATAL) << DebugName() << " is not defined for double values";
3586 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003587 }
3588
3589 DECLARE_INSTRUCTION(Above);
3590
3591 IfCondition GetCondition() const OVERRIDE {
3592 return kCondA;
3593 }
3594
3595 IfCondition GetOppositeCondition() const OVERRIDE {
3596 return kCondBE;
3597 }
3598
3599 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003600 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003601 return MakeUnsigned(x) > MakeUnsigned(y);
3602 }
Aart Bike9f37602015-10-09 11:15:55 -07003603
3604 DISALLOW_COPY_AND_ASSIGN(HAbove);
3605};
3606
Vladimir Markofcb503c2016-05-18 12:48:17 +01003607class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003608 public:
3609 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3610 : HCondition(first, second, dex_pc) {}
3611
3612 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003613 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003614 }
3615 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3617 }
3618 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3619 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3620 LOG(FATAL) << DebugName() << " is not defined for float values";
3621 UNREACHABLE();
3622 }
3623 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3624 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3625 LOG(FATAL) << DebugName() << " is not defined for double values";
3626 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003627 }
3628
3629 DECLARE_INSTRUCTION(AboveOrEqual);
3630
3631 IfCondition GetCondition() const OVERRIDE {
3632 return kCondAE;
3633 }
3634
3635 IfCondition GetOppositeCondition() const OVERRIDE {
3636 return kCondB;
3637 }
3638
3639 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003640 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 return MakeUnsigned(x) >= MakeUnsigned(y);
3642 }
Aart Bike9f37602015-10-09 11:15:55 -07003643
3644 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3645};
Dave Allison20dfc792014-06-16 20:44:29 -07003646
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003647// Instruction to check how two inputs compare to each other.
3648// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003649class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003650 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003651 // Note that `comparison_type` is the type of comparison performed
3652 // between the comparison's inputs, not the type of the instantiated
3653 // HCompare instruction (which is always Primitive::kPrimInt).
3654 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003655 HInstruction* first,
3656 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003657 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003658 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003659 : HBinaryOperation(Primitive::kPrimInt,
3660 first,
3661 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003662 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003663 dex_pc) {
3664 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003665 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3666 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003667 }
3668
Roland Levillain9867bc72015-08-05 10:21:34 +01003669 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003670 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3671
3672 template <typename T>
3673 int32_t ComputeFP(T x, T y) const {
3674 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3675 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3676 // Handle the bias.
3677 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3678 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003679
Roland Levillain9867bc72015-08-05 10:21:34 +01003680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3682 // reach this code path when processing a freshly built HIR
3683 // graph. However HCompare integer instructions can be synthesized
3684 // by the instruction simplifier to implement IntegerCompare and
3685 // IntegerSignum intrinsics, so we have to handle this case.
3686 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003687 }
3688 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003689 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3690 }
3691 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3692 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3693 }
3694 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3695 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003696 }
3697
Vladimir Marko372f10e2016-05-17 16:30:10 +01003698 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003699 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003700 }
3701
Vladimir Markoa1de9182016-02-25 11:37:38 +00003702 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003703
Roland Levillain31dd3d62016-02-16 12:21:02 +00003704 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003705 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003706 bool IsGtBias() const {
3707 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003708 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003710
Roland Levillain1693a1f2016-03-15 14:57:31 +00003711 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3712 // Comparisons do not require a runtime call in any back end.
3713 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003714 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003715
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003716 DECLARE_INSTRUCTION(Compare);
3717
Roland Levillain31dd3d62016-02-16 12:21:02 +00003718 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003719 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3720 static constexpr size_t kFieldComparisonBiasSize =
3721 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3722 static constexpr size_t kNumberOfComparePackedBits =
3723 kFieldComparisonBias + kFieldComparisonBiasSize;
3724 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3725 using ComparisonBiasField =
3726 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3727
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 // Return an integer constant containing the result of a comparison evaluated at compile time.
3729 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3730 DCHECK(value == -1 || value == 0 || value == 1) << value;
3731 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3732 }
3733
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003734 private:
3735 DISALLOW_COPY_AND_ASSIGN(HCompare);
3736};
3737
Vladimir Markofcb503c2016-05-18 12:48:17 +01003738class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003739 public:
3740 HNewInstance(HInstruction* cls,
3741 HCurrentMethod* current_method,
3742 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003743 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003744 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003745 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003746 bool finalizable,
3747 QuickEntrypointEnum entrypoint)
3748 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3749 type_index_(type_index),
3750 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003751 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003752 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003753 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003754 SetRawInputAt(0, cls);
3755 SetRawInputAt(1, current_method);
3756 }
3757
Andreas Gampea5b09a62016-11-17 15:21:22 -08003758 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003759 const DexFile& GetDexFile() const { return dex_file_; }
3760
3761 // Calls runtime so needs an environment.
3762 bool NeedsEnvironment() const OVERRIDE { return true; }
3763
Mingyao Yang062157f2016-03-02 10:15:36 -08003764 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3765 bool CanThrow() const OVERRIDE { return true; }
3766
3767 // Needs to call into runtime to make sure it's instantiable/accessible.
3768 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003769
Vladimir Markoa1de9182016-02-25 11:37:38 +00003770 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003771
3772 bool CanBeNull() const OVERRIDE { return false; }
3773
3774 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3775
3776 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3777 entrypoint_ = entrypoint;
3778 }
3779
3780 bool IsStringAlloc() const;
3781
3782 DECLARE_INSTRUCTION(NewInstance);
3783
3784 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003785 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3786 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003787 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3788 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3789 "Too many packed fields.");
3790
Andreas Gampea5b09a62016-11-17 15:21:22 -08003791 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003792 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003793 QuickEntrypointEnum entrypoint_;
3794
3795 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3796};
3797
Agi Csaki05f20562015-08-19 14:58:14 -07003798enum IntrinsicNeedsEnvironmentOrCache {
3799 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3800 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003801};
3802
Aart Bik5d75afe2015-12-14 11:57:01 -08003803enum IntrinsicSideEffects {
3804 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3805 kReadSideEffects, // Intrinsic may read heap memory.
3806 kWriteSideEffects, // Intrinsic may write heap memory.
3807 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3808};
3809
3810enum IntrinsicExceptions {
3811 kNoThrow, // Intrinsic does not throw any exceptions.
3812 kCanThrow // Intrinsic may throw exceptions.
3813};
3814
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003815class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003816 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003817 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003818
Vladimir Marko372f10e2016-05-17 16:30:10 +01003819 using HInstruction::GetInputRecords; // Keep the const version visible.
3820 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3821 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3822 }
3823
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003824 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003825 SetRawInputAt(index, argument);
3826 }
3827
Roland Levillain3e3d7332015-04-28 11:00:54 +01003828 // Return the number of arguments. This number can be lower than
3829 // the number of inputs returned by InputCount(), as some invoke
3830 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3831 // inputs at the end of their list of inputs.
3832 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3833
Vladimir Markoa1de9182016-02-25 11:37:38 +00003834 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003835
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003836 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003837 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003838
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003839 InvokeType GetInvokeType() const {
3840 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003841 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003842
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003843 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003844 return intrinsic_;
3845 }
3846
Aart Bik5d75afe2015-12-14 11:57:01 -08003847 void SetIntrinsic(Intrinsics intrinsic,
3848 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3849 IntrinsicSideEffects side_effects,
3850 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003851
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003852 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003853 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003854 }
3855
Aart Bikff7d89c2016-11-07 08:49:28 -08003856 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3857
Vladimir Markoa1de9182016-02-25 11:37:38 +00003858 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003859
Aart Bik71bf7b42016-11-16 10:17:46 -08003860 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003861
Vladimir Marko372f10e2016-05-17 16:30:10 +01003862 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003863 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3864 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003865
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003866 uint32_t* GetIntrinsicOptimizations() {
3867 return &intrinsic_optimizations_;
3868 }
3869
3870 const uint32_t* GetIntrinsicOptimizations() const {
3871 return &intrinsic_optimizations_;
3872 }
3873
3874 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3875
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003876 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3877
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003878 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003879
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003880 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003881 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3882 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003883 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3884 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003885 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003886 static constexpr size_t kFieldReturnTypeSize =
3887 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3888 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3889 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3890 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003891 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3893
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003894 HInvoke(ArenaAllocator* arena,
3895 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003896 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003897 Primitive::Type return_type,
3898 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003899 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003900 ArtMethod* resolved_method,
3901 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003902 : HVariableInputSizeInstruction(
3903 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3904 dex_pc,
3905 arena,
3906 number_of_arguments + number_of_other_inputs,
3907 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003908 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003909 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003910 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003911 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003912 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003913 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003914 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003915 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003916 }
3917
Roland Levillain3e3d7332015-04-28 11:00:54 +01003918 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003919 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003920 const uint32_t dex_method_index_;
3921 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003922
3923 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3924 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003925
3926 private:
3927 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3928};
3929
Vladimir Markofcb503c2016-05-18 12:48:17 +01003930class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003931 public:
3932 HInvokeUnresolved(ArenaAllocator* arena,
3933 uint32_t number_of_arguments,
3934 Primitive::Type return_type,
3935 uint32_t dex_pc,
3936 uint32_t dex_method_index,
3937 InvokeType invoke_type)
3938 : HInvoke(arena,
3939 number_of_arguments,
3940 0u /* number_of_other_inputs */,
3941 return_type,
3942 dex_pc,
3943 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003944 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003945 invoke_type) {
3946 }
3947
3948 DECLARE_INSTRUCTION(InvokeUnresolved);
3949
3950 private:
3951 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3952};
3953
Vladimir Markofcb503c2016-05-18 12:48:17 +01003954class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003955 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003956 // Requirements of this method call regarding the class
3957 // initialization (clinit) check of its declaring class.
3958 enum class ClinitCheckRequirement {
3959 kNone, // Class already initialized.
3960 kExplicit, // Static call having explicit clinit check as last input.
3961 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003962 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003963 };
3964
Vladimir Marko58155012015-08-19 12:49:41 +00003965 // Determines how to load the target ArtMethod*.
3966 enum class MethodLoadKind {
3967 // Use a String init ArtMethod* loaded from Thread entrypoints.
3968 kStringInit,
3969
3970 // Use the method's own ArtMethod* loaded by the register allocator.
3971 kRecursive,
3972
3973 // Use ArtMethod* at a known address, embed the direct address in the code.
3974 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3975 kDirectAddress,
3976
3977 // Use ArtMethod* at an address that will be known at link time, embed the direct
3978 // address in the code. If the image is relocatable, emit .patch_oat entry.
3979 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3980 // the image relocatable or not.
3981 kDirectAddressWithFixup,
3982
Vladimir Markoa1de9182016-02-25 11:37:38 +00003983 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003984 // Used when we need to use the dex cache, for example for invoke-static that
3985 // may cause class initialization (the entry may point to a resolution method),
3986 // and we know that we can access the dex cache arrays using a PC-relative load.
3987 kDexCachePcRelative,
3988
3989 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3990 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3991 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3992 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3993 kDexCacheViaMethod,
3994 };
3995
3996 // Determines the location of the code pointer.
3997 enum class CodePtrLocation {
3998 // Recursive call, use local PC-relative call instruction.
3999 kCallSelf,
4000
4001 // Use PC-relative call instruction patched at link time.
4002 // Used for calls within an oat file, boot->boot or app->app.
4003 kCallPCRelative,
4004
4005 // Call to a known target address, embed the direct address in code.
4006 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
4007 kCallDirect,
4008
4009 // Call to a target address that will be known at link time, embed the direct
4010 // address in code. If the image is relocatable, emit .patch_oat entry.
4011 // Used for app->boot calls with relocatable image and boot->boot calls, whether
4012 // the image relocatable or not.
4013 kCallDirectWithFixup,
4014
4015 // Use code pointer from the ArtMethod*.
4016 // Used when we don't know the target code. This is also the last-resort-kind used when
4017 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4018 kCallArtMethod,
4019 };
4020
4021 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004022 MethodLoadKind method_load_kind;
4023 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004024 // The method load data holds
4025 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4026 // Note that there are multiple string init methods, each having its own offset.
4027 // - the method address for kDirectAddress
4028 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004029 uint64_t method_load_data;
4030 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00004031 };
4032
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004033 HInvokeStaticOrDirect(ArenaAllocator* arena,
4034 uint32_t number_of_arguments,
4035 Primitive::Type return_type,
4036 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004037 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004038 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004039 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004040 InvokeType invoke_type,
4041 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004042 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004043 : HInvoke(arena,
4044 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004045 // There is potentially one extra argument for the HCurrentMethod node, and
4046 // potentially one other if the clinit check is explicit, and potentially
4047 // one other if the method is a string factory.
4048 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004049 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004050 return_type,
4051 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004052 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004053 resolved_method,
4054 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004055 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004056 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004057 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4058 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004059
Vladimir Markodc151b22015-10-15 18:02:30 +01004060 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004061 bool had_current_method_input = HasCurrentMethodInput();
4062 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4063
4064 // Using the current method is the default and once we find a better
4065 // method load kind, we should not go back to using the current method.
4066 DCHECK(had_current_method_input || !needs_current_method_input);
4067
4068 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004069 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4070 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004071 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004072 dispatch_info_ = dispatch_info;
4073 }
4074
Vladimir Markoc53c0792015-11-19 15:48:33 +00004075 void AddSpecialInput(HInstruction* input) {
4076 // We allow only one special input.
4077 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4078 DCHECK(InputCount() == GetSpecialInputIndex() ||
4079 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4080 InsertInputAt(GetSpecialInputIndex(), input);
4081 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004082
Vladimir Marko372f10e2016-05-17 16:30:10 +01004083 using HInstruction::GetInputRecords; // Keep the const version visible.
4084 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4085 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4086 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4087 DCHECK(!input_records.empty());
4088 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4089 HInstruction* last_input = input_records.back().GetInstruction();
4090 // Note: `last_input` may be null during arguments setup.
4091 if (last_input != nullptr) {
4092 // `last_input` is the last input of a static invoke marked as having
4093 // an explicit clinit check. It must either be:
4094 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4095 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4096 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4097 }
4098 }
4099 return input_records;
4100 }
4101
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004102 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004103 // We access the method via the dex cache so we can't do an implicit null check.
4104 // TODO: for intrinsics we can generate implicit null checks.
4105 return false;
4106 }
4107
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004108 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004109 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004110 }
4111
Vladimir Markoc53c0792015-11-19 15:48:33 +00004112 // Get the index of the special input, if any.
4113 //
David Brazdil6de19382016-01-08 17:37:10 +00004114 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4115 // method pointer; otherwise there may be one platform-specific special input,
4116 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004117 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004118 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004119
Vladimir Marko58155012015-08-19 12:49:41 +00004120 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4121 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4122 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004123 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004124 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004125 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004126 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004127 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4128 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004129 bool HasCurrentMethodInput() const {
4130 // This function can be called only after the invoke has been fully initialized by the builder.
4131 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004132 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004133 return true;
4134 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004135 DCHECK(InputCount() == GetSpecialInputIndex() ||
4136 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004137 return false;
4138 }
4139 }
Vladimir Marko58155012015-08-19 12:49:41 +00004140 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004141
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004142 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004143 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004144 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004145 }
4146
4147 uint64_t GetMethodAddress() const {
4148 DCHECK(HasMethodAddress());
4149 return dispatch_info_.method_load_data;
4150 }
4151
4152 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004153 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004154 return dispatch_info_.method_load_data;
4155 }
4156
4157 uint64_t GetDirectCodePtr() const {
4158 DCHECK(HasDirectCodePtr());
4159 return dispatch_info_.direct_code_ptr;
4160 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004161
Vladimir Markoa1de9182016-02-25 11:37:38 +00004162 ClinitCheckRequirement GetClinitCheckRequirement() const {
4163 return GetPackedField<ClinitCheckRequirementField>();
4164 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004165
Roland Levillain4c0eb422015-04-24 16:43:49 +01004166 // Is this instruction a call to a static method?
4167 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004168 return GetInvokeType() == kStatic;
4169 }
4170
4171 MethodReference GetTargetMethod() const {
4172 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004173 }
4174
Vladimir Markofbb184a2015-11-13 14:47:00 +00004175 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4176 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4177 // instruction; only relevant for static calls with explicit clinit check.
4178 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004179 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004180 size_t last_input_index = inputs_.size() - 1u;
4181 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004182 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004183 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004184 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004185 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004186 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004187 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004188 }
4189
4190 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004191 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004192 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004194 }
4195
4196 // Is this a call to a static method whose declaring class has an
4197 // implicit intialization check requirement?
4198 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004200 }
4201
Vladimir Markob554b5a2015-11-06 12:57:55 +00004202 // Does this method load kind need the current method as an input?
4203 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4204 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4205 }
4206
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004207 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004208
4209 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004210 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004211 static constexpr size_t kFieldClinitCheckRequirementSize =
4212 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4213 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4214 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4215 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4216 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004217 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4218 kFieldClinitCheckRequirement,
4219 kFieldClinitCheckRequirementSize>;
4220
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004221 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004222 MethodReference target_method_;
4223 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004225 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004226};
Vladimir Markof64242a2015-12-01 14:58:23 +00004227std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004228std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004229
Vladimir Markofcb503c2016-05-18 12:48:17 +01004230class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004231 public:
4232 HInvokeVirtual(ArenaAllocator* arena,
4233 uint32_t number_of_arguments,
4234 Primitive::Type return_type,
4235 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004236 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004237 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004238 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004239 : HInvoke(arena,
4240 number_of_arguments,
4241 0u,
4242 return_type,
4243 dex_pc,
4244 dex_method_index,
4245 resolved_method,
4246 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004247 vtable_index_(vtable_index) {}
4248
Aart Bik71bf7b42016-11-16 10:17:46 -08004249 bool CanBeNull() const OVERRIDE {
4250 switch (GetIntrinsic()) {
4251 case Intrinsics::kThreadCurrentThread:
4252 case Intrinsics::kStringBufferAppend:
4253 case Intrinsics::kStringBufferToString:
4254 case Intrinsics::kStringBuilderAppend:
4255 case Intrinsics::kStringBuilderToString:
4256 return false;
4257 default:
4258 return HInvoke::CanBeNull();
4259 }
4260 }
4261
Calin Juravle641547a2015-04-21 22:08:51 +01004262 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004263 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004264 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004265 }
4266
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004267 uint32_t GetVTableIndex() const { return vtable_index_; }
4268
4269 DECLARE_INSTRUCTION(InvokeVirtual);
4270
4271 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004272 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004273 const uint32_t vtable_index_;
4274
4275 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4276};
4277
Vladimir Markofcb503c2016-05-18 12:48:17 +01004278class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004279 public:
4280 HInvokeInterface(ArenaAllocator* arena,
4281 uint32_t number_of_arguments,
4282 Primitive::Type return_type,
4283 uint32_t dex_pc,
4284 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004285 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004286 uint32_t imt_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 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004295 imt_index_(imt_index) {}
4296
Calin Juravle641547a2015-04-21 22:08:51 +01004297 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004298 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004299 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004300 }
4301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004302 uint32_t GetImtIndex() const { return imt_index_; }
4303 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4304
4305 DECLARE_INSTRUCTION(InvokeInterface);
4306
4307 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004308 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004309 const uint32_t imt_index_;
4310
4311 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4312};
4313
Vladimir Markofcb503c2016-05-18 12:48:17 +01004314class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004315 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004316 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004317 : HUnaryOperation(result_type, input, dex_pc) {
4318 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4319 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004320
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004321 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004322
4323 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004324 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004325 }
4326 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004327 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004328 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004329 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4330 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4331 }
4332 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4333 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4334 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004335
Roland Levillain88cb1752014-10-20 16:36:47 +01004336 DECLARE_INSTRUCTION(Neg);
4337
4338 private:
4339 DISALLOW_COPY_AND_ASSIGN(HNeg);
4340};
4341
Vladimir Markofcb503c2016-05-18 12:48:17 +01004342class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004343 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004344 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004345 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004346 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004347 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004348 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004349 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004350 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004351 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004352 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004353 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004354 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004355 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004356 }
4357
Andreas Gampea5b09a62016-11-17 15:21:22 -08004358 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004359 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004360
4361 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004362 bool NeedsEnvironment() const OVERRIDE { return true; }
4363
Mingyao Yang0c365e62015-03-31 15:09:29 -07004364 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4365 bool CanThrow() const OVERRIDE { return true; }
4366
Calin Juravle10e244f2015-01-26 18:54:32 +00004367 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004368
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004369 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4370
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004371 DECLARE_INSTRUCTION(NewArray);
4372
4373 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004374 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004375 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004376 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004377
4378 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4379};
4380
Vladimir Markofcb503c2016-05-18 12:48:17 +01004381class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004382 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004383 HAdd(Primitive::Type result_type,
4384 HInstruction* left,
4385 HInstruction* right,
4386 uint32_t dex_pc = kNoDexPc)
4387 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004388
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004389 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004390
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004391 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004392
4393 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004394 return GetBlock()->GetGraph()->GetIntConstant(
4395 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004396 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004397 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004398 return GetBlock()->GetGraph()->GetLongConstant(
4399 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004400 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004401 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4402 return GetBlock()->GetGraph()->GetFloatConstant(
4403 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4404 }
4405 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4406 return GetBlock()->GetGraph()->GetDoubleConstant(
4407 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4408 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004409
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004410 DECLARE_INSTRUCTION(Add);
4411
4412 private:
4413 DISALLOW_COPY_AND_ASSIGN(HAdd);
4414};
4415
Vladimir Markofcb503c2016-05-18 12:48:17 +01004416class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004417 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004418 HSub(Primitive::Type result_type,
4419 HInstruction* left,
4420 HInstruction* right,
4421 uint32_t dex_pc = kNoDexPc)
4422 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004423
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004424 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004425
4426 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004427 return GetBlock()->GetGraph()->GetIntConstant(
4428 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004429 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004430 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004431 return GetBlock()->GetGraph()->GetLongConstant(
4432 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004433 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004434 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4435 return GetBlock()->GetGraph()->GetFloatConstant(
4436 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4437 }
4438 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4439 return GetBlock()->GetGraph()->GetDoubleConstant(
4440 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4441 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004442
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004443 DECLARE_INSTRUCTION(Sub);
4444
4445 private:
4446 DISALLOW_COPY_AND_ASSIGN(HSub);
4447};
4448
Vladimir Markofcb503c2016-05-18 12:48:17 +01004449class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004450 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004451 HMul(Primitive::Type result_type,
4452 HInstruction* left,
4453 HInstruction* right,
4454 uint32_t dex_pc = kNoDexPc)
4455 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004456
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004457 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004458
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004459 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004460
4461 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004462 return GetBlock()->GetGraph()->GetIntConstant(
4463 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004464 }
4465 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 return GetBlock()->GetGraph()->GetLongConstant(
4467 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004468 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004469 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4470 return GetBlock()->GetGraph()->GetFloatConstant(
4471 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4472 }
4473 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4474 return GetBlock()->GetGraph()->GetDoubleConstant(
4475 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4476 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004477
4478 DECLARE_INSTRUCTION(Mul);
4479
4480 private:
4481 DISALLOW_COPY_AND_ASSIGN(HMul);
4482};
4483
Vladimir Markofcb503c2016-05-18 12:48:17 +01004484class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004485 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004486 HDiv(Primitive::Type result_type,
4487 HInstruction* left,
4488 HInstruction* right,
4489 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004490 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004491
Roland Levillain9867bc72015-08-05 10:21:34 +01004492 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004493 T ComputeIntegral(T x, T y) const {
4494 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 // Our graph structure ensures we never have 0 for `y` during
4496 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004497 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004498 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004499 return (y == -1) ? -x : x / y;
4500 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004501
Roland Levillain31dd3d62016-02-16 12:21:02 +00004502 template <typename T>
4503 T ComputeFP(T x, T y) const {
4504 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4505 return x / y;
4506 }
4507
Roland Levillain9867bc72015-08-05 10:21:34 +01004508 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004509 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004510 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004511 }
4512 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004513 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004514 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4515 }
4516 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4517 return GetBlock()->GetGraph()->GetFloatConstant(
4518 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4519 }
4520 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4521 return GetBlock()->GetGraph()->GetDoubleConstant(
4522 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004523 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004524
4525 DECLARE_INSTRUCTION(Div);
4526
4527 private:
4528 DISALLOW_COPY_AND_ASSIGN(HDiv);
4529};
4530
Vladimir Markofcb503c2016-05-18 12:48:17 +01004531class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004532 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004533 HRem(Primitive::Type result_type,
4534 HInstruction* left,
4535 HInstruction* right,
4536 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004537 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004538
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004540 T ComputeIntegral(T x, T y) const {
4541 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 // Our graph structure ensures we never have 0 for `y` during
4543 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004544 DCHECK_NE(y, 0);
4545 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4546 return (y == -1) ? 0 : x % y;
4547 }
4548
Roland Levillain31dd3d62016-02-16 12:21:02 +00004549 template <typename T>
4550 T ComputeFP(T x, T y) const {
4551 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4552 return std::fmod(x, y);
4553 }
4554
Roland Levillain9867bc72015-08-05 10:21:34 +01004555 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004556 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004557 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004558 }
4559 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004561 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004562 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004563 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4564 return GetBlock()->GetGraph()->GetFloatConstant(
4565 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4566 }
4567 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4568 return GetBlock()->GetGraph()->GetDoubleConstant(
4569 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4570 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004571
4572 DECLARE_INSTRUCTION(Rem);
4573
4574 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004575 DISALLOW_COPY_AND_ASSIGN(HRem);
4576};
4577
Vladimir Markofcb503c2016-05-18 12:48:17 +01004578class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004579 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004580 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4581 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004582 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004583 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004584 SetRawInputAt(0, value);
4585 }
4586
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004587 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4588
Calin Juravled0d48522014-11-04 16:40:20 +00004589 bool CanBeMoved() const OVERRIDE { return true; }
4590
Vladimir Marko372f10e2016-05-17 16:30:10 +01004591 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004592 return true;
4593 }
4594
4595 bool NeedsEnvironment() const OVERRIDE { return true; }
4596 bool CanThrow() const OVERRIDE { return true; }
4597
Calin Juravled0d48522014-11-04 16:40:20 +00004598 DECLARE_INSTRUCTION(DivZeroCheck);
4599
4600 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004601 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4602};
4603
Vladimir Markofcb503c2016-05-18 12:48:17 +01004604class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004605 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004606 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004607 HInstruction* value,
4608 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4611 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4612 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
4614
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004616 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004617 return value << (distance & max_shift_distance);
4618 }
4619
4620 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004621 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004622 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004623 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004624 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004625 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004627 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004628 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4629 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4630 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4631 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004633 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4634 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004635 LOG(FATAL) << DebugName() << " is not defined for float values";
4636 UNREACHABLE();
4637 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4639 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004640 LOG(FATAL) << DebugName() << " is not defined for double values";
4641 UNREACHABLE();
4642 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004643
4644 DECLARE_INSTRUCTION(Shl);
4645
4646 private:
4647 DISALLOW_COPY_AND_ASSIGN(HShl);
4648};
4649
Vladimir Markofcb503c2016-05-18 12:48:17 +01004650class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004652 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 HInstruction* value,
4654 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4657 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4658 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
4660
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004662 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004663 return value >> (distance & max_shift_distance);
4664 }
4665
4666 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004667 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004668 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004669 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004670 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004671 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004672 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004673 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004674 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4675 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4676 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4677 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004678 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004679 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4680 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004681 LOG(FATAL) << DebugName() << " is not defined for float values";
4682 UNREACHABLE();
4683 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4685 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 LOG(FATAL) << DebugName() << " is not defined for double values";
4687 UNREACHABLE();
4688 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004689
4690 DECLARE_INSTRUCTION(Shr);
4691
4692 private:
4693 DISALLOW_COPY_AND_ASSIGN(HShr);
4694};
4695
Vladimir Markofcb503c2016-05-18 12:48:17 +01004696class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004697 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 HInstruction* value,
4700 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4703 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4704 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004705 }
4706
Roland Levillain5b5b9312016-03-22 14:57:31 +00004707 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004708 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 typedef typename std::make_unsigned<T>::type V;
4710 V ux = static_cast<V>(value);
4711 return static_cast<T>(ux >> (distance & max_shift_distance));
4712 }
4713
4714 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004715 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004717 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004718 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004720 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004721 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004722 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4723 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4724 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4725 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004726 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004727 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4728 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004729 LOG(FATAL) << DebugName() << " is not defined for float values";
4730 UNREACHABLE();
4731 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004732 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4733 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004734 LOG(FATAL) << DebugName() << " is not defined for double values";
4735 UNREACHABLE();
4736 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004737
4738 DECLARE_INSTRUCTION(UShr);
4739
4740 private:
4741 DISALLOW_COPY_AND_ASSIGN(HUShr);
4742};
4743
Vladimir Markofcb503c2016-05-18 12:48:17 +01004744class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004745 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004746 HAnd(Primitive::Type result_type,
4747 HInstruction* left,
4748 HInstruction* right,
4749 uint32_t dex_pc = kNoDexPc)
4750 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004751
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004752 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004753
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004754 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004755
4756 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004757 return GetBlock()->GetGraph()->GetIntConstant(
4758 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004759 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004760 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004761 return GetBlock()->GetGraph()->GetLongConstant(
4762 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004764 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4765 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4766 LOG(FATAL) << DebugName() << " is not defined for float values";
4767 UNREACHABLE();
4768 }
4769 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4770 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4771 LOG(FATAL) << DebugName() << " is not defined for double values";
4772 UNREACHABLE();
4773 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004774
4775 DECLARE_INSTRUCTION(And);
4776
4777 private:
4778 DISALLOW_COPY_AND_ASSIGN(HAnd);
4779};
4780
Vladimir Markofcb503c2016-05-18 12:48:17 +01004781class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004782 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004783 HOr(Primitive::Type result_type,
4784 HInstruction* left,
4785 HInstruction* right,
4786 uint32_t dex_pc = kNoDexPc)
4787 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004788
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004789 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004790
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004791 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004792
4793 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004794 return GetBlock()->GetGraph()->GetIntConstant(
4795 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004796 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004797 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004798 return GetBlock()->GetGraph()->GetLongConstant(
4799 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004800 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004801 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4802 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4803 LOG(FATAL) << DebugName() << " is not defined for float values";
4804 UNREACHABLE();
4805 }
4806 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4807 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4808 LOG(FATAL) << DebugName() << " is not defined for double values";
4809 UNREACHABLE();
4810 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004811
4812 DECLARE_INSTRUCTION(Or);
4813
4814 private:
4815 DISALLOW_COPY_AND_ASSIGN(HOr);
4816};
4817
Vladimir Markofcb503c2016-05-18 12:48:17 +01004818class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004819 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004820 HXor(Primitive::Type result_type,
4821 HInstruction* left,
4822 HInstruction* right,
4823 uint32_t dex_pc = kNoDexPc)
4824 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004825
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004826 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004827
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004828 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004829
4830 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004831 return GetBlock()->GetGraph()->GetIntConstant(
4832 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004834 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004835 return GetBlock()->GetGraph()->GetLongConstant(
4836 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004838 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4839 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for float values";
4841 UNREACHABLE();
4842 }
4843 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4844 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4845 LOG(FATAL) << DebugName() << " is not defined for double values";
4846 UNREACHABLE();
4847 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004848
4849 DECLARE_INSTRUCTION(Xor);
4850
4851 private:
4852 DISALLOW_COPY_AND_ASSIGN(HXor);
4853};
4854
Vladimir Markofcb503c2016-05-18 12:48:17 +01004855class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004856 public:
4857 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004858 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004859 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4860 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004861 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004862
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004864 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 typedef typename std::make_unsigned<T>::type V;
4866 V ux = static_cast<V>(value);
4867 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004868 return static_cast<T>(ux);
4869 } else {
4870 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 return static_cast<T>(ux >> (distance & max_shift_value)) |
4872 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004873 }
4874 }
4875
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004877 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004879 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004881 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004883 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004884 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4885 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4886 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4887 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004888 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004889 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4890 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004891 LOG(FATAL) << DebugName() << " is not defined for float values";
4892 UNREACHABLE();
4893 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4895 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004896 LOG(FATAL) << DebugName() << " is not defined for double values";
4897 UNREACHABLE();
4898 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004899
4900 DECLARE_INSTRUCTION(Ror);
4901
4902 private:
4903 DISALLOW_COPY_AND_ASSIGN(HRor);
4904};
4905
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004906// The value of a parameter in this method. Its location depends on
4907// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004908class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004909 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004910 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004911 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004912 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004913 Primitive::Type parameter_type,
4914 bool is_this = false)
4915 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004916 dex_file_(dex_file),
4917 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004918 index_(index) {
4919 SetPackedFlag<kFlagIsThis>(is_this);
4920 SetPackedFlag<kFlagCanBeNull>(!is_this);
4921 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004922
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004923 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004924 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004925 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004926 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927
Vladimir Markoa1de9182016-02-25 11:37:38 +00004928 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4929 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004930
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004931 DECLARE_INSTRUCTION(ParameterValue);
4932
4933 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004934 // Whether or not the parameter value corresponds to 'this' argument.
4935 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4936 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4937 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4938 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4939 "Too many packed fields.");
4940
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004941 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004942 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004943 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004944 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004945 const uint8_t index_;
4946
4947 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4948};
4949
Vladimir Markofcb503c2016-05-18 12:48:17 +01004950class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004951 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004952 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4953 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004954
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004955 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004956 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004957 return true;
4958 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004959
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004960 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004961
4962 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004963 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004964 }
4965 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004967 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004968 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4969 LOG(FATAL) << DebugName() << " is not defined for float values";
4970 UNREACHABLE();
4971 }
4972 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4973 LOG(FATAL) << DebugName() << " is not defined for double values";
4974 UNREACHABLE();
4975 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004976
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004977 DECLARE_INSTRUCTION(Not);
4978
4979 private:
4980 DISALLOW_COPY_AND_ASSIGN(HNot);
4981};
4982
Vladimir Markofcb503c2016-05-18 12:48:17 +01004983class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004984 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004985 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4986 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004987
4988 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004989 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004990 return true;
4991 }
4992
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004993 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004994 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004995 return !x;
4996 }
4997
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004999 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005000 }
5001 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5002 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005003 UNREACHABLE();
5004 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005005 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5006 LOG(FATAL) << DebugName() << " is not defined for float values";
5007 UNREACHABLE();
5008 }
5009 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5010 LOG(FATAL) << DebugName() << " is not defined for double values";
5011 UNREACHABLE();
5012 }
David Brazdil66d126e2015-04-03 16:02:44 +01005013
5014 DECLARE_INSTRUCTION(BooleanNot);
5015
5016 private:
5017 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5018};
5019
Vladimir Markofcb503c2016-05-18 12:48:17 +01005020class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005021 public:
5022 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005023 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005024 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005025 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005026 // Invariant: We should never generate a conversion to a Boolean value.
5027 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005028 }
5029
5030 HInstruction* GetInput() const { return InputAt(0); }
5031 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5032 Primitive::Type GetResultType() const { return GetType(); }
5033
5034 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005035 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5036 return true;
5037 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005038
Mark Mendelle82549b2015-05-06 10:55:34 -04005039 // Try to statically evaluate the conversion and return a HConstant
5040 // containing the result. If the input cannot be converted, return nullptr.
5041 HConstant* TryStaticEvaluation() const;
5042
Roland Levillaindff1f282014-11-05 14:15:05 +00005043 DECLARE_INSTRUCTION(TypeConversion);
5044
5045 private:
5046 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5047};
5048
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005049static constexpr uint32_t kNoRegNumber = -1;
5050
Vladimir Markofcb503c2016-05-18 12:48:17 +01005051class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005052 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005053 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5054 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005055 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005056 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 SetRawInputAt(0, value);
5058 }
5059
Calin Juravle10e244f2015-01-26 18:54:32 +00005060 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005061 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005062 return true;
5063 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005064
Calin Juravle10e244f2015-01-26 18:54:32 +00005065 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005066
Calin Juravle10e244f2015-01-26 18:54:32 +00005067 bool CanThrow() const OVERRIDE { return true; }
5068
5069 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005070
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005071
5072 DECLARE_INSTRUCTION(NullCheck);
5073
5074 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005075 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5076};
5077
5078class FieldInfo : public ValueObject {
5079 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005080 FieldInfo(MemberOffset field_offset,
5081 Primitive::Type field_type,
5082 bool is_volatile,
5083 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005084 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005085 const DexFile& dex_file,
5086 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005087 : field_offset_(field_offset),
5088 field_type_(field_type),
5089 is_volatile_(is_volatile),
5090 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005091 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005092 dex_file_(dex_file),
5093 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005094
5095 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005096 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005097 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005098 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005099 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005100 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005101 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102
5103 private:
5104 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005105 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005106 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005107 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005108 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005109 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005110 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111};
5112
Vladimir Markofcb503c2016-05-18 12:48:17 +01005113class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114 public:
5115 HInstanceFieldGet(HInstruction* value,
5116 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005117 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005118 bool is_volatile,
5119 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005120 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005121 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005122 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005123 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005124 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005125 field_info_(field_offset,
5126 field_type,
5127 is_volatile,
5128 field_idx,
5129 declaring_class_def_index,
5130 dex_file,
5131 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132 SetRawInputAt(0, value);
5133 }
5134
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005135 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005136
Vladimir Marko372f10e2016-05-17 16:30:10 +01005137 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5138 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005139 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005140 }
5141
Calin Juravle641547a2015-04-21 22:08:51 +01005142 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005143 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005144 }
5145
5146 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005147 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5148 }
5149
Calin Juravle52c48962014-12-16 17:02:57 +00005150 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005152 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005153 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154
5155 DECLARE_INSTRUCTION(InstanceFieldGet);
5156
5157 private:
5158 const FieldInfo field_info_;
5159
5160 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5161};
5162
Vladimir Markofcb503c2016-05-18 12:48:17 +01005163class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164 public:
5165 HInstanceFieldSet(HInstruction* object,
5166 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005167 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005168 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005169 bool is_volatile,
5170 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005171 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005172 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005173 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005174 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005175 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005176 field_info_(field_offset,
5177 field_type,
5178 is_volatile,
5179 field_idx,
5180 declaring_class_def_index,
5181 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005182 dex_cache) {
5183 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 SetRawInputAt(0, object);
5185 SetRawInputAt(1, value);
5186 }
5187
Calin Juravle641547a2015-04-21 22:08:51 +01005188 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005189 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005190 }
5191
Calin Juravle52c48962014-12-16 17:02:57 +00005192 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005193 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005194 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005195 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005196 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005197 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5198 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005199
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200 DECLARE_INSTRUCTION(InstanceFieldSet);
5201
5202 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005203 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5204 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5205 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5206 "Too many packed fields.");
5207
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208 const FieldInfo field_info_;
5209
5210 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5211};
5212
Vladimir Markofcb503c2016-05-18 12:48:17 +01005213class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005215 HArrayGet(HInstruction* array,
5216 HInstruction* index,
5217 Primitive::Type type,
5218 uint32_t dex_pc,
5219 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005220 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005221 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005222 SetRawInputAt(0, array);
5223 SetRawInputAt(1, index);
5224 }
5225
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005227 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005228 return true;
5229 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005230 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005231 // TODO: We can be smarter here.
5232 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5233 // which generates the implicit null check. There are cases when these can be removed
5234 // to produce better code. If we ever add optimizations to do so we should allow an
5235 // implicit check here (as long as the address falls in the first page).
5236 return false;
5237 }
5238
David Brazdil4833f5a2015-12-16 10:37:39 +00005239 bool IsEquivalentOf(HArrayGet* other) const {
5240 bool result = (GetDexPc() == other->GetDexPc());
5241 if (kIsDebugBuild && result) {
5242 DCHECK_EQ(GetBlock(), other->GetBlock());
5243 DCHECK_EQ(GetArray(), other->GetArray());
5244 DCHECK_EQ(GetIndex(), other->GetIndex());
5245 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005246 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005247 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005248 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5249 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005250 }
5251 }
5252 return result;
5253 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005254
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005255 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5256
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005257 HInstruction* GetArray() const { return InputAt(0); }
5258 HInstruction* GetIndex() const { return InputAt(1); }
5259
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 DECLARE_INSTRUCTION(ArrayGet);
5261
5262 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005263 // We treat a String as an array, creating the HArrayGet from String.charAt()
5264 // intrinsic in the instruction simplifier. We can always determine whether
5265 // a particular HArrayGet is actually a String.charAt() by looking at the type
5266 // of the input but that requires holding the mutator lock, so we prefer to use
5267 // a flag, so that code generators don't need to do the locking.
5268 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5269 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5270 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5271 "Too many packed fields.");
5272
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5274};
5275
Vladimir Markofcb503c2016-05-18 12:48:17 +01005276class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 public:
5278 HArraySet(HInstruction* array,
5279 HInstruction* index,
5280 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005281 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005282 uint32_t dex_pc)
5283 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005284 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5285 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5286 SetPackedFlag<kFlagValueCanBeNull>(true);
5287 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 SetRawInputAt(0, array);
5289 SetRawInputAt(1, index);
5290 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005291 // Make a best guess now, may be refined during SSA building.
5292 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 }
5294
Calin Juravle77520bc2015-01-12 18:45:46 +00005295 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005296 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005297 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 }
5299
Mingyao Yang81014cb2015-06-02 03:16:27 -07005300 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005301 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005302
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005303 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005304 // TODO: Same as for ArrayGet.
5305 return false;
5306 }
5307
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005308 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005309 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005310 }
5311
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005312 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005313 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005314 }
5315
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005316 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005317 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 }
5319
Vladimir Markoa1de9182016-02-25 11:37:38 +00005320 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5321 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5322 bool StaticTypeOfArrayIsObjectArray() const {
5323 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5324 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005325
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005326 HInstruction* GetArray() const { return InputAt(0); }
5327 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005328 HInstruction* GetValue() const { return InputAt(2); }
5329
5330 Primitive::Type GetComponentType() const {
5331 // The Dex format does not type floating point index operations. Since the
5332 // `expected_component_type_` is set during building and can therefore not
5333 // be correct, we also check what is the value type. If it is a floating
5334 // point type, we must use that type.
5335 Primitive::Type value_type = GetValue()->GetType();
5336 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5337 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005338 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005339 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005340
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005341 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005342 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005343 }
5344
Aart Bik18b36ab2016-04-13 16:41:35 -07005345 void ComputeSideEffects() {
5346 Primitive::Type type = GetComponentType();
5347 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5348 SideEffectsForArchRuntimeCalls(type)));
5349 }
5350
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005351 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5352 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5353 }
5354
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355 DECLARE_INSTRUCTION(ArraySet);
5356
5357 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005358 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5359 static constexpr size_t kFieldExpectedComponentTypeSize =
5360 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5361 static constexpr size_t kFlagNeedsTypeCheck =
5362 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5363 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005364 // Cached information for the reference_type_info_ so that codegen
5365 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005366 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5367 static constexpr size_t kNumberOfArraySetPackedBits =
5368 kFlagStaticTypeOfArrayIsObjectArray + 1;
5369 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5370 using ExpectedComponentTypeField =
5371 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372
5373 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5374};
5375
Vladimir Markofcb503c2016-05-18 12:48:17 +01005376class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005378 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005379 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005380 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005381 // Note that arrays do not change length, so the instruction does not
5382 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383 SetRawInputAt(0, array);
5384 }
5385
Calin Juravle77520bc2015-01-12 18:45:46 +00005386 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005387 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005388 return true;
5389 }
Calin Juravle641547a2015-04-21 22:08:51 +01005390 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5391 return obj == InputAt(0);
5392 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005393
Vladimir Markodce016e2016-04-28 13:10:02 +01005394 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5395
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396 DECLARE_INSTRUCTION(ArrayLength);
5397
5398 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005399 // We treat a String as an array, creating the HArrayLength from String.length()
5400 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5401 // determine whether a particular HArrayLength is actually a String.length() by
5402 // looking at the type of the input but that requires holding the mutator lock, so
5403 // we prefer to use a flag, so that code generators don't need to do the locking.
5404 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5405 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5406 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5407 "Too many packed fields.");
5408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5410};
5411
Vladimir Markofcb503c2016-05-18 12:48:17 +01005412class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005414 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5415 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005416 HBoundsCheck(HInstruction* index,
5417 HInstruction* length,
5418 uint32_t dex_pc,
5419 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5420 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5421 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005422 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 SetRawInputAt(0, index);
5424 SetRawInputAt(1, length);
5425 }
5426
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005427 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005428 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005429 return true;
5430 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005431
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005432 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005434 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005435
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005436 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5437 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5438
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005439 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440
5441 DECLARE_INSTRUCTION(BoundsCheck);
5442
5443 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005444 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5445 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5446 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5447 // so we need to create an HEnvironment which will be translated to an InlineInfo
5448 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5449 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5450 // from the invoke as we don't want to look again at the dex bytecode.
5451 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5452
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5454};
5455
Vladimir Markofcb503c2016-05-18 12:48:17 +01005456class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005457 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005458 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005459 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005460
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005461 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005462 return true;
5463 }
5464
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005465 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5466 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005467
5468 DECLARE_INSTRUCTION(SuspendCheck);
5469
5470 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005471 // Only used for code generation, in order to share the same slow path between back edges
5472 // of a same loop.
5473 SlowPathCode* slow_path_;
5474
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005475 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5476};
5477
David Srbecky0cf44932015-12-09 14:09:59 +00005478// Pseudo-instruction which provides the native debugger with mapping information.
5479// It ensures that we can generate line number and local variables at this point.
5480class HNativeDebugInfo : public HTemplateInstruction<0> {
5481 public:
5482 explicit HNativeDebugInfo(uint32_t dex_pc)
5483 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5484
5485 bool NeedsEnvironment() const OVERRIDE {
5486 return true;
5487 }
5488
5489 DECLARE_INSTRUCTION(NativeDebugInfo);
5490
5491 private:
5492 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5493};
5494
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005495/**
5496 * Instruction to load a Class object.
5497 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005498class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005499 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005500 // Determines how to load the Class.
5501 enum class LoadKind {
5502 // Use the Class* from the method's own ArtMethod*.
5503 kReferrersClass,
5504
5505 // Use boot image Class* address that will be known at link time.
5506 // Used for boot image classes referenced by boot image code in non-PIC mode.
5507 kBootImageLinkTimeAddress,
5508
5509 // Use PC-relative boot image Class* address that will be known at link time.
5510 // Used for boot image classes referenced by boot image code in PIC mode.
5511 kBootImageLinkTimePcRelative,
5512
5513 // Use a known boot image Class* address, embedded in the code by the codegen.
5514 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5515 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5516 // GetIncludePatchInformation().
5517 kBootImageAddress,
5518
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005519 // Load from the root table associated with the JIT compiled method.
5520 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005521
5522 // Load from resolved types array in the dex cache using a PC-relative load.
5523 // Used for classes outside boot image when we know that we can access
5524 // the dex cache arrays using a PC-relative load.
5525 kDexCachePcRelative,
5526
5527 // Load from resolved types array accessed through the class loaded from
5528 // the compiled method's own ArtMethod*. This is the default access type when
5529 // all other types are unavailable.
5530 kDexCacheViaMethod,
5531
5532 kLast = kDexCacheViaMethod
5533 };
5534
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005535 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005536 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005537 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005538 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005539 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005540 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005541 bool is_in_dex_cache,
5542 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005543 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5544 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005545 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005546 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005547 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005548 // Referrers class should not need access check. We never inline unverified
5549 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005550 DCHECK(!is_referrers_class || !needs_access_check);
5551
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005552 SetPackedField<LoadKindField>(
5553 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005554 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5555 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005556 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005557 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005558 }
5559
5560 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5561 DCHECK(HasAddress(load_kind));
5562 load_data_.address = address;
5563 SetLoadKindInternal(load_kind);
5564 }
5565
5566 void SetLoadKindWithTypeReference(LoadKind load_kind,
5567 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005568 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005569 DCHECK(HasTypeReference(load_kind));
5570 DCHECK(IsSameDexFile(dex_file_, dex_file));
5571 DCHECK_EQ(type_index_, type_index);
5572 SetLoadKindInternal(load_kind);
5573 }
5574
5575 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5576 const DexFile& dex_file,
5577 uint32_t element_index) {
5578 DCHECK(HasDexCacheReference(load_kind));
5579 DCHECK(IsSameDexFile(dex_file_, dex_file));
5580 load_data_.dex_cache_element_index = element_index;
5581 SetLoadKindInternal(load_kind);
5582 }
5583
5584 LoadKind GetLoadKind() const {
5585 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005586 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005587
5588 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005589
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005590 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005591
Andreas Gampea5b09a62016-11-17 15:21:22 -08005592 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005593
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005594 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005595
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005596 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005597 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005598 }
5599
Calin Juravle0ba218d2015-05-19 18:46:01 +01005600 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005601 // The entrypoint the code generator is going to call does not do
5602 // clinit of the class.
5603 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005604 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005605 }
5606
5607 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005608 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 (!IsReferrersClass() && !IsInDexCache()) ||
5610 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005611 }
5612
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005613 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005614 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005615 }
5616
Calin Juravleacf735c2015-02-12 15:25:22 +00005617 ReferenceTypeInfo GetLoadedClassRTI() {
5618 return loaded_class_rti_;
5619 }
5620
5621 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5622 // Make sure we only set exact types (the loaded class should never be merged).
5623 DCHECK(rti.IsExact());
5624 loaded_class_rti_ = rti;
5625 }
5626
Andreas Gampea5b09a62016-11-17 15:21:22 -08005627 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005628 const DexFile& GetDexFile() const { return dex_file_; }
5629
5630 uint32_t GetDexCacheElementOffset() const;
5631
5632 uint64_t GetAddress() const {
5633 DCHECK(HasAddress(GetLoadKind()));
5634 return load_data_.address;
5635 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005636
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005637 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5638 return !IsReferrersClass();
5639 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005640
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005641 static SideEffects SideEffectsForArchRuntimeCalls() {
5642 return SideEffects::CanTriggerGC();
5643 }
5644
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005645 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005646 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5647 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005648 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005649 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005650
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005651 void MarkInDexCache() {
5652 SetPackedFlag<kFlagIsInDexCache>(true);
5653 DCHECK(!NeedsEnvironment());
5654 RemoveEnvironment();
5655 SetSideEffects(SideEffects::None());
5656 }
5657
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005658 void MarkInBootImage() {
5659 SetPackedFlag<kFlagIsInBootImage>(true);
5660 }
5661
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662 void AddSpecialInput(HInstruction* special_input);
5663
5664 using HInstruction::GetInputRecords; // Keep the const version visible.
5665 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5666 return ArrayRef<HUserRecord<HInstruction*>>(
5667 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5668 }
5669
5670 Primitive::Type GetType() const OVERRIDE {
5671 return Primitive::kPrimNot;
5672 }
5673
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005674 DECLARE_INSTRUCTION(LoadClass);
5675
5676 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005678 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005679 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005680 // Whether this instruction must generate the initialization check.
5681 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005682 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005683 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5684 static constexpr size_t kFieldLoadKindSize =
5685 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5686 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005687 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005688 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5689
5690 static bool HasTypeReference(LoadKind load_kind) {
5691 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5692 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5693 load_kind == LoadKind::kDexCacheViaMethod ||
5694 load_kind == LoadKind::kReferrersClass;
5695 }
5696
5697 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005698 return load_kind == LoadKind::kBootImageAddress ||
5699 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 }
5701
5702 static bool HasDexCacheReference(LoadKind load_kind) {
5703 return load_kind == LoadKind::kDexCachePcRelative;
5704 }
5705
5706 void SetLoadKindInternal(LoadKind load_kind);
5707
5708 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5709 // For other load kinds it's empty or possibly some architecture-specific instruction
5710 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5711 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712
Andreas Gampea5b09a62016-11-17 15:21:22 -08005713 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005714 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 union {
5717 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005718 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 } load_data_;
5720
Calin Juravleacf735c2015-02-12 15:25:22 +00005721 ReferenceTypeInfo loaded_class_rti_;
5722
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5724};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5726
5727// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5728inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5729 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5730 return load_data_.dex_cache_element_index;
5731}
5732
5733// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5734inline 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::kDexCachePcRelative ||
5739 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5740 GetLoadKind() == LoadKind::kBootImageAddress) << 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.
5760 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5761 // GetIncludePatchInformation().
5762 kBootImageAddress,
5763
Vladimir Markoaad75c62016-10-03 08:46:48 +00005764 // Load from an entry in the .bss section using a PC-relative load.
5765 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5766 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005767
5768 // Load from resolved strings array accessed through the class loaded from
5769 // the compiled method's own ArtMethod*. This is the default access type when
5770 // all other types are unavailable.
5771 kDexCacheViaMethod,
5772
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005773 // Load from the root table associated with the JIT compiled method.
5774 kJitTableAddress,
5775
5776 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005777 };
5778
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005779 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005780 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005781 const DexFile& dex_file,
5782 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005783 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5784 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005785 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005786 SetPackedFlag<kFlagIsInDexCache>(false);
5787 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005788 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005789 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005790
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005791 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5792 DCHECK(HasAddress(load_kind));
5793 load_data_.address = address;
5794 SetLoadKindInternal(load_kind);
5795 }
5796
5797 void SetLoadKindWithStringReference(LoadKind load_kind,
5798 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005799 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005800 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005801 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005802 string_index_ = string_index;
5803 SetLoadKindInternal(load_kind);
5804 }
5805
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005806 LoadKind GetLoadKind() const {
5807 return GetPackedField<LoadKindField>();
5808 }
5809
5810 const DexFile& GetDexFile() const;
5811
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005812 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005813 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5814 return string_index_;
5815 }
5816
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005817 uint64_t GetAddress() const {
5818 DCHECK(HasAddress(GetLoadKind()));
5819 return load_data_.address;
5820 }
5821
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005822 bool CanBeMoved() const OVERRIDE { return true; }
5823
Vladimir Marko372f10e2016-05-17 16:30:10 +01005824 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005825
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005826 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005827
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828 // Will call the runtime if we need to load the string through
5829 // the dex cache and the string is not guaranteed to be there yet.
5830 bool NeedsEnvironment() const OVERRIDE {
5831 LoadKind load_kind = GetLoadKind();
5832 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5833 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005834 load_kind == LoadKind::kBootImageAddress ||
5835 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005836 return false;
5837 }
5838 return !IsInDexCache();
5839 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005840
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5842 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5843 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005844
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005845 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005848 static SideEffects SideEffectsForArchRuntimeCalls() {
5849 return SideEffects::CanTriggerGC();
5850 }
5851
Vladimir Markoa1de9182016-02-25 11:37:38 +00005852 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5853
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854 void MarkInDexCache() {
5855 SetPackedFlag<kFlagIsInDexCache>(true);
5856 DCHECK(!NeedsEnvironment());
5857 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005858 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005859 }
5860
Vladimir Marko372f10e2016-05-17 16:30:10 +01005861 void AddSpecialInput(HInstruction* special_input);
5862
5863 using HInstruction::GetInputRecords; // Keep the const version visible.
5864 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5865 return ArrayRef<HUserRecord<HInstruction*>>(
5866 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867 }
5868
Vladimir Marko372f10e2016-05-17 16:30:10 +01005869 Primitive::Type GetType() const OVERRIDE {
5870 return Primitive::kPrimNot;
5871 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005873 DECLARE_INSTRUCTION(LoadString);
5874
5875 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005876 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005877 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5878 static constexpr size_t kFieldLoadKindSize =
5879 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5880 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005881 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005883
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005884 static bool HasStringReference(LoadKind load_kind) {
5885 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5886 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005887 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005888 load_kind == LoadKind::kDexCacheViaMethod ||
5889 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 }
5891
5892 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005893 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 }
5895
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896 void SetLoadKindInternal(LoadKind load_kind);
5897
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005898 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5899 // For other load kinds it's empty or possibly some architecture-specific instruction
5900 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005901 HUserRecord<HInstruction*> special_input_;
5902
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 // String index serves also as the hash code and it's also needed for slow-paths,
5904 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005905 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906
5907 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005908 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5910 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005911
5912 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5913};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5915
5916// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5917inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005918 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5919 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005920}
5921
5922// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5923inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005924 // The special input is used for PC-relative loads on some architectures,
5925 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005926 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005927 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005928 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5929 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005930 // HLoadString::GetInputRecords() returns an empty array at this point,
5931 // so use the GetInputRecords() from the base class to set the input record.
5932 DCHECK(special_input_.GetInstruction() == nullptr);
5933 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 special_input->AddUseAt(this, 0);
5935}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005936
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937/**
5938 * Performs an initialization check on its Class object input.
5939 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005940class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005942 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005943 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005944 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005945 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5946 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005947 SetRawInputAt(0, constant);
5948 }
5949
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005950 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005951 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005952 return true;
5953 }
5954
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955 bool NeedsEnvironment() const OVERRIDE {
5956 // May call runtime to initialize the class.
5957 return true;
5958 }
5959
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005960 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961
5962 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5963
5964 DECLARE_INSTRUCTION(ClinitCheck);
5965
5966 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005967 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5968};
5969
Vladimir Markofcb503c2016-05-18 12:48:17 +01005970class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971 public:
5972 HStaticFieldGet(HInstruction* cls,
5973 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005974 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005975 bool is_volatile,
5976 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005977 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005978 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005979 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005980 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005981 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005982 field_info_(field_offset,
5983 field_type,
5984 is_volatile,
5985 field_idx,
5986 declaring_class_def_index,
5987 dex_file,
5988 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005989 SetRawInputAt(0, cls);
5990 }
5991
Calin Juravle52c48962014-12-16 17:02:57 +00005992
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005993 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005994
Vladimir Marko372f10e2016-05-17 16:30:10 +01005995 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5996 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005997 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005998 }
5999
6000 size_t ComputeHashCode() const OVERRIDE {
6001 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6002 }
6003
Calin Juravle52c48962014-12-16 17:02:57 +00006004 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6006 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006007 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008
6009 DECLARE_INSTRUCTION(StaticFieldGet);
6010
6011 private:
6012 const FieldInfo field_info_;
6013
6014 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6015};
6016
Vladimir Markofcb503c2016-05-18 12:48:17 +01006017class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018 public:
6019 HStaticFieldSet(HInstruction* cls,
6020 HInstruction* value,
6021 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006022 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006023 bool is_volatile,
6024 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006025 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006026 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006027 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01006028 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006029 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006030 field_info_(field_offset,
6031 field_type,
6032 is_volatile,
6033 field_idx,
6034 declaring_class_def_index,
6035 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 dex_cache) {
6037 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038 SetRawInputAt(0, cls);
6039 SetRawInputAt(1, value);
6040 }
6041
Calin Juravle52c48962014-12-16 17:02:57 +00006042 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006043 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6044 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006045 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006046
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006047 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6049 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006050
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006051 DECLARE_INSTRUCTION(StaticFieldSet);
6052
6053 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6055 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6056 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6057 "Too many packed fields.");
6058
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006059 const FieldInfo field_info_;
6060
6061 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6062};
6063
Vladimir Markofcb503c2016-05-18 12:48:17 +01006064class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006065 public:
6066 HUnresolvedInstanceFieldGet(HInstruction* obj,
6067 Primitive::Type field_type,
6068 uint32_t field_index,
6069 uint32_t dex_pc)
6070 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6071 field_index_(field_index) {
6072 SetRawInputAt(0, obj);
6073 }
6074
6075 bool NeedsEnvironment() const OVERRIDE { return true; }
6076 bool CanThrow() const OVERRIDE { return true; }
6077
6078 Primitive::Type GetFieldType() const { return GetType(); }
6079 uint32_t GetFieldIndex() const { return field_index_; }
6080
6081 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6082
6083 private:
6084 const uint32_t field_index_;
6085
6086 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6087};
6088
Vladimir Markofcb503c2016-05-18 12:48:17 +01006089class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006090 public:
6091 HUnresolvedInstanceFieldSet(HInstruction* obj,
6092 HInstruction* value,
6093 Primitive::Type field_type,
6094 uint32_t field_index,
6095 uint32_t dex_pc)
6096 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006097 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006098 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006099 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006100 SetRawInputAt(0, obj);
6101 SetRawInputAt(1, value);
6102 }
6103
6104 bool NeedsEnvironment() const OVERRIDE { return true; }
6105 bool CanThrow() const OVERRIDE { return true; }
6106
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006108 uint32_t GetFieldIndex() const { return field_index_; }
6109
6110 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6111
6112 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006113 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6114 static constexpr size_t kFieldFieldTypeSize =
6115 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6116 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6117 kFieldFieldType + kFieldFieldTypeSize;
6118 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6119 "Too many packed fields.");
6120 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6121
Calin Juravlee460d1d2015-09-29 04:52:17 +01006122 const uint32_t field_index_;
6123
6124 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6125};
6126
Vladimir Markofcb503c2016-05-18 12:48:17 +01006127class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006128 public:
6129 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6130 uint32_t field_index,
6131 uint32_t dex_pc)
6132 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6133 field_index_(field_index) {
6134 }
6135
6136 bool NeedsEnvironment() const OVERRIDE { return true; }
6137 bool CanThrow() const OVERRIDE { return true; }
6138
6139 Primitive::Type GetFieldType() const { return GetType(); }
6140 uint32_t GetFieldIndex() const { return field_index_; }
6141
6142 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6143
6144 private:
6145 const uint32_t field_index_;
6146
6147 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6148};
6149
Vladimir Markofcb503c2016-05-18 12:48:17 +01006150class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006151 public:
6152 HUnresolvedStaticFieldSet(HInstruction* value,
6153 Primitive::Type field_type,
6154 uint32_t field_index,
6155 uint32_t dex_pc)
6156 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006157 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006159 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006160 SetRawInputAt(0, value);
6161 }
6162
6163 bool NeedsEnvironment() const OVERRIDE { return true; }
6164 bool CanThrow() const OVERRIDE { return true; }
6165
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 uint32_t GetFieldIndex() const { return field_index_; }
6168
6169 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6170
6171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6173 static constexpr size_t kFieldFieldTypeSize =
6174 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6175 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6176 kFieldFieldType + kFieldFieldTypeSize;
6177 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6178 "Too many packed fields.");
6179 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6180
Calin Juravlee460d1d2015-09-29 04:52:17 +01006181 const uint32_t field_index_;
6182
6183 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6184};
6185
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006186// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006187class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006189 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6190 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006191
David Brazdilbbd733e2015-08-18 17:48:17 +01006192 bool CanBeNull() const OVERRIDE { return false; }
6193
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006194 DECLARE_INSTRUCTION(LoadException);
6195
6196 private:
6197 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6198};
6199
David Brazdilcb1c0552015-08-04 16:22:25 +01006200// Implicit part of move-exception which clears thread-local exception storage.
6201// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006202class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006203 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006204 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6205 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006206
6207 DECLARE_INSTRUCTION(ClearException);
6208
6209 private:
6210 DISALLOW_COPY_AND_ASSIGN(HClearException);
6211};
6212
Vladimir Markofcb503c2016-05-18 12:48:17 +01006213class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006214 public:
6215 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006216 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006217 SetRawInputAt(0, exception);
6218 }
6219
6220 bool IsControlFlow() const OVERRIDE { return true; }
6221
6222 bool NeedsEnvironment() const OVERRIDE { return true; }
6223
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006224 bool CanThrow() const OVERRIDE { return true; }
6225
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006226
6227 DECLARE_INSTRUCTION(Throw);
6228
6229 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006230 DISALLOW_COPY_AND_ASSIGN(HThrow);
6231};
6232
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233/**
6234 * Implementation strategies for the code generator of a HInstanceOf
6235 * or `HCheckCast`.
6236 */
6237enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006238 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 kExactCheck, // Can do a single class compare.
6240 kClassHierarchyCheck, // Can just walk the super class chain.
6241 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6242 kInterfaceCheck, // No optimization yet when checking against an interface.
6243 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006244 kArrayCheck, // No optimization yet when checking against a generic array.
6245 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246};
6247
Roland Levillain86503782016-02-11 19:07:30 +00006248std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6249
Vladimir Markofcb503c2016-05-18 12:48:17 +01006250class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006251 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006252 HInstanceOf(HInstruction* object,
6253 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006254 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006255 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006256 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006258 dex_pc) {
6259 SetPackedField<TypeCheckKindField>(check_kind);
6260 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006261 SetRawInputAt(0, object);
6262 SetRawInputAt(1, constant);
6263 }
6264
6265 bool CanBeMoved() const OVERRIDE { return true; }
6266
Vladimir Marko372f10e2016-05-17 16:30:10 +01006267 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006268 return true;
6269 }
6270
6271 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006272 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006273 }
6274
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006275 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006276 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6277 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6278 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6279 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006280
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006281 static bool CanCallRuntime(TypeCheckKind check_kind) {
6282 // Mips currently does runtime calls for any other checks.
6283 return check_kind != TypeCheckKind::kExactCheck;
6284 }
6285
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006287 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006288 }
6289
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006290 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006291
6292 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006293 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6294 static constexpr size_t kFieldTypeCheckKindSize =
6295 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6296 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6297 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6298 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6299 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006300
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006301 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6302};
6303
Vladimir Markofcb503c2016-05-18 12:48:17 +01006304class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006305 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006306 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006307 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006308 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6309 SetPackedFlag<kFlagUpperCanBeNull>(true);
6310 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006311 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006312 SetRawInputAt(0, input);
6313 }
6314
David Brazdilf5552582015-12-27 13:36:12 +00006315 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006316 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006318 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006319
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006320 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006321 DCHECK(GetUpperCanBeNull() || !can_be_null);
6322 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006323 }
6324
Vladimir Markoa1de9182016-02-25 11:37:38 +00006325 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006326
Calin Juravleb1498f62015-02-16 13:13:29 +00006327 DECLARE_INSTRUCTION(BoundType);
6328
6329 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6331 // is false then CanBeNull() cannot be true).
6332 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6333 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6334 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6335 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6336
Calin Juravleb1498f62015-02-16 13:13:29 +00006337 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006338 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6339 // It is used to bound the type in cases like:
6340 // if (x instanceof ClassX) {
6341 // // uper_bound_ will be ClassX
6342 // }
David Brazdilf5552582015-12-27 13:36:12 +00006343 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006344
6345 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6346};
6347
Vladimir Markofcb503c2016-05-18 12:48:17 +01006348class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006349 public:
6350 HCheckCast(HInstruction* object,
6351 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006353 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006354 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6355 SetPackedField<TypeCheckKindField>(check_kind);
6356 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357 SetRawInputAt(0, object);
6358 SetRawInputAt(1, constant);
6359 }
6360
6361 bool CanBeMoved() const OVERRIDE { return true; }
6362
Vladimir Marko372f10e2016-05-17 16:30:10 +01006363 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006364 return true;
6365 }
6366
6367 bool NeedsEnvironment() const OVERRIDE {
6368 // Instruction may throw a CheckCastError.
6369 return true;
6370 }
6371
6372 bool CanThrow() const OVERRIDE { return true; }
6373
Vladimir Markoa1de9182016-02-25 11:37:38 +00006374 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6375 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6376 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6377 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006378
6379 DECLARE_INSTRUCTION(CheckCast);
6380
6381 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6383 static constexpr size_t kFieldTypeCheckKindSize =
6384 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6385 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6386 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6387 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6388 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006389
6390 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006391};
6392
Andreas Gampe26de38b2016-07-27 17:53:11 -07006393/**
6394 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6395 * @details We define the combined barrier types that are actually required
6396 * by the Java Memory Model, rather than using exactly the terminology from
6397 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6398 * primitives. Note that the JSR-133 cookbook generally does not deal with
6399 * store atomicity issues, and the recipes there are not always entirely sufficient.
6400 * The current recipe is as follows:
6401 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6402 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6403 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6404 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6405 * class has final fields.
6406 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6407 * store-to-memory instructions. Only generated together with non-temporal stores.
6408 */
6409enum MemBarrierKind {
6410 kAnyStore,
6411 kLoadAny,
6412 kStoreStore,
6413 kAnyAny,
6414 kNTStoreStore,
6415 kLastBarrierKind = kNTStoreStore
6416};
6417std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6418
Vladimir Markofcb503c2016-05-18 12:48:17 +01006419class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006420 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006421 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006422 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006423 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6424 SetPackedField<BarrierKindField>(barrier_kind);
6425 }
Calin Juravle27df7582015-04-17 19:12:31 +01006426
Vladimir Markoa1de9182016-02-25 11:37:38 +00006427 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006428
6429 DECLARE_INSTRUCTION(MemoryBarrier);
6430
6431 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006432 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6433 static constexpr size_t kFieldBarrierKindSize =
6434 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6435 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6436 kFieldBarrierKind + kFieldBarrierKindSize;
6437 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6438 "Too many packed fields.");
6439 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006440
6441 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6442};
6443
Vladimir Markofcb503c2016-05-18 12:48:17 +01006444class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006445 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006446 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006447 kEnter,
6448 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006449 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006450 };
6451
6452 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006453 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006454 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6455 dex_pc) {
6456 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006457 SetRawInputAt(0, object);
6458 }
6459
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006460 // Instruction may go into runtime, so we need an environment.
6461 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006462
6463 bool CanThrow() const OVERRIDE {
6464 // Verifier guarantees that monitor-exit cannot throw.
6465 // This is important because it allows the HGraphBuilder to remove
6466 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6467 return IsEnter();
6468 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006469
Vladimir Markoa1de9182016-02-25 11:37:38 +00006470 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6471 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006472
6473 DECLARE_INSTRUCTION(MonitorOperation);
6474
Calin Juravle52c48962014-12-16 17:02:57 +00006475 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006476 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6477 static constexpr size_t kFieldOperationKindSize =
6478 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6479 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6480 kFieldOperationKind + kFieldOperationKindSize;
6481 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6482 "Too many packed fields.");
6483 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006484
6485 private:
6486 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6487};
6488
Vladimir Markofcb503c2016-05-18 12:48:17 +01006489class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006490 public:
6491 HSelect(HInstruction* condition,
6492 HInstruction* true_value,
6493 HInstruction* false_value,
6494 uint32_t dex_pc)
6495 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6496 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6497
6498 // First input must be `true_value` or `false_value` to allow codegens to
6499 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6500 // that architectures which implement HSelect as a conditional move also
6501 // will not need to invert the condition.
6502 SetRawInputAt(0, false_value);
6503 SetRawInputAt(1, true_value);
6504 SetRawInputAt(2, condition);
6505 }
6506
6507 HInstruction* GetFalseValue() const { return InputAt(0); }
6508 HInstruction* GetTrueValue() const { return InputAt(1); }
6509 HInstruction* GetCondition() const { return InputAt(2); }
6510
6511 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006512 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6513 return true;
6514 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006515
6516 bool CanBeNull() const OVERRIDE {
6517 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6518 }
6519
6520 DECLARE_INSTRUCTION(Select);
6521
6522 private:
6523 DISALLOW_COPY_AND_ASSIGN(HSelect);
6524};
6525
Vladimir Markof9f64412015-09-02 14:05:49 +01006526class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006527 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006528 MoveOperands(Location source,
6529 Location destination,
6530 Primitive::Type type,
6531 HInstruction* instruction)
6532 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006533
6534 Location GetSource() const { return source_; }
6535 Location GetDestination() const { return destination_; }
6536
6537 void SetSource(Location value) { source_ = value; }
6538 void SetDestination(Location value) { destination_ = value; }
6539
6540 // The parallel move resolver marks moves as "in-progress" by clearing the
6541 // destination (but not the source).
6542 Location MarkPending() {
6543 DCHECK(!IsPending());
6544 Location dest = destination_;
6545 destination_ = Location::NoLocation();
6546 return dest;
6547 }
6548
6549 void ClearPending(Location dest) {
6550 DCHECK(IsPending());
6551 destination_ = dest;
6552 }
6553
6554 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006555 DCHECK(source_.IsValid() || destination_.IsInvalid());
6556 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006557 }
6558
6559 // True if this blocks a move from the given location.
6560 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006561 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006562 }
6563
6564 // A move is redundant if it's been eliminated, if its source and
6565 // destination are the same, or if its destination is unneeded.
6566 bool IsRedundant() const {
6567 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6568 }
6569
6570 // We clear both operands to indicate move that's been eliminated.
6571 void Eliminate() {
6572 source_ = destination_ = Location::NoLocation();
6573 }
6574
6575 bool IsEliminated() const {
6576 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6577 return source_.IsInvalid();
6578 }
6579
Alexey Frunze4dda3372015-06-01 18:31:49 -07006580 Primitive::Type GetType() const { return type_; }
6581
Nicolas Geoffray90218252015-04-15 11:56:51 +01006582 bool Is64BitMove() const {
6583 return Primitive::Is64BitType(type_);
6584 }
6585
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006586 HInstruction* GetInstruction() const { return instruction_; }
6587
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006588 private:
6589 Location source_;
6590 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006591 // The type this move is for.
6592 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006593 // The instruction this move is assocatied with. Null when this move is
6594 // for moving an input in the expected locations of user (including a phi user).
6595 // This is only used in debug mode, to ensure we do not connect interval siblings
6596 // in the same parallel move.
6597 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006598};
6599
Roland Levillainc9285912015-12-18 10:38:42 +00006600std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6601
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006602static constexpr size_t kDefaultNumberOfMoves = 4;
6603
Vladimir Markofcb503c2016-05-18 12:48:17 +01006604class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006605 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006606 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006607 : HTemplateInstruction(SideEffects::None(), dex_pc),
6608 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6609 moves_.reserve(kDefaultNumberOfMoves);
6610 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006611
Nicolas Geoffray90218252015-04-15 11:56:51 +01006612 void AddMove(Location source,
6613 Location destination,
6614 Primitive::Type type,
6615 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006616 DCHECK(source.IsValid());
6617 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006618 if (kIsDebugBuild) {
6619 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006620 for (const MoveOperands& move : moves_) {
6621 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006622 // Special case the situation where the move is for the spill slot
6623 // of the instruction.
6624 if ((GetPrevious() == instruction)
6625 || ((GetPrevious() == nullptr)
6626 && instruction->IsPhi()
6627 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006628 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006629 << "Doing parallel moves for the same instruction.";
6630 } else {
6631 DCHECK(false) << "Doing parallel moves for the same instruction.";
6632 }
6633 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006634 }
6635 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006636 for (const MoveOperands& move : moves_) {
6637 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006638 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006639 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006640 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006641 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006642 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006643 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006644 }
6645
Vladimir Marko225b6462015-09-28 12:17:40 +01006646 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006647 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006648 }
6649
Vladimir Marko225b6462015-09-28 12:17:40 +01006650 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006651
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006652 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006653
6654 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006655 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006656
6657 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6658};
6659
Mark Mendell0616ae02015-04-17 12:49:27 -04006660} // namespace art
6661
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006662#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6663#include "nodes_shared.h"
6664#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006665#ifdef ART_ENABLE_CODEGEN_arm
6666#include "nodes_arm.h"
6667#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006668#ifdef ART_ENABLE_CODEGEN_arm64
6669#include "nodes_arm64.h"
6670#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006671#ifdef ART_ENABLE_CODEGEN_mips
6672#include "nodes_mips.h"
6673#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006674#ifdef ART_ENABLE_CODEGEN_x86
6675#include "nodes_x86.h"
6676#endif
6677
6678namespace art {
6679
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006680class HGraphVisitor : public ValueObject {
6681 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006682 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6683 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006684
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006685 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006686 virtual void VisitBasicBlock(HBasicBlock* block);
6687
Roland Levillain633021e2014-10-01 14:12:25 +01006688 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006689 void VisitInsertionOrder();
6690
Roland Levillain633021e2014-10-01 14:12:25 +01006691 // Visit the graph following dominator tree reverse post-order.
6692 void VisitReversePostOrder();
6693
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006694 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006695
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006696 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006697#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006698 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6699
6700 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6701
6702#undef DECLARE_VISIT_INSTRUCTION
6703
6704 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006705 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006706
6707 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6708};
6709
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006710class HGraphDelegateVisitor : public HGraphVisitor {
6711 public:
6712 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6713 virtual ~HGraphDelegateVisitor() {}
6714
6715 // Visit functions that delegate to to super class.
6716#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006717 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006718
6719 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6720
6721#undef DECLARE_VISIT_INSTRUCTION
6722
6723 private:
6724 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6725};
6726
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006727// Iterator over the blocks that art part of the loop. Includes blocks part
6728// of an inner loop. The order in which the blocks are iterated is on their
6729// block id.
6730class HBlocksInLoopIterator : public ValueObject {
6731 public:
6732 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6733 : blocks_in_loop_(info.GetBlocks()),
6734 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6735 index_(0) {
6736 if (!blocks_in_loop_.IsBitSet(index_)) {
6737 Advance();
6738 }
6739 }
6740
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006741 bool Done() const { return index_ == blocks_.size(); }
6742 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006743 void Advance() {
6744 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006745 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006746 if (blocks_in_loop_.IsBitSet(index_)) {
6747 break;
6748 }
6749 }
6750 }
6751
6752 private:
6753 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006754 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006755 size_t index_;
6756
6757 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6758};
6759
Mingyao Yang3584bce2015-05-19 16:01:59 -07006760// Iterator over the blocks that art part of the loop. Includes blocks part
6761// of an inner loop. The order in which the blocks are iterated is reverse
6762// post order.
6763class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6764 public:
6765 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6766 : blocks_in_loop_(info.GetBlocks()),
6767 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6768 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006769 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006770 Advance();
6771 }
6772 }
6773
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006774 bool Done() const { return index_ == blocks_.size(); }
6775 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006776 void Advance() {
6777 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006778 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6779 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006780 break;
6781 }
6782 }
6783 }
6784
6785 private:
6786 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006787 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006788 size_t index_;
6789
6790 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6791};
6792
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006793inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006794 if (constant->IsIntConstant()) {
6795 return constant->AsIntConstant()->GetValue();
6796 } else if (constant->IsLongConstant()) {
6797 return constant->AsLongConstant()->GetValue();
6798 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006799 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006800 return 0;
6801 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006802}
6803
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006804#define INSTRUCTION_TYPE_CHECK(type, super) \
6805 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6806 inline const H##type* HInstruction::As##type() const { \
6807 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6808 } \
6809 inline H##type* HInstruction::As##type() { \
6810 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6811 }
6812
6813 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6814#undef INSTRUCTION_TYPE_CHECK
6815
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006816// Create space in `blocks` for adding `number_of_new_blocks` entries
6817// starting at location `at`. Blocks after `at` are moved accordingly.
6818inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6819 size_t number_of_new_blocks,
6820 size_t after) {
6821 DCHECK_LT(after, blocks->size());
6822 size_t old_size = blocks->size();
6823 size_t new_size = old_size + number_of_new_blocks;
6824 blocks->resize(new_size);
6825 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6826}
6827
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006828} // namespace art
6829
6830#endif // ART_COMPILER_OPTIMIZING_NODES_H_