blob: 4a8cfcb15899dbe28dd352d651713a1a782d0462 [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
Vladimir Markof9f64412015-09-02 14:05:49 +01002350template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351class HTemplateInstruction: public HInstruction {
2352 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002353 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002354 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002355 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356
Vladimir Marko372f10e2016-05-17 16:30:10 +01002357 using HInstruction::GetInputRecords; // Keep the const version visible.
2358 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2359 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002360 }
2361
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002362 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002363 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002364
2365 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002366};
2367
Vladimir Markof9f64412015-09-02 14:05:49 +01002368// HTemplateInstruction specialization for N=0.
2369template<>
2370class HTemplateInstruction<0>: public HInstruction {
2371 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002372 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002373 : HInstruction(side_effects, dex_pc) {}
2374
Vladimir Markof9f64412015-09-02 14:05:49 +01002375 virtual ~HTemplateInstruction() {}
2376
Vladimir Marko372f10e2016-05-17 16:30:10 +01002377 using HInstruction::GetInputRecords; // Keep the const version visible.
2378 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2379 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002380 }
2381
2382 private:
2383 friend class SsaBuilder;
2384};
2385
Dave Allison20dfc792014-06-16 20:44:29 -07002386template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002387class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002388 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002389 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002390 : HTemplateInstruction<N>(side_effects, dex_pc) {
2391 this->template SetPackedField<TypeField>(type);
2392 }
Dave Allison20dfc792014-06-16 20:44:29 -07002393 virtual ~HExpression() {}
2394
Vladimir Markoa1de9182016-02-25 11:37:38 +00002395 Primitive::Type GetType() const OVERRIDE {
2396 return TypeField::Decode(this->GetPackedFields());
2397 }
Dave Allison20dfc792014-06-16 20:44:29 -07002398
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002399 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002400 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2401 static constexpr size_t kFieldTypeSize =
2402 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2403 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2404 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2405 "Too many packed fields.");
2406 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002407};
2408
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002409// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2410// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002411class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002412 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2414 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002416 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002417
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002418 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002419
2420 private:
2421 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2422};
2423
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002424// Represents dex's RETURN opcodes. A HReturn is a control flow
2425// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002426class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002427 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002428 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2429 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002430 SetRawInputAt(0, value);
2431 }
2432
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002433 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002434
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002435 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002436
2437 private:
2438 DISALLOW_COPY_AND_ASSIGN(HReturn);
2439};
2440
Vladimir Markofcb503c2016-05-18 12:48:17 +01002441class HPhi FINAL : public HInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002442 public:
2443 HPhi(ArenaAllocator* arena,
2444 uint32_t reg_number,
2445 size_t number_of_inputs,
2446 Primitive::Type type,
2447 uint32_t dex_pc = kNoDexPc)
2448 : HInstruction(SideEffects::None(), dex_pc),
2449 inputs_(number_of_inputs, arena->Adapter(kArenaAllocPhiInputs)),
2450 reg_number_(reg_number) {
2451 SetPackedField<TypeField>(ToPhiType(type));
2452 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2453 // Phis are constructed live and marked dead if conflicting or unused.
2454 // Individual steps of SsaBuilder should assume that if a phi has been
2455 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2456 SetPackedFlag<kFlagIsLive>(true);
2457 SetPackedFlag<kFlagCanBeNull>(true);
2458 }
2459
2460 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2461 static Primitive::Type ToPhiType(Primitive::Type type) {
2462 return Primitive::PrimitiveKind(type);
2463 }
2464
2465 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2466
Vladimir Marko372f10e2016-05-17 16:30:10 +01002467 using HInstruction::GetInputRecords; // Keep the const version visible.
2468 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2469 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2470 }
David Brazdildee58d62016-04-07 09:54:26 +00002471
2472 void AddInput(HInstruction* input);
2473 void RemoveInputAt(size_t index);
2474
2475 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2476 void SetType(Primitive::Type new_type) {
2477 // Make sure that only valid type changes occur. The following are allowed:
2478 // (1) int -> float/ref (primitive type propagation),
2479 // (2) long -> double (primitive type propagation).
2480 DCHECK(GetType() == new_type ||
2481 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2482 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2483 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2484 SetPackedField<TypeField>(new_type);
2485 }
2486
2487 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2488 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2489
2490 uint32_t GetRegNumber() const { return reg_number_; }
2491
2492 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2493 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2494 bool IsDead() const { return !IsLive(); }
2495 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2496
Vladimir Markoe9004912016-06-16 16:50:52 +01002497 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002498 return other != nullptr
2499 && other->IsPhi()
2500 && other->AsPhi()->GetBlock() == GetBlock()
2501 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2502 }
2503
2504 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2505 // An equivalent phi is a phi having the same dex register and type.
2506 // It assumes that phis with the same dex register are adjacent.
2507 HPhi* GetNextEquivalentPhiWithSameType() {
2508 HInstruction* next = GetNext();
2509 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2510 if (next->GetType() == GetType()) {
2511 return next->AsPhi();
2512 }
2513 next = next->GetNext();
2514 }
2515 return nullptr;
2516 }
2517
2518 DECLARE_INSTRUCTION(Phi);
2519
David Brazdildee58d62016-04-07 09:54:26 +00002520 private:
2521 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2522 static constexpr size_t kFieldTypeSize =
2523 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2524 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2525 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2526 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2527 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2528 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2529
Vladimir Marko372f10e2016-05-17 16:30:10 +01002530 ArenaVector<HUserRecord<HInstruction*>> inputs_;
David Brazdildee58d62016-04-07 09:54:26 +00002531 const uint32_t reg_number_;
2532
2533 DISALLOW_COPY_AND_ASSIGN(HPhi);
2534};
2535
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002537// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002538// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002539class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002540 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002541 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002542
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002543 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002544
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002545 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002546
2547 private:
2548 DISALLOW_COPY_AND_ASSIGN(HExit);
2549};
2550
2551// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002552class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002553 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002554 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002555
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002556 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002557
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002558 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002559 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002560 }
2561
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002562 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002563
2564 private:
2565 DISALLOW_COPY_AND_ASSIGN(HGoto);
2566};
2567
Roland Levillain9867bc72015-08-05 10:21:34 +01002568class HConstant : public HExpression<0> {
2569 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002570 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2571 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002572
2573 bool CanBeMoved() const OVERRIDE { return true; }
2574
Roland Levillain1a653882016-03-18 18:05:57 +00002575 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002576 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002577 // Is this constant 0 in the arithmetic sense?
2578 virtual bool IsArithmeticZero() const { return false; }
2579 // Is this constant a 0-bit pattern?
2580 virtual bool IsZeroBitPattern() const { return false; }
2581 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002582 virtual bool IsOne() const { return false; }
2583
David Brazdil77a48ae2015-09-15 12:34:04 +00002584 virtual uint64_t GetValueAsUint64() const = 0;
2585
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002586 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002587
2588 private:
2589 DISALLOW_COPY_AND_ASSIGN(HConstant);
2590};
2591
Vladimir Markofcb503c2016-05-18 12:48:17 +01002592class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002593 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002594 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002595 return true;
2596 }
2597
David Brazdil77a48ae2015-09-15 12:34:04 +00002598 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2599
Roland Levillain9867bc72015-08-05 10:21:34 +01002600 size_t ComputeHashCode() const OVERRIDE { return 0; }
2601
Roland Levillain1a653882016-03-18 18:05:57 +00002602 // The null constant representation is a 0-bit pattern.
2603 virtual bool IsZeroBitPattern() const { return true; }
2604
Roland Levillain9867bc72015-08-05 10:21:34 +01002605 DECLARE_INSTRUCTION(NullConstant);
2606
2607 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002609
2610 friend class HGraph;
2611 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2612};
2613
2614// Constants of the type int. Those can be from Dex instructions, or
2615// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002616class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002617 public:
2618 int32_t GetValue() const { return value_; }
2619
David Brazdil9f389d42015-10-01 14:32:56 +01002620 uint64_t GetValueAsUint64() const OVERRIDE {
2621 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2622 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002623
Vladimir Marko372f10e2016-05-17 16:30:10 +01002624 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002625 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002626 return other->AsIntConstant()->value_ == value_;
2627 }
2628
2629 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2630
2631 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002632 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2633 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2635
Roland Levillain1a653882016-03-18 18:05:57 +00002636 // Integer constants are used to encode Boolean values as well,
2637 // where 1 means true and 0 means false.
2638 bool IsTrue() const { return GetValue() == 1; }
2639 bool IsFalse() const { return GetValue() == 0; }
2640
Roland Levillain9867bc72015-08-05 10:21:34 +01002641 DECLARE_INSTRUCTION(IntConstant);
2642
2643 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002644 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2645 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2646 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2647 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002648
2649 const int32_t value_;
2650
2651 friend class HGraph;
2652 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2653 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2654 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2655};
2656
Vladimir Markofcb503c2016-05-18 12:48:17 +01002657class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 public:
2659 int64_t GetValue() const { return value_; }
2660
David Brazdil77a48ae2015-09-15 12:34:04 +00002661 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2662
Vladimir Marko372f10e2016-05-17 16:30:10 +01002663 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002664 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002665 return other->AsLongConstant()->value_ == value_;
2666 }
2667
2668 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2669
2670 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002671 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2672 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002673 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2674
2675 DECLARE_INSTRUCTION(LongConstant);
2676
2677 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002678 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2679 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002680
2681 const int64_t value_;
2682
2683 friend class HGraph;
2684 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2685};
Dave Allison20dfc792014-06-16 20:44:29 -07002686
Vladimir Markofcb503c2016-05-18 12:48:17 +01002687class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002688 public:
2689 float GetValue() const { return value_; }
2690
2691 uint64_t GetValueAsUint64() const OVERRIDE {
2692 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2693 }
2694
Vladimir Marko372f10e2016-05-17 16:30:10 +01002695 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002696 DCHECK(other->IsFloatConstant()) << other->DebugName();
2697 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2698 }
2699
2700 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2701
2702 bool IsMinusOne() const OVERRIDE {
2703 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2704 }
Roland Levillain1a653882016-03-18 18:05:57 +00002705 bool IsArithmeticZero() const OVERRIDE {
2706 return std::fpclassify(value_) == FP_ZERO;
2707 }
2708 bool IsArithmeticPositiveZero() const {
2709 return IsArithmeticZero() && !std::signbit(value_);
2710 }
2711 bool IsArithmeticNegativeZero() const {
2712 return IsArithmeticZero() && std::signbit(value_);
2713 }
2714 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002715 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002716 }
2717 bool IsOne() const OVERRIDE {
2718 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2719 }
2720 bool IsNaN() const {
2721 return std::isnan(value_);
2722 }
2723
2724 DECLARE_INSTRUCTION(FloatConstant);
2725
2726 private:
2727 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2728 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2729 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2730 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2731
2732 const float value_;
2733
2734 // Only the SsaBuilder and HGraph can create floating-point constants.
2735 friend class SsaBuilder;
2736 friend class HGraph;
2737 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2738};
2739
Vladimir Markofcb503c2016-05-18 12:48:17 +01002740class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002741 public:
2742 double GetValue() const { return value_; }
2743
2744 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2745
Vladimir Marko372f10e2016-05-17 16:30:10 +01002746 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002747 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2748 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2749 }
2750
2751 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2752
2753 bool IsMinusOne() const OVERRIDE {
2754 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2755 }
Roland Levillain1a653882016-03-18 18:05:57 +00002756 bool IsArithmeticZero() const OVERRIDE {
2757 return std::fpclassify(value_) == FP_ZERO;
2758 }
2759 bool IsArithmeticPositiveZero() const {
2760 return IsArithmeticZero() && !std::signbit(value_);
2761 }
2762 bool IsArithmeticNegativeZero() const {
2763 return IsArithmeticZero() && std::signbit(value_);
2764 }
2765 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002766 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002767 }
2768 bool IsOne() const OVERRIDE {
2769 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2770 }
2771 bool IsNaN() const {
2772 return std::isnan(value_);
2773 }
2774
2775 DECLARE_INSTRUCTION(DoubleConstant);
2776
2777 private:
2778 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2779 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2780 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2781 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2782
2783 const double value_;
2784
2785 // Only the SsaBuilder and HGraph can create floating-point constants.
2786 friend class SsaBuilder;
2787 friend class HGraph;
2788 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2789};
2790
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002791// Conditional branch. A block ending with an HIf instruction must have
2792// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002793class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002794 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002795 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2796 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002797 SetRawInputAt(0, input);
2798 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002799
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002800 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002801
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002802 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002803 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002804 }
2805
2806 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002807 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002808 }
2809
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002810 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002811
2812 private:
2813 DISALLOW_COPY_AND_ASSIGN(HIf);
2814};
2815
David Brazdilfc6a86a2015-06-26 10:33:45 +00002816
2817// Abstract instruction which marks the beginning and/or end of a try block and
2818// links it to the respective exception handlers. Behaves the same as a Goto in
2819// non-exceptional control flow.
2820// Normal-flow successor is stored at index zero, exception handlers under
2821// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002822class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002824 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002825 kEntry,
2826 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002827 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002828 };
2829
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002830 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002831 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2832 SetPackedField<BoundaryKindField>(kind);
2833 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002834
2835 bool IsControlFlow() const OVERRIDE { return true; }
2836
2837 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002838 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002839
David Brazdild26a4112015-11-10 11:07:31 +00002840 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2841 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2842 }
2843
David Brazdilfc6a86a2015-06-26 10:33:45 +00002844 // Returns whether `handler` is among its exception handlers (non-zero index
2845 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002846 bool HasExceptionHandler(const HBasicBlock& handler) const {
2847 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002848 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002849 }
2850
2851 // If not present already, adds `handler` to its block's list of exception
2852 // handlers.
2853 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002854 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002855 GetBlock()->AddSuccessor(handler);
2856 }
2857 }
2858
Vladimir Markoa1de9182016-02-25 11:37:38 +00002859 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2860 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002861
David Brazdilffee3d32015-07-06 11:48:53 +01002862 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2863
David Brazdilfc6a86a2015-06-26 10:33:45 +00002864 DECLARE_INSTRUCTION(TryBoundary);
2865
2866 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002867 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2868 static constexpr size_t kFieldBoundaryKindSize =
2869 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2870 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2871 kFieldBoundaryKind + kFieldBoundaryKindSize;
2872 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2873 "Too many packed fields.");
2874 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002875
2876 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2877};
2878
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002879// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002880class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002881 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002882 // We set CanTriggerGC to prevent any intermediate address to be live
2883 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002884 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002885 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002886 SetRawInputAt(0, cond);
2887 }
2888
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002889 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002890 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002891 return true;
2892 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002893 bool NeedsEnvironment() const OVERRIDE { return true; }
2894 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002895
2896 DECLARE_INSTRUCTION(Deoptimize);
2897
2898 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002899 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2900};
2901
Mingyao Yang063fc772016-08-02 11:02:54 -07002902// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2903// The compiled code checks this flag value in a guard before devirtualized call and
2904// if it's true, starts to do deoptimization.
2905// It has a 4-byte slot on stack.
2906// TODO: allocate a register for this flag.
2907class HShouldDeoptimizeFlag FINAL : public HExpression<0> {
2908 public:
2909 // TODO: use SideEffects to aid eliminating some CHA guards.
2910 explicit HShouldDeoptimizeFlag(uint32_t dex_pc)
2911 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
2912 }
2913
2914 // We don't eliminate CHA guards yet.
2915 bool CanBeMoved() const OVERRIDE { return false; }
2916
2917 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2918
2919 private:
2920 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2921};
2922
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002923// Represents the ArtMethod that was passed as a first argument to
2924// the method. It is used by instructions that depend on it, like
2925// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002926class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002928 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2929 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002930
2931 DECLARE_INSTRUCTION(CurrentMethod);
2932
2933 private:
2934 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2935};
2936
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002937// Fetches an ArtMethod from the virtual table or the interface method table
2938// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002939class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002940 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002941 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002942 kVTable,
2943 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002944 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002945 };
2946 HClassTableGet(HInstruction* cls,
2947 Primitive::Type type,
2948 TableKind kind,
2949 size_t index,
2950 uint32_t dex_pc)
2951 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00002952 index_(index) {
2953 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002954 SetRawInputAt(0, cls);
2955 }
2956
2957 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002958 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002959 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00002960 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002961 }
2962
Vladimir Markoa1de9182016-02-25 11:37:38 +00002963 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002964 size_t GetIndex() const { return index_; }
2965
2966 DECLARE_INSTRUCTION(ClassTableGet);
2967
2968 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002969 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
2970 static constexpr size_t kFieldTableKindSize =
2971 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
2972 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
2973 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
2974 "Too many packed fields.");
2975 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
2976
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002977 // The index of the ArtMethod in the table.
2978 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002979
2980 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
2981};
2982
Mark Mendellfe57faa2015-09-18 09:26:15 -04002983// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
2984// have one successor for each entry in the switch table, and the final successor
2985// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002986class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04002987 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04002988 HPackedSwitch(int32_t start_value,
2989 uint32_t num_entries,
2990 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04002991 uint32_t dex_pc = kNoDexPc)
2992 : HTemplateInstruction(SideEffects::None(), dex_pc),
2993 start_value_(start_value),
2994 num_entries_(num_entries) {
2995 SetRawInputAt(0, input);
2996 }
2997
2998 bool IsControlFlow() const OVERRIDE { return true; }
2999
3000 int32_t GetStartValue() const { return start_value_; }
3001
Vladimir Marko211c2112015-09-24 16:52:33 +01003002 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003003
3004 HBasicBlock* GetDefaultBlock() const {
3005 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003006 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003007 }
3008 DECLARE_INSTRUCTION(PackedSwitch);
3009
3010 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003011 const int32_t start_value_;
3012 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003013
3014 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3015};
3016
Roland Levillain88cb1752014-10-20 16:36:47 +01003017class HUnaryOperation : public HExpression<1> {
3018 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003019 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3020 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003021 SetRawInputAt(0, input);
3022 }
3023
3024 HInstruction* GetInput() const { return InputAt(0); }
3025 Primitive::Type GetResultType() const { return GetType(); }
3026
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003027 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003028 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003029 return true;
3030 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003031
Roland Levillain31dd3d62016-02-16 12:21:02 +00003032 // Try to statically evaluate `this` and return a HConstant
3033 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003034 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003035 HConstant* TryStaticEvaluation() const;
3036
3037 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003038 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3039 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003040 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3041 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003042
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003044
3045 private:
3046 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3047};
3048
Dave Allison20dfc792014-06-16 20:44:29 -07003049class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003050 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003051 HBinaryOperation(Primitive::Type result_type,
3052 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003053 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003054 SideEffects side_effects = SideEffects::None(),
3055 uint32_t dex_pc = kNoDexPc)
3056 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003057 SetRawInputAt(0, left);
3058 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003059 }
3060
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003061 HInstruction* GetLeft() const { return InputAt(0); }
3062 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003063 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003064
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003065 virtual bool IsCommutative() const { return false; }
3066
3067 // Put constant on the right.
3068 // Returns whether order is changed.
3069 bool OrderInputsWithConstantOnTheRight() {
3070 HInstruction* left = InputAt(0);
3071 HInstruction* right = InputAt(1);
3072 if (left->IsConstant() && !right->IsConstant()) {
3073 ReplaceInput(right, 0);
3074 ReplaceInput(left, 1);
3075 return true;
3076 }
3077 return false;
3078 }
3079
3080 // Order inputs by instruction id, but favor constant on the right side.
3081 // This helps GVN for commutative ops.
3082 void OrderInputs() {
3083 DCHECK(IsCommutative());
3084 HInstruction* left = InputAt(0);
3085 HInstruction* right = InputAt(1);
3086 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3087 return;
3088 }
3089 if (OrderInputsWithConstantOnTheRight()) {
3090 return;
3091 }
3092 // Order according to instruction id.
3093 if (left->GetId() > right->GetId()) {
3094 ReplaceInput(right, 0);
3095 ReplaceInput(left, 1);
3096 }
3097 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003098
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003099 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003100 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003101 return true;
3102 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003103
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 // Try to statically evaluate `this` and return a HConstant
3105 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003106 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003107 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003108
3109 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003110 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3111 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003112 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3113 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003114 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003115 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3116 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003117 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3118 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003119 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3120 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003121 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003122 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3123 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003124
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003125 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003126 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003127 HConstant* GetConstantRight() const;
3128
3129 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003130 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003131 HInstruction* GetLeastConstantLeft() const;
3132
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003133 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003134
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003135 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003136 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3137};
3138
Mark Mendellc4701932015-04-10 13:18:51 -04003139// The comparison bias applies for floating point operations and indicates how NaN
3140// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003141enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003142 kNoBias, // bias is not applicable (i.e. for long operation)
3143 kGtBias, // return 1 for NaN comparisons
3144 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003145 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003146};
3147
Roland Levillain31dd3d62016-02-16 12:21:02 +00003148std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3149
Dave Allison20dfc792014-06-16 20:44:29 -07003150class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003151 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003152 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003153 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3154 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3155 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003156
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003157 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003158 // `instruction`, and disregard moves in between.
3159 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003160
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003161 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003162
3163 virtual IfCondition GetCondition() const = 0;
3164
Mark Mendellc4701932015-04-10 13:18:51 -04003165 virtual IfCondition GetOppositeCondition() const = 0;
3166
Vladimir Markoa1de9182016-02-25 11:37:38 +00003167 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003168 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3169
Vladimir Markoa1de9182016-02-25 11:37:38 +00003170 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3171 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003172
Vladimir Marko372f10e2016-05-17 16:30:10 +01003173 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003174 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003175 }
3176
Roland Levillain4fa13f62015-07-06 18:11:54 +01003177 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003179 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003180 if (if_cond == kCondNE) {
3181 return true;
3182 } else if (if_cond == kCondEQ) {
3183 return false;
3184 }
3185 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003186 }
3187
3188 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003189 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003190 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003191 if (if_cond == kCondEQ) {
3192 return true;
3193 } else if (if_cond == kCondNE) {
3194 return false;
3195 }
3196 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003197 }
3198
Roland Levillain31dd3d62016-02-16 12:21:02 +00003199 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003200 // Needed if we merge a HCompare into a HCondition.
3201 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3202 static constexpr size_t kFieldComparisonBiasSize =
3203 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3204 static constexpr size_t kNumberOfConditionPackedBits =
3205 kFieldComparisonBias + kFieldComparisonBiasSize;
3206 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3207 using ComparisonBiasField =
3208 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3209
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 template <typename T>
3211 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3212
3213 template <typename T>
3214 int32_t CompareFP(T x, T y) const {
3215 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3216 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3217 // Handle the bias.
3218 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3219 }
3220
3221 // Return an integer constant containing the result of a condition evaluated at compile time.
3222 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3223 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3224 }
3225
Dave Allison20dfc792014-06-16 20:44:29 -07003226 private:
3227 DISALLOW_COPY_AND_ASSIGN(HCondition);
3228};
3229
3230// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003231class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003232 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003233 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3234 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003235
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003236 bool IsCommutative() const OVERRIDE { return true; }
3237
Vladimir Marko9e23df52015-11-10 17:14:35 +00003238 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3239 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003240 return MakeConstantCondition(true, GetDexPc());
3241 }
3242 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3243 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3244 }
3245 // In the following Evaluate methods, a HCompare instruction has
3246 // been merged into this HEqual instruction; evaluate it as
3247 // `Compare(x, y) == 0`.
3248 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3249 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3250 GetDexPc());
3251 }
3252 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3253 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3254 }
3255 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3256 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003257 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003258
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003259 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003260
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003261 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003262 return kCondEQ;
3263 }
3264
Mark Mendellc4701932015-04-10 13:18:51 -04003265 IfCondition GetOppositeCondition() const OVERRIDE {
3266 return kCondNE;
3267 }
3268
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003269 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003270 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003271
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003272 DISALLOW_COPY_AND_ASSIGN(HEqual);
3273};
3274
Vladimir Markofcb503c2016-05-18 12:48:17 +01003275class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003276 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003277 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3278 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003279
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003280 bool IsCommutative() const OVERRIDE { return true; }
3281
Vladimir Marko9e23df52015-11-10 17:14:35 +00003282 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3283 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 return MakeConstantCondition(false, GetDexPc());
3285 }
3286 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3287 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3288 }
3289 // In the following Evaluate methods, a HCompare instruction has
3290 // been merged into this HNotEqual instruction; evaluate it as
3291 // `Compare(x, y) != 0`.
3292 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3293 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3294 }
3295 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3296 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3297 }
3298 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3299 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003300 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003301
Dave Allison20dfc792014-06-16 20:44:29 -07003302 DECLARE_INSTRUCTION(NotEqual);
3303
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003304 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003305 return kCondNE;
3306 }
3307
Mark Mendellc4701932015-04-10 13:18:51 -04003308 IfCondition GetOppositeCondition() const OVERRIDE {
3309 return kCondEQ;
3310 }
3311
Dave Allison20dfc792014-06-16 20:44:29 -07003312 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003313 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003314
Dave Allison20dfc792014-06-16 20:44:29 -07003315 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3316};
3317
Vladimir Markofcb503c2016-05-18 12:48:17 +01003318class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003320 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3321 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003322
Roland Levillain9867bc72015-08-05 10:21:34 +01003323 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003324 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003325 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003326 // In the following Evaluate methods, a HCompare instruction has
3327 // been merged into this HLessThan instruction; evaluate it as
3328 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003329 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003330 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3331 }
3332 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3333 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3334 }
3335 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3336 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003337 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003338
Dave Allison20dfc792014-06-16 20:44:29 -07003339 DECLARE_INSTRUCTION(LessThan);
3340
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003341 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003342 return kCondLT;
3343 }
3344
Mark Mendellc4701932015-04-10 13:18:51 -04003345 IfCondition GetOppositeCondition() const OVERRIDE {
3346 return kCondGE;
3347 }
3348
Dave Allison20dfc792014-06-16 20:44:29 -07003349 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003350 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003351
Dave Allison20dfc792014-06-16 20:44:29 -07003352 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3353};
3354
Vladimir Markofcb503c2016-05-18 12:48:17 +01003355class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003356 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003357 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3358 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003359
Roland Levillain9867bc72015-08-05 10:21:34 +01003360 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003361 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003362 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003363 // In the following Evaluate methods, a HCompare instruction has
3364 // been merged into this HLessThanOrEqual instruction; evaluate it as
3365 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003366 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003367 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3368 }
3369 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3370 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3371 }
3372 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3373 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003374 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 DECLARE_INSTRUCTION(LessThanOrEqual);
3377
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003378 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003379 return kCondLE;
3380 }
3381
Mark Mendellc4701932015-04-10 13:18:51 -04003382 IfCondition GetOppositeCondition() const OVERRIDE {
3383 return kCondGT;
3384 }
3385
Dave Allison20dfc792014-06-16 20:44:29 -07003386 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003387 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003388
Dave Allison20dfc792014-06-16 20:44:29 -07003389 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3390};
3391
Vladimir Markofcb503c2016-05-18 12:48:17 +01003392class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003393 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003394 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3395 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003396
Roland Levillain9867bc72015-08-05 10:21:34 +01003397 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003398 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003399 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003400 // In the following Evaluate methods, a HCompare instruction has
3401 // been merged into this HGreaterThan instruction; evaluate it as
3402 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003403 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003404 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3405 }
3406 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3407 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3408 }
3409 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3410 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003411 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 DECLARE_INSTRUCTION(GreaterThan);
3414
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003415 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003416 return kCondGT;
3417 }
3418
Mark Mendellc4701932015-04-10 13:18:51 -04003419 IfCondition GetOppositeCondition() const OVERRIDE {
3420 return kCondLE;
3421 }
3422
Dave Allison20dfc792014-06-16 20:44:29 -07003423 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003424 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003425
Dave Allison20dfc792014-06-16 20:44:29 -07003426 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3427};
3428
Vladimir Markofcb503c2016-05-18 12:48:17 +01003429class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003430 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003431 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3432 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003433
Roland Levillain9867bc72015-08-05 10:21:34 +01003434 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003435 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003436 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003437 // In the following Evaluate methods, a HCompare instruction has
3438 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3439 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003440 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003441 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3442 }
3443 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3444 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3445 }
3446 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3447 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003448 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003449
Dave Allison20dfc792014-06-16 20:44:29 -07003450 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3451
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003452 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003453 return kCondGE;
3454 }
3455
Mark Mendellc4701932015-04-10 13:18:51 -04003456 IfCondition GetOppositeCondition() const OVERRIDE {
3457 return kCondLT;
3458 }
3459
Dave Allison20dfc792014-06-16 20:44:29 -07003460 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003461 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003462
Dave Allison20dfc792014-06-16 20:44:29 -07003463 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3464};
3465
Vladimir Markofcb503c2016-05-18 12:48:17 +01003466class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003467 public:
3468 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3469 : HCondition(first, second, dex_pc) {}
3470
3471 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003472 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003473 }
3474 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003475 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3476 }
3477 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3478 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3479 LOG(FATAL) << DebugName() << " is not defined for float values";
3480 UNREACHABLE();
3481 }
3482 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3483 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3484 LOG(FATAL) << DebugName() << " is not defined for double values";
3485 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003486 }
3487
3488 DECLARE_INSTRUCTION(Below);
3489
3490 IfCondition GetCondition() const OVERRIDE {
3491 return kCondB;
3492 }
3493
3494 IfCondition GetOppositeCondition() const OVERRIDE {
3495 return kCondAE;
3496 }
3497
3498 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003499 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 return MakeUnsigned(x) < MakeUnsigned(y);
3501 }
Aart Bike9f37602015-10-09 11:15:55 -07003502
3503 DISALLOW_COPY_AND_ASSIGN(HBelow);
3504};
3505
Vladimir Markofcb503c2016-05-18 12:48:17 +01003506class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003507 public:
3508 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3509 : HCondition(first, second, dex_pc) {}
3510
3511 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003512 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003513 }
3514 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003515 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3516 }
3517 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3518 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3519 LOG(FATAL) << DebugName() << " is not defined for float values";
3520 UNREACHABLE();
3521 }
3522 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3523 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3524 LOG(FATAL) << DebugName() << " is not defined for double values";
3525 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003526 }
3527
3528 DECLARE_INSTRUCTION(BelowOrEqual);
3529
3530 IfCondition GetCondition() const OVERRIDE {
3531 return kCondBE;
3532 }
3533
3534 IfCondition GetOppositeCondition() const OVERRIDE {
3535 return kCondA;
3536 }
3537
3538 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003539 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003540 return MakeUnsigned(x) <= MakeUnsigned(y);
3541 }
Aart Bike9f37602015-10-09 11:15:55 -07003542
3543 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3544};
3545
Vladimir Markofcb503c2016-05-18 12:48:17 +01003546class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003547 public:
3548 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3549 : HCondition(first, second, dex_pc) {}
3550
3551 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003552 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003553 }
3554 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003555 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3556 }
3557 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3558 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3559 LOG(FATAL) << DebugName() << " is not defined for float values";
3560 UNREACHABLE();
3561 }
3562 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3563 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3564 LOG(FATAL) << DebugName() << " is not defined for double values";
3565 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003566 }
3567
3568 DECLARE_INSTRUCTION(Above);
3569
3570 IfCondition GetCondition() const OVERRIDE {
3571 return kCondA;
3572 }
3573
3574 IfCondition GetOppositeCondition() const OVERRIDE {
3575 return kCondBE;
3576 }
3577
3578 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003579 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003580 return MakeUnsigned(x) > MakeUnsigned(y);
3581 }
Aart Bike9f37602015-10-09 11:15:55 -07003582
3583 DISALLOW_COPY_AND_ASSIGN(HAbove);
3584};
3585
Vladimir Markofcb503c2016-05-18 12:48:17 +01003586class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003587 public:
3588 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3589 : HCondition(first, second, dex_pc) {}
3590
3591 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003592 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003593 }
3594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003595 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3596 }
3597 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3598 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3599 LOG(FATAL) << DebugName() << " is not defined for float values";
3600 UNREACHABLE();
3601 }
3602 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3603 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3604 LOG(FATAL) << DebugName() << " is not defined for double values";
3605 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003606 }
3607
3608 DECLARE_INSTRUCTION(AboveOrEqual);
3609
3610 IfCondition GetCondition() const OVERRIDE {
3611 return kCondAE;
3612 }
3613
3614 IfCondition GetOppositeCondition() const OVERRIDE {
3615 return kCondB;
3616 }
3617
3618 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003619 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003620 return MakeUnsigned(x) >= MakeUnsigned(y);
3621 }
Aart Bike9f37602015-10-09 11:15:55 -07003622
3623 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3624};
Dave Allison20dfc792014-06-16 20:44:29 -07003625
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003626// Instruction to check how two inputs compare to each other.
3627// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003628class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003629 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003630 // Note that `comparison_type` is the type of comparison performed
3631 // between the comparison's inputs, not the type of the instantiated
3632 // HCompare instruction (which is always Primitive::kPrimInt).
3633 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003634 HInstruction* first,
3635 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003636 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003637 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003638 : HBinaryOperation(Primitive::kPrimInt,
3639 first,
3640 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003641 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003642 dex_pc) {
3643 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003644 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3645 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003646 }
3647
Roland Levillain9867bc72015-08-05 10:21:34 +01003648 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003649 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3650
3651 template <typename T>
3652 int32_t ComputeFP(T x, T y) const {
3653 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3654 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3655 // Handle the bias.
3656 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3657 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003658
Roland Levillain9867bc72015-08-05 10:21:34 +01003659 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003660 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3661 // reach this code path when processing a freshly built HIR
3662 // graph. However HCompare integer instructions can be synthesized
3663 // by the instruction simplifier to implement IntegerCompare and
3664 // IntegerSignum intrinsics, so we have to handle this case.
3665 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003666 }
3667 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3669 }
3670 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3671 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3672 }
3673 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3674 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003675 }
3676
Vladimir Marko372f10e2016-05-17 16:30:10 +01003677 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003678 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003679 }
3680
Vladimir Markoa1de9182016-02-25 11:37:38 +00003681 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003682
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003684 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 bool IsGtBias() const {
3686 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003687 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003688 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003689
Roland Levillain1693a1f2016-03-15 14:57:31 +00003690 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3691 // Comparisons do not require a runtime call in any back end.
3692 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003693 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003694
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003695 DECLARE_INSTRUCTION(Compare);
3696
Roland Levillain31dd3d62016-02-16 12:21:02 +00003697 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003698 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3699 static constexpr size_t kFieldComparisonBiasSize =
3700 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3701 static constexpr size_t kNumberOfComparePackedBits =
3702 kFieldComparisonBias + kFieldComparisonBiasSize;
3703 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3704 using ComparisonBiasField =
3705 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3706
Roland Levillain31dd3d62016-02-16 12:21:02 +00003707 // Return an integer constant containing the result of a comparison evaluated at compile time.
3708 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3709 DCHECK(value == -1 || value == 0 || value == 1) << value;
3710 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3711 }
3712
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003713 private:
3714 DISALLOW_COPY_AND_ASSIGN(HCompare);
3715};
3716
Vladimir Markofcb503c2016-05-18 12:48:17 +01003717class HNewInstance FINAL : public HExpression<2> {
David Brazdil6de19382016-01-08 17:37:10 +00003718 public:
3719 HNewInstance(HInstruction* cls,
3720 HCurrentMethod* current_method,
3721 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003722 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003723 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003724 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003725 bool finalizable,
3726 QuickEntrypointEnum entrypoint)
3727 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3728 type_index_(type_index),
3729 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003730 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003731 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003732 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003733 SetRawInputAt(0, cls);
3734 SetRawInputAt(1, current_method);
3735 }
3736
Andreas Gampea5b09a62016-11-17 15:21:22 -08003737 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003738 const DexFile& GetDexFile() const { return dex_file_; }
3739
3740 // Calls runtime so needs an environment.
3741 bool NeedsEnvironment() const OVERRIDE { return true; }
3742
Mingyao Yang062157f2016-03-02 10:15:36 -08003743 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3744 bool CanThrow() const OVERRIDE { return true; }
3745
3746 // Needs to call into runtime to make sure it's instantiable/accessible.
3747 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003748
Vladimir Markoa1de9182016-02-25 11:37:38 +00003749 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003750
3751 bool CanBeNull() const OVERRIDE { return false; }
3752
3753 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3754
3755 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3756 entrypoint_ = entrypoint;
3757 }
3758
3759 bool IsStringAlloc() const;
3760
3761 DECLARE_INSTRUCTION(NewInstance);
3762
3763 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003764 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3765 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003766 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3767 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3768 "Too many packed fields.");
3769
Andreas Gampea5b09a62016-11-17 15:21:22 -08003770 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003771 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003772 QuickEntrypointEnum entrypoint_;
3773
3774 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3775};
3776
Agi Csaki05f20562015-08-19 14:58:14 -07003777enum IntrinsicNeedsEnvironmentOrCache {
3778 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3779 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003780};
3781
Aart Bik5d75afe2015-12-14 11:57:01 -08003782enum IntrinsicSideEffects {
3783 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3784 kReadSideEffects, // Intrinsic may read heap memory.
3785 kWriteSideEffects, // Intrinsic may write heap memory.
3786 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3787};
3788
3789enum IntrinsicExceptions {
3790 kNoThrow, // Intrinsic does not throw any exceptions.
3791 kCanThrow // Intrinsic may throw exceptions.
3792};
3793
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003794class HInvoke : public HInstruction {
3795 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003796 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003797
Vladimir Marko372f10e2016-05-17 16:30:10 +01003798 using HInstruction::GetInputRecords; // Keep the const version visible.
3799 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
3800 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
3801 }
3802
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003803 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003804 SetRawInputAt(index, argument);
3805 }
3806
Roland Levillain3e3d7332015-04-28 11:00:54 +01003807 // Return the number of arguments. This number can be lower than
3808 // the number of inputs returned by InputCount(), as some invoke
3809 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3810 // inputs at the end of their list of inputs.
3811 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3812
Vladimir Markoa1de9182016-02-25 11:37:38 +00003813 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003814
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003815 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003816 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003817
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003818 InvokeType GetInvokeType() const {
3819 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003820 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003821
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003822 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003823 return intrinsic_;
3824 }
3825
Aart Bik5d75afe2015-12-14 11:57:01 -08003826 void SetIntrinsic(Intrinsics intrinsic,
3827 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3828 IntrinsicSideEffects side_effects,
3829 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003830
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003831 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003832 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003833 }
3834
Aart Bikff7d89c2016-11-07 08:49:28 -08003835 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3836
Vladimir Markoa1de9182016-02-25 11:37:38 +00003837 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003838
Aart Bik71bf7b42016-11-16 10:17:46 -08003839 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003840
Vladimir Marko372f10e2016-05-17 16:30:10 +01003841 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003842 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3843 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003844
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003845 uint32_t* GetIntrinsicOptimizations() {
3846 return &intrinsic_optimizations_;
3847 }
3848
3849 const uint32_t* GetIntrinsicOptimizations() const {
3850 return &intrinsic_optimizations_;
3851 }
3852
3853 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3854
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003855 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3856
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003857 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003858
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003859 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003860 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3861 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003862 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3863 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003864 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 static constexpr size_t kFieldReturnTypeSize =
3866 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3867 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3868 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3869 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003870 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003871 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3872
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003873 HInvoke(ArenaAllocator* arena,
3874 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003875 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003876 Primitive::Type return_type,
3877 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003878 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003879 ArtMethod* resolved_method,
3880 InvokeType invoke_type)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003881 : HInstruction(
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003882 SideEffects::AllExceptGCDependency(), dex_pc), // Assume write/read on all fields/arrays.
Roland Levillain3e3d7332015-04-28 11:00:54 +01003883 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003884 resolved_method_(resolved_method),
Vladimir Markob7d8e8c2015-09-17 15:47:05 +01003885 inputs_(number_of_arguments + number_of_other_inputs,
3886 arena->Adapter(kArenaAllocInvokeInputs)),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003887 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003888 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003889 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003890 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003891 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003892 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003893 }
3894
Roland Levillain3e3d7332015-04-28 11:00:54 +01003895 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003896 ArtMethod* const resolved_method_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01003897 ArenaVector<HUserRecord<HInstruction*>> inputs_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003898 const uint32_t dex_method_index_;
3899 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003900
3901 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3902 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003903
3904 private:
3905 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3906};
3907
Vladimir Markofcb503c2016-05-18 12:48:17 +01003908class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003909 public:
3910 HInvokeUnresolved(ArenaAllocator* arena,
3911 uint32_t number_of_arguments,
3912 Primitive::Type return_type,
3913 uint32_t dex_pc,
3914 uint32_t dex_method_index,
3915 InvokeType invoke_type)
3916 : HInvoke(arena,
3917 number_of_arguments,
3918 0u /* number_of_other_inputs */,
3919 return_type,
3920 dex_pc,
3921 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003922 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003923 invoke_type) {
3924 }
3925
3926 DECLARE_INSTRUCTION(InvokeUnresolved);
3927
3928 private:
3929 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3930};
3931
Vladimir Markofcb503c2016-05-18 12:48:17 +01003932class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003933 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01003934 // Requirements of this method call regarding the class
3935 // initialization (clinit) check of its declaring class.
3936 enum class ClinitCheckRequirement {
3937 kNone, // Class already initialized.
3938 kExplicit, // Static call having explicit clinit check as last input.
3939 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00003940 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01003941 };
3942
Vladimir Marko58155012015-08-19 12:49:41 +00003943 // Determines how to load the target ArtMethod*.
3944 enum class MethodLoadKind {
3945 // Use a String init ArtMethod* loaded from Thread entrypoints.
3946 kStringInit,
3947
3948 // Use the method's own ArtMethod* loaded by the register allocator.
3949 kRecursive,
3950
3951 // Use ArtMethod* at a known address, embed the direct address in the code.
3952 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
3953 kDirectAddress,
3954
3955 // Use ArtMethod* at an address that will be known at link time, embed the direct
3956 // address in the code. If the image is relocatable, emit .patch_oat entry.
3957 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3958 // the image relocatable or not.
3959 kDirectAddressWithFixup,
3960
Vladimir Markoa1de9182016-02-25 11:37:38 +00003961 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00003962 // Used when we need to use the dex cache, for example for invoke-static that
3963 // may cause class initialization (the entry may point to a resolution method),
3964 // and we know that we can access the dex cache arrays using a PC-relative load.
3965 kDexCachePcRelative,
3966
3967 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
3968 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
3969 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
3970 // or unimplemented or impractical (i.e. slow) on a particular architecture.
3971 kDexCacheViaMethod,
3972 };
3973
3974 // Determines the location of the code pointer.
3975 enum class CodePtrLocation {
3976 // Recursive call, use local PC-relative call instruction.
3977 kCallSelf,
3978
3979 // Use PC-relative call instruction patched at link time.
3980 // Used for calls within an oat file, boot->boot or app->app.
3981 kCallPCRelative,
3982
3983 // Call to a known target address, embed the direct address in code.
3984 // Used for app->boot call with non-relocatable image and for JIT-compiled calls.
3985 kCallDirect,
3986
3987 // Call to a target address that will be known at link time, embed the direct
3988 // address in code. If the image is relocatable, emit .patch_oat entry.
3989 // Used for app->boot calls with relocatable image and boot->boot calls, whether
3990 // the image relocatable or not.
3991 kCallDirectWithFixup,
3992
3993 // Use code pointer from the ArtMethod*.
3994 // Used when we don't know the target code. This is also the last-resort-kind used when
3995 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
3996 kCallArtMethod,
3997 };
3998
3999 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004000 MethodLoadKind method_load_kind;
4001 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004002 // The method load data holds
4003 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4004 // Note that there are multiple string init methods, each having its own offset.
4005 // - the method address for kDirectAddress
4006 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004007 uint64_t method_load_data;
4008 uint64_t direct_code_ptr;
Vladimir Marko58155012015-08-19 12:49:41 +00004009 };
4010
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004011 HInvokeStaticOrDirect(ArenaAllocator* arena,
4012 uint32_t number_of_arguments,
4013 Primitive::Type return_type,
4014 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004015 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004016 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004017 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004018 InvokeType invoke_type,
4019 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004020 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004021 : HInvoke(arena,
4022 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004023 // There is potentially one extra argument for the HCurrentMethod node, and
4024 // potentially one other if the clinit check is explicit, and potentially
4025 // one other if the method is a string factory.
4026 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004027 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004028 return_type,
4029 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004030 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004031 resolved_method,
4032 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004033 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004034 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4036 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004037
Vladimir Markodc151b22015-10-15 18:02:30 +01004038 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004039 bool had_current_method_input = HasCurrentMethodInput();
4040 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4041
4042 // Using the current method is the default and once we find a better
4043 // method load kind, we should not go back to using the current method.
4044 DCHECK(had_current_method_input || !needs_current_method_input);
4045
4046 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004047 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4048 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004049 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004050 dispatch_info_ = dispatch_info;
4051 }
4052
Vladimir Markoc53c0792015-11-19 15:48:33 +00004053 void AddSpecialInput(HInstruction* input) {
4054 // We allow only one special input.
4055 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4056 DCHECK(InputCount() == GetSpecialInputIndex() ||
4057 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4058 InsertInputAt(GetSpecialInputIndex(), input);
4059 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004060
Vladimir Marko372f10e2016-05-17 16:30:10 +01004061 using HInstruction::GetInputRecords; // Keep the const version visible.
4062 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4063 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4064 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4065 DCHECK(!input_records.empty());
4066 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4067 HInstruction* last_input = input_records.back().GetInstruction();
4068 // Note: `last_input` may be null during arguments setup.
4069 if (last_input != nullptr) {
4070 // `last_input` is the last input of a static invoke marked as having
4071 // an explicit clinit check. It must either be:
4072 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4073 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4074 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4075 }
4076 }
4077 return input_records;
4078 }
4079
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004080 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004081 // We access the method via the dex cache so we can't do an implicit null check.
4082 // TODO: for intrinsics we can generate implicit null checks.
4083 return false;
4084 }
4085
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004086 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004087 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004088 }
4089
Vladimir Markoc53c0792015-11-19 15:48:33 +00004090 // Get the index of the special input, if any.
4091 //
David Brazdil6de19382016-01-08 17:37:10 +00004092 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4093 // method pointer; otherwise there may be one platform-specific special input,
4094 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004095 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004096 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004097
Vladimir Marko58155012015-08-19 12:49:41 +00004098 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4099 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4100 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004101 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004102 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004103 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004104 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004105 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4106 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004107 bool HasCurrentMethodInput() const {
4108 // This function can be called only after the invoke has been fully initialized by the builder.
4109 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004110 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004111 return true;
4112 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004113 DCHECK(InputCount() == GetSpecialInputIndex() ||
4114 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004115 return false;
4116 }
4117 }
Vladimir Marko58155012015-08-19 12:49:41 +00004118 bool HasDirectCodePtr() const { return GetCodePtrLocation() == CodePtrLocation::kCallDirect; }
Vladimir Marko58155012015-08-19 12:49:41 +00004119
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004120 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004121 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004122 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004123 }
4124
4125 uint64_t GetMethodAddress() const {
4126 DCHECK(HasMethodAddress());
4127 return dispatch_info_.method_load_data;
4128 }
4129
4130 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004131 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004132 return dispatch_info_.method_load_data;
4133 }
4134
4135 uint64_t GetDirectCodePtr() const {
4136 DCHECK(HasDirectCodePtr());
4137 return dispatch_info_.direct_code_ptr;
4138 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004139
Vladimir Markoa1de9182016-02-25 11:37:38 +00004140 ClinitCheckRequirement GetClinitCheckRequirement() const {
4141 return GetPackedField<ClinitCheckRequirementField>();
4142 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004143
Roland Levillain4c0eb422015-04-24 16:43:49 +01004144 // Is this instruction a call to a static method?
4145 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004146 return GetInvokeType() == kStatic;
4147 }
4148
4149 MethodReference GetTargetMethod() const {
4150 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004151 }
4152
Vladimir Markofbb184a2015-11-13 14:47:00 +00004153 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4154 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4155 // instruction; only relevant for static calls with explicit clinit check.
4156 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004157 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004158 size_t last_input_index = inputs_.size() - 1u;
4159 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004160 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004161 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004162 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004163 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004164 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004165 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004166 }
4167
4168 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004169 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004170 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004171 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004172 }
4173
4174 // Is this a call to a static method whose declaring class has an
4175 // implicit intialization check requirement?
4176 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004177 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004178 }
4179
Vladimir Markob554b5a2015-11-06 12:57:55 +00004180 // Does this method load kind need the current method as an input?
4181 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4182 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4183 }
4184
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004185 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004186
Roland Levillain4c0eb422015-04-24 16:43:49 +01004187 protected:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004188 void InsertInputAt(size_t index, HInstruction* input);
4189 void RemoveInputAt(size_t index);
4190
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004191 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004192 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 static constexpr size_t kFieldClinitCheckRequirementSize =
4194 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4195 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4196 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4197 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4198 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004199 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4200 kFieldClinitCheckRequirement,
4201 kFieldClinitCheckRequirementSize>;
4202
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004203 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004204 MethodReference target_method_;
4205 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004206
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004207 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004208};
Vladimir Markof64242a2015-12-01 14:58:23 +00004209std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004210std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004211
Vladimir Markofcb503c2016-05-18 12:48:17 +01004212class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004213 public:
4214 HInvokeVirtual(ArenaAllocator* arena,
4215 uint32_t number_of_arguments,
4216 Primitive::Type return_type,
4217 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004218 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004219 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004220 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004221 : HInvoke(arena,
4222 number_of_arguments,
4223 0u,
4224 return_type,
4225 dex_pc,
4226 dex_method_index,
4227 resolved_method,
4228 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004229 vtable_index_(vtable_index) {}
4230
Aart Bik71bf7b42016-11-16 10:17:46 -08004231 bool CanBeNull() const OVERRIDE {
4232 switch (GetIntrinsic()) {
4233 case Intrinsics::kThreadCurrentThread:
4234 case Intrinsics::kStringBufferAppend:
4235 case Intrinsics::kStringBufferToString:
4236 case Intrinsics::kStringBuilderAppend:
4237 case Intrinsics::kStringBuilderToString:
4238 return false;
4239 default:
4240 return HInvoke::CanBeNull();
4241 }
4242 }
4243
Calin Juravle641547a2015-04-21 22:08:51 +01004244 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004245 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004246 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004247 }
4248
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004249 uint32_t GetVTableIndex() const { return vtable_index_; }
4250
4251 DECLARE_INSTRUCTION(InvokeVirtual);
4252
4253 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004254 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004255 const uint32_t vtable_index_;
4256
4257 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4258};
4259
Vladimir Markofcb503c2016-05-18 12:48:17 +01004260class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004261 public:
4262 HInvokeInterface(ArenaAllocator* arena,
4263 uint32_t number_of_arguments,
4264 Primitive::Type return_type,
4265 uint32_t dex_pc,
4266 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004267 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004268 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004269 : HInvoke(arena,
4270 number_of_arguments,
4271 0u,
4272 return_type,
4273 dex_pc,
4274 dex_method_index,
4275 resolved_method,
4276 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004277 imt_index_(imt_index) {}
4278
Calin Juravle641547a2015-04-21 22:08:51 +01004279 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004280 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004281 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004282 }
4283
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004284 uint32_t GetImtIndex() const { return imt_index_; }
4285 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4286
4287 DECLARE_INSTRUCTION(InvokeInterface);
4288
4289 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004290 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004291 const uint32_t imt_index_;
4292
4293 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4294};
4295
Vladimir Markofcb503c2016-05-18 12:48:17 +01004296class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004297 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004298 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004299 : HUnaryOperation(result_type, input, dex_pc) {
4300 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4301 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004302
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004303 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004304
4305 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004306 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004307 }
4308 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004309 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004310 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004311 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4312 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4313 }
4314 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4315 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4316 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004317
Roland Levillain88cb1752014-10-20 16:36:47 +01004318 DECLARE_INSTRUCTION(Neg);
4319
4320 private:
4321 DISALLOW_COPY_AND_ASSIGN(HNeg);
4322};
4323
Vladimir Markofcb503c2016-05-18 12:48:17 +01004324class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004325 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004326 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004327 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004328 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004329 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004330 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004331 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004332 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004333 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004334 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004335 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004336 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004337 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004338 }
4339
Andreas Gampea5b09a62016-11-17 15:21:22 -08004340 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004341 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004342
4343 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004344 bool NeedsEnvironment() const OVERRIDE { return true; }
4345
Mingyao Yang0c365e62015-03-31 15:09:29 -07004346 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4347 bool CanThrow() const OVERRIDE { return true; }
4348
Calin Juravle10e244f2015-01-26 18:54:32 +00004349 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004350
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004351 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4352
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004353 DECLARE_INSTRUCTION(NewArray);
4354
4355 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004356 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004357 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004358 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004359
4360 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4361};
4362
Vladimir Markofcb503c2016-05-18 12:48:17 +01004363class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004364 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004365 HAdd(Primitive::Type result_type,
4366 HInstruction* left,
4367 HInstruction* right,
4368 uint32_t dex_pc = kNoDexPc)
4369 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004370
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004371 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004372
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004373 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004374
4375 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004376 return GetBlock()->GetGraph()->GetIntConstant(
4377 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004378 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004379 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004380 return GetBlock()->GetGraph()->GetLongConstant(
4381 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004382 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004383 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4384 return GetBlock()->GetGraph()->GetFloatConstant(
4385 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4386 }
4387 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4388 return GetBlock()->GetGraph()->GetDoubleConstant(
4389 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4390 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004391
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004392 DECLARE_INSTRUCTION(Add);
4393
4394 private:
4395 DISALLOW_COPY_AND_ASSIGN(HAdd);
4396};
4397
Vladimir Markofcb503c2016-05-18 12:48:17 +01004398class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004399 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004400 HSub(Primitive::Type result_type,
4401 HInstruction* left,
4402 HInstruction* right,
4403 uint32_t dex_pc = kNoDexPc)
4404 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004405
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004406 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004407
4408 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004409 return GetBlock()->GetGraph()->GetIntConstant(
4410 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004411 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004412 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004413 return GetBlock()->GetGraph()->GetLongConstant(
4414 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004415 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004416 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4417 return GetBlock()->GetGraph()->GetFloatConstant(
4418 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4419 }
4420 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4421 return GetBlock()->GetGraph()->GetDoubleConstant(
4422 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4423 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004424
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004425 DECLARE_INSTRUCTION(Sub);
4426
4427 private:
4428 DISALLOW_COPY_AND_ASSIGN(HSub);
4429};
4430
Vladimir Markofcb503c2016-05-18 12:48:17 +01004431class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004432 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004433 HMul(Primitive::Type result_type,
4434 HInstruction* left,
4435 HInstruction* right,
4436 uint32_t dex_pc = kNoDexPc)
4437 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004438
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004439 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004440
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004441 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004442
4443 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004444 return GetBlock()->GetGraph()->GetIntConstant(
4445 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004446 }
4447 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004448 return GetBlock()->GetGraph()->GetLongConstant(
4449 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004450 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004451 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4452 return GetBlock()->GetGraph()->GetFloatConstant(
4453 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4454 }
4455 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4456 return GetBlock()->GetGraph()->GetDoubleConstant(
4457 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4458 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004459
4460 DECLARE_INSTRUCTION(Mul);
4461
4462 private:
4463 DISALLOW_COPY_AND_ASSIGN(HMul);
4464};
4465
Vladimir Markofcb503c2016-05-18 12:48:17 +01004466class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004467 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004468 HDiv(Primitive::Type result_type,
4469 HInstruction* left,
4470 HInstruction* right,
4471 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004472 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004473
Roland Levillain9867bc72015-08-05 10:21:34 +01004474 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004475 T ComputeIntegral(T x, T y) const {
4476 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004477 // Our graph structure ensures we never have 0 for `y` during
4478 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004479 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004480 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004481 return (y == -1) ? -x : x / y;
4482 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004483
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 template <typename T>
4485 T ComputeFP(T x, T y) const {
4486 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4487 return x / y;
4488 }
4489
Roland Levillain9867bc72015-08-05 10:21:34 +01004490 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004491 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004492 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004493 }
4494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004495 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004496 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4497 }
4498 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4499 return GetBlock()->GetGraph()->GetFloatConstant(
4500 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4501 }
4502 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4503 return GetBlock()->GetGraph()->GetDoubleConstant(
4504 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004505 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004506
4507 DECLARE_INSTRUCTION(Div);
4508
4509 private:
4510 DISALLOW_COPY_AND_ASSIGN(HDiv);
4511};
4512
Vladimir Markofcb503c2016-05-18 12:48:17 +01004513class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004514 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004515 HRem(Primitive::Type result_type,
4516 HInstruction* left,
4517 HInstruction* right,
4518 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004519 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004520
Roland Levillain9867bc72015-08-05 10:21:34 +01004521 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004522 T ComputeIntegral(T x, T y) const {
4523 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004524 // Our graph structure ensures we never have 0 for `y` during
4525 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004526 DCHECK_NE(y, 0);
4527 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4528 return (y == -1) ? 0 : x % y;
4529 }
4530
Roland Levillain31dd3d62016-02-16 12:21:02 +00004531 template <typename T>
4532 T ComputeFP(T x, T y) const {
4533 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4534 return std::fmod(x, y);
4535 }
4536
Roland Levillain9867bc72015-08-05 10:21:34 +01004537 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004538 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004539 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 }
4541 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004542 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004543 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004544 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4546 return GetBlock()->GetGraph()->GetFloatConstant(
4547 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4548 }
4549 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4550 return GetBlock()->GetGraph()->GetDoubleConstant(
4551 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4552 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004553
4554 DECLARE_INSTRUCTION(Rem);
4555
4556 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004557 DISALLOW_COPY_AND_ASSIGN(HRem);
4558};
4559
Vladimir Markofcb503c2016-05-18 12:48:17 +01004560class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004561 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004562 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4563 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004564 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004565 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004566 SetRawInputAt(0, value);
4567 }
4568
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004569 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4570
Calin Juravled0d48522014-11-04 16:40:20 +00004571 bool CanBeMoved() const OVERRIDE { return true; }
4572
Vladimir Marko372f10e2016-05-17 16:30:10 +01004573 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004574 return true;
4575 }
4576
4577 bool NeedsEnvironment() const OVERRIDE { return true; }
4578 bool CanThrow() const OVERRIDE { return true; }
4579
Calin Juravled0d48522014-11-04 16:40:20 +00004580 DECLARE_INSTRUCTION(DivZeroCheck);
4581
4582 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004583 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4584};
4585
Vladimir Markofcb503c2016-05-18 12:48:17 +01004586class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004587 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004589 HInstruction* value,
4590 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4593 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4594 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004595 }
4596
Roland Levillain5b5b9312016-03-22 14:57:31 +00004597 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004598 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004599 return value << (distance & max_shift_distance);
4600 }
4601
4602 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004603 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004604 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004605 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004607 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004608 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004610 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4611 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4612 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4613 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004615 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4616 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004617 LOG(FATAL) << DebugName() << " is not defined for float values";
4618 UNREACHABLE();
4619 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4621 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004622 LOG(FATAL) << DebugName() << " is not defined for double values";
4623 UNREACHABLE();
4624 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004625
4626 DECLARE_INSTRUCTION(Shl);
4627
4628 private:
4629 DISALLOW_COPY_AND_ASSIGN(HShl);
4630};
4631
Vladimir Markofcb503c2016-05-18 12:48:17 +01004632class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004634 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004635 HInstruction* value,
4636 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4639 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4640 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 }
4642
Roland Levillain5b5b9312016-03-22 14:57:31 +00004643 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004644 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004645 return value >> (distance & max_shift_distance);
4646 }
4647
4648 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004649 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004650 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004651 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004652 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004654 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4657 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4658 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4659 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004660 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004661 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4662 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004663 LOG(FATAL) << DebugName() << " is not defined for float values";
4664 UNREACHABLE();
4665 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004666 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4667 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004668 LOG(FATAL) << DebugName() << " is not defined for double values";
4669 UNREACHABLE();
4670 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004671
4672 DECLARE_INSTRUCTION(Shr);
4673
4674 private:
4675 DISALLOW_COPY_AND_ASSIGN(HShr);
4676};
4677
Vladimir Markofcb503c2016-05-18 12:48:17 +01004678class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004681 HInstruction* value,
4682 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4685 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4686 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004687 }
4688
Roland Levillain5b5b9312016-03-22 14:57:31 +00004689 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004690 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004691 typedef typename std::make_unsigned<T>::type V;
4692 V ux = static_cast<V>(value);
4693 return static_cast<T>(ux >> (distance & max_shift_distance));
4694 }
4695
4696 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004698 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004699 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004700 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4705 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4706 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4707 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004708 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004709 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4710 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004711 LOG(FATAL) << DebugName() << " is not defined for float values";
4712 UNREACHABLE();
4713 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4715 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004716 LOG(FATAL) << DebugName() << " is not defined for double values";
4717 UNREACHABLE();
4718 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004719
4720 DECLARE_INSTRUCTION(UShr);
4721
4722 private:
4723 DISALLOW_COPY_AND_ASSIGN(HUShr);
4724};
4725
Vladimir Markofcb503c2016-05-18 12:48:17 +01004726class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004727 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004728 HAnd(Primitive::Type result_type,
4729 HInstruction* left,
4730 HInstruction* right,
4731 uint32_t dex_pc = kNoDexPc)
4732 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004733
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004734 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004735
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004736 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004737
4738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004739 return GetBlock()->GetGraph()->GetIntConstant(
4740 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004741 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004742 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004743 return GetBlock()->GetGraph()->GetLongConstant(
4744 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004745 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004746 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4747 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4748 LOG(FATAL) << DebugName() << " is not defined for float values";
4749 UNREACHABLE();
4750 }
4751 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4752 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4753 LOG(FATAL) << DebugName() << " is not defined for double values";
4754 UNREACHABLE();
4755 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004756
4757 DECLARE_INSTRUCTION(And);
4758
4759 private:
4760 DISALLOW_COPY_AND_ASSIGN(HAnd);
4761};
4762
Vladimir Markofcb503c2016-05-18 12:48:17 +01004763class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004764 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004765 HOr(Primitive::Type result_type,
4766 HInstruction* left,
4767 HInstruction* right,
4768 uint32_t dex_pc = kNoDexPc)
4769 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004770
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004771 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004772
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004773 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004774
4775 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004776 return GetBlock()->GetGraph()->GetIntConstant(
4777 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004778 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004779 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004780 return GetBlock()->GetGraph()->GetLongConstant(
4781 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004782 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004783 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4784 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4785 LOG(FATAL) << DebugName() << " is not defined for float values";
4786 UNREACHABLE();
4787 }
4788 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4789 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4790 LOG(FATAL) << DebugName() << " is not defined for double values";
4791 UNREACHABLE();
4792 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004793
4794 DECLARE_INSTRUCTION(Or);
4795
4796 private:
4797 DISALLOW_COPY_AND_ASSIGN(HOr);
4798};
4799
Vladimir Markofcb503c2016-05-18 12:48:17 +01004800class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004801 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 HXor(Primitive::Type result_type,
4803 HInstruction* left,
4804 HInstruction* right,
4805 uint32_t dex_pc = kNoDexPc)
4806 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004807
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004808 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004809
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004810 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004811
4812 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004813 return GetBlock()->GetGraph()->GetIntConstant(
4814 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004817 return GetBlock()->GetGraph()->GetLongConstant(
4818 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004820 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4821 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for float values";
4823 UNREACHABLE();
4824 }
4825 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4826 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4827 LOG(FATAL) << DebugName() << " is not defined for double values";
4828 UNREACHABLE();
4829 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004830
4831 DECLARE_INSTRUCTION(Xor);
4832
4833 private:
4834 DISALLOW_COPY_AND_ASSIGN(HXor);
4835};
4836
Vladimir Markofcb503c2016-05-18 12:48:17 +01004837class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004838 public:
4839 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004840 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004841 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4842 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004843 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004844
Roland Levillain5b5b9312016-03-22 14:57:31 +00004845 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004846 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004847 typedef typename std::make_unsigned<T>::type V;
4848 V ux = static_cast<V>(value);
4849 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004850 return static_cast<T>(ux);
4851 } else {
4852 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004853 return static_cast<T>(ux >> (distance & max_shift_value)) |
4854 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004855 }
4856 }
4857
Roland Levillain5b5b9312016-03-22 14:57:31 +00004858 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004859 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004860 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004861 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004863 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004865 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4867 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4868 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4869 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004870 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4872 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004873 LOG(FATAL) << DebugName() << " is not defined for float values";
4874 UNREACHABLE();
4875 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004876 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4877 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004878 LOG(FATAL) << DebugName() << " is not defined for double values";
4879 UNREACHABLE();
4880 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004881
4882 DECLARE_INSTRUCTION(Ror);
4883
4884 private:
4885 DISALLOW_COPY_AND_ASSIGN(HRor);
4886};
4887
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004888// The value of a parameter in this method. Its location depends on
4889// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004890class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004891 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004892 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004893 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004894 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004895 Primitive::Type parameter_type,
4896 bool is_this = false)
4897 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004898 dex_file_(dex_file),
4899 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004900 index_(index) {
4901 SetPackedFlag<kFlagIsThis>(is_this);
4902 SetPackedFlag<kFlagCanBeNull>(!is_this);
4903 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004904
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004905 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004906 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004907 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004908 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004909
Vladimir Markoa1de9182016-02-25 11:37:38 +00004910 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4911 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004912
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004913 DECLARE_INSTRUCTION(ParameterValue);
4914
4915 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004916 // Whether or not the parameter value corresponds to 'this' argument.
4917 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4918 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4919 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4920 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4921 "Too many packed fields.");
4922
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004923 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004924 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004925 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004926 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927 const uint8_t index_;
4928
4929 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4930};
4931
Vladimir Markofcb503c2016-05-18 12:48:17 +01004932class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004933 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004934 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4935 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004936
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004937 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004938 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004939 return true;
4940 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004941
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004942 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004943
4944 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
4947 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004948 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004949 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004950 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4951 LOG(FATAL) << DebugName() << " is not defined for float values";
4952 UNREACHABLE();
4953 }
4954 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4955 LOG(FATAL) << DebugName() << " is not defined for double values";
4956 UNREACHABLE();
4957 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004958
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004959 DECLARE_INSTRUCTION(Not);
4960
4961 private:
4962 DISALLOW_COPY_AND_ASSIGN(HNot);
4963};
4964
Vladimir Markofcb503c2016-05-18 12:48:17 +01004965class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01004966 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
4968 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01004969
4970 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004971 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01004972 return true;
4973 }
4974
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004975 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004976 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01004977 return !x;
4978 }
4979
Roland Levillain9867bc72015-08-05 10:21:34 +01004980 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 }
4983 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4984 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01004985 UNREACHABLE();
4986 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004987 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4988 LOG(FATAL) << DebugName() << " is not defined for float values";
4989 UNREACHABLE();
4990 }
4991 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
4992 LOG(FATAL) << DebugName() << " is not defined for double values";
4993 UNREACHABLE();
4994 }
David Brazdil66d126e2015-04-03 16:02:44 +01004995
4996 DECLARE_INSTRUCTION(BooleanNot);
4997
4998 private:
4999 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5000};
5001
Vladimir Markofcb503c2016-05-18 12:48:17 +01005002class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005003 public:
5004 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005005 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005006 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005007 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005008 // Invariant: We should never generate a conversion to a Boolean value.
5009 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005010 }
5011
5012 HInstruction* GetInput() const { return InputAt(0); }
5013 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5014 Primitive::Type GetResultType() const { return GetType(); }
5015
5016 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005017 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5018 return true;
5019 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005020
Mark Mendelle82549b2015-05-06 10:55:34 -04005021 // Try to statically evaluate the conversion and return a HConstant
5022 // containing the result. If the input cannot be converted, return nullptr.
5023 HConstant* TryStaticEvaluation() const;
5024
Roland Levillaindff1f282014-11-05 14:15:05 +00005025 DECLARE_INSTRUCTION(TypeConversion);
5026
5027 private:
5028 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5029};
5030
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005031static constexpr uint32_t kNoRegNumber = -1;
5032
Vladimir Markofcb503c2016-05-18 12:48:17 +01005033class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005034 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005035 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5036 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005037 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005038 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039 SetRawInputAt(0, value);
5040 }
5041
Calin Juravle10e244f2015-01-26 18:54:32 +00005042 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005043 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005044 return true;
5045 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005046
Calin Juravle10e244f2015-01-26 18:54:32 +00005047 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005048
Calin Juravle10e244f2015-01-26 18:54:32 +00005049 bool CanThrow() const OVERRIDE { return true; }
5050
5051 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005052
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053
5054 DECLARE_INSTRUCTION(NullCheck);
5055
5056 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005057 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5058};
5059
5060class FieldInfo : public ValueObject {
5061 public:
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005062 FieldInfo(MemberOffset field_offset,
5063 Primitive::Type field_type,
5064 bool is_volatile,
5065 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005066 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005067 const DexFile& dex_file,
5068 Handle<mirror::DexCache> dex_cache)
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005069 : field_offset_(field_offset),
5070 field_type_(field_type),
5071 is_volatile_(is_volatile),
5072 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005073 declaring_class_def_index_(declaring_class_def_index),
Mathieu Chartier736b5602015-09-02 14:54:11 -07005074 dex_file_(dex_file),
5075 dex_cache_(dex_cache) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005076
5077 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005078 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005079 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005080 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005081 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005082 bool IsVolatile() const { return is_volatile_; }
Mathieu Chartier736b5602015-09-02 14:54:11 -07005083 Handle<mirror::DexCache> GetDexCache() const { return dex_cache_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005084
5085 private:
5086 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005088 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005089 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005090 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005091 const DexFile& dex_file_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005092 const Handle<mirror::DexCache> dex_cache_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093};
5094
Vladimir Markofcb503c2016-05-18 12:48:17 +01005095class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005096 public:
5097 HInstanceFieldGet(HInstruction* value,
5098 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005099 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005100 bool is_volatile,
5101 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005102 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005103 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005104 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005105 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005106 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005107 field_info_(field_offset,
5108 field_type,
5109 is_volatile,
5110 field_idx,
5111 declaring_class_def_index,
5112 dex_file,
5113 dex_cache) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114 SetRawInputAt(0, value);
5115 }
5116
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005117 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005118
Vladimir Marko372f10e2016-05-17 16:30:10 +01005119 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5120 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005121 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005122 }
5123
Calin Juravle641547a2015-04-21 22:08:51 +01005124 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005125 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005126 }
5127
5128 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005129 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5130 }
5131
Calin Juravle52c48962014-12-16 17:02:57 +00005132 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005134 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005135 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005136
5137 DECLARE_INSTRUCTION(InstanceFieldGet);
5138
5139 private:
5140 const FieldInfo field_info_;
5141
5142 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5143};
5144
Vladimir Markofcb503c2016-05-18 12:48:17 +01005145class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146 public:
5147 HInstanceFieldSet(HInstruction* object,
5148 HInstruction* value,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005149 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005150 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005151 bool is_volatile,
5152 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005153 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005154 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005155 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005156 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005157 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005158 field_info_(field_offset,
5159 field_type,
5160 is_volatile,
5161 field_idx,
5162 declaring_class_def_index,
5163 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00005164 dex_cache) {
5165 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166 SetRawInputAt(0, object);
5167 SetRawInputAt(1, value);
5168 }
5169
Calin Juravle641547a2015-04-21 22:08:51 +01005170 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005171 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005172 }
5173
Calin Juravle52c48962014-12-16 17:02:57 +00005174 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005176 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005177 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005178 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005179 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5180 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005181
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005182 DECLARE_INSTRUCTION(InstanceFieldSet);
5183
5184 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005185 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5186 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5187 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5188 "Too many packed fields.");
5189
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 const FieldInfo field_info_;
5191
5192 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5193};
5194
Vladimir Markofcb503c2016-05-18 12:48:17 +01005195class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005197 HArrayGet(HInstruction* array,
5198 HInstruction* index,
5199 Primitive::Type type,
5200 uint32_t dex_pc,
5201 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005202 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005203 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204 SetRawInputAt(0, array);
5205 SetRawInputAt(1, index);
5206 }
5207
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005208 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005209 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005210 return true;
5211 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005212 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 // TODO: We can be smarter here.
5214 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5215 // which generates the implicit null check. There are cases when these can be removed
5216 // to produce better code. If we ever add optimizations to do so we should allow an
5217 // implicit check here (as long as the address falls in the first page).
5218 return false;
5219 }
5220
David Brazdil4833f5a2015-12-16 10:37:39 +00005221 bool IsEquivalentOf(HArrayGet* other) const {
5222 bool result = (GetDexPc() == other->GetDexPc());
5223 if (kIsDebugBuild && result) {
5224 DCHECK_EQ(GetBlock(), other->GetBlock());
5225 DCHECK_EQ(GetArray(), other->GetArray());
5226 DCHECK_EQ(GetIndex(), other->GetIndex());
5227 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005228 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005229 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005230 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5231 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005232 }
5233 }
5234 return result;
5235 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005236
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005237 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5238
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005239 HInstruction* GetArray() const { return InputAt(0); }
5240 HInstruction* GetIndex() const { return InputAt(1); }
5241
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242 DECLARE_INSTRUCTION(ArrayGet);
5243
5244 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005245 // We treat a String as an array, creating the HArrayGet from String.charAt()
5246 // intrinsic in the instruction simplifier. We can always determine whether
5247 // a particular HArrayGet is actually a String.charAt() by looking at the type
5248 // of the input but that requires holding the mutator lock, so we prefer to use
5249 // a flag, so that code generators don't need to do the locking.
5250 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5251 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5252 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5253 "Too many packed fields.");
5254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5256};
5257
Vladimir Markofcb503c2016-05-18 12:48:17 +01005258class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 public:
5260 HArraySet(HInstruction* array,
5261 HInstruction* index,
5262 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005263 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005264 uint32_t dex_pc)
5265 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005266 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5267 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5268 SetPackedFlag<kFlagValueCanBeNull>(true);
5269 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 SetRawInputAt(0, array);
5271 SetRawInputAt(1, index);
5272 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005273 // Make a best guess now, may be refined during SSA building.
5274 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 }
5276
Calin Juravle77520bc2015-01-12 18:45:46 +00005277 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005278 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005279 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 }
5281
Mingyao Yang81014cb2015-06-02 03:16:27 -07005282 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005283 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005284
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005285 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005286 // TODO: Same as for ArrayGet.
5287 return false;
5288 }
5289
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005290 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005291 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005292 }
5293
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005294 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005295 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005296 }
5297
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005299 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300 }
5301
Vladimir Markoa1de9182016-02-25 11:37:38 +00005302 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5303 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5304 bool StaticTypeOfArrayIsObjectArray() const {
5305 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5306 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005307
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005308 HInstruction* GetArray() const { return InputAt(0); }
5309 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005310 HInstruction* GetValue() const { return InputAt(2); }
5311
5312 Primitive::Type GetComponentType() const {
5313 // The Dex format does not type floating point index operations. Since the
5314 // `expected_component_type_` is set during building and can therefore not
5315 // be correct, we also check what is the value type. If it is a floating
5316 // point type, we must use that type.
5317 Primitive::Type value_type = GetValue()->GetType();
5318 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5319 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005320 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005321 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005322
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005323 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005324 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005325 }
5326
Aart Bik18b36ab2016-04-13 16:41:35 -07005327 void ComputeSideEffects() {
5328 Primitive::Type type = GetComponentType();
5329 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5330 SideEffectsForArchRuntimeCalls(type)));
5331 }
5332
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005333 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5334 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5335 }
5336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 DECLARE_INSTRUCTION(ArraySet);
5338
5339 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005340 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5341 static constexpr size_t kFieldExpectedComponentTypeSize =
5342 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5343 static constexpr size_t kFlagNeedsTypeCheck =
5344 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5345 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005346 // Cached information for the reference_type_info_ so that codegen
5347 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005348 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5349 static constexpr size_t kNumberOfArraySetPackedBits =
5350 kFlagStaticTypeOfArrayIsObjectArray + 1;
5351 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5352 using ExpectedComponentTypeField =
5353 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354
5355 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5356};
5357
Vladimir Markofcb503c2016-05-18 12:48:17 +01005358class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005360 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005361 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005362 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005363 // Note that arrays do not change length, so the instruction does not
5364 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365 SetRawInputAt(0, array);
5366 }
5367
Calin Juravle77520bc2015-01-12 18:45:46 +00005368 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005369 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005370 return true;
5371 }
Calin Juravle641547a2015-04-21 22:08:51 +01005372 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5373 return obj == InputAt(0);
5374 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005375
Vladimir Markodce016e2016-04-28 13:10:02 +01005376 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5377
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 DECLARE_INSTRUCTION(ArrayLength);
5379
5380 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005381 // We treat a String as an array, creating the HArrayLength from String.length()
5382 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5383 // determine whether a particular HArrayLength is actually a String.length() by
5384 // looking at the type of the input but that requires holding the mutator lock, so
5385 // we prefer to use a flag, so that code generators don't need to do the locking.
5386 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5387 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5388 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5389 "Too many packed fields.");
5390
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5392};
5393
Vladimir Markofcb503c2016-05-18 12:48:17 +01005394class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005396 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5397 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005398 HBoundsCheck(HInstruction* index,
5399 HInstruction* length,
5400 uint32_t dex_pc,
5401 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5402 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5403 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005404 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 SetRawInputAt(0, index);
5406 SetRawInputAt(1, length);
5407 }
5408
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005409 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005410 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005411 return true;
5412 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005413
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005414 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005416 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005417
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005418 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5419 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5420
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005421 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005422
5423 DECLARE_INSTRUCTION(BoundsCheck);
5424
5425 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005426 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5427 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5428 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5429 // so we need to create an HEnvironment which will be translated to an InlineInfo
5430 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5431 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5432 // from the invoke as we don't want to look again at the dex bytecode.
5433 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5434
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5436};
5437
Vladimir Markofcb503c2016-05-18 12:48:17 +01005438class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005439 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005440 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005441 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005442
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005443 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444 return true;
5445 }
5446
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005447 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5448 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005449
5450 DECLARE_INSTRUCTION(SuspendCheck);
5451
5452 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005453 // Only used for code generation, in order to share the same slow path between back edges
5454 // of a same loop.
5455 SlowPathCode* slow_path_;
5456
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005457 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5458};
5459
David Srbecky0cf44932015-12-09 14:09:59 +00005460// Pseudo-instruction which provides the native debugger with mapping information.
5461// It ensures that we can generate line number and local variables at this point.
5462class HNativeDebugInfo : public HTemplateInstruction<0> {
5463 public:
5464 explicit HNativeDebugInfo(uint32_t dex_pc)
5465 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5466
5467 bool NeedsEnvironment() const OVERRIDE {
5468 return true;
5469 }
5470
5471 DECLARE_INSTRUCTION(NativeDebugInfo);
5472
5473 private:
5474 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5475};
5476
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005477/**
5478 * Instruction to load a Class object.
5479 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005481 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005482 // Determines how to load the Class.
5483 enum class LoadKind {
5484 // Use the Class* from the method's own ArtMethod*.
5485 kReferrersClass,
5486
5487 // Use boot image Class* address that will be known at link time.
5488 // Used for boot image classes referenced by boot image code in non-PIC mode.
5489 kBootImageLinkTimeAddress,
5490
5491 // Use PC-relative boot image Class* address that will be known at link time.
5492 // Used for boot image classes referenced by boot image code in PIC mode.
5493 kBootImageLinkTimePcRelative,
5494
5495 // Use a known boot image Class* address, embedded in the code by the codegen.
5496 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5497 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5498 // GetIncludePatchInformation().
5499 kBootImageAddress,
5500
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005501 // Load from the root table associated with the JIT compiled method.
5502 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005503
5504 // Load from resolved types array in the dex cache using a PC-relative load.
5505 // Used for classes outside boot image when we know that we can access
5506 // the dex cache arrays using a PC-relative load.
5507 kDexCachePcRelative,
5508
5509 // Load from resolved types array accessed through the class loaded from
5510 // the compiled method's own ArtMethod*. This is the default access type when
5511 // all other types are unavailable.
5512 kDexCacheViaMethod,
5513
5514 kLast = kDexCacheViaMethod
5515 };
5516
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005517 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005518 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005519 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005520 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005521 uint32_t dex_pc,
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005522 bool needs_access_check,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005523 bool is_in_dex_cache,
5524 bool is_in_boot_image)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005525 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5526 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005527 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005528 dex_file_(dex_file),
Calin Juravle2e768302015-07-28 14:41:11 +00005529 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005530 // Referrers class should not need access check. We never inline unverified
5531 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005532 DCHECK(!is_referrers_class || !needs_access_check);
5533
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005534 SetPackedField<LoadKindField>(
5535 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005536 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
5537 SetPackedFlag<kFlagIsInDexCache>(is_in_dex_cache);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005538 SetPackedFlag<kFlagIsInBootImage>(is_in_boot_image);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005539 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005540 }
5541
5542 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5543 DCHECK(HasAddress(load_kind));
5544 load_data_.address = address;
5545 SetLoadKindInternal(load_kind);
5546 }
5547
5548 void SetLoadKindWithTypeReference(LoadKind load_kind,
5549 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005550 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005551 DCHECK(HasTypeReference(load_kind));
5552 DCHECK(IsSameDexFile(dex_file_, dex_file));
5553 DCHECK_EQ(type_index_, type_index);
5554 SetLoadKindInternal(load_kind);
5555 }
5556
5557 void SetLoadKindWithDexCacheReference(LoadKind load_kind,
5558 const DexFile& dex_file,
5559 uint32_t element_index) {
5560 DCHECK(HasDexCacheReference(load_kind));
5561 DCHECK(IsSameDexFile(dex_file_, dex_file));
5562 load_data_.dex_cache_element_index = element_index;
5563 SetLoadKindInternal(load_kind);
5564 }
5565
5566 LoadKind GetLoadKind() const {
5567 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005568 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005569
5570 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005573
Andreas Gampea5b09a62016-11-17 15:21:22 -08005574 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005575
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005576 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005578 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005579 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005580 }
5581
Calin Juravle0ba218d2015-05-19 18:46:01 +01005582 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005583 // The entrypoint the code generator is going to call does not do
5584 // clinit of the class.
5585 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005586 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005587 }
5588
5589 bool CanCallRuntime() const {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005590 return MustGenerateClinitCheck() ||
Vladimir Markoa1de9182016-02-25 11:37:38 +00005591 (!IsReferrersClass() && !IsInDexCache()) ||
5592 NeedsAccessCheck();
Calin Juravle98893e12015-10-02 21:05:03 +01005593 }
5594
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005595 bool CanThrow() const OVERRIDE {
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01005596 return CanCallRuntime();
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005597 }
5598
Calin Juravleacf735c2015-02-12 15:25:22 +00005599 ReferenceTypeInfo GetLoadedClassRTI() {
5600 return loaded_class_rti_;
5601 }
5602
5603 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5604 // Make sure we only set exact types (the loaded class should never be merged).
5605 DCHECK(rti.IsExact());
5606 loaded_class_rti_ = rti;
5607 }
5608
Andreas Gampea5b09a62016-11-17 15:21:22 -08005609 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005610 const DexFile& GetDexFile() const { return dex_file_; }
5611
5612 uint32_t GetDexCacheElementOffset() const;
5613
5614 uint64_t GetAddress() const {
5615 DCHECK(HasAddress(GetLoadKind()));
5616 return load_data_.address;
5617 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005618
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005619 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5620 return !IsReferrersClass();
5621 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005622
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005623 static SideEffects SideEffectsForArchRuntimeCalls() {
5624 return SideEffects::CanTriggerGC();
5625 }
5626
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005627 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005628 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
5629 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005630 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005631 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005632
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005633 void MarkInDexCache() {
5634 SetPackedFlag<kFlagIsInDexCache>(true);
5635 DCHECK(!NeedsEnvironment());
5636 RemoveEnvironment();
5637 SetSideEffects(SideEffects::None());
5638 }
5639
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005640 void MarkInBootImage() {
5641 SetPackedFlag<kFlagIsInBootImage>(true);
5642 }
5643
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005644 void AddSpecialInput(HInstruction* special_input);
5645
5646 using HInstruction::GetInputRecords; // Keep the const version visible.
5647 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5648 return ArrayRef<HUserRecord<HInstruction*>>(
5649 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5650 }
5651
5652 Primitive::Type GetType() const OVERRIDE {
5653 return Primitive::kPrimNot;
5654 }
5655
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005656 DECLARE_INSTRUCTION(LoadClass);
5657
5658 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005659 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005660 static constexpr size_t kFlagIsInDexCache = kFlagNeedsAccessCheck + 1;
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005661 static constexpr size_t kFlagIsInBootImage = kFlagIsInDexCache + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005662 // Whether this instruction must generate the initialization check.
5663 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005664 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5666 static constexpr size_t kFieldLoadKindSize =
5667 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5668 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005670 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5671
5672 static bool HasTypeReference(LoadKind load_kind) {
5673 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5674 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5675 load_kind == LoadKind::kDexCacheViaMethod ||
5676 load_kind == LoadKind::kReferrersClass;
5677 }
5678
5679 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005680 return load_kind == LoadKind::kBootImageAddress ||
5681 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 }
5683
5684 static bool HasDexCacheReference(LoadKind load_kind) {
5685 return load_kind == LoadKind::kDexCachePcRelative;
5686 }
5687
5688 void SetLoadKindInternal(LoadKind load_kind);
5689
5690 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5691 // For other load kinds it's empty or possibly some architecture-specific instruction
5692 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
5693 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005694
Andreas Gampea5b09a62016-11-17 15:21:22 -08005695 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005696 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005697
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005698 union {
5699 uint32_t dex_cache_element_index; // Only for dex cache reference.
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005700 uint64_t address; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005701 } load_data_;
5702
Calin Juravleacf735c2015-02-12 15:25:22 +00005703 ReferenceTypeInfo loaded_class_rti_;
5704
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005705 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5706};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5708
5709// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5710inline uint32_t HLoadClass::GetDexCacheElementOffset() const {
5711 DCHECK(HasDexCacheReference(GetLoadKind())) << GetLoadKind();
5712 return load_data_.dex_cache_element_index;
5713}
5714
5715// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
5716inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005717 // The special input is used for PC-relative loads on some architectures,
5718 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005720 GetLoadKind() == LoadKind::kDexCachePcRelative ||
5721 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5722 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723 DCHECK(special_input_.GetInstruction() == nullptr);
5724 special_input_ = HUserRecord<HInstruction*>(special_input);
5725 special_input->AddUseAt(this, 0);
5726}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727
Vladimir Marko372f10e2016-05-17 16:30:10 +01005728class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005729 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005730 // Determines how to load the String.
5731 enum class LoadKind {
5732 // Use boot image String* address that will be known at link time.
5733 // Used for boot image strings referenced by boot image code in non-PIC mode.
5734 kBootImageLinkTimeAddress,
5735
5736 // Use PC-relative boot image String* address that will be known at link time.
5737 // Used for boot image strings referenced by boot image code in PIC mode.
5738 kBootImageLinkTimePcRelative,
5739
5740 // Use a known boot image String* address, embedded in the code by the codegen.
5741 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5742 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5743 // GetIncludePatchInformation().
5744 kBootImageAddress,
5745
Vladimir Markoaad75c62016-10-03 08:46:48 +00005746 // Load from an entry in the .bss section using a PC-relative load.
5747 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5748 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005749
5750 // Load from resolved strings array accessed through the class loaded from
5751 // the compiled method's own ArtMethod*. This is the default access type when
5752 // all other types are unavailable.
5753 kDexCacheViaMethod,
5754
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005755 // Load from the root table associated with the JIT compiled method.
5756 kJitTableAddress,
5757
5758 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005759 };
5760
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005761 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005762 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005763 const DexFile& dex_file,
5764 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005765 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5766 special_input_(HUserRecord<HInstruction*>(current_method)),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005767 string_index_(string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005768 SetPackedFlag<kFlagIsInDexCache>(false);
5769 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005770 load_data_.dex_file_ = &dex_file;
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005771 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005772
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005773 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5774 DCHECK(HasAddress(load_kind));
5775 load_data_.address = address;
5776 SetLoadKindInternal(load_kind);
5777 }
5778
5779 void SetLoadKindWithStringReference(LoadKind load_kind,
5780 const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005781 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005782 DCHECK(HasStringReference(load_kind));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005783 load_data_.dex_file_ = &dex_file;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005784 string_index_ = string_index;
5785 SetLoadKindInternal(load_kind);
5786 }
5787
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005788 LoadKind GetLoadKind() const {
5789 return GetPackedField<LoadKindField>();
5790 }
5791
5792 const DexFile& GetDexFile() const;
5793
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005794 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005795 DCHECK(HasStringReference(GetLoadKind()) || /* For slow paths. */ !IsInDexCache());
5796 return string_index_;
5797 }
5798
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005799 uint64_t GetAddress() const {
5800 DCHECK(HasAddress(GetLoadKind()));
5801 return load_data_.address;
5802 }
5803
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005804 bool CanBeMoved() const OVERRIDE { return true; }
5805
Vladimir Marko372f10e2016-05-17 16:30:10 +01005806 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005807
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005808 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005809
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005810 // Will call the runtime if we need to load the string through
5811 // the dex cache and the string is not guaranteed to be there yet.
5812 bool NeedsEnvironment() const OVERRIDE {
5813 LoadKind load_kind = GetLoadKind();
5814 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5815 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005816 load_kind == LoadKind::kBootImageAddress ||
5817 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 return false;
5819 }
5820 return !IsInDexCache();
5821 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005822
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005823 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5824 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5825 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005826
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005827 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005828 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005829
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005830 static SideEffects SideEffectsForArchRuntimeCalls() {
5831 return SideEffects::CanTriggerGC();
5832 }
5833
Vladimir Markoa1de9182016-02-25 11:37:38 +00005834 bool IsInDexCache() const { return GetPackedFlag<kFlagIsInDexCache>(); }
5835
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005836 void MarkInDexCache() {
5837 SetPackedFlag<kFlagIsInDexCache>(true);
5838 DCHECK(!NeedsEnvironment());
5839 RemoveEnvironment();
Vladimir Markoace7a002016-04-05 11:18:49 +01005840 SetSideEffects(SideEffects::None());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005841 }
5842
Vladimir Marko372f10e2016-05-17 16:30:10 +01005843 void AddSpecialInput(HInstruction* special_input);
5844
5845 using HInstruction::GetInputRecords; // Keep the const version visible.
5846 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5847 return ArrayRef<HUserRecord<HInstruction*>>(
5848 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005849 }
5850
Vladimir Marko372f10e2016-05-17 16:30:10 +01005851 Primitive::Type GetType() const OVERRIDE {
5852 return Primitive::kPrimNot;
5853 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005854
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005855 DECLARE_INSTRUCTION(LoadString);
5856
5857 private:
Vladimir Marko372f10e2016-05-17 16:30:10 +01005858 static constexpr size_t kFlagIsInDexCache = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005859 static constexpr size_t kFieldLoadKind = kFlagIsInDexCache + 1;
5860 static constexpr size_t kFieldLoadKindSize =
5861 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5862 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005863 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005864 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005865
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 static bool HasStringReference(LoadKind load_kind) {
5867 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5868 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005869 load_kind == LoadKind::kBssEntry ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005870 load_kind == LoadKind::kDexCacheViaMethod ||
5871 load_kind == LoadKind::kJitTableAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 }
5873
5874 static bool HasAddress(LoadKind load_kind) {
Vladimir Marko54d6a202016-11-09 12:46:38 +00005875 return load_kind == LoadKind::kBootImageAddress;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 }
5877
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005878 void SetLoadKindInternal(LoadKind load_kind);
5879
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005880 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5881 // For other load kinds it's empty or possibly some architecture-specific instruction
5882 // for PC-relative loads, i.e. kDexCachePcRelative or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005883 HUserRecord<HInstruction*> special_input_;
5884
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 // String index serves also as the hash code and it's also needed for slow-paths,
5886 // so it must not be overwritten with other load data.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005887 dex::StringIndex string_index_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888
5889 union {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005890 const DexFile* dex_file_; // For string reference.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 uint64_t address; // Up to 64-bit, needed for kDexCacheAddress on 64-bit targets.
5892 } load_data_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005893
5894 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5895};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5897
5898// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5899inline const DexFile& HLoadString::GetDexFile() const {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005900 DCHECK(HasStringReference(GetLoadKind())) << GetLoadKind();
5901 return *load_data_.dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902}
5903
5904// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
5905inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005906 // The special input is used for PC-relative loads on some architectures,
5907 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005909 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005910 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5911 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005912 // HLoadString::GetInputRecords() returns an empty array at this point,
5913 // so use the GetInputRecords() from the base class to set the input record.
5914 DCHECK(special_input_.GetInstruction() == nullptr);
5915 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 special_input->AddUseAt(this, 0);
5917}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005918
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005919/**
5920 * Performs an initialization check on its Class object input.
5921 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005922class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005924 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005925 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005926 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005927 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5928 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005929 SetRawInputAt(0, constant);
5930 }
5931
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005932 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005933 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005934 return true;
5935 }
5936
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005937 bool NeedsEnvironment() const OVERRIDE {
5938 // May call runtime to initialize the class.
5939 return true;
5940 }
5941
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005942 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943
5944 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5945
5946 DECLARE_INSTRUCTION(ClinitCheck);
5947
5948 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5950};
5951
Vladimir Markofcb503c2016-05-18 12:48:17 +01005952class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953 public:
5954 HStaticFieldGet(HInstruction* cls,
5955 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005956 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005957 bool is_volatile,
5958 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005959 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005960 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005961 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01005962 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005963 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005964 field_info_(field_offset,
5965 field_type,
5966 is_volatile,
5967 field_idx,
5968 declaring_class_def_index,
5969 dex_file,
5970 dex_cache) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005971 SetRawInputAt(0, cls);
5972 }
5973
Calin Juravle52c48962014-12-16 17:02:57 +00005974
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005975 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005976
Vladimir Marko372f10e2016-05-17 16:30:10 +01005977 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5978 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005979 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005980 }
5981
5982 size_t ComputeHashCode() const OVERRIDE {
5983 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5984 }
5985
Calin Juravle52c48962014-12-16 17:02:57 +00005986 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005987 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5988 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005989 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005990
5991 DECLARE_INSTRUCTION(StaticFieldGet);
5992
5993 private:
5994 const FieldInfo field_info_;
5995
5996 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5997};
5998
Vladimir Markofcb503c2016-05-18 12:48:17 +01005999class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006000 public:
6001 HStaticFieldSet(HInstruction* cls,
6002 HInstruction* value,
6003 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006004 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006005 bool is_volatile,
6006 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006007 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006008 const DexFile& dex_file,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006009 Handle<mirror::DexCache> dex_cache,
Calin Juravle154746b2015-10-06 15:46:54 +01006010 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006011 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Mingyao Yang8df69d42015-10-22 15:40:58 -07006012 field_info_(field_offset,
6013 field_type,
6014 is_volatile,
6015 field_idx,
6016 declaring_class_def_index,
6017 dex_file,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006018 dex_cache) {
6019 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006020 SetRawInputAt(0, cls);
6021 SetRawInputAt(1, value);
6022 }
6023
Calin Juravle52c48962014-12-16 17:02:57 +00006024 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6026 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006027 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006028
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006029 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006030 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6031 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006032
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006033 DECLARE_INSTRUCTION(StaticFieldSet);
6034
6035 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006036 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6037 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6038 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6039 "Too many packed fields.");
6040
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041 const FieldInfo field_info_;
6042
6043 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6044};
6045
Vladimir Markofcb503c2016-05-18 12:48:17 +01006046class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006047 public:
6048 HUnresolvedInstanceFieldGet(HInstruction* obj,
6049 Primitive::Type field_type,
6050 uint32_t field_index,
6051 uint32_t dex_pc)
6052 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6053 field_index_(field_index) {
6054 SetRawInputAt(0, obj);
6055 }
6056
6057 bool NeedsEnvironment() const OVERRIDE { return true; }
6058 bool CanThrow() const OVERRIDE { return true; }
6059
6060 Primitive::Type GetFieldType() const { return GetType(); }
6061 uint32_t GetFieldIndex() const { return field_index_; }
6062
6063 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6064
6065 private:
6066 const uint32_t field_index_;
6067
6068 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6069};
6070
Vladimir Markofcb503c2016-05-18 12:48:17 +01006071class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 public:
6073 HUnresolvedInstanceFieldSet(HInstruction* obj,
6074 HInstruction* value,
6075 Primitive::Type field_type,
6076 uint32_t field_index,
6077 uint32_t dex_pc)
6078 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006079 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006080 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006081 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006082 SetRawInputAt(0, obj);
6083 SetRawInputAt(1, value);
6084 }
6085
6086 bool NeedsEnvironment() const OVERRIDE { return true; }
6087 bool CanThrow() const OVERRIDE { return true; }
6088
Vladimir Markoa1de9182016-02-25 11:37:38 +00006089 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006090 uint32_t GetFieldIndex() const { return field_index_; }
6091
6092 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6093
6094 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006095 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6096 static constexpr size_t kFieldFieldTypeSize =
6097 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6098 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6099 kFieldFieldType + kFieldFieldTypeSize;
6100 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6101 "Too many packed fields.");
6102 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6103
Calin Juravlee460d1d2015-09-29 04:52:17 +01006104 const uint32_t field_index_;
6105
6106 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6107};
6108
Vladimir Markofcb503c2016-05-18 12:48:17 +01006109class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006110 public:
6111 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6112 uint32_t field_index,
6113 uint32_t dex_pc)
6114 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6115 field_index_(field_index) {
6116 }
6117
6118 bool NeedsEnvironment() const OVERRIDE { return true; }
6119 bool CanThrow() const OVERRIDE { return true; }
6120
6121 Primitive::Type GetFieldType() const { return GetType(); }
6122 uint32_t GetFieldIndex() const { return field_index_; }
6123
6124 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6125
6126 private:
6127 const uint32_t field_index_;
6128
6129 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6130};
6131
Vladimir Markofcb503c2016-05-18 12:48:17 +01006132class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006133 public:
6134 HUnresolvedStaticFieldSet(HInstruction* value,
6135 Primitive::Type field_type,
6136 uint32_t field_index,
6137 uint32_t dex_pc)
6138 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006139 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006140 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006141 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006142 SetRawInputAt(0, value);
6143 }
6144
6145 bool NeedsEnvironment() const OVERRIDE { return true; }
6146 bool CanThrow() const OVERRIDE { return true; }
6147
Vladimir Markoa1de9182016-02-25 11:37:38 +00006148 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006149 uint32_t GetFieldIndex() const { return field_index_; }
6150
6151 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6152
6153 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6155 static constexpr size_t kFieldFieldTypeSize =
6156 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6157 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6158 kFieldFieldType + kFieldFieldTypeSize;
6159 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6160 "Too many packed fields.");
6161 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6162
Calin Juravlee460d1d2015-09-29 04:52:17 +01006163 const uint32_t field_index_;
6164
6165 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6166};
6167
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006168// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006169class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006170 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006171 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6172 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006173
David Brazdilbbd733e2015-08-18 17:48:17 +01006174 bool CanBeNull() const OVERRIDE { return false; }
6175
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006176 DECLARE_INSTRUCTION(LoadException);
6177
6178 private:
6179 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6180};
6181
David Brazdilcb1c0552015-08-04 16:22:25 +01006182// Implicit part of move-exception which clears thread-local exception storage.
6183// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006184class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006185 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006186 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6187 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006188
6189 DECLARE_INSTRUCTION(ClearException);
6190
6191 private:
6192 DISALLOW_COPY_AND_ASSIGN(HClearException);
6193};
6194
Vladimir Markofcb503c2016-05-18 12:48:17 +01006195class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006196 public:
6197 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006198 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006199 SetRawInputAt(0, exception);
6200 }
6201
6202 bool IsControlFlow() const OVERRIDE { return true; }
6203
6204 bool NeedsEnvironment() const OVERRIDE { return true; }
6205
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006206 bool CanThrow() const OVERRIDE { return true; }
6207
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006208
6209 DECLARE_INSTRUCTION(Throw);
6210
6211 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006212 DISALLOW_COPY_AND_ASSIGN(HThrow);
6213};
6214
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215/**
6216 * Implementation strategies for the code generator of a HInstanceOf
6217 * or `HCheckCast`.
6218 */
6219enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006220 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 kExactCheck, // Can do a single class compare.
6222 kClassHierarchyCheck, // Can just walk the super class chain.
6223 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6224 kInterfaceCheck, // No optimization yet when checking against an interface.
6225 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 kArrayCheck, // No optimization yet when checking against a generic array.
6227 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228};
6229
Roland Levillain86503782016-02-11 19:07:30 +00006230std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6231
Vladimir Markofcb503c2016-05-18 12:48:17 +01006232class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006233 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006234 HInstanceOf(HInstruction* object,
6235 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006237 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006238 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006240 dex_pc) {
6241 SetPackedField<TypeCheckKindField>(check_kind);
6242 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006243 SetRawInputAt(0, object);
6244 SetRawInputAt(1, constant);
6245 }
6246
6247 bool CanBeMoved() const OVERRIDE { return true; }
6248
Vladimir Marko372f10e2016-05-17 16:30:10 +01006249 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006250 return true;
6251 }
6252
6253 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006254 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006255 }
6256
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006257 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006258 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6259 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6260 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6261 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006262
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006263 static bool CanCallRuntime(TypeCheckKind check_kind) {
6264 // Mips currently does runtime calls for any other checks.
6265 return check_kind != TypeCheckKind::kExactCheck;
6266 }
6267
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006269 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006270 }
6271
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006272 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006273
6274 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006275 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6276 static constexpr size_t kFieldTypeCheckKindSize =
6277 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6278 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6279 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6280 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6281 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006282
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006283 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6284};
6285
Vladimir Markofcb503c2016-05-18 12:48:17 +01006286class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006287 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006288 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006289 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006290 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6291 SetPackedFlag<kFlagUpperCanBeNull>(true);
6292 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006293 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006294 SetRawInputAt(0, input);
6295 }
6296
David Brazdilf5552582015-12-27 13:36:12 +00006297 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006298 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006299 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006300 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006301
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006302 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 DCHECK(GetUpperCanBeNull() || !can_be_null);
6304 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006305 }
6306
Vladimir Markoa1de9182016-02-25 11:37:38 +00006307 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006308
Calin Juravleb1498f62015-02-16 13:13:29 +00006309 DECLARE_INSTRUCTION(BoundType);
6310
6311 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006312 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6313 // is false then CanBeNull() cannot be true).
6314 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6315 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6316 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6317 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6318
Calin Juravleb1498f62015-02-16 13:13:29 +00006319 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006320 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6321 // It is used to bound the type in cases like:
6322 // if (x instanceof ClassX) {
6323 // // uper_bound_ will be ClassX
6324 // }
David Brazdilf5552582015-12-27 13:36:12 +00006325 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006326
6327 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6328};
6329
Vladimir Markofcb503c2016-05-18 12:48:17 +01006330class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006331 public:
6332 HCheckCast(HInstruction* object,
6333 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006335 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6337 SetPackedField<TypeCheckKindField>(check_kind);
6338 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006339 SetRawInputAt(0, object);
6340 SetRawInputAt(1, constant);
6341 }
6342
6343 bool CanBeMoved() const OVERRIDE { return true; }
6344
Vladimir Marko372f10e2016-05-17 16:30:10 +01006345 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006346 return true;
6347 }
6348
6349 bool NeedsEnvironment() const OVERRIDE {
6350 // Instruction may throw a CheckCastError.
6351 return true;
6352 }
6353
6354 bool CanThrow() const OVERRIDE { return true; }
6355
Vladimir Markoa1de9182016-02-25 11:37:38 +00006356 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6357 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6358 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6359 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360
6361 DECLARE_INSTRUCTION(CheckCast);
6362
6363 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6365 static constexpr size_t kFieldTypeCheckKindSize =
6366 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6367 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6368 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6369 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6370 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006371
6372 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006373};
6374
Andreas Gampe26de38b2016-07-27 17:53:11 -07006375/**
6376 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6377 * @details We define the combined barrier types that are actually required
6378 * by the Java Memory Model, rather than using exactly the terminology from
6379 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6380 * primitives. Note that the JSR-133 cookbook generally does not deal with
6381 * store atomicity issues, and the recipes there are not always entirely sufficient.
6382 * The current recipe is as follows:
6383 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6384 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6385 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6386 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6387 * class has final fields.
6388 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6389 * store-to-memory instructions. Only generated together with non-temporal stores.
6390 */
6391enum MemBarrierKind {
6392 kAnyStore,
6393 kLoadAny,
6394 kStoreStore,
6395 kAnyAny,
6396 kNTStoreStore,
6397 kLastBarrierKind = kNTStoreStore
6398};
6399std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6400
Vladimir Markofcb503c2016-05-18 12:48:17 +01006401class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006402 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006403 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006404 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6406 SetPackedField<BarrierKindField>(barrier_kind);
6407 }
Calin Juravle27df7582015-04-17 19:12:31 +01006408
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006410
6411 DECLARE_INSTRUCTION(MemoryBarrier);
6412
6413 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6415 static constexpr size_t kFieldBarrierKindSize =
6416 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6417 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6418 kFieldBarrierKind + kFieldBarrierKindSize;
6419 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6420 "Too many packed fields.");
6421 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006422
6423 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6424};
6425
Vladimir Markofcb503c2016-05-18 12:48:17 +01006426class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006427 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006428 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006429 kEnter,
6430 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006431 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006432 };
6433
6434 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006435 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006436 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6437 dex_pc) {
6438 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006439 SetRawInputAt(0, object);
6440 }
6441
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006442 // Instruction may go into runtime, so we need an environment.
6443 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006444
6445 bool CanThrow() const OVERRIDE {
6446 // Verifier guarantees that monitor-exit cannot throw.
6447 // This is important because it allows the HGraphBuilder to remove
6448 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6449 return IsEnter();
6450 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006451
Vladimir Markoa1de9182016-02-25 11:37:38 +00006452 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6453 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006454
6455 DECLARE_INSTRUCTION(MonitorOperation);
6456
Calin Juravle52c48962014-12-16 17:02:57 +00006457 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6459 static constexpr size_t kFieldOperationKindSize =
6460 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6461 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6462 kFieldOperationKind + kFieldOperationKindSize;
6463 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6464 "Too many packed fields.");
6465 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006466
6467 private:
6468 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6469};
6470
Vladimir Markofcb503c2016-05-18 12:48:17 +01006471class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006472 public:
6473 HSelect(HInstruction* condition,
6474 HInstruction* true_value,
6475 HInstruction* false_value,
6476 uint32_t dex_pc)
6477 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6478 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6479
6480 // First input must be `true_value` or `false_value` to allow codegens to
6481 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6482 // that architectures which implement HSelect as a conditional move also
6483 // will not need to invert the condition.
6484 SetRawInputAt(0, false_value);
6485 SetRawInputAt(1, true_value);
6486 SetRawInputAt(2, condition);
6487 }
6488
6489 HInstruction* GetFalseValue() const { return InputAt(0); }
6490 HInstruction* GetTrueValue() const { return InputAt(1); }
6491 HInstruction* GetCondition() const { return InputAt(2); }
6492
6493 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006494 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6495 return true;
6496 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006497
6498 bool CanBeNull() const OVERRIDE {
6499 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6500 }
6501
6502 DECLARE_INSTRUCTION(Select);
6503
6504 private:
6505 DISALLOW_COPY_AND_ASSIGN(HSelect);
6506};
6507
Vladimir Markof9f64412015-09-02 14:05:49 +01006508class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006509 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006510 MoveOperands(Location source,
6511 Location destination,
6512 Primitive::Type type,
6513 HInstruction* instruction)
6514 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006515
6516 Location GetSource() const { return source_; }
6517 Location GetDestination() const { return destination_; }
6518
6519 void SetSource(Location value) { source_ = value; }
6520 void SetDestination(Location value) { destination_ = value; }
6521
6522 // The parallel move resolver marks moves as "in-progress" by clearing the
6523 // destination (but not the source).
6524 Location MarkPending() {
6525 DCHECK(!IsPending());
6526 Location dest = destination_;
6527 destination_ = Location::NoLocation();
6528 return dest;
6529 }
6530
6531 void ClearPending(Location dest) {
6532 DCHECK(IsPending());
6533 destination_ = dest;
6534 }
6535
6536 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006537 DCHECK(source_.IsValid() || destination_.IsInvalid());
6538 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006539 }
6540
6541 // True if this blocks a move from the given location.
6542 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006543 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006544 }
6545
6546 // A move is redundant if it's been eliminated, if its source and
6547 // destination are the same, or if its destination is unneeded.
6548 bool IsRedundant() const {
6549 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6550 }
6551
6552 // We clear both operands to indicate move that's been eliminated.
6553 void Eliminate() {
6554 source_ = destination_ = Location::NoLocation();
6555 }
6556
6557 bool IsEliminated() const {
6558 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6559 return source_.IsInvalid();
6560 }
6561
Alexey Frunze4dda3372015-06-01 18:31:49 -07006562 Primitive::Type GetType() const { return type_; }
6563
Nicolas Geoffray90218252015-04-15 11:56:51 +01006564 bool Is64BitMove() const {
6565 return Primitive::Is64BitType(type_);
6566 }
6567
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006568 HInstruction* GetInstruction() const { return instruction_; }
6569
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006570 private:
6571 Location source_;
6572 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006573 // The type this move is for.
6574 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006575 // The instruction this move is assocatied with. Null when this move is
6576 // for moving an input in the expected locations of user (including a phi user).
6577 // This is only used in debug mode, to ensure we do not connect interval siblings
6578 // in the same parallel move.
6579 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006580};
6581
Roland Levillainc9285912015-12-18 10:38:42 +00006582std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6583
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006584static constexpr size_t kDefaultNumberOfMoves = 4;
6585
Vladimir Markofcb503c2016-05-18 12:48:17 +01006586class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006587 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006588 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006589 : HTemplateInstruction(SideEffects::None(), dex_pc),
6590 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6591 moves_.reserve(kDefaultNumberOfMoves);
6592 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006593
Nicolas Geoffray90218252015-04-15 11:56:51 +01006594 void AddMove(Location source,
6595 Location destination,
6596 Primitive::Type type,
6597 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006598 DCHECK(source.IsValid());
6599 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006600 if (kIsDebugBuild) {
6601 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006602 for (const MoveOperands& move : moves_) {
6603 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006604 // Special case the situation where the move is for the spill slot
6605 // of the instruction.
6606 if ((GetPrevious() == instruction)
6607 || ((GetPrevious() == nullptr)
6608 && instruction->IsPhi()
6609 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006610 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006611 << "Doing parallel moves for the same instruction.";
6612 } else {
6613 DCHECK(false) << "Doing parallel moves for the same instruction.";
6614 }
6615 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006616 }
6617 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006618 for (const MoveOperands& move : moves_) {
6619 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006620 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006621 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006622 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006623 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006624 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006625 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006626 }
6627
Vladimir Marko225b6462015-09-28 12:17:40 +01006628 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006629 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006630 }
6631
Vladimir Marko225b6462015-09-28 12:17:40 +01006632 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006633
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006634 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006635
6636 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006637 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006638
6639 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6640};
6641
Mark Mendell0616ae02015-04-17 12:49:27 -04006642} // namespace art
6643
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006644#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6645#include "nodes_shared.h"
6646#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006647#ifdef ART_ENABLE_CODEGEN_arm
6648#include "nodes_arm.h"
6649#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006650#ifdef ART_ENABLE_CODEGEN_arm64
6651#include "nodes_arm64.h"
6652#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006653#ifdef ART_ENABLE_CODEGEN_mips
6654#include "nodes_mips.h"
6655#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006656#ifdef ART_ENABLE_CODEGEN_x86
6657#include "nodes_x86.h"
6658#endif
6659
6660namespace art {
6661
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006662class HGraphVisitor : public ValueObject {
6663 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006664 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6665 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006666
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006667 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006668 virtual void VisitBasicBlock(HBasicBlock* block);
6669
Roland Levillain633021e2014-10-01 14:12:25 +01006670 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006671 void VisitInsertionOrder();
6672
Roland Levillain633021e2014-10-01 14:12:25 +01006673 // Visit the graph following dominator tree reverse post-order.
6674 void VisitReversePostOrder();
6675
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006676 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006677
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006678 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006679#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006680 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6681
6682 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6683
6684#undef DECLARE_VISIT_INSTRUCTION
6685
6686 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006687 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006688
6689 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6690};
6691
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006692class HGraphDelegateVisitor : public HGraphVisitor {
6693 public:
6694 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6695 virtual ~HGraphDelegateVisitor() {}
6696
6697 // Visit functions that delegate to to super class.
6698#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006699 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006700
6701 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6702
6703#undef DECLARE_VISIT_INSTRUCTION
6704
6705 private:
6706 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6707};
6708
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006709// Iterator over the blocks that art part of the loop. Includes blocks part
6710// of an inner loop. The order in which the blocks are iterated is on their
6711// block id.
6712class HBlocksInLoopIterator : public ValueObject {
6713 public:
6714 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6715 : blocks_in_loop_(info.GetBlocks()),
6716 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6717 index_(0) {
6718 if (!blocks_in_loop_.IsBitSet(index_)) {
6719 Advance();
6720 }
6721 }
6722
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006723 bool Done() const { return index_ == blocks_.size(); }
6724 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006725 void Advance() {
6726 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006727 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006728 if (blocks_in_loop_.IsBitSet(index_)) {
6729 break;
6730 }
6731 }
6732 }
6733
6734 private:
6735 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006736 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006737 size_t index_;
6738
6739 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6740};
6741
Mingyao Yang3584bce2015-05-19 16:01:59 -07006742// Iterator over the blocks that art part of the loop. Includes blocks part
6743// of an inner loop. The order in which the blocks are iterated is reverse
6744// post order.
6745class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6746 public:
6747 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6748 : blocks_in_loop_(info.GetBlocks()),
6749 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6750 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006751 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006752 Advance();
6753 }
6754 }
6755
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006756 bool Done() const { return index_ == blocks_.size(); }
6757 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006758 void Advance() {
6759 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006760 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6761 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006762 break;
6763 }
6764 }
6765 }
6766
6767 private:
6768 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006769 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006770 size_t index_;
6771
6772 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6773};
6774
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006775inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006776 if (constant->IsIntConstant()) {
6777 return constant->AsIntConstant()->GetValue();
6778 } else if (constant->IsLongConstant()) {
6779 return constant->AsLongConstant()->GetValue();
6780 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006781 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006782 return 0;
6783 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006784}
6785
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006786#define INSTRUCTION_TYPE_CHECK(type, super) \
6787 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6788 inline const H##type* HInstruction::As##type() const { \
6789 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6790 } \
6791 inline H##type* HInstruction::As##type() { \
6792 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6793 }
6794
6795 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6796#undef INSTRUCTION_TYPE_CHECK
6797
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006798// Create space in `blocks` for adding `number_of_new_blocks` entries
6799// starting at location `at`. Blocks after `at` are moved accordingly.
6800inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6801 size_t number_of_new_blocks,
6802 size_t after) {
6803 DCHECK_LT(after, blocks->size());
6804 size_t old_size = blocks->size();
6805 size_t new_size = old_size + number_of_new_blocks;
6806 blocks->resize(new_size);
6807 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6808}
6809
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006810} // namespace art
6811
6812#endif // ART_COMPILER_OPTIMIZING_NODES_H_