blob: e40361eb5d53153e24d208c14d95fbcba64b42a4 [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;
Igor Murashkind01745e2017-04-05 16:40:31 -070049class HConstructorFence;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010050class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000051class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010052class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000053class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000054class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000055class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000056class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000057class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000058class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000059class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000060class HNullConstant;
Igor Murashkind01745e2017-04-05 16:40:31 -070061class HParameterValue;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010062class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010063class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010064class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010065class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000066class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010067class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000068class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000069
Mathieu Chartier736b5602015-09-02 14:54:11 -070070namespace mirror {
71class DexCache;
72} // namespace mirror
73
Nicolas Geoffray818f2102014-02-18 16:43:35 +000074static const int kDefaultNumberOfBlocks = 8;
75static const int kDefaultNumberOfSuccessors = 2;
76static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010077static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010078static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000079static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000080
Roland Levillain5b5b9312016-03-22 14:57:31 +000081// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
82static constexpr int32_t kMaxIntShiftDistance = 0x1f;
83// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
84static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000085
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070087static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010088
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010089static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
90
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060091static constexpr uint32_t kNoDexPc = -1;
92
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010093inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
94 // For the purposes of the compiler, the dex files must actually be the same object
95 // if we want to safely treat them as the same. This is especially important for JIT
96 // as custom class loaders can open the same underlying file (or memory) multiple
97 // times and provide different class resolution but no two class loaders should ever
98 // use the same DexFile object - doing so is an unsupported hack that can lead to
99 // all sorts of weird failures.
100 return &lhs == &rhs;
101}
102
Dave Allison20dfc792014-06-16 20:44:29 -0700103enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700104 // All types.
105 kCondEQ, // ==
106 kCondNE, // !=
107 // Signed integers and floating-point numbers.
108 kCondLT, // <
109 kCondLE, // <=
110 kCondGT, // >
111 kCondGE, // >=
112 // Unsigned integers.
113 kCondB, // <
114 kCondBE, // <=
115 kCondA, // >
116 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100117 // First and last aliases.
118 kCondFirst = kCondEQ,
119 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700120};
121
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000122enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000123 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000124 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000125 kAnalysisFailThrowCatchLoop,
126 kAnalysisFailAmbiguousArrayOp,
127 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000128};
129
Andreas Gampe9186ced2016-12-12 14:28:21 -0800130template <typename T>
131static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
132 return static_cast<typename std::make_unsigned<T>::type>(x);
133}
134
Vladimir Markof9f64412015-09-02 14:05:49 +0100135class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100136 public:
137 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
138
139 void AddInstruction(HInstruction* instruction);
140 void RemoveInstruction(HInstruction* instruction);
141
David Brazdilc3d743f2015-04-22 13:40:50 +0100142 // Insert `instruction` before/after an existing instruction `cursor`.
143 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
144 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
145
Roland Levillain6b469232014-09-25 10:10:38 +0100146 // Return true if this list contains `instruction`.
147 bool Contains(HInstruction* instruction) const;
148
Roland Levillainccc07a92014-09-16 14:48:16 +0100149 // Return true if `instruction1` is found before `instruction2` in
150 // this instruction list and false otherwise. Abort if none
151 // of these instructions is found.
152 bool FoundBefore(const HInstruction* instruction1,
153 const HInstruction* instruction2) const;
154
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000155 bool IsEmpty() const { return first_instruction_ == nullptr; }
156 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
157
158 // Update the block of all instructions to be `block`.
159 void SetBlockOfInstructions(HBasicBlock* block) const;
160
161 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000162 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000163 void Add(const HInstructionList& instruction_list);
164
David Brazdil2d7352b2015-04-20 14:52:42 +0100165 // Return the number of instructions in the list. This is an expensive operation.
166 size_t CountSize() const;
167
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100168 private:
169 HInstruction* first_instruction_;
170 HInstruction* last_instruction_;
171
172 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000173 friend class HGraph;
174 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100175 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000176 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100177 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100178
179 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
180};
181
David Brazdil4833f5a2015-12-16 10:37:39 +0000182class ReferenceTypeInfo : ValueObject {
183 public:
184 typedef Handle<mirror::Class> TypeHandle;
185
Vladimir Markoa1de9182016-02-25 11:37:38 +0000186 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
187
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700188 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100189 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
190 }
191
Vladimir Markoa1de9182016-02-25 11:37:38 +0000192 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000193 return ReferenceTypeInfo(type_handle, is_exact);
194 }
195
196 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
197
Vladimir Markof39745e2016-01-26 12:16:55 +0000198 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000199 return handle.GetReference() != nullptr;
200 }
201
Vladimir Marko456307a2016-04-19 14:12:13 +0000202 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000203 return IsValidHandle(type_handle_);
204 }
205
206 bool IsExact() const { return is_exact_; }
207
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000209 DCHECK(IsValid());
210 return GetTypeHandle()->IsObjectClass();
211 }
212
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700213 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000214 DCHECK(IsValid());
215 return GetTypeHandle()->IsStringClass();
216 }
217
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700218 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000219 DCHECK(IsValid());
220 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
221 }
222
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700223 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000224 DCHECK(IsValid());
225 return GetTypeHandle()->IsInterface();
226 }
227
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700228 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000229 DCHECK(IsValid());
230 return GetTypeHandle()->IsArrayClass();
231 }
232
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700233 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000234 DCHECK(IsValid());
235 return GetTypeHandle()->IsPrimitiveArray();
236 }
237
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700238 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000239 DCHECK(IsValid());
240 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
241 }
242
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700243 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000244 DCHECK(IsValid());
245 if (!IsExact()) return false;
246 if (!IsArrayClass()) return false;
247 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
248 }
249
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700250 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000251 DCHECK(IsValid());
252 if (!IsExact()) return false;
253 if (!IsArrayClass()) return false;
254 if (!rti.IsArrayClass()) return false;
255 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
256 rti.GetTypeHandle()->GetComponentType());
257 }
258
259 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
260
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700261 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000262 DCHECK(IsValid());
263 DCHECK(rti.IsValid());
264 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
265 }
266
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700267 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000268 DCHECK(IsValid());
269 DCHECK(rti.IsValid());
270 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
271 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
272 }
273
274 // Returns true if the type information provide the same amount of details.
275 // Note that it does not mean that the instructions have the same actual type
276 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700277 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000278 if (!IsValid() && !rti.IsValid()) {
279 // Invalid types are equal.
280 return true;
281 }
282 if (!IsValid() || !rti.IsValid()) {
283 // One is valid, the other not.
284 return false;
285 }
286 return IsExact() == rti.IsExact()
287 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
288 }
289
290 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000291 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
292 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
293 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000294
295 // The class of the object.
296 TypeHandle type_handle_;
297 // Whether or not the type is exact or a superclass of the actual type.
298 // Whether or not we have any information about this type.
299 bool is_exact_;
300};
301
302std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
303
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100305class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000306 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100307 HGraph(ArenaAllocator* arena,
308 const DexFile& dex_file,
309 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100311 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000313 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000315 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100316 blocks_(arena->Adapter(kArenaAllocBlockList)),
317 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
318 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700319 entry_block_(nullptr),
320 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100321 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100322 number_of_vregs_(0),
323 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000324 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400325 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000326 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700327 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800328 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000329 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000330 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000331 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100332 dex_file_(dex_file),
333 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100334 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100335 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800336 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000338 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100339 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
342 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000343 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100344 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000345 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700346 osr_(osr),
347 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100348 blocks_.reserve(kDefaultNumberOfBlocks);
349 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000350
David Brazdilbadd8262016-02-02 16:28:56 +0000351 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700352 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000353
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000354 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100355 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
356
David Brazdil69ba7b72015-06-23 18:27:30 +0100357 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000358 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100359
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000360 HBasicBlock* GetEntryBlock() const { return entry_block_; }
361 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100362 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000364 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
365 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000366
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000367 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100368
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100369 void ComputeDominanceInformation();
370 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000371 void ClearLoopInformation();
372 void FindBackEdges(ArenaBitVector* visited);
373 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100374 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100375 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000376
David Brazdil4833f5a2015-12-16 10:37:39 +0000377 // Analyze all natural loops in this graph. Returns a code specifying that it
378 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000379 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000380 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100381
David Brazdilffee3d32015-07-06 11:48:53 +0100382 // Iterate over blocks to compute try block membership. Needs reverse post
383 // order and loop information.
384 void ComputeTryBlockInformation();
385
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000386 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000387 // Returns the instruction to replace the invoke expression or null if the
388 // invoke is for a void method. Note that the caller is responsible for replacing
389 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000390 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000391
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000392 // Update the loop and try membership of `block`, which was spawned from `reference`.
393 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
394 // should be the new back edge.
395 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
396 HBasicBlock* reference,
397 bool replace_if_back_edge);
398
Mingyao Yang3584bce2015-05-19 16:01:59 -0700399 // Need to add a couple of blocks to test if the loop body is entered and
400 // put deoptimization instructions, etc.
401 void TransformLoopHeaderForBCE(HBasicBlock* header);
402
Aart Bikf8f5a162017-02-06 15:35:29 -0800403 // Adds a new loop directly after the loop with the given header and exit.
404 // Returns the new preheader.
405 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
406 HBasicBlock* body,
407 HBasicBlock* exit);
408
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000409 // Removes `block` from the graph. Assumes `block` has been disconnected from
410 // other blocks and has no instructions or phis.
411 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000412
David Brazdilfc6a86a2015-06-26 10:33:45 +0000413 // Splits the edge between `block` and `successor` while preserving the
414 // indices in the predecessor/successor lists. If there are multiple edges
415 // between the blocks, the lowest indices are used.
416 // Returns the new block which is empty and has the same dex pc as `successor`.
417 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
418
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100419 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
420 void SimplifyLoop(HBasicBlock* header);
421
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000422 int32_t GetNextInstructionId() {
423 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000424 return current_instruction_id_++;
425 }
426
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000427 int32_t GetCurrentInstructionId() const {
428 return current_instruction_id_;
429 }
430
431 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000432 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000433 current_instruction_id_ = id;
434 }
435
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100436 uint16_t GetMaximumNumberOfOutVRegs() const {
437 return maximum_number_of_out_vregs_;
438 }
439
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000440 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
441 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100442 }
443
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
445 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
446 }
447
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000448 void UpdateTemporariesVRegSlots(size_t slots) {
449 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100450 }
451
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000452 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100453 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000454 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100455 }
456
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100457 void SetNumberOfVRegs(uint16_t number_of_vregs) {
458 number_of_vregs_ = number_of_vregs;
459 }
460
461 uint16_t GetNumberOfVRegs() const {
462 return number_of_vregs_;
463 }
464
465 void SetNumberOfInVRegs(uint16_t value) {
466 number_of_in_vregs_ = value;
467 }
468
David Brazdildee58d62016-04-07 09:54:26 +0000469 uint16_t GetNumberOfInVRegs() const {
470 return number_of_in_vregs_;
471 }
472
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100473 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100474 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100475 return number_of_vregs_ - number_of_in_vregs_;
476 }
477
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100478 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100479 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100480 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100481
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100482 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
483 DCHECK(GetReversePostOrder()[0] == entry_block_);
484 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
485 }
486
487 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
488 return ReverseRange(GetReversePostOrder());
489 }
490
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100491 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100492 return linear_order_;
493 }
494
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100495 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
496 return ReverseRange(GetLinearOrder());
497 }
498
Mark Mendell1152c922015-04-24 17:06:35 -0400499 bool HasBoundsChecks() const {
500 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800501 }
502
Mark Mendell1152c922015-04-24 17:06:35 -0400503 void SetHasBoundsChecks(bool value) {
504 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800505 }
506
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000507 bool IsDebuggable() const { return debuggable_; }
508
David Brazdil8d5b8b22015-03-24 10:51:52 +0000509 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000510 // already, it is created and inserted into the graph. This method is only for
511 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
514 // TODO: This is problematic for the consistency of reference type propagation
515 // because it can be created anytime after the pass and thus it will be left
516 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000518
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600519 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
520 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000521 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600522 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
523 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000524 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600525 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
526 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000527 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600528 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
529 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000530 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000531
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100532 HCurrentMethod* GetCurrentMethod();
533
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100534 const DexFile& GetDexFile() const {
535 return dex_file_;
536 }
537
538 uint32_t GetMethodIdx() const {
539 return method_idx_;
540 }
541
Igor Murashkind01745e2017-04-05 16:40:31 -0700542 // Get the method name (without the signature), e.g. "<init>"
543 const char* GetMethodName() const;
544
545 // Get the pretty method name (class + name + optionally signature).
546 std::string PrettyMethod(bool with_signature = true) const;
547
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100548 InvokeType GetInvokeType() const {
549 return invoke_type_;
550 }
551
Mark Mendellc4701932015-04-10 13:18:51 -0400552 InstructionSet GetInstructionSet() const {
553 return instruction_set_;
554 }
555
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000556 bool IsCompilingOsr() const { return osr_; }
557
Mingyao Yang063fc772016-08-02 11:02:54 -0700558 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
559 return cha_single_implementation_list_;
560 }
561
562 void AddCHASingleImplementationDependency(ArtMethod* method) {
563 cha_single_implementation_list_.insert(method);
564 }
565
566 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800567 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700568 }
569
David Brazdil77a48ae2015-09-15 12:34:04 +0000570 bool HasTryCatch() const { return has_try_catch_; }
571 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100572
Aart Bikb13c65b2017-03-21 20:14:07 -0700573 bool HasSIMD() const { return has_simd_; }
574 void SetHasSIMD(bool value) { has_simd_ = value; }
575
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800576 bool HasLoops() const { return has_loops_; }
577 void SetHasLoops(bool value) { has_loops_ = value; }
578
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000579 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
580 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
581
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100582 ArtMethod* GetArtMethod() const { return art_method_; }
583 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
584
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100585 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500586 // The instruction has been inserted into the graph, either as a constant, or
587 // before cursor.
588 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
589
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000590 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
591
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800592 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
593 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
594 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
595
David Brazdil2d7352b2015-04-20 14:52:42 +0100596 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000597 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100598 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000599
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000600 template <class InstructionType, typename ValueType>
601 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600602 ArenaSafeMap<ValueType, InstructionType*>* cache,
603 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000604 // Try to find an existing constant of the given value.
605 InstructionType* constant = nullptr;
606 auto cached_constant = cache->find(value);
607 if (cached_constant != cache->end()) {
608 constant = cached_constant->second;
609 }
610
611 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100612 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000613 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600614 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 cache->Overwrite(value, constant);
616 InsertConstant(constant);
617 }
618 return constant;
619 }
620
David Brazdil8d5b8b22015-03-24 10:51:52 +0000621 void InsertConstant(HConstant* instruction);
622
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000623 // Cache a float constant into the graph. This method should only be
624 // called by the SsaBuilder when creating "equivalent" instructions.
625 void CacheFloatConstant(HFloatConstant* constant);
626
627 // See CacheFloatConstant comment.
628 void CacheDoubleConstant(HDoubleConstant* constant);
629
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000630 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000631
632 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100633 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000634
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100635 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100636 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000637
Aart Bik281c6812016-08-26 11:31:48 -0700638 // List of blocks to perform a linear order tree traversal. Unlike the reverse
639 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100640 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100641
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000642 HBasicBlock* entry_block_;
643 HBasicBlock* exit_block_;
644
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100645 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100646 uint16_t maximum_number_of_out_vregs_;
647
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100648 // The number of virtual registers in this method. Contains the parameters.
649 uint16_t number_of_vregs_;
650
651 // The number of virtual registers used by parameters of this method.
652 uint16_t number_of_in_vregs_;
653
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000654 // Number of vreg size slots that the temporaries use (used in baseline compiler).
655 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100656
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800657 // Flag whether there are bounds checks in the graph. We can skip
658 // BCE if it's false. It's only best effort to keep it up to date in
659 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400660 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800661
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800662 // Flag whether there are try/catch blocks in the graph. We will skip
663 // try/catch-related passes if it's false. It's only best effort to keep
664 // it up to date in the presence of code elimination so there might be
665 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000666 bool has_try_catch_;
667
Aart Bikb13c65b2017-03-21 20:14:07 -0700668 // Flag whether SIMD instructions appear in the graph. If true, the
669 // code generators may have to be more careful spilling the wider
670 // contents of SIMD registers.
671 bool has_simd_;
672
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800673 // Flag whether there are any loops in the graph. We can skip loop
674 // optimization if it's false. It's only best effort to keep it up
675 // to date in the presence of code elimination so there might be false
676 // positives.
677 bool has_loops_;
678
679 // Flag whether there are any irreducible loops in the graph. It's only
680 // best effort to keep it up to date in the presence of code elimination
681 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000682 bool has_irreducible_loops_;
683
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000684 // Indicates whether the graph should be compiled in a way that
685 // ensures full debuggability. If false, we can apply more
686 // aggressive optimizations that may limit the level of debugging.
687 const bool debuggable_;
688
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000689 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000690 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000691
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100692 // The dex file from which the method is from.
693 const DexFile& dex_file_;
694
695 // The method index in the dex file.
696 const uint32_t method_idx_;
697
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100698 // If inlined, this encodes how the callee is being invoked.
699 const InvokeType invoke_type_;
700
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100701 // Whether the graph has been transformed to SSA form. Only used
702 // in debug mode to ensure we are not using properties only valid
703 // for non-SSA form (like the number of temporaries).
704 bool in_ssa_form_;
705
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800706 // Number of CHA guards in the graph. Used to short-circuit the
707 // CHA guard optimization pass when there is no CHA guard left.
708 uint32_t number_of_cha_guards_;
709
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710 const InstructionSet instruction_set_;
711
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000712 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000713 HNullConstant* cached_null_constant_;
714 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000715 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000716 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000717 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000718
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100719 HCurrentMethod* cached_current_method_;
720
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100721 // The ArtMethod this graph is for. Note that for AOT, it may be null,
722 // for example for methods whose declaring class could not be resolved
723 // (such as when the superclass could not be found).
724 ArtMethod* art_method_;
725
David Brazdil4833f5a2015-12-16 10:37:39 +0000726 // Keep the RTI of inexact Object to avoid having to pass stack handle
727 // collection pointer to passes which may create NullConstant.
728 ReferenceTypeInfo inexact_object_rti_;
729
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000730 // Whether we are compiling this graph for on stack replacement: this will
731 // make all loops seen as irreducible and emit special stack maps to mark
732 // compiled code entries which the interpreter can directly jump to.
733 const bool osr_;
734
Mingyao Yang063fc772016-08-02 11:02:54 -0700735 // List of methods that are assumed to have single implementation.
736 ArenaSet<ArtMethod*> cha_single_implementation_list_;
737
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000738 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100739 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000740 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000741 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000742 DISALLOW_COPY_AND_ASSIGN(HGraph);
743};
744
Vladimir Markof9f64412015-09-02 14:05:49 +0100745class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000746 public:
747 HLoopInformation(HBasicBlock* header, HGraph* graph)
748 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100749 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000750 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100751 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100753 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000754 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100755 back_edges_.reserve(kDefaultNumberOfBackEdges);
756 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100757
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000758 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100759 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000760
761 void Dump(std::ostream& os);
762
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 HBasicBlock* GetHeader() const {
764 return header_;
765 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000766
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000767 void SetHeader(HBasicBlock* block) {
768 header_ = block;
769 }
770
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100771 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
772 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
773 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
774
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000775 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000777 }
778
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100779 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100781 }
782
David Brazdil46e2a392015-03-16 17:31:52 +0000783 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100784 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100785 }
786
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000787 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100788 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000789 }
790
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100791 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100792
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100793 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100794 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100795 }
796
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100797 // Returns the lifetime position of the back edge that has the
798 // greatest lifetime position.
799 size_t GetLifetimeEnd() const;
800
801 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100802 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100803 }
804
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000805 // Finds blocks that are part of this loop.
806 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100807
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100808 // Returns whether this loop information contains `block`.
809 // Note that this loop information *must* be populated before entering this function.
810 bool Contains(const HBasicBlock& block) const;
811
812 // Returns whether this loop information is an inner loop of `other`.
813 // Note that `other` *must* be populated before entering this function.
814 bool IsIn(const HLoopInformation& other) const;
815
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800816 // Returns true if instruction is not defined within this loop.
817 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700818
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100819 const ArenaBitVector& GetBlocks() const { return blocks_; }
820
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000821 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000822 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000823
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000824 void ClearAllBlocks() {
825 blocks_.ClearAllBits();
826 }
827
David Brazdil3f4a5222016-05-06 12:46:21 +0100828 bool HasBackEdgeNotDominatedByHeader() const;
829
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100830 bool IsPopulated() const {
831 return blocks_.GetHighestBitSet() != -1;
832 }
833
Anton Shaminf89381f2016-05-16 16:44:13 +0600834 bool DominatesAllBackEdges(HBasicBlock* block);
835
David Sehrc757dec2016-11-04 15:48:34 -0700836 bool HasExitEdge() const;
837
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000838 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100839 // Internal recursive implementation of `Populate`.
840 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100841 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100842
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000843 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100844 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000845 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100846 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100847 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100848 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000849
850 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
851};
852
David Brazdilec16f792015-08-19 15:04:01 +0100853// Stores try/catch information for basic blocks.
854// Note that HGraph is constructed so that catch blocks cannot simultaneously
855// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100856class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100857 public:
858 // Try block information constructor.
859 explicit TryCatchInformation(const HTryBoundary& try_entry)
860 : try_entry_(&try_entry),
861 catch_dex_file_(nullptr),
862 catch_type_index_(DexFile::kDexNoIndex16) {
863 DCHECK(try_entry_ != nullptr);
864 }
865
866 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800867 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100868 : try_entry_(nullptr),
869 catch_dex_file_(&dex_file),
870 catch_type_index_(catch_type_index) {}
871
872 bool IsTryBlock() const { return try_entry_ != nullptr; }
873
874 const HTryBoundary& GetTryEntry() const {
875 DCHECK(IsTryBlock());
876 return *try_entry_;
877 }
878
879 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
880
881 bool IsCatchAllTypeIndex() const {
882 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800883 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100884 }
885
Andreas Gampea5b09a62016-11-17 15:21:22 -0800886 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100887 DCHECK(IsCatchBlock());
888 return catch_type_index_;
889 }
890
891 const DexFile& GetCatchDexFile() const {
892 DCHECK(IsCatchBlock());
893 return *catch_dex_file_;
894 }
895
896 private:
897 // One of possibly several TryBoundary instructions entering the block's try.
898 // Only set for try blocks.
899 const HTryBoundary* try_entry_;
900
901 // Exception type information. Only set for catch blocks.
902 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800903 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100904};
905
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100906static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100907static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000909// A block in a method. Contains the list of instructions represented
910// as a double linked list. Each block knows its predecessors and
911// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100912
Vladimir Markof9f64412015-09-02 14:05:49 +0100913class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000914 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700915 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000916 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000917 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
918 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000919 loop_information_(nullptr),
920 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000921 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100922 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100923 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100924 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000925 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000926 try_catch_information_(nullptr) {
927 predecessors_.reserve(kDefaultNumberOfPredecessors);
928 successors_.reserve(kDefaultNumberOfSuccessors);
929 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
930 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000931
Vladimir Marko60584552015-09-03 13:35:12 +0000932 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100933 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000934 }
935
Vladimir Marko60584552015-09-03 13:35:12 +0000936 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100937 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000938 }
939
David Brazdild26a4112015-11-10 11:07:31 +0000940 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
941 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
942
Vladimir Marko60584552015-09-03 13:35:12 +0000943 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
944 return ContainsElement(successors_, block, start_from);
945 }
946
947 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100948 return dominated_blocks_;
949 }
950
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100951 bool IsEntryBlock() const {
952 return graph_->GetEntryBlock() == this;
953 }
954
955 bool IsExitBlock() const {
956 return graph_->GetExitBlock() == this;
957 }
958
David Brazdil46e2a392015-03-16 17:31:52 +0000959 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000960 bool IsSingleTryBoundary() const;
961
962 // Returns true if this block emits nothing but a jump.
963 bool IsSingleJump() const {
964 HLoopInformation* loop_info = GetLoopInformation();
965 return (IsSingleGoto() || IsSingleTryBoundary())
966 // Back edges generate a suspend check.
967 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
968 }
David Brazdil46e2a392015-03-16 17:31:52 +0000969
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000970 void AddBackEdge(HBasicBlock* back_edge) {
971 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000972 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000973 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100974 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000975 loop_information_->AddBackEdge(back_edge);
976 }
977
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000978 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000979 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100981 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000982 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600983 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000984
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000985 HBasicBlock* GetDominator() const { return dominator_; }
986 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000987 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
988
989 void RemoveDominatedBlock(HBasicBlock* block) {
990 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100991 }
Vladimir Marko60584552015-09-03 13:35:12 +0000992
993 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
994 ReplaceElement(dominated_blocks_, existing, new_block);
995 }
996
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100997 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000998
999 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +01001000 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001001 }
1002
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001003 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
1004 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +01001005 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001006 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +01001007 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1008 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001009
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001010 HInstruction* GetFirstInstructionDisregardMoves() const;
1011
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001012 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001013 successors_.push_back(block);
1014 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001015 }
1016
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001017 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1018 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001019 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001020 new_block->predecessors_.push_back(this);
1021 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001022 }
1023
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001024 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1025 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001026 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001027 new_block->successors_.push_back(this);
1028 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001029 }
1030
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001031 // Insert `this` between `predecessor` and `successor. This method
1032 // preserves the indicies, and will update the first edge found between
1033 // `predecessor` and `successor`.
1034 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1035 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001036 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001037 successor->predecessors_[predecessor_index] = this;
1038 predecessor->successors_[successor_index] = this;
1039 successors_.push_back(successor);
1040 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001041 }
1042
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001043 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001044 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 }
1046
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001047 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001048 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001049 }
1050
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001051 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001053 }
1054
1055 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001056 predecessors_.push_back(block);
1057 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001058 }
1059
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001060 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001061 DCHECK_EQ(predecessors_.size(), 2u);
1062 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001063 }
1064
David Brazdil769c9e52015-04-27 13:54:09 +01001065 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001066 DCHECK_EQ(successors_.size(), 2u);
1067 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001068 }
1069
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001072 }
1073
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001075 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001076 }
1077
David Brazdilfc6a86a2015-06-26 10:33:45 +00001078 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001079 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001080 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001081 }
1082
1083 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001084 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001085 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001086 }
1087
1088 // Returns whether the first occurrence of `predecessor` in the list of
1089 // predecessors is at index `idx`.
1090 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001091 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001092 return GetPredecessorIndexOf(predecessor) == idx;
1093 }
1094
David Brazdild7558da2015-09-22 13:04:14 +01001095 // Create a new block between this block and its predecessors. The new block
1096 // is added to the graph, all predecessor edges are relinked to it and an edge
1097 // is created to `this`. Returns the new empty block. Reverse post order or
1098 // loop and try/catch information are not updated.
1099 HBasicBlock* CreateImmediateDominator();
1100
David Brazdilfc6a86a2015-06-26 10:33:45 +00001101 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001102 // created, latter block. Note that this method will add the block to the
1103 // graph, create a Goto at the end of the former block and will create an edge
1104 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001105 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001106 HBasicBlock* SplitBefore(HInstruction* cursor);
1107
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001108 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109 // created block. Note that this method just updates raw block information,
1110 // like predecessors, successors, dominators, and instruction list. It does not
1111 // update the graph, reverse post order, loop information, nor make sure the
1112 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001113 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1114
1115 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1116 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001117
1118 // Merge `other` at the end of `this`. Successors and dominated blocks of
1119 // `other` are changed to be successors and dominated blocks of `this`. Note
1120 // that this method does not update the graph, reverse post order, loop
1121 // information, nor make sure the blocks are consistent (for example ending
1122 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001123 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001124
1125 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1126 // of `this` are moved to `other`.
1127 // Note that this method does not update the graph, reverse post order, loop
1128 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001129 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001130 void ReplaceWith(HBasicBlock* other);
1131
Aart Bik6b69e0a2017-01-11 10:20:43 -08001132 // Merges the instructions of `other` at the end of `this`.
1133 void MergeInstructionsWith(HBasicBlock* other);
1134
David Brazdil2d7352b2015-04-20 14:52:42 +01001135 // Merge `other` at the end of `this`. This method updates loops, reverse post
1136 // order, links to predecessors, successors, dominators and deletes the block
1137 // from the graph. The two blocks must be successive, i.e. `this` the only
1138 // predecessor of `other` and vice versa.
1139 void MergeWith(HBasicBlock* other);
1140
1141 // Disconnects `this` from all its predecessors, successors and dominator,
1142 // removes it from all loops it is included in and eventually from the graph.
1143 // The block must not dominate any other block. Predecessors and successors
1144 // are safely updated.
1145 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001146
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001147 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001148 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001149 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001150 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001151 // Replace instruction `initial` with `replacement` within this block.
1152 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1153 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001155 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001156 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1157 // instruction list. With 'ensure_safety' set to true, it verifies that the
1158 // instruction is not in use and removes it from the use lists of its inputs.
1159 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1160 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001161 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001162
1163 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001164 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001165 }
1166
Roland Levillain6b879dd2014-09-22 17:13:44 +01001167 bool IsLoopPreHeaderFirstPredecessor() const {
1168 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001169 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001170 }
1171
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001172 bool IsFirstPredecessorBackEdge() const {
1173 DCHECK(IsLoopHeader());
1174 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1175 }
1176
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001177 HLoopInformation* GetLoopInformation() const {
1178 return loop_information_;
1179 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001180
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001181 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001182 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001183 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001184 void SetInLoop(HLoopInformation* info) {
1185 if (IsLoopHeader()) {
1186 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001187 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001188 loop_information_ = info;
1189 } else if (loop_information_->Contains(*info->GetHeader())) {
1190 // Block is currently part of an outer loop. Make it part of this inner loop.
1191 // Note that a non loop header having a loop information means this loop information
1192 // has already been populated
1193 loop_information_ = info;
1194 } else {
1195 // Block is part of an inner loop. Do not update the loop information.
1196 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1197 // at this point, because this method is being called while populating `info`.
1198 }
1199 }
1200
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001201 // Raw update of the loop information.
1202 void SetLoopInformation(HLoopInformation* info) {
1203 loop_information_ = info;
1204 }
1205
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001206 bool IsInLoop() const { return loop_information_ != nullptr; }
1207
David Brazdilec16f792015-08-19 15:04:01 +01001208 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1209
1210 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1211 try_catch_information_ = try_catch_information;
1212 }
1213
1214 bool IsTryBlock() const {
1215 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1216 }
1217
1218 bool IsCatchBlock() const {
1219 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1220 }
David Brazdilffee3d32015-07-06 11:48:53 +01001221
1222 // Returns the try entry that this block's successors should have. They will
1223 // be in the same try, unless the block ends in a try boundary. In that case,
1224 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001225 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001226
David Brazdild7558da2015-09-22 13:04:14 +01001227 bool HasThrowingInstructions() const;
1228
David Brazdila4b8c212015-05-07 09:59:30 +01001229 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001230 bool Dominates(HBasicBlock* block) const;
1231
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001232 size_t GetLifetimeStart() const { return lifetime_start_; }
1233 size_t GetLifetimeEnd() const { return lifetime_end_; }
1234
1235 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1236 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1237
David Brazdil8d5b8b22015-03-24 10:51:52 +00001238 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001239 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001240 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001241 bool HasSinglePhi() const;
1242
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001243 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001244 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001245 ArenaVector<HBasicBlock*> predecessors_;
1246 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001247 HInstructionList instructions_;
1248 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001249 HLoopInformation* loop_information_;
1250 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001251 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001252 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001253 // The dex program counter of the first instruction of this block.
1254 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001255 size_t lifetime_start_;
1256 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001257 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001258
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001259 friend class HGraph;
1260 friend class HInstruction;
1261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001262 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1263};
1264
David Brazdilb2bd1c52015-03-25 11:17:37 +00001265// Iterates over the LoopInformation of all loops which contain 'block'
1266// from the innermost to the outermost.
1267class HLoopInformationOutwardIterator : public ValueObject {
1268 public:
1269 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1270 : current_(block.GetLoopInformation()) {}
1271
1272 bool Done() const { return current_ == nullptr; }
1273
1274 void Advance() {
1275 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001276 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001277 }
1278
1279 HLoopInformation* Current() const {
1280 DCHECK(!Done());
1281 return current_;
1282 }
1283
1284 private:
1285 HLoopInformation* current_;
1286
1287 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1288};
1289
Alexandre Ramesef20f712015-06-09 10:29:30 +01001290#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001291 M(Above, Condition) \
1292 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001293 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001294 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(ArrayGet, Instruction) \
1296 M(ArrayLength, Instruction) \
1297 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001298 M(Below, Condition) \
1299 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001300 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001301 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001302 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001303 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001304 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001305 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001306 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(Compare, BinaryOperation) \
Igor Murashkind01745e2017-04-05 16:40:31 -07001308 M(ConstructorFence, Instruction) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001309 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001310 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001311 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001312 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001313 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001315 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001316 M(Exit, Instruction) \
1317 M(FloatConstant, Constant) \
1318 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319 M(GreaterThan, Condition) \
1320 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001322 M(InstanceFieldGet, Instruction) \
1323 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001324 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001325 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001326 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001327 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001328 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001329 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001330 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001331 M(LessThan, Condition) \
1332 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001333 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001334 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001335 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001336 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001337 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001338 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001339 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001340 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001341 M(Neg, UnaryOperation) \
1342 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001344 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001345 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001346 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001347 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001348 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001349 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001351 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001352 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001353 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001354 M(Return, Instruction) \
1355 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001356 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001357 M(Shl, BinaryOperation) \
1358 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001359 M(StaticFieldGet, Instruction) \
1360 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001361 M(UnresolvedInstanceFieldGet, Instruction) \
1362 M(UnresolvedInstanceFieldSet, Instruction) \
1363 M(UnresolvedStaticFieldGet, Instruction) \
1364 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001365 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001366 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001367 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001368 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001369 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001370 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001371 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001372 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001373 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001374 M(VecSumReduce, VecUnaryOperation) \
1375 M(VecCnv, VecUnaryOperation) \
1376 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001377 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001378 M(VecNot, VecUnaryOperation) \
1379 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001380 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001381 M(VecSub, VecBinaryOperation) \
1382 M(VecMul, VecBinaryOperation) \
1383 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001384 M(VecMin, VecBinaryOperation) \
1385 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001386 M(VecAnd, VecBinaryOperation) \
1387 M(VecAndNot, VecBinaryOperation) \
1388 M(VecOr, VecBinaryOperation) \
1389 M(VecXor, VecBinaryOperation) \
1390 M(VecShl, VecBinaryOperation) \
1391 M(VecShr, VecBinaryOperation) \
1392 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001393 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001394 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001395 M(VecLoad, VecMemoryOperation) \
1396 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001397
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001398/*
1399 * Instructions, shared across several (not all) architectures.
1400 */
1401#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1402#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1403#else
1404#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001405 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001406 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001407 M(MultiplyAccumulate, Instruction) \
1408 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001409#endif
1410
Vladimir Markob4536b72015-11-24 13:45:23 +00001411#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001412#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001413#else
1414#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1415 M(ArmDexCacheArraysBase, Instruction)
1416#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001417
1418#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1419
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001420#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001421#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001422#else
1423#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1424 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001425 M(MipsDexCacheArraysBase, Instruction) \
1426 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001427#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001428
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001429#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1430
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001431#ifndef ART_ENABLE_CODEGEN_x86
1432#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1433#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001434#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1435 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001436 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001437 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001438 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001439#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001440
1441#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1442
1443#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1444 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001445 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001446 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1447 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001449 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001450 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1451 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1452
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001453#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1454 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001455 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001456 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001457 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001458 M(Invoke, Instruction) \
1459 M(VecOperation, Instruction) \
1460 M(VecUnaryOperation, VecOperation) \
1461 M(VecBinaryOperation, VecOperation) \
1462 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001463
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001464#define FOR_EACH_INSTRUCTION(M) \
1465 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1466 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1467
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001468#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001469FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1470#undef FORWARD_DECLARATION
1471
Vladimir Marko372f10e2016-05-17 16:30:10 +01001472#define DECLARE_INSTRUCTION(type) \
1473 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1474 const char* DebugName() const OVERRIDE { return #type; } \
1475 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1476 return other->Is##type(); \
1477 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001478 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001479
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001480#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1481 bool Is##type() const { return As##type() != nullptr; } \
1482 const H##type* As##type() const { return this; } \
1483 H##type* As##type() { return this; }
1484
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001485template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001486class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487 public:
Igor Murashkind01745e2017-04-05 16:40:31 -07001488 // Get the instruction which has this use as one of the inputs.
David Brazdiled596192015-01-23 10:39:45 +00001489 T GetUser() const { return user_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001490 // Get the position of the input record that this use corresponds to.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001491 size_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07001492 // Set the position of the input record that this use corresponds to.
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001493 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001494
Vladimir Marko46817b82016-03-29 12:21:58 +01001495 // Hook for the IntrusiveForwardList<>.
1496 // TODO: Hide this better.
1497 IntrusiveForwardListHook hook;
1498
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001499 private:
David Brazdiled596192015-01-23 10:39:45 +00001500 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001501 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001502
1503 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001504 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001505
Vladimir Marko46817b82016-03-29 12:21:58 +01001506 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001507
1508 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1509};
1510
David Brazdiled596192015-01-23 10:39:45 +00001511template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001512using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001513
David Brazdil1abb4192015-02-17 18:33:36 +00001514// This class is used by HEnvironment and HInstruction classes to record the
1515// instructions they use and pointers to the corresponding HUseListNodes kept
1516// by the used instructions.
1517template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001518class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001519 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001520 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1521 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001522
Vladimir Marko46817b82016-03-29 12:21:58 +01001523 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1524 : HUserRecord(old_record.instruction_, before_use_node) {}
1525 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1526 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001527 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001528 }
1529
1530 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001531 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1532 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001533
1534 private:
1535 // Instruction used by the user.
1536 HInstruction* instruction_;
1537
Vladimir Marko46817b82016-03-29 12:21:58 +01001538 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1539 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001540};
1541
Vladimir Markoe9004912016-06-16 16:50:52 +01001542// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1543// This is used for HInstruction::GetInputs() to return a container wrapper providing
1544// HInstruction* values even though the underlying container has HUserRecord<>s.
1545struct HInputExtractor {
1546 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1547 return record.GetInstruction();
1548 }
1549 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1550 return record.GetInstruction();
1551 }
1552};
1553
1554using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1555using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1556
Aart Bik854a02b2015-07-14 16:07:00 -07001557/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001559 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001560 * For write/read dependences on fields/arrays, the dependence analysis uses
1561 * type disambiguation (e.g. a float field write cannot modify the value of an
1562 * integer field read) and the access type (e.g. a reference array write cannot
1563 * modify the value of a reference field read [although it may modify the
1564 * reference fetch prior to reading the field, which is represented by its own
1565 * write/read dependence]). The analysis makes conservative points-to
1566 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1567 * the same, and any reference read depends on any reference read without
1568 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001569 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001570 * The internal representation uses 38-bit and is described in the table below.
1571 * The first line indicates the side effect, and for field/array accesses the
1572 * second line indicates the type of the access (in the order of the
1573 * Primitive::Type enum).
1574 * The two numbered lines below indicate the bit position in the bitfield (read
1575 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001576 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001577 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1578 * +-------------+---------+---------+--------------+---------+---------+
1579 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1580 * | 3 |333333322|222222221| 1 |111111110|000000000|
1581 * | 7 |654321098|765432109| 8 |765432109|876543210|
1582 *
1583 * Note that, to ease the implementation, 'changes' bits are least significant
1584 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001585 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001586class SideEffects : public ValueObject {
1587 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001588 SideEffects() : flags_(0) {}
1589
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001590 static SideEffects None() {
1591 return SideEffects(0);
1592 }
1593
1594 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 return SideEffects(kAllChangeBits | kAllDependOnBits);
1596 }
1597
1598 static SideEffects AllChanges() {
1599 return SideEffects(kAllChangeBits);
1600 }
1601
1602 static SideEffects AllDependencies() {
1603 return SideEffects(kAllDependOnBits);
1604 }
1605
1606 static SideEffects AllExceptGCDependency() {
1607 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1608 }
1609
1610 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001611 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001612 }
1613
Aart Bik34c3ba92015-07-20 14:08:59 -07001614 static SideEffects AllWrites() {
1615 return SideEffects(kAllWrites);
1616 }
1617
1618 static SideEffects AllReads() {
1619 return SideEffects(kAllReads);
1620 }
1621
1622 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1623 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001624 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001625 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001626 }
1627
Aart Bik854a02b2015-07-14 16:07:00 -07001628 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001629 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001630 }
1631
Aart Bik34c3ba92015-07-20 14:08:59 -07001632 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1633 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001634 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001635 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001636 }
1637
1638 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001639 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001640 }
1641
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 static SideEffects CanTriggerGC() {
1643 return SideEffects(1ULL << kCanTriggerGCBit);
1644 }
1645
1646 static SideEffects DependsOnGC() {
1647 return SideEffects(1ULL << kDependsOnGCBit);
1648 }
1649
Aart Bik854a02b2015-07-14 16:07:00 -07001650 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001651 SideEffects Union(SideEffects other) const {
1652 return SideEffects(flags_ | other.flags_);
1653 }
1654
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001655 SideEffects Exclusion(SideEffects other) const {
1656 return SideEffects(flags_ & ~other.flags_);
1657 }
1658
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001659 void Add(SideEffects other) {
1660 flags_ |= other.flags_;
1661 }
1662
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001663 bool Includes(SideEffects other) const {
1664 return (other.flags_ & flags_) == other.flags_;
1665 }
1666
1667 bool HasSideEffects() const {
1668 return (flags_ & kAllChangeBits);
1669 }
1670
1671 bool HasDependencies() const {
1672 return (flags_ & kAllDependOnBits);
1673 }
1674
1675 // Returns true if there are no side effects or dependencies.
1676 bool DoesNothing() const {
1677 return flags_ == 0;
1678 }
1679
Aart Bik854a02b2015-07-14 16:07:00 -07001680 // Returns true if something is written.
1681 bool DoesAnyWrite() const {
1682 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001683 }
1684
Aart Bik854a02b2015-07-14 16:07:00 -07001685 // Returns true if something is read.
1686 bool DoesAnyRead() const {
1687 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001688 }
1689
Aart Bik854a02b2015-07-14 16:07:00 -07001690 // Returns true if potentially everything is written and read
1691 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001692 bool DoesAllReadWrite() const {
1693 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1694 }
1695
Aart Bik854a02b2015-07-14 16:07:00 -07001696 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001698 }
1699
Roland Levillain0d5a2812015-11-13 10:07:31 +00001700 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001701 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1703 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001704 }
1705
1706 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001707 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001708 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001709 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001710 for (int s = kLastBit; s >= 0; s--) {
1711 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1712 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1713 // This is a bit for the GC side effect.
1714 if (current_bit_is_set) {
1715 flags += "GC";
1716 }
Aart Bik854a02b2015-07-14 16:07:00 -07001717 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 } else {
1719 // This is a bit for the array/field analysis.
1720 // The underscore character stands for the 'can trigger GC' bit.
1721 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1722 if (current_bit_is_set) {
1723 flags += kDebug[s];
1724 }
1725 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1726 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1727 flags += "|";
1728 }
1729 }
Aart Bik854a02b2015-07-14 16:07:00 -07001730 }
1731 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001732 }
1733
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001734 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001735
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001736 private:
1737 static constexpr int kFieldArrayAnalysisBits = 9;
1738
1739 static constexpr int kFieldWriteOffset = 0;
1740 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1741 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1742 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1743
1744 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1745
1746 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1747 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1748 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1749 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1750
1751 static constexpr int kLastBit = kDependsOnGCBit;
1752 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1753
1754 // Aliases.
1755
1756 static_assert(kChangeBits == kDependOnBits,
1757 "the 'change' bits should match the 'depend on' bits.");
1758
1759 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1760 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1761 static constexpr uint64_t kAllWrites =
1762 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1763 static constexpr uint64_t kAllReads =
1764 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001765
Aart Bik854a02b2015-07-14 16:07:00 -07001766 // Translates type to bit flag.
1767 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1768 CHECK_NE(type, Primitive::kPrimVoid);
1769 const uint64_t one = 1;
1770 const int shift = type; // 0-based consecutive enum
1771 DCHECK_LE(kFieldWriteOffset, shift);
1772 DCHECK_LT(shift, kArrayWriteOffset);
1773 return one << (type + offset);
1774 }
1775
1776 // Private constructor on direct flags value.
1777 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1778
1779 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001780};
1781
David Brazdiled596192015-01-23 10:39:45 +00001782// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001783class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001784 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001785 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1786 size_t number_of_vregs,
1787 ArtMethod* method,
1788 uint32_t dex_pc,
1789 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1791 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001793 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001794 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001795 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001796 }
1797
Mingyao Yang01b47b02017-02-03 12:09:57 -08001798 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001799 : HEnvironment(arena,
1800 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001801 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001802 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001803 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001804
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001806 if (parent_ != nullptr) {
1807 parent_->SetAndCopyParentChain(allocator, parent);
1808 } else {
1809 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1810 parent_->CopyFrom(parent);
1811 if (parent->GetParent() != nullptr) {
1812 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1813 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001814 }
David Brazdiled596192015-01-23 10:39:45 +00001815 }
1816
Vladimir Marko71bf8092015-09-15 15:33:14 +01001817 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001818 void CopyFrom(HEnvironment* environment);
1819
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001820 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1821 // input to the loop phi instead. This is for inserting instructions that
1822 // require an environment (like HDeoptimization) in the loop pre-header.
1823 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001824
1825 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001826 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001827 }
1828
1829 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001830 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001831 }
1832
David Brazdil1abb4192015-02-17 18:33:36 +00001833 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001834
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001835 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001836
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 HEnvironment* GetParent() const { return parent_; }
1838
1839 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001840 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 }
1842
1843 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001844 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001845 }
1846
1847 uint32_t GetDexPc() const {
1848 return dex_pc_;
1849 }
1850
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001851 ArtMethod* GetMethod() const {
1852 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001853 }
1854
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001855 HInstruction* GetHolder() const {
1856 return holder_;
1857 }
1858
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001859
1860 bool IsFromInlinedInvoke() const {
1861 return GetParent() != nullptr;
1862 }
1863
David Brazdiled596192015-01-23 10:39:45 +00001864 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001865 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1866 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001867 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001868 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001869 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001870
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001871 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001872 HInstruction* const holder_;
1873
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001874 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001875
1876 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1877};
1878
Vladimir Markof9f64412015-09-02 14:05:49 +01001879class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001880 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001881 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001882 : previous_(nullptr),
1883 next_(nullptr),
1884 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001885 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001886 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001887 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001888 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001889 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001890 locations_(nullptr),
1891 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001892 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001893 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001894 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1895 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1896 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001897
Dave Allison20dfc792014-06-16 20:44:29 -07001898 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001899
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001900#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001901 enum InstructionKind {
1902 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1903 };
1904#undef DECLARE_KIND
1905
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 HInstruction* GetNext() const { return next_; }
1907 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001908
Calin Juravle77520bc2015-01-12 18:45:46 +00001909 HInstruction* GetNextDisregardingMoves() const;
1910 HInstruction* GetPreviousDisregardingMoves() const;
1911
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001912 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001913 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001915 bool IsInBlock() const { return block_ != nullptr; }
1916 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001917 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1918 bool IsIrreducibleLoopHeaderPhi() const {
1919 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1920 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001921
Vladimir Marko372f10e2016-05-17 16:30:10 +01001922 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1923
1924 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1925 // One virtual method is enough, just const_cast<> and then re-add the const.
1926 return ArrayRef<const HUserRecord<HInstruction*>>(
1927 const_cast<HInstruction*>(this)->GetInputRecords());
1928 }
1929
Vladimir Markoe9004912016-06-16 16:50:52 +01001930 HInputsRef GetInputs() {
1931 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001932 }
1933
Vladimir Markoe9004912016-06-16 16:50:52 +01001934 HConstInputsRef GetInputs() const {
1935 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001936 }
1937
1938 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001939 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001940
Aart Bik2767f4b2016-10-28 15:03:53 -07001941 bool HasInput(HInstruction* input) const {
1942 for (const HInstruction* i : GetInputs()) {
1943 if (i == input) {
1944 return true;
1945 }
1946 }
1947 return false;
1948 }
1949
Vladimir Marko372f10e2016-05-17 16:30:10 +01001950 void SetRawInputAt(size_t index, HInstruction* input) {
1951 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1952 }
1953
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001954 virtual void Accept(HGraphVisitor* visitor) = 0;
1955 virtual const char* DebugName() const = 0;
1956
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1958
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001959 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001960
1961 uint32_t GetDexPc() const { return dex_pc_; }
1962
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001963 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001964
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001965 // Can the instruction throw?
1966 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1967 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001968 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001969 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001970
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001971 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001972 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973
Calin Juravle10e244f2015-01-26 18:54:32 +00001974 // Does not apply for all instructions, but having this at top level greatly
1975 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001976 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001977 virtual bool CanBeNull() const {
1978 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1979 return true;
1980 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001981
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001982 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001983 return false;
1984 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001985
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001986 virtual bool IsActualObject() const {
1987 return GetType() == Primitive::kPrimNot;
1988 }
1989
Calin Juravle2e768302015-07-28 14:41:11 +00001990 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001991
Calin Juravle61d544b2015-02-23 16:46:57 +00001992 ReferenceTypeInfo GetReferenceTypeInfo() const {
1993 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001994 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001995 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001996 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001997
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001998 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001999 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002000 // Note: fixup_end remains valid across push_front().
2001 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
2002 HUseListNode<HInstruction*>* new_node =
2003 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
2004 uses_.push_front(*new_node);
2005 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002006 }
2007
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01002009 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01002010 // Note: env_fixup_end remains valid across push_front().
2011 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2012 HUseListNode<HEnvironment*>* new_node =
2013 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2014 env_uses_.push_front(*new_node);
2015 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002016 }
2017
David Brazdil1abb4192015-02-17 18:33:36 +00002018 void RemoveAsUserOfInput(size_t input) {
2019 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002020 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2021 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2022 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002023 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002024
Vladimir Marko372f10e2016-05-17 16:30:10 +01002025 void RemoveAsUserOfAllInputs() {
2026 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2027 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2028 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2029 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2030 }
2031 }
2032
David Brazdil1abb4192015-02-17 18:33:36 +00002033 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2034 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002035
Vladimir Marko46817b82016-03-29 12:21:58 +01002036 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2037 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2038 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002039 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002040 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002041 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002042
Aart Bikcc42be02016-10-20 16:14:16 -07002043 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002044 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002045 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002046 !CanThrow() &&
2047 !IsSuspendCheck() &&
2048 !IsControlFlow() &&
2049 !IsNativeDebugInfo() &&
2050 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002051 // If we added an explicit barrier then we should keep it.
Igor Murashkind01745e2017-04-05 16:40:31 -07002052 !IsMemoryBarrier() &&
2053 !IsConstructorFence();
Aart Bik482095d2016-10-10 15:39:10 -07002054 }
2055
Aart Bikcc42be02016-10-20 16:14:16 -07002056 bool IsDeadAndRemovable() const {
2057 return IsRemovable() && !HasUses();
2058 }
2059
Roland Levillain6c82d402014-10-13 16:10:27 +01002060 // Does this instruction strictly dominate `other_instruction`?
2061 // Returns false if this instruction and `other_instruction` are the same.
2062 // Aborts if this instruction and `other_instruction` are both phis.
2063 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002064
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002065 int GetId() const { return id_; }
2066 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002067
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002068 int GetSsaIndex() const { return ssa_index_; }
2069 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2070 bool HasSsaIndex() const { return ssa_index_ != -1; }
2071
2072 bool HasEnvironment() const { return environment_ != nullptr; }
2073 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002074 // Set the `environment_` field. Raw because this method does not
2075 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002076 void SetRawEnvironment(HEnvironment* environment) {
2077 DCHECK(environment_ == nullptr);
2078 DCHECK_EQ(environment->GetHolder(), this);
2079 environment_ = environment;
2080 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002081
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002082 void InsertRawEnvironment(HEnvironment* environment) {
2083 DCHECK(environment_ != nullptr);
2084 DCHECK_EQ(environment->GetHolder(), this);
2085 DCHECK(environment->GetParent() == nullptr);
2086 environment->parent_ = environment_;
2087 environment_ = environment;
2088 }
2089
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002090 void RemoveEnvironment();
2091
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002092 // Set the environment of this instruction, copying it from `environment`. While
2093 // copying, the uses lists are being updated.
2094 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002095 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002096 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002097 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002098 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002099 if (environment->GetParent() != nullptr) {
2100 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2101 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002102 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002103
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002104 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2105 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002106 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002107 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002108 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002109 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002110 if (environment->GetParent() != nullptr) {
2111 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2112 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002113 }
2114
Nicolas Geoffray39468442014-09-02 15:17:15 +01002115 // Returns the number of entries in the environment. Typically, that is the
2116 // number of dex registers in a method. It could be more in case of inlining.
2117 size_t EnvironmentSize() const;
2118
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002119 LocationSummary* GetLocations() const { return locations_; }
2120 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002122 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002123 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002124 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002125
Alexandre Rames188d4312015-04-09 18:30:21 +01002126 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2127 // uses of this instruction by `other` are *not* updated.
2128 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2129 ReplaceWith(other);
2130 other->ReplaceInput(this, use_index);
2131 }
2132
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002133 // Move `this` instruction before `cursor`
2134 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002135
Vladimir Markofb337ea2015-11-25 15:25:10 +00002136 // Move `this` before its first user and out of any loops. If there is no
2137 // out-of-loop user that dominates all other users, move the instruction
2138 // to the end of the out-of-loop common dominator of the user's blocks.
2139 //
2140 // This can be used only on non-throwing instructions with no side effects that
2141 // have at least one use but no environment uses.
2142 void MoveBeforeFirstUserAndOutOfLoops();
2143
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002144#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002145 bool Is##type() const; \
2146 const H##type* As##type() const; \
2147 H##type* As##type();
2148
2149 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2150#undef INSTRUCTION_TYPE_CHECK
2151
2152#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002153 bool Is##type() const { return (As##type() != nullptr); } \
2154 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002155 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002156 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002157#undef INSTRUCTION_TYPE_CHECK
2158
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002159 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002160 // TODO: this method is used by LICM and GVN with possibly different
2161 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162 virtual bool CanBeMoved() const { return false; }
2163
2164 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002165 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002166 return false;
2167 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168
2169 // Returns whether any data encoded in the two instructions is equal.
2170 // This method does not look at the inputs. Both instructions must be
2171 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002172 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002173 return false;
2174 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002175
2176 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002177 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002178 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002179 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002180
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002181 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2182 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2183 // the virtual function because the __attribute__((__pure__)) doesn't really
2184 // apply the strong requirement for virtual functions, preventing optimizations.
2185 InstructionKind GetKind() const PURE;
2186 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002187
2188 virtual size_t ComputeHashCode() const {
2189 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002190 for (const HInstruction* input : GetInputs()) {
2191 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002192 }
2193 return result;
2194 }
2195
2196 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002197 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002198 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002200 size_t GetLifetimePosition() const { return lifetime_position_; }
2201 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2202 LiveInterval* GetLiveInterval() const { return live_interval_; }
2203 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2204 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2205
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002206 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2207
2208 // Returns whether the code generation of the instruction will require to have access
2209 // to the current method. Such instructions are:
2210 // (1): Instructions that require an environment, as calling the runtime requires
2211 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002212 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2213 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002214 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002215 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002216 }
2217
Vladimir Markodc151b22015-10-15 18:02:30 +01002218 // Returns whether the code generation of the instruction will require to have access
2219 // to the dex cache of the current method's declaring class via the current method.
2220 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002221
Mark Mendellc4701932015-04-10 13:18:51 -04002222 // Does this instruction have any use in an environment before
2223 // control flow hits 'other'?
2224 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2225
2226 // Remove all references to environment uses of this instruction.
2227 // The caller must ensure that this is safe to do.
2228 void RemoveEnvironmentUsers();
2229
Vladimir Markoa1de9182016-02-25 11:37:38 +00002230 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2231 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002232
David Brazdil1abb4192015-02-17 18:33:36 +00002233 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002234 // If set, the machine code for this instruction is assumed to be generated by
2235 // its users. Used by liveness analysis to compute use positions accordingly.
2236 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2237 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2238 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2239 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2240
Vladimir Marko372f10e2016-05-17 16:30:10 +01002241 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2242 return GetInputRecords()[i];
2243 }
2244
2245 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2246 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2247 input_records[index] = input;
2248 }
David Brazdil1abb4192015-02-17 18:33:36 +00002249
Vladimir Markoa1de9182016-02-25 11:37:38 +00002250 uint32_t GetPackedFields() const {
2251 return packed_fields_;
2252 }
2253
2254 template <size_t flag>
2255 bool GetPackedFlag() const {
2256 return (packed_fields_ & (1u << flag)) != 0u;
2257 }
2258
2259 template <size_t flag>
2260 void SetPackedFlag(bool value = true) {
2261 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2262 }
2263
2264 template <typename BitFieldType>
2265 typename BitFieldType::value_type GetPackedField() const {
2266 return BitFieldType::Decode(packed_fields_);
2267 }
2268
2269 template <typename BitFieldType>
2270 void SetPackedField(typename BitFieldType::value_type value) {
2271 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2272 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2273 }
2274
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002275 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002276 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2277 auto before_use_node = uses_.before_begin();
2278 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2279 HInstruction* user = use_node->GetUser();
2280 size_t input_index = use_node->GetIndex();
2281 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2282 before_use_node = use_node;
2283 }
2284 }
2285
2286 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2287 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2288 if (next != uses_.end()) {
2289 HInstruction* next_user = next->GetUser();
2290 size_t next_index = next->GetIndex();
2291 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2292 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2293 }
2294 }
2295
2296 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2297 auto before_env_use_node = env_uses_.before_begin();
2298 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2299 HEnvironment* user = env_use_node->GetUser();
2300 size_t input_index = env_use_node->GetIndex();
2301 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2302 before_env_use_node = env_use_node;
2303 }
2304 }
2305
2306 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2307 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2308 if (next != env_uses_.end()) {
2309 HEnvironment* next_user = next->GetUser();
2310 size_t next_index = next->GetIndex();
2311 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2312 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2313 }
2314 }
David Brazdil1abb4192015-02-17 18:33:36 +00002315
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002316 HInstruction* previous_;
2317 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002318 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002319 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002320
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002321 // An instruction gets an id when it is added to the graph.
2322 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002323 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002324 int id_;
2325
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002326 // When doing liveness analysis, instructions that have uses get an SSA index.
2327 int ssa_index_;
2328
Vladimir Markoa1de9182016-02-25 11:37:38 +00002329 // Packed fields.
2330 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002331
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002332 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002333 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002334
2335 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002336 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002337
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338 // The environment associated with this instruction. Not null if the instruction
2339 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002340 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002341
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002342 // Set by the code generator.
2343 LocationSummary* locations_;
2344
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002345 // Set by the liveness analysis.
2346 LiveInterval* live_interval_;
2347
2348 // Set by the liveness analysis, this is the position in a linear
2349 // order of blocks where this instruction's live interval start.
2350 size_t lifetime_position_;
2351
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002352 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002353
Vladimir Markoa1de9182016-02-25 11:37:38 +00002354 // The reference handle part of the reference type info.
2355 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002356 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002357 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002358
David Brazdil1abb4192015-02-17 18:33:36 +00002359 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002360 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002361 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002362 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002363 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364
2365 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2366};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002367std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002368
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002369// Iterates over the instructions, while preserving the next instruction
2370// in case the current instruction gets removed from the list by the user
2371// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002372class HInstructionIterator : public ValueObject {
2373 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002374 explicit HInstructionIterator(const HInstructionList& instructions)
2375 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002376 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002377 }
2378
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002379 bool Done() const { return instruction_ == nullptr; }
2380 HInstruction* Current() const { return instruction_; }
2381 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002382 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002383 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002384 }
2385
2386 private:
2387 HInstruction* instruction_;
2388 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002389
2390 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002391};
2392
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002393// Iterates over the instructions without saving the next instruction,
2394// therefore handling changes in the graph potentially made by the user
2395// of this iterator.
2396class HInstructionIteratorHandleChanges : public ValueObject {
2397 public:
2398 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2399 : instruction_(instructions.first_instruction_) {
2400 }
2401
2402 bool Done() const { return instruction_ == nullptr; }
2403 HInstruction* Current() const { return instruction_; }
2404 void Advance() {
2405 instruction_ = instruction_->GetNext();
2406 }
2407
2408 private:
2409 HInstruction* instruction_;
2410
2411 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2412};
2413
2414
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002415class HBackwardInstructionIterator : public ValueObject {
2416 public:
2417 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2418 : instruction_(instructions.last_instruction_) {
2419 next_ = Done() ? nullptr : instruction_->GetPrevious();
2420 }
2421
2422 bool Done() const { return instruction_ == nullptr; }
2423 HInstruction* Current() const { return instruction_; }
2424 void Advance() {
2425 instruction_ = next_;
2426 next_ = Done() ? nullptr : instruction_->GetPrevious();
2427 }
2428
2429 private:
2430 HInstruction* instruction_;
2431 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002432
2433 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002434};
2435
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002436class HVariableInputSizeInstruction : public HInstruction {
2437 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002438 using HInstruction::GetInputRecords; // Keep the const version visible.
2439 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2440 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2441 }
2442
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002443 void AddInput(HInstruction* input);
2444 void InsertInputAt(size_t index, HInstruction* input);
2445 void RemoveInputAt(size_t index);
2446
Igor Murashkind01745e2017-04-05 16:40:31 -07002447 // Removes all the inputs.
2448 // Also removes this instructions from each input's use list
2449 // (for non-environment uses only).
2450 void RemoveAllInputs();
2451
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002452 protected:
2453 HVariableInputSizeInstruction(SideEffects side_effects,
2454 uint32_t dex_pc,
2455 ArenaAllocator* arena,
2456 size_t number_of_inputs,
2457 ArenaAllocKind kind)
2458 : HInstruction(side_effects, dex_pc),
2459 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2460
2461 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2462
2463 private:
2464 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2465};
2466
Vladimir Markof9f64412015-09-02 14:05:49 +01002467template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468class HTemplateInstruction: public HInstruction {
2469 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002470 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002472 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002473
Vladimir Marko372f10e2016-05-17 16:30:10 +01002474 using HInstruction::GetInputRecords; // Keep the const version visible.
2475 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2476 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002477 }
2478
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002479 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002480 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002481
2482 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002483};
2484
Vladimir Markof9f64412015-09-02 14:05:49 +01002485// HTemplateInstruction specialization for N=0.
2486template<>
2487class HTemplateInstruction<0>: public HInstruction {
2488 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002489 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002490 : HInstruction(side_effects, dex_pc) {}
2491
Vladimir Markof9f64412015-09-02 14:05:49 +01002492 virtual ~HTemplateInstruction() {}
2493
Vladimir Marko372f10e2016-05-17 16:30:10 +01002494 using HInstruction::GetInputRecords; // Keep the const version visible.
2495 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2496 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002497 }
2498
2499 private:
2500 friend class SsaBuilder;
2501};
2502
Dave Allison20dfc792014-06-16 20:44:29 -07002503template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002504class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002505 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002506 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002507 : HTemplateInstruction<N>(side_effects, dex_pc) {
2508 this->template SetPackedField<TypeField>(type);
2509 }
Dave Allison20dfc792014-06-16 20:44:29 -07002510 virtual ~HExpression() {}
2511
Vladimir Markoa1de9182016-02-25 11:37:38 +00002512 Primitive::Type GetType() const OVERRIDE {
2513 return TypeField::Decode(this->GetPackedFields());
2514 }
Dave Allison20dfc792014-06-16 20:44:29 -07002515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002516 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002517 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2518 static constexpr size_t kFieldTypeSize =
2519 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2520 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2521 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2522 "Too many packed fields.");
2523 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002524};
2525
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002526// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2527// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002528class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002530 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2531 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002533 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002535 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2539};
2540
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002541// Represents dex's RETURN opcodes. A HReturn is a control flow
2542// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002543class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002544 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002545 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2546 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002547 SetRawInputAt(0, value);
2548 }
2549
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002550 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002551
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002552 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002553
2554 private:
2555 DISALLOW_COPY_AND_ASSIGN(HReturn);
2556};
2557
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002558class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002559 public:
2560 HPhi(ArenaAllocator* arena,
2561 uint32_t reg_number,
2562 size_t number_of_inputs,
2563 Primitive::Type type,
2564 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002565 : HVariableInputSizeInstruction(
2566 SideEffects::None(),
2567 dex_pc,
2568 arena,
2569 number_of_inputs,
2570 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002571 reg_number_(reg_number) {
2572 SetPackedField<TypeField>(ToPhiType(type));
2573 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2574 // Phis are constructed live and marked dead if conflicting or unused.
2575 // Individual steps of SsaBuilder should assume that if a phi has been
2576 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2577 SetPackedFlag<kFlagIsLive>(true);
2578 SetPackedFlag<kFlagCanBeNull>(true);
2579 }
2580
2581 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2582 static Primitive::Type ToPhiType(Primitive::Type type) {
2583 return Primitive::PrimitiveKind(type);
2584 }
2585
2586 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2587
David Brazdildee58d62016-04-07 09:54:26 +00002588 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2589 void SetType(Primitive::Type new_type) {
2590 // Make sure that only valid type changes occur. The following are allowed:
2591 // (1) int -> float/ref (primitive type propagation),
2592 // (2) long -> double (primitive type propagation).
2593 DCHECK(GetType() == new_type ||
2594 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2595 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2596 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2597 SetPackedField<TypeField>(new_type);
2598 }
2599
2600 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2601 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2602
2603 uint32_t GetRegNumber() const { return reg_number_; }
2604
2605 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2606 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2607 bool IsDead() const { return !IsLive(); }
2608 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2609
Vladimir Markoe9004912016-06-16 16:50:52 +01002610 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002611 return other != nullptr
2612 && other->IsPhi()
2613 && other->AsPhi()->GetBlock() == GetBlock()
2614 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2615 }
2616
2617 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2618 // An equivalent phi is a phi having the same dex register and type.
2619 // It assumes that phis with the same dex register are adjacent.
2620 HPhi* GetNextEquivalentPhiWithSameType() {
2621 HInstruction* next = GetNext();
2622 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2623 if (next->GetType() == GetType()) {
2624 return next->AsPhi();
2625 }
2626 next = next->GetNext();
2627 }
2628 return nullptr;
2629 }
2630
2631 DECLARE_INSTRUCTION(Phi);
2632
David Brazdildee58d62016-04-07 09:54:26 +00002633 private:
2634 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2635 static constexpr size_t kFieldTypeSize =
2636 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2637 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2638 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2639 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2640 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2641 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2642
David Brazdildee58d62016-04-07 09:54:26 +00002643 const uint32_t reg_number_;
2644
2645 DISALLOW_COPY_AND_ASSIGN(HPhi);
2646};
2647
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002649// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002651class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002653 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002654
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002655 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002657 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658
2659 private:
2660 DISALLOW_COPY_AND_ASSIGN(HExit);
2661};
2662
2663// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002664class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002665 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002667
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002668 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002669
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002670 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002671 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002672 }
2673
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002674 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002675
2676 private:
2677 DISALLOW_COPY_AND_ASSIGN(HGoto);
2678};
2679
Roland Levillain9867bc72015-08-05 10:21:34 +01002680class HConstant : public HExpression<0> {
2681 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002682 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2683 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002684
2685 bool CanBeMoved() const OVERRIDE { return true; }
2686
Roland Levillain1a653882016-03-18 18:05:57 +00002687 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002689 // Is this constant 0 in the arithmetic sense?
2690 virtual bool IsArithmeticZero() const { return false; }
2691 // Is this constant a 0-bit pattern?
2692 virtual bool IsZeroBitPattern() const { return false; }
2693 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 virtual bool IsOne() const { return false; }
2695
David Brazdil77a48ae2015-09-15 12:34:04 +00002696 virtual uint64_t GetValueAsUint64() const = 0;
2697
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002698 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002699
2700 private:
2701 DISALLOW_COPY_AND_ASSIGN(HConstant);
2702};
2703
Vladimir Markofcb503c2016-05-18 12:48:17 +01002704class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002705 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002706 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 return true;
2708 }
2709
David Brazdil77a48ae2015-09-15 12:34:04 +00002710 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2711
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 size_t ComputeHashCode() const OVERRIDE { return 0; }
2713
Roland Levillain1a653882016-03-18 18:05:57 +00002714 // The null constant representation is a 0-bit pattern.
2715 virtual bool IsZeroBitPattern() const { return true; }
2716
Roland Levillain9867bc72015-08-05 10:21:34 +01002717 DECLARE_INSTRUCTION(NullConstant);
2718
2719 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002720 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002721
2722 friend class HGraph;
2723 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2724};
2725
2726// Constants of the type int. Those can be from Dex instructions, or
2727// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002728class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 public:
2730 int32_t GetValue() const { return value_; }
2731
David Brazdil9f389d42015-10-01 14:32:56 +01002732 uint64_t GetValueAsUint64() const OVERRIDE {
2733 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2734 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002735
Vladimir Marko372f10e2016-05-17 16:30:10 +01002736 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002737 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002738 return other->AsIntConstant()->value_ == value_;
2739 }
2740
2741 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2742
2743 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002744 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2745 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002746 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2747
Roland Levillain1a653882016-03-18 18:05:57 +00002748 // Integer constants are used to encode Boolean values as well,
2749 // where 1 means true and 0 means false.
2750 bool IsTrue() const { return GetValue() == 1; }
2751 bool IsFalse() const { return GetValue() == 0; }
2752
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 DECLARE_INSTRUCTION(IntConstant);
2754
2755 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002756 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2757 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2758 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2759 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002760
2761 const int32_t value_;
2762
2763 friend class HGraph;
2764 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2765 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2766 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2767};
2768
Vladimir Markofcb503c2016-05-18 12:48:17 +01002769class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002770 public:
2771 int64_t GetValue() const { return value_; }
2772
David Brazdil77a48ae2015-09-15 12:34:04 +00002773 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2774
Vladimir Marko372f10e2016-05-17 16:30:10 +01002775 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002776 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002777 return other->AsLongConstant()->value_ == value_;
2778 }
2779
2780 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2781
2782 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002783 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2784 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002785 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2786
2787 DECLARE_INSTRUCTION(LongConstant);
2788
2789 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002790 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2791 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002792
2793 const int64_t value_;
2794
2795 friend class HGraph;
2796 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2797};
Dave Allison20dfc792014-06-16 20:44:29 -07002798
Vladimir Markofcb503c2016-05-18 12:48:17 +01002799class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002800 public:
2801 float GetValue() const { return value_; }
2802
2803 uint64_t GetValueAsUint64() const OVERRIDE {
2804 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2805 }
2806
Vladimir Marko372f10e2016-05-17 16:30:10 +01002807 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002808 DCHECK(other->IsFloatConstant()) << other->DebugName();
2809 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2810 }
2811
2812 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2813
2814 bool IsMinusOne() const OVERRIDE {
2815 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2816 }
Roland Levillain1a653882016-03-18 18:05:57 +00002817 bool IsArithmeticZero() const OVERRIDE {
2818 return std::fpclassify(value_) == FP_ZERO;
2819 }
2820 bool IsArithmeticPositiveZero() const {
2821 return IsArithmeticZero() && !std::signbit(value_);
2822 }
2823 bool IsArithmeticNegativeZero() const {
2824 return IsArithmeticZero() && std::signbit(value_);
2825 }
2826 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002827 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002828 }
2829 bool IsOne() const OVERRIDE {
2830 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2831 }
2832 bool IsNaN() const {
2833 return std::isnan(value_);
2834 }
2835
2836 DECLARE_INSTRUCTION(FloatConstant);
2837
2838 private:
2839 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2840 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2841 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2842 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2843
2844 const float value_;
2845
2846 // Only the SsaBuilder and HGraph can create floating-point constants.
2847 friend class SsaBuilder;
2848 friend class HGraph;
2849 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2850};
2851
Vladimir Markofcb503c2016-05-18 12:48:17 +01002852class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002853 public:
2854 double GetValue() const { return value_; }
2855
2856 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2857
Vladimir Marko372f10e2016-05-17 16:30:10 +01002858 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002859 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2860 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2861 }
2862
2863 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2864
2865 bool IsMinusOne() const OVERRIDE {
2866 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2867 }
Roland Levillain1a653882016-03-18 18:05:57 +00002868 bool IsArithmeticZero() const OVERRIDE {
2869 return std::fpclassify(value_) == FP_ZERO;
2870 }
2871 bool IsArithmeticPositiveZero() const {
2872 return IsArithmeticZero() && !std::signbit(value_);
2873 }
2874 bool IsArithmeticNegativeZero() const {
2875 return IsArithmeticZero() && std::signbit(value_);
2876 }
2877 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002878 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002879 }
2880 bool IsOne() const OVERRIDE {
2881 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2882 }
2883 bool IsNaN() const {
2884 return std::isnan(value_);
2885 }
2886
2887 DECLARE_INSTRUCTION(DoubleConstant);
2888
2889 private:
2890 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2891 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2892 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2893 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2894
2895 const double value_;
2896
2897 // Only the SsaBuilder and HGraph can create floating-point constants.
2898 friend class SsaBuilder;
2899 friend class HGraph;
2900 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2901};
2902
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002903// Conditional branch. A block ending with an HIf instruction must have
2904// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002905class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002906 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002907 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2908 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002909 SetRawInputAt(0, input);
2910 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002911
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002912 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002913
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002914 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002915 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002916 }
2917
2918 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002919 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002920 }
2921
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002922 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002923
2924 private:
2925 DISALLOW_COPY_AND_ASSIGN(HIf);
2926};
2927
David Brazdilfc6a86a2015-06-26 10:33:45 +00002928
2929// Abstract instruction which marks the beginning and/or end of a try block and
2930// links it to the respective exception handlers. Behaves the same as a Goto in
2931// non-exceptional control flow.
2932// Normal-flow successor is stored at index zero, exception handlers under
2933// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002934class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002935 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002936 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002937 kEntry,
2938 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002939 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002940 };
2941
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002942 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002943 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2944 SetPackedField<BoundaryKindField>(kind);
2945 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002946
2947 bool IsControlFlow() const OVERRIDE { return true; }
2948
2949 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002950 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002951
David Brazdild26a4112015-11-10 11:07:31 +00002952 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2953 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2954 }
2955
David Brazdilfc6a86a2015-06-26 10:33:45 +00002956 // Returns whether `handler` is among its exception handlers (non-zero index
2957 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002958 bool HasExceptionHandler(const HBasicBlock& handler) const {
2959 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002960 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002961 }
2962
2963 // If not present already, adds `handler` to its block's list of exception
2964 // handlers.
2965 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002966 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002967 GetBlock()->AddSuccessor(handler);
2968 }
2969 }
2970
Vladimir Markoa1de9182016-02-25 11:37:38 +00002971 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2972 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002973
David Brazdilffee3d32015-07-06 11:48:53 +01002974 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2975
David Brazdilfc6a86a2015-06-26 10:33:45 +00002976 DECLARE_INSTRUCTION(TryBoundary);
2977
2978 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002979 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2980 static constexpr size_t kFieldBoundaryKindSize =
2981 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2982 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2983 kFieldBoundaryKind + kFieldBoundaryKindSize;
2984 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2985 "Too many packed fields.");
2986 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002987
2988 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2989};
2990
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002991// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002992class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002993 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002994 enum class Kind {
2995 kBCE,
2996 kInline,
2997 kLast = kInline
2998 };
2999
3000 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
3001 // across.
3002 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
3003 : HVariableInputSizeInstruction(
3004 SideEffects::All(),
3005 dex_pc,
3006 arena,
3007 /* number_of_inputs */ 1,
3008 kArenaAllocMisc) {
3009 SetPackedFlag<kFieldCanBeMoved>(false);
3010 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003011 SetRawInputAt(0, cond);
3012 }
3013
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003014 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
3015 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
3016 // instead of `guard`.
3017 // We set CanTriggerGC to prevent any intermediate address to be live
3018 // at the point of the `HDeoptimize`.
3019 HDeoptimize(ArenaAllocator* arena,
3020 HInstruction* cond,
3021 HInstruction* guard,
3022 Kind kind,
3023 uint32_t dex_pc)
3024 : HVariableInputSizeInstruction(
3025 SideEffects::CanTriggerGC(),
3026 dex_pc,
3027 arena,
3028 /* number_of_inputs */ 2,
3029 kArenaAllocMisc) {
3030 SetPackedFlag<kFieldCanBeMoved>(true);
3031 SetPackedField<DeoptimizeKindField>(kind);
3032 SetRawInputAt(0, cond);
3033 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003034 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003035
3036 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3037
3038 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3039 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3040 }
3041
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003042 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003043
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003044 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003045
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003046 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3047
3048 Primitive::Type GetType() const OVERRIDE {
3049 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3050 }
3051
3052 bool GuardsAnInput() const {
3053 return InputCount() == 2;
3054 }
3055
3056 HInstruction* GuardedInput() const {
3057 DCHECK(GuardsAnInput());
3058 return InputAt(1);
3059 }
3060
3061 void RemoveGuard() {
3062 RemoveInputAt(1);
3063 }
3064
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003065 DECLARE_INSTRUCTION(Deoptimize);
3066
3067 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003068 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3069 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3070 static constexpr size_t kFieldDeoptimizeKindSize =
3071 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3072 static constexpr size_t kNumberOfDeoptimizePackedBits =
3073 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3074 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3075 "Too many packed fields.");
3076 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3077
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003078 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3079};
3080
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003081std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3082
Mingyao Yang063fc772016-08-02 11:02:54 -07003083// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3084// The compiled code checks this flag value in a guard before devirtualized call and
3085// if it's true, starts to do deoptimization.
3086// It has a 4-byte slot on stack.
3087// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003088class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003089 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003090 // CHA guards are only optimized in a separate pass and it has no side effects
3091 // with regard to other passes.
3092 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3093 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003094 }
3095
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003096 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3097
3098 // We do all CHA guard elimination/motion in a single pass, after which there is no
3099 // further guard elimination/motion since a guard might have been used for justification
3100 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3101 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003102 bool CanBeMoved() const OVERRIDE { return false; }
3103
3104 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3105
3106 private:
3107 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3108};
3109
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003110// Represents the ArtMethod that was passed as a first argument to
3111// the method. It is used by instructions that depend on it, like
3112// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003113class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003114 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003115 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3116 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003117
3118 DECLARE_INSTRUCTION(CurrentMethod);
3119
3120 private:
3121 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3122};
3123
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003124// Fetches an ArtMethod from the virtual table or the interface method table
3125// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003126class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003129 kVTable,
3130 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003131 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 };
3133 HClassTableGet(HInstruction* cls,
3134 Primitive::Type type,
3135 TableKind kind,
3136 size_t index,
3137 uint32_t dex_pc)
3138 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003139 index_(index) {
3140 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003141 SetRawInputAt(0, cls);
3142 }
3143
3144 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003145 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003146 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003147 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003148 }
3149
Vladimir Markoa1de9182016-02-25 11:37:38 +00003150 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003151 size_t GetIndex() const { return index_; }
3152
3153 DECLARE_INSTRUCTION(ClassTableGet);
3154
3155 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003156 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3157 static constexpr size_t kFieldTableKindSize =
3158 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3159 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3160 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3161 "Too many packed fields.");
3162 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3163
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003164 // The index of the ArtMethod in the table.
3165 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003166
3167 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3168};
3169
Mark Mendellfe57faa2015-09-18 09:26:15 -04003170// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3171// have one successor for each entry in the switch table, and the final successor
3172// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003173class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003174 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003175 HPackedSwitch(int32_t start_value,
3176 uint32_t num_entries,
3177 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003178 uint32_t dex_pc = kNoDexPc)
3179 : HTemplateInstruction(SideEffects::None(), dex_pc),
3180 start_value_(start_value),
3181 num_entries_(num_entries) {
3182 SetRawInputAt(0, input);
3183 }
3184
3185 bool IsControlFlow() const OVERRIDE { return true; }
3186
3187 int32_t GetStartValue() const { return start_value_; }
3188
Vladimir Marko211c2112015-09-24 16:52:33 +01003189 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003190
3191 HBasicBlock* GetDefaultBlock() const {
3192 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003193 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003194 }
3195 DECLARE_INSTRUCTION(PackedSwitch);
3196
3197 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003198 const int32_t start_value_;
3199 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003200
3201 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3202};
3203
Roland Levillain88cb1752014-10-20 16:36:47 +01003204class HUnaryOperation : public HExpression<1> {
3205 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003206 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3207 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003208 SetRawInputAt(0, input);
3209 }
3210
3211 HInstruction* GetInput() const { return InputAt(0); }
3212 Primitive::Type GetResultType() const { return GetType(); }
3213
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003214 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003215 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003216 return true;
3217 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003218
Roland Levillain31dd3d62016-02-16 12:21:02 +00003219 // Try to statically evaluate `this` and return a HConstant
3220 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003221 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003222 HConstant* TryStaticEvaluation() const;
3223
3224 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003225 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3226 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003227 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3228 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003229
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003230 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003231
3232 private:
3233 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3234};
3235
Dave Allison20dfc792014-06-16 20:44:29 -07003236class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003237 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003238 HBinaryOperation(Primitive::Type result_type,
3239 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003240 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003241 SideEffects side_effects = SideEffects::None(),
3242 uint32_t dex_pc = kNoDexPc)
3243 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003244 SetRawInputAt(0, left);
3245 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003246 }
3247
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003248 HInstruction* GetLeft() const { return InputAt(0); }
3249 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003250 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003251
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003252 virtual bool IsCommutative() const { return false; }
3253
3254 // Put constant on the right.
3255 // Returns whether order is changed.
3256 bool OrderInputsWithConstantOnTheRight() {
3257 HInstruction* left = InputAt(0);
3258 HInstruction* right = InputAt(1);
3259 if (left->IsConstant() && !right->IsConstant()) {
3260 ReplaceInput(right, 0);
3261 ReplaceInput(left, 1);
3262 return true;
3263 }
3264 return false;
3265 }
3266
3267 // Order inputs by instruction id, but favor constant on the right side.
3268 // This helps GVN for commutative ops.
3269 void OrderInputs() {
3270 DCHECK(IsCommutative());
3271 HInstruction* left = InputAt(0);
3272 HInstruction* right = InputAt(1);
3273 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3274 return;
3275 }
3276 if (OrderInputsWithConstantOnTheRight()) {
3277 return;
3278 }
3279 // Order according to instruction id.
3280 if (left->GetId() > right->GetId()) {
3281 ReplaceInput(right, 0);
3282 ReplaceInput(left, 1);
3283 }
3284 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003285
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003286 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003287 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003288 return true;
3289 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003290
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 // Try to statically evaluate `this` and return a HConstant
3292 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003293 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003294 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003295
3296 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003297 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3298 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003299 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3300 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003301 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003302 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3303 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003304 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3305 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003306 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3307 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003308 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003309 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3310 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003311
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003312 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003313 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003314 HConstant* GetConstantRight() const;
3315
3316 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003317 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003318 HInstruction* GetLeastConstantLeft() const;
3319
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003320 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003321
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003322 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003323 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3324};
3325
Mark Mendellc4701932015-04-10 13:18:51 -04003326// The comparison bias applies for floating point operations and indicates how NaN
3327// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003328enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003329 kNoBias, // bias is not applicable (i.e. for long operation)
3330 kGtBias, // return 1 for NaN comparisons
3331 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003333};
3334
Roland Levillain31dd3d62016-02-16 12:21:02 +00003335std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3336
Dave Allison20dfc792014-06-16 20:44:29 -07003337class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003338 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003339 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3341 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3342 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003343
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003344 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003345 // `instruction`, and disregard moves in between.
3346 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003347
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003348 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003349
3350 virtual IfCondition GetCondition() const = 0;
3351
Mark Mendellc4701932015-04-10 13:18:51 -04003352 virtual IfCondition GetOppositeCondition() const = 0;
3353
Vladimir Markoa1de9182016-02-25 11:37:38 +00003354 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003355 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3356
Vladimir Markoa1de9182016-02-25 11:37:38 +00003357 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3358 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003359
Vladimir Marko372f10e2016-05-17 16:30:10 +01003360 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003361 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003362 }
3363
Roland Levillain4fa13f62015-07-06 18:11:54 +01003364 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003365 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003366 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003367 if (if_cond == kCondNE) {
3368 return true;
3369 } else if (if_cond == kCondEQ) {
3370 return false;
3371 }
3372 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003373 }
3374
3375 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003376 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003377 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003378 if (if_cond == kCondEQ) {
3379 return true;
3380 } else if (if_cond == kCondNE) {
3381 return false;
3382 }
3383 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003384 }
3385
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003387 // Needed if we merge a HCompare into a HCondition.
3388 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3389 static constexpr size_t kFieldComparisonBiasSize =
3390 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3391 static constexpr size_t kNumberOfConditionPackedBits =
3392 kFieldComparisonBias + kFieldComparisonBiasSize;
3393 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3394 using ComparisonBiasField =
3395 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3396
Roland Levillain31dd3d62016-02-16 12:21:02 +00003397 template <typename T>
3398 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3399
3400 template <typename T>
3401 int32_t CompareFP(T x, T y) const {
3402 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3403 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3404 // Handle the bias.
3405 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3406 }
3407
3408 // Return an integer constant containing the result of a condition evaluated at compile time.
3409 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3410 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3411 }
3412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 private:
3414 DISALLOW_COPY_AND_ASSIGN(HCondition);
3415};
3416
3417// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003418class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003419 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003420 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3421 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003422
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003423 bool IsCommutative() const OVERRIDE { return true; }
3424
Vladimir Marko9e23df52015-11-10 17:14:35 +00003425 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3426 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(true, GetDexPc());
3428 }
3429 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3431 }
3432 // In the following Evaluate methods, a HCompare instruction has
3433 // been merged into this HEqual instruction; evaluate it as
3434 // `Compare(x, y) == 0`.
3435 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3436 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3437 GetDexPc());
3438 }
3439 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3440 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3441 }
3442 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3443 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003444 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003445
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003446 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003447
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003448 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003449 return kCondEQ;
3450 }
3451
Mark Mendellc4701932015-04-10 13:18:51 -04003452 IfCondition GetOppositeCondition() const OVERRIDE {
3453 return kCondNE;
3454 }
3455
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003456 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003457 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003458
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003459 DISALLOW_COPY_AND_ASSIGN(HEqual);
3460};
3461
Vladimir Markofcb503c2016-05-18 12:48:17 +01003462class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003463 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003464 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3465 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003466
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003467 bool IsCommutative() const OVERRIDE { return true; }
3468
Vladimir Marko9e23df52015-11-10 17:14:35 +00003469 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3470 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003471 return MakeConstantCondition(false, GetDexPc());
3472 }
3473 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3474 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3475 }
3476 // In the following Evaluate methods, a HCompare instruction has
3477 // been merged into this HNotEqual instruction; evaluate it as
3478 // `Compare(x, y) != 0`.
3479 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3480 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3481 }
3482 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3483 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3484 }
3485 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3486 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003487 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003488
Dave Allison20dfc792014-06-16 20:44:29 -07003489 DECLARE_INSTRUCTION(NotEqual);
3490
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003491 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003492 return kCondNE;
3493 }
3494
Mark Mendellc4701932015-04-10 13:18:51 -04003495 IfCondition GetOppositeCondition() const OVERRIDE {
3496 return kCondEQ;
3497 }
3498
Dave Allison20dfc792014-06-16 20:44:29 -07003499 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003500 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003501
Dave Allison20dfc792014-06-16 20:44:29 -07003502 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3503};
3504
Vladimir Markofcb503c2016-05-18 12:48:17 +01003505class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003506 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003507 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3508 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003509
Roland Levillain9867bc72015-08-05 10:21:34 +01003510 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003511 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003512 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003513 // In the following Evaluate methods, a HCompare instruction has
3514 // been merged into this HLessThan instruction; evaluate it as
3515 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003516 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003517 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3518 }
3519 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3520 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3521 }
3522 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3523 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003524 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003525
Dave Allison20dfc792014-06-16 20:44:29 -07003526 DECLARE_INSTRUCTION(LessThan);
3527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003528 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003529 return kCondLT;
3530 }
3531
Mark Mendellc4701932015-04-10 13:18:51 -04003532 IfCondition GetOppositeCondition() const OVERRIDE {
3533 return kCondGE;
3534 }
3535
Dave Allison20dfc792014-06-16 20:44:29 -07003536 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003537 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003538
Dave Allison20dfc792014-06-16 20:44:29 -07003539 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3540};
3541
Vladimir Markofcb503c2016-05-18 12:48:17 +01003542class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003543 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003544 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3545 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003546
Roland Levillain9867bc72015-08-05 10:21:34 +01003547 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003548 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003549 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003550 // In the following Evaluate methods, a HCompare instruction has
3551 // been merged into this HLessThanOrEqual instruction; evaluate it as
3552 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003553 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003554 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3555 }
3556 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3557 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3558 }
3559 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3560 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003561 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003562
Dave Allison20dfc792014-06-16 20:44:29 -07003563 DECLARE_INSTRUCTION(LessThanOrEqual);
3564
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003565 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003566 return kCondLE;
3567 }
3568
Mark Mendellc4701932015-04-10 13:18:51 -04003569 IfCondition GetOppositeCondition() const OVERRIDE {
3570 return kCondGT;
3571 }
3572
Dave Allison20dfc792014-06-16 20:44:29 -07003573 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003574 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003575
Dave Allison20dfc792014-06-16 20:44:29 -07003576 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3577};
3578
Vladimir Markofcb503c2016-05-18 12:48:17 +01003579class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003580 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003581 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3582 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003583
Roland Levillain9867bc72015-08-05 10:21:34 +01003584 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003585 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003586 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003587 // In the following Evaluate methods, a HCompare instruction has
3588 // been merged into this HGreaterThan instruction; evaluate it as
3589 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003590 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003591 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3592 }
3593 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3594 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3595 }
3596 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3597 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003598 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003599
Dave Allison20dfc792014-06-16 20:44:29 -07003600 DECLARE_INSTRUCTION(GreaterThan);
3601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003602 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003603 return kCondGT;
3604 }
3605
Mark Mendellc4701932015-04-10 13:18:51 -04003606 IfCondition GetOppositeCondition() const OVERRIDE {
3607 return kCondLE;
3608 }
3609
Dave Allison20dfc792014-06-16 20:44:29 -07003610 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003611 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003612
Dave Allison20dfc792014-06-16 20:44:29 -07003613 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3614};
3615
Vladimir Markofcb503c2016-05-18 12:48:17 +01003616class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003617 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003618 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3619 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003620
Roland Levillain9867bc72015-08-05 10:21:34 +01003621 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003622 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003623 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003624 // In the following Evaluate methods, a HCompare instruction has
3625 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3626 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003627 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003628 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3629 }
3630 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3631 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3632 }
3633 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3634 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003635 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003636
Dave Allison20dfc792014-06-16 20:44:29 -07003637 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3638
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003639 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003640 return kCondGE;
3641 }
3642
Mark Mendellc4701932015-04-10 13:18:51 -04003643 IfCondition GetOppositeCondition() const OVERRIDE {
3644 return kCondLT;
3645 }
3646
Dave Allison20dfc792014-06-16 20:44:29 -07003647 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003648 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003649
Dave Allison20dfc792014-06-16 20:44:29 -07003650 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3651};
3652
Vladimir Markofcb503c2016-05-18 12:48:17 +01003653class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003654 public:
3655 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3656 : HCondition(first, second, dex_pc) {}
3657
3658 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003660 }
3661 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003662 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3663 }
3664 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3665 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3666 LOG(FATAL) << DebugName() << " is not defined for float values";
3667 UNREACHABLE();
3668 }
3669 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3670 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3671 LOG(FATAL) << DebugName() << " is not defined for double values";
3672 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003673 }
3674
3675 DECLARE_INSTRUCTION(Below);
3676
3677 IfCondition GetCondition() const OVERRIDE {
3678 return kCondB;
3679 }
3680
3681 IfCondition GetOppositeCondition() const OVERRIDE {
3682 return kCondAE;
3683 }
3684
3685 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003686 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003687 return MakeUnsigned(x) < MakeUnsigned(y);
3688 }
Aart Bike9f37602015-10-09 11:15:55 -07003689
3690 DISALLOW_COPY_AND_ASSIGN(HBelow);
3691};
3692
Vladimir Markofcb503c2016-05-18 12:48:17 +01003693class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003694 public:
3695 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3696 : HCondition(first, second, dex_pc) {}
3697
3698 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003699 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003700 }
3701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003702 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3703 }
3704 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3705 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3706 LOG(FATAL) << DebugName() << " is not defined for float values";
3707 UNREACHABLE();
3708 }
3709 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3710 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3711 LOG(FATAL) << DebugName() << " is not defined for double values";
3712 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003713 }
3714
3715 DECLARE_INSTRUCTION(BelowOrEqual);
3716
3717 IfCondition GetCondition() const OVERRIDE {
3718 return kCondBE;
3719 }
3720
3721 IfCondition GetOppositeCondition() const OVERRIDE {
3722 return kCondA;
3723 }
3724
3725 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003726 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003727 return MakeUnsigned(x) <= MakeUnsigned(y);
3728 }
Aart Bike9f37602015-10-09 11:15:55 -07003729
3730 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3731};
3732
Vladimir Markofcb503c2016-05-18 12:48:17 +01003733class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003734 public:
3735 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3736 : HCondition(first, second, dex_pc) {}
3737
3738 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003739 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003740 }
3741 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003742 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3743 }
3744 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3745 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3746 LOG(FATAL) << DebugName() << " is not defined for float values";
3747 UNREACHABLE();
3748 }
3749 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3750 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3751 LOG(FATAL) << DebugName() << " is not defined for double values";
3752 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003753 }
3754
3755 DECLARE_INSTRUCTION(Above);
3756
3757 IfCondition GetCondition() const OVERRIDE {
3758 return kCondA;
3759 }
3760
3761 IfCondition GetOppositeCondition() const OVERRIDE {
3762 return kCondBE;
3763 }
3764
3765 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003766 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 return MakeUnsigned(x) > MakeUnsigned(y);
3768 }
Aart Bike9f37602015-10-09 11:15:55 -07003769
3770 DISALLOW_COPY_AND_ASSIGN(HAbove);
3771};
3772
Vladimir Markofcb503c2016-05-18 12:48:17 +01003773class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003774 public:
3775 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3776 : HCondition(first, second, dex_pc) {}
3777
3778 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003779 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003780 }
3781 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003782 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3783 }
3784 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3785 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3786 LOG(FATAL) << DebugName() << " is not defined for float values";
3787 UNREACHABLE();
3788 }
3789 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3790 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3791 LOG(FATAL) << DebugName() << " is not defined for double values";
3792 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003793 }
3794
3795 DECLARE_INSTRUCTION(AboveOrEqual);
3796
3797 IfCondition GetCondition() const OVERRIDE {
3798 return kCondAE;
3799 }
3800
3801 IfCondition GetOppositeCondition() const OVERRIDE {
3802 return kCondB;
3803 }
3804
3805 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003806 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003807 return MakeUnsigned(x) >= MakeUnsigned(y);
3808 }
Aart Bike9f37602015-10-09 11:15:55 -07003809
3810 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3811};
Dave Allison20dfc792014-06-16 20:44:29 -07003812
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003813// Instruction to check how two inputs compare to each other.
3814// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003815class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003816 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003817 // Note that `comparison_type` is the type of comparison performed
3818 // between the comparison's inputs, not the type of the instantiated
3819 // HCompare instruction (which is always Primitive::kPrimInt).
3820 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003821 HInstruction* first,
3822 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003823 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003824 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003825 : HBinaryOperation(Primitive::kPrimInt,
3826 first,
3827 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003828 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003829 dex_pc) {
3830 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003831 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3832 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003833 }
3834
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3837
3838 template <typename T>
3839 int32_t ComputeFP(T x, T y) const {
3840 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3841 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3842 // Handle the bias.
3843 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3844 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003845
Roland Levillain9867bc72015-08-05 10:21:34 +01003846 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003847 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3848 // reach this code path when processing a freshly built HIR
3849 // graph. However HCompare integer instructions can be synthesized
3850 // by the instruction simplifier to implement IntegerCompare and
3851 // IntegerSignum intrinsics, so we have to handle this case.
3852 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003853 }
3854 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3856 }
3857 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3858 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3859 }
3860 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3861 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003862 }
3863
Vladimir Marko372f10e2016-05-17 16:30:10 +01003864 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003865 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003866 }
3867
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003869
Roland Levillain31dd3d62016-02-16 12:21:02 +00003870 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003871 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 bool IsGtBias() const {
3873 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003874 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003876
Roland Levillain1693a1f2016-03-15 14:57:31 +00003877 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3878 // Comparisons do not require a runtime call in any back end.
3879 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003880 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003881
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003882 DECLARE_INSTRUCTION(Compare);
3883
Roland Levillain31dd3d62016-02-16 12:21:02 +00003884 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003885 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3886 static constexpr size_t kFieldComparisonBiasSize =
3887 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3888 static constexpr size_t kNumberOfComparePackedBits =
3889 kFieldComparisonBias + kFieldComparisonBiasSize;
3890 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3891 using ComparisonBiasField =
3892 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3893
Roland Levillain31dd3d62016-02-16 12:21:02 +00003894 // Return an integer constant containing the result of a comparison evaluated at compile time.
3895 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3896 DCHECK(value == -1 || value == 0 || value == 1) << value;
3897 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3898 }
3899
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003900 private:
3901 DISALLOW_COPY_AND_ASSIGN(HCompare);
3902};
3903
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003904class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003905 public:
3906 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003907 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003908 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003909 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003910 bool finalizable,
3911 QuickEntrypointEnum entrypoint)
3912 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3913 type_index_(type_index),
3914 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003915 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003917 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003918 }
3919
Andreas Gampea5b09a62016-11-17 15:21:22 -08003920 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003921 const DexFile& GetDexFile() const { return dex_file_; }
3922
3923 // Calls runtime so needs an environment.
3924 bool NeedsEnvironment() const OVERRIDE { return true; }
3925
Mingyao Yang062157f2016-03-02 10:15:36 -08003926 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3927 bool CanThrow() const OVERRIDE { return true; }
3928
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003929 bool NeedsChecks() const {
3930 return entrypoint_ == kQuickAllocObjectWithChecks;
3931 }
David Brazdil6de19382016-01-08 17:37:10 +00003932
Vladimir Markoa1de9182016-02-25 11:37:38 +00003933 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003934
3935 bool CanBeNull() const OVERRIDE { return false; }
3936
3937 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3938
3939 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3940 entrypoint_ = entrypoint;
3941 }
3942
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003943 HLoadClass* GetLoadClass() const {
3944 HInstruction* input = InputAt(0);
3945 if (input->IsClinitCheck()) {
3946 input = input->InputAt(0);
3947 }
3948 DCHECK(input->IsLoadClass());
3949 return input->AsLoadClass();
3950 }
3951
David Brazdil6de19382016-01-08 17:37:10 +00003952 bool IsStringAlloc() const;
3953
3954 DECLARE_INSTRUCTION(NewInstance);
3955
3956 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003957 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003958 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3959 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3960 "Too many packed fields.");
3961
Andreas Gampea5b09a62016-11-17 15:21:22 -08003962 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003963 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003964 QuickEntrypointEnum entrypoint_;
3965
3966 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3967};
3968
Agi Csaki05f20562015-08-19 14:58:14 -07003969enum IntrinsicNeedsEnvironmentOrCache {
3970 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3971 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003972};
3973
Aart Bik5d75afe2015-12-14 11:57:01 -08003974enum IntrinsicSideEffects {
3975 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3976 kReadSideEffects, // Intrinsic may read heap memory.
3977 kWriteSideEffects, // Intrinsic may write heap memory.
3978 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3979};
3980
3981enum IntrinsicExceptions {
3982 kNoThrow, // Intrinsic does not throw any exceptions.
3983 kCanThrow // Intrinsic may throw exceptions.
3984};
3985
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003986class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003987 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003988 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003989
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003990 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003991 SetRawInputAt(index, argument);
3992 }
3993
Roland Levillain3e3d7332015-04-28 11:00:54 +01003994 // Return the number of arguments. This number can be lower than
3995 // the number of inputs returned by InputCount(), as some invoke
3996 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3997 // inputs at the end of their list of inputs.
3998 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3999
Vladimir Markoa1de9182016-02-25 11:37:38 +00004000 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004001
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004002 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4003
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004004 InvokeType GetInvokeType() const {
4005 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004006 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004007
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01004008 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004009 return intrinsic_;
4010 }
4011
Aart Bik5d75afe2015-12-14 11:57:01 -08004012 void SetIntrinsic(Intrinsics intrinsic,
4013 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
4014 IntrinsicSideEffects side_effects,
4015 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004016
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004017 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004018 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004019 }
4020
Aart Bikff7d89c2016-11-07 08:49:28 -08004021 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4022
Vladimir Markoa1de9182016-02-25 11:37:38 +00004023 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004024
Aart Bik71bf7b42016-11-16 10:17:46 -08004025 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004026
Vladimir Marko372f10e2016-05-17 16:30:10 +01004027 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004028 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4029 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004030
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004031 uint32_t* GetIntrinsicOptimizations() {
4032 return &intrinsic_optimizations_;
4033 }
4034
4035 const uint32_t* GetIntrinsicOptimizations() const {
4036 return &intrinsic_optimizations_;
4037 }
4038
4039 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4040
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004041 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004042 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004043
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004044 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004045
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004046 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004047 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4048 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004049 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4050 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004051 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004052 static constexpr size_t kFieldReturnTypeSize =
4053 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4054 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4055 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4056 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004057 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004058 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4059
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004060 HInvoke(ArenaAllocator* arena,
4061 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004062 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004063 Primitive::Type return_type,
4064 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004065 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004066 ArtMethod* resolved_method,
4067 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004068 : HVariableInputSizeInstruction(
4069 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4070 dex_pc,
4071 arena,
4072 number_of_arguments + number_of_other_inputs,
4073 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004074 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004075 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004076 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004077 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004078 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004079 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004080 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004081 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004082 }
4083
Roland Levillain3e3d7332015-04-28 11:00:54 +01004084 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004085 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004086 const uint32_t dex_method_index_;
4087 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004088
4089 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4090 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004091
4092 private:
4093 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4094};
4095
Vladimir Markofcb503c2016-05-18 12:48:17 +01004096class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004097 public:
4098 HInvokeUnresolved(ArenaAllocator* arena,
4099 uint32_t number_of_arguments,
4100 Primitive::Type return_type,
4101 uint32_t dex_pc,
4102 uint32_t dex_method_index,
4103 InvokeType invoke_type)
4104 : HInvoke(arena,
4105 number_of_arguments,
4106 0u /* number_of_other_inputs */,
4107 return_type,
4108 dex_pc,
4109 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004110 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004111 invoke_type) {
4112 }
4113
4114 DECLARE_INSTRUCTION(InvokeUnresolved);
4115
4116 private:
4117 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4118};
4119
Orion Hodsonac141392017-01-13 11:53:47 +00004120class HInvokePolymorphic FINAL : public HInvoke {
4121 public:
4122 HInvokePolymorphic(ArenaAllocator* arena,
4123 uint32_t number_of_arguments,
4124 Primitive::Type return_type,
4125 uint32_t dex_pc,
4126 uint32_t dex_method_index)
4127 : HInvoke(arena,
4128 number_of_arguments,
4129 0u /* number_of_other_inputs */,
4130 return_type,
4131 dex_pc,
4132 dex_method_index,
4133 nullptr,
4134 kVirtual) {}
4135
4136 DECLARE_INSTRUCTION(InvokePolymorphic);
4137
4138 private:
4139 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4140};
4141
Vladimir Markofcb503c2016-05-18 12:48:17 +01004142class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004143 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004144 // Requirements of this method call regarding the class
4145 // initialization (clinit) check of its declaring class.
4146 enum class ClinitCheckRequirement {
4147 kNone, // Class already initialized.
4148 kExplicit, // Static call having explicit clinit check as last input.
4149 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004150 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004151 };
4152
Vladimir Marko58155012015-08-19 12:49:41 +00004153 // Determines how to load the target ArtMethod*.
4154 enum class MethodLoadKind {
4155 // Use a String init ArtMethod* loaded from Thread entrypoints.
4156 kStringInit,
4157
4158 // Use the method's own ArtMethod* loaded by the register allocator.
4159 kRecursive,
4160
4161 // Use ArtMethod* at a known address, embed the direct address in the code.
4162 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4163 kDirectAddress,
4164
Vladimir Markoa1de9182016-02-25 11:37:38 +00004165 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004166 // Used when we need to use the dex cache, for example for invoke-static that
4167 // may cause class initialization (the entry may point to a resolution method),
4168 // and we know that we can access the dex cache arrays using a PC-relative load.
4169 kDexCachePcRelative,
4170
4171 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4172 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4173 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4174 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4175 kDexCacheViaMethod,
4176 };
4177
4178 // Determines the location of the code pointer.
4179 enum class CodePtrLocation {
4180 // Recursive call, use local PC-relative call instruction.
4181 kCallSelf,
4182
Vladimir Marko58155012015-08-19 12:49:41 +00004183 // Use code pointer from the ArtMethod*.
4184 // Used when we don't know the target code. This is also the last-resort-kind used when
4185 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4186 kCallArtMethod,
4187 };
4188
4189 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004190 MethodLoadKind method_load_kind;
4191 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004192 // The method load data holds
4193 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4194 // Note that there are multiple string init methods, each having its own offset.
4195 // - the method address for kDirectAddress
4196 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004197 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004198 };
4199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004200 HInvokeStaticOrDirect(ArenaAllocator* arena,
4201 uint32_t number_of_arguments,
4202 Primitive::Type return_type,
4203 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004204 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004205 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004206 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004207 InvokeType invoke_type,
4208 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004209 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004210 : HInvoke(arena,
4211 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004212 // There is potentially one extra argument for the HCurrentMethod node, and
4213 // potentially one other if the clinit check is explicit, and potentially
4214 // one other if the method is a string factory.
4215 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004216 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004217 return_type,
4218 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004219 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004220 resolved_method,
4221 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004222 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004223 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004224 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4225 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004226
Vladimir Markodc151b22015-10-15 18:02:30 +01004227 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004228 bool had_current_method_input = HasCurrentMethodInput();
4229 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4230
4231 // Using the current method is the default and once we find a better
4232 // method load kind, we should not go back to using the current method.
4233 DCHECK(had_current_method_input || !needs_current_method_input);
4234
4235 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004236 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4237 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004238 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004239 dispatch_info_ = dispatch_info;
4240 }
4241
Aart Bik6daebeb2017-04-03 14:35:41 -07004242 DispatchInfo GetDispatchInfo() const {
4243 return dispatch_info_;
4244 }
4245
Vladimir Markoc53c0792015-11-19 15:48:33 +00004246 void AddSpecialInput(HInstruction* input) {
4247 // We allow only one special input.
4248 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4249 DCHECK(InputCount() == GetSpecialInputIndex() ||
4250 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4251 InsertInputAt(GetSpecialInputIndex(), input);
4252 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004253
Vladimir Marko372f10e2016-05-17 16:30:10 +01004254 using HInstruction::GetInputRecords; // Keep the const version visible.
4255 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4256 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4257 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4258 DCHECK(!input_records.empty());
4259 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4260 HInstruction* last_input = input_records.back().GetInstruction();
4261 // Note: `last_input` may be null during arguments setup.
4262 if (last_input != nullptr) {
4263 // `last_input` is the last input of a static invoke marked as having
4264 // an explicit clinit check. It must either be:
4265 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4266 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4267 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4268 }
4269 }
4270 return input_records;
4271 }
4272
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004273 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004274 // We access the method via the dex cache so we can't do an implicit null check.
4275 // TODO: for intrinsics we can generate implicit null checks.
4276 return false;
4277 }
4278
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004279 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004280 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004281 }
4282
Vladimir Markoc53c0792015-11-19 15:48:33 +00004283 // Get the index of the special input, if any.
4284 //
David Brazdil6de19382016-01-08 17:37:10 +00004285 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4286 // method pointer; otherwise there may be one platform-specific special input,
4287 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004288 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004289 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004290
Vladimir Marko58155012015-08-19 12:49:41 +00004291 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4292 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4293 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004294 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004295 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004296 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004297 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004298 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4299 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004300 bool HasCurrentMethodInput() const {
4301 // This function can be called only after the invoke has been fully initialized by the builder.
4302 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004303 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004304 return true;
4305 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004306 DCHECK(InputCount() == GetSpecialInputIndex() ||
4307 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004308 return false;
4309 }
4310 }
Vladimir Marko58155012015-08-19 12:49:41 +00004311
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004312 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004313 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004314 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004315 }
4316
4317 uint64_t GetMethodAddress() const {
4318 DCHECK(HasMethodAddress());
4319 return dispatch_info_.method_load_data;
4320 }
4321
4322 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004323 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004324 return dispatch_info_.method_load_data;
4325 }
4326
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004327 const DexFile& GetDexFileForPcRelativeDexCache() const;
4328
Vladimir Markoa1de9182016-02-25 11:37:38 +00004329 ClinitCheckRequirement GetClinitCheckRequirement() const {
4330 return GetPackedField<ClinitCheckRequirementField>();
4331 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004332
Roland Levillain4c0eb422015-04-24 16:43:49 +01004333 // Is this instruction a call to a static method?
4334 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004335 return GetInvokeType() == kStatic;
4336 }
4337
4338 MethodReference GetTargetMethod() const {
4339 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004340 }
4341
Vladimir Markofbb184a2015-11-13 14:47:00 +00004342 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4343 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4344 // instruction; only relevant for static calls with explicit clinit check.
4345 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004346 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004347 size_t last_input_index = inputs_.size() - 1u;
4348 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004349 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004350 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004351 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004352 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004353 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004354 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004355 }
4356
4357 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004358 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004359 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004360 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004361 }
4362
4363 // Is this a call to a static method whose declaring class has an
4364 // implicit intialization check requirement?
4365 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004366 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004367 }
4368
Vladimir Markob554b5a2015-11-06 12:57:55 +00004369 // Does this method load kind need the current method as an input?
4370 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4371 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4372 }
4373
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004374 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375
4376 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004377 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004378 static constexpr size_t kFieldClinitCheckRequirementSize =
4379 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4380 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4381 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4382 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4383 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004384 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4385 kFieldClinitCheckRequirement,
4386 kFieldClinitCheckRequirementSize>;
4387
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004389 MethodReference target_method_;
4390 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004391
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004392 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004393};
Vladimir Markof64242a2015-12-01 14:58:23 +00004394std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004395std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004396
Vladimir Markofcb503c2016-05-18 12:48:17 +01004397class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004398 public:
4399 HInvokeVirtual(ArenaAllocator* arena,
4400 uint32_t number_of_arguments,
4401 Primitive::Type return_type,
4402 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004403 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004404 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004405 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004406 : HInvoke(arena,
4407 number_of_arguments,
4408 0u,
4409 return_type,
4410 dex_pc,
4411 dex_method_index,
4412 resolved_method,
4413 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004414 vtable_index_(vtable_index) {}
4415
Aart Bik71bf7b42016-11-16 10:17:46 -08004416 bool CanBeNull() const OVERRIDE {
4417 switch (GetIntrinsic()) {
4418 case Intrinsics::kThreadCurrentThread:
4419 case Intrinsics::kStringBufferAppend:
4420 case Intrinsics::kStringBufferToString:
4421 case Intrinsics::kStringBuilderAppend:
4422 case Intrinsics::kStringBuilderToString:
4423 return false;
4424 default:
4425 return HInvoke::CanBeNull();
4426 }
4427 }
4428
Calin Juravle641547a2015-04-21 22:08:51 +01004429 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004430 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004431 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004432 }
4433
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004434 uint32_t GetVTableIndex() const { return vtable_index_; }
4435
4436 DECLARE_INSTRUCTION(InvokeVirtual);
4437
4438 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004439 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004440 const uint32_t vtable_index_;
4441
4442 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4443};
4444
Vladimir Markofcb503c2016-05-18 12:48:17 +01004445class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004446 public:
4447 HInvokeInterface(ArenaAllocator* arena,
4448 uint32_t number_of_arguments,
4449 Primitive::Type return_type,
4450 uint32_t dex_pc,
4451 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004452 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004453 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004454 : HInvoke(arena,
4455 number_of_arguments,
4456 0u,
4457 return_type,
4458 dex_pc,
4459 dex_method_index,
4460 resolved_method,
4461 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004462 imt_index_(imt_index) {}
4463
Calin Juravle641547a2015-04-21 22:08:51 +01004464 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004465 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004466 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004467 }
4468
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004469 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4470 // The assembly stub currently needs it.
4471 return true;
4472 }
4473
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004474 uint32_t GetImtIndex() const { return imt_index_; }
4475 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4476
4477 DECLARE_INSTRUCTION(InvokeInterface);
4478
4479 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004480 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004481 const uint32_t imt_index_;
4482
4483 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4484};
4485
Vladimir Markofcb503c2016-05-18 12:48:17 +01004486class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004487 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004488 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004489 : HUnaryOperation(result_type, input, dex_pc) {
4490 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4491 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004492
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004493 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004494
4495 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004496 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004497 }
4498 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004499 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004501 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4502 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4503 }
4504 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4505 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4506 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004507
Roland Levillain88cb1752014-10-20 16:36:47 +01004508 DECLARE_INSTRUCTION(Neg);
4509
4510 private:
4511 DISALLOW_COPY_AND_ASSIGN(HNeg);
4512};
4513
Vladimir Markofcb503c2016-05-18 12:48:17 +01004514class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004515 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004516 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4517 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4518 SetRawInputAt(0, cls);
4519 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004520 }
4521
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004522 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004523 bool NeedsEnvironment() const OVERRIDE { return true; }
4524
Mingyao Yang0c365e62015-03-31 15:09:29 -07004525 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4526 bool CanThrow() const OVERRIDE { return true; }
4527
Calin Juravle10e244f2015-01-26 18:54:32 +00004528 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004529
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004530 HLoadClass* GetLoadClass() const {
4531 DCHECK(InputAt(0)->IsLoadClass());
4532 return InputAt(0)->AsLoadClass();
4533 }
4534
4535 HInstruction* GetLength() const {
4536 return InputAt(1);
4537 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004538
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004539 DECLARE_INSTRUCTION(NewArray);
4540
4541 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004542 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4543};
4544
Vladimir Markofcb503c2016-05-18 12:48:17 +01004545class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004546 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004547 HAdd(Primitive::Type result_type,
4548 HInstruction* left,
4549 HInstruction* right,
4550 uint32_t dex_pc = kNoDexPc)
4551 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004552
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004553 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004554
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004555 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004556
4557 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004558 return GetBlock()->GetGraph()->GetIntConstant(
4559 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004560 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004561 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004562 return GetBlock()->GetGraph()->GetLongConstant(
4563 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004564 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004565 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4566 return GetBlock()->GetGraph()->GetFloatConstant(
4567 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4568 }
4569 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4570 return GetBlock()->GetGraph()->GetDoubleConstant(
4571 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4572 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004573
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004574 DECLARE_INSTRUCTION(Add);
4575
4576 private:
4577 DISALLOW_COPY_AND_ASSIGN(HAdd);
4578};
4579
Vladimir Markofcb503c2016-05-18 12:48:17 +01004580class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004581 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004582 HSub(Primitive::Type result_type,
4583 HInstruction* left,
4584 HInstruction* right,
4585 uint32_t dex_pc = kNoDexPc)
4586 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004587
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004588 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004589
4590 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 return GetBlock()->GetGraph()->GetIntConstant(
4592 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004593 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004594 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004595 return GetBlock()->GetGraph()->GetLongConstant(
4596 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004597 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004598 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4599 return GetBlock()->GetGraph()->GetFloatConstant(
4600 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4601 }
4602 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4603 return GetBlock()->GetGraph()->GetDoubleConstant(
4604 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4605 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004606
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004607 DECLARE_INSTRUCTION(Sub);
4608
4609 private:
4610 DISALLOW_COPY_AND_ASSIGN(HSub);
4611};
4612
Vladimir Markofcb503c2016-05-18 12:48:17 +01004613class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004614 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004615 HMul(Primitive::Type result_type,
4616 HInstruction* left,
4617 HInstruction* right,
4618 uint32_t dex_pc = kNoDexPc)
4619 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004621 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004622
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004623 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004624
4625 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004626 return GetBlock()->GetGraph()->GetIntConstant(
4627 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004628 }
4629 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004630 return GetBlock()->GetGraph()->GetLongConstant(
4631 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004632 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004633 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4634 return GetBlock()->GetGraph()->GetFloatConstant(
4635 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4636 }
4637 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4638 return GetBlock()->GetGraph()->GetDoubleConstant(
4639 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4640 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004641
4642 DECLARE_INSTRUCTION(Mul);
4643
4644 private:
4645 DISALLOW_COPY_AND_ASSIGN(HMul);
4646};
4647
Vladimir Markofcb503c2016-05-18 12:48:17 +01004648class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004649 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004650 HDiv(Primitive::Type result_type,
4651 HInstruction* left,
4652 HInstruction* right,
4653 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004654 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004655
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004657 T ComputeIntegral(T x, T y) const {
4658 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 // Our graph structure ensures we never have 0 for `y` during
4660 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004661 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004662 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004663 return (y == -1) ? -x : x / y;
4664 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004665
Roland Levillain31dd3d62016-02-16 12:21:02 +00004666 template <typename T>
4667 T ComputeFP(T x, T y) const {
4668 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4669 return x / y;
4670 }
4671
Roland Levillain9867bc72015-08-05 10:21:34 +01004672 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004673 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004674 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004675 }
4676 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004677 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004678 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4679 }
4680 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4681 return GetBlock()->GetGraph()->GetFloatConstant(
4682 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4683 }
4684 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4685 return GetBlock()->GetGraph()->GetDoubleConstant(
4686 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004687 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004688
4689 DECLARE_INSTRUCTION(Div);
4690
4691 private:
4692 DISALLOW_COPY_AND_ASSIGN(HDiv);
4693};
4694
Vladimir Markofcb503c2016-05-18 12:48:17 +01004695class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004696 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004697 HRem(Primitive::Type result_type,
4698 HInstruction* left,
4699 HInstruction* right,
4700 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004701 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004702
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004704 T ComputeIntegral(T x, T y) const {
4705 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004706 // Our graph structure ensures we never have 0 for `y` during
4707 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004708 DCHECK_NE(y, 0);
4709 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4710 return (y == -1) ? 0 : x % y;
4711 }
4712
Roland Levillain31dd3d62016-02-16 12:21:02 +00004713 template <typename T>
4714 T ComputeFP(T x, T y) const {
4715 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4716 return std::fmod(x, y);
4717 }
4718
Roland Levillain9867bc72015-08-05 10:21:34 +01004719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004720 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004721 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004722 }
4723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004724 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004725 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004726 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004727 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4728 return GetBlock()->GetGraph()->GetFloatConstant(
4729 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4730 }
4731 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4732 return GetBlock()->GetGraph()->GetDoubleConstant(
4733 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4734 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004735
4736 DECLARE_INSTRUCTION(Rem);
4737
4738 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004739 DISALLOW_COPY_AND_ASSIGN(HRem);
4740};
4741
Vladimir Markofcb503c2016-05-18 12:48:17 +01004742class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004743 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004744 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4745 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004746 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004747 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004748 SetRawInputAt(0, value);
4749 }
4750
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004751 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4752
Calin Juravled0d48522014-11-04 16:40:20 +00004753 bool CanBeMoved() const OVERRIDE { return true; }
4754
Vladimir Marko372f10e2016-05-17 16:30:10 +01004755 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004756 return true;
4757 }
4758
4759 bool NeedsEnvironment() const OVERRIDE { return true; }
4760 bool CanThrow() const OVERRIDE { return true; }
4761
Calin Juravled0d48522014-11-04 16:40:20 +00004762 DECLARE_INSTRUCTION(DivZeroCheck);
4763
4764 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004765 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4766};
4767
Vladimir Markofcb503c2016-05-18 12:48:17 +01004768class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004769 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004770 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 HInstruction* value,
4772 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004773 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4775 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4776 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
4778
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004780 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004781 return value << (distance & max_shift_distance);
4782 }
4783
4784 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004785 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004786 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004787 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004788 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004789 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004790 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004792 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4793 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4794 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4795 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004796 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004797 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4798 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004799 LOG(FATAL) << DebugName() << " is not defined for float values";
4800 UNREACHABLE();
4801 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4803 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004804 LOG(FATAL) << DebugName() << " is not defined for double values";
4805 UNREACHABLE();
4806 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004807
4808 DECLARE_INSTRUCTION(Shl);
4809
4810 private:
4811 DISALLOW_COPY_AND_ASSIGN(HShl);
4812};
4813
Vladimir Markofcb503c2016-05-18 12:48:17 +01004814class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004815 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004816 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004817 HInstruction* value,
4818 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004819 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4821 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4822 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
4824
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004826 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004827 return value >> (distance & max_shift_distance);
4828 }
4829
4830 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004832 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004833 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004834 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004835 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004836 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004837 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004838 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4839 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4841 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004842 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004843 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4844 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004845 LOG(FATAL) << DebugName() << " is not defined for float values";
4846 UNREACHABLE();
4847 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4849 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004850 LOG(FATAL) << DebugName() << " is not defined for double values";
4851 UNREACHABLE();
4852 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004853
4854 DECLARE_INSTRUCTION(Shr);
4855
4856 private:
4857 DISALLOW_COPY_AND_ASSIGN(HShr);
4858};
4859
Vladimir Markofcb503c2016-05-18 12:48:17 +01004860class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004861 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 HInstruction* value,
4864 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004865 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4867 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4868 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004869 }
4870
Roland Levillain5b5b9312016-03-22 14:57:31 +00004871 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004872 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004873 typedef typename std::make_unsigned<T>::type V;
4874 V ux = static_cast<V>(value);
4875 return static_cast<T>(ux >> (distance & max_shift_distance));
4876 }
4877
4878 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004879 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004880 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004881 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004882 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004883 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004884 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004885 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004886 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4887 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4888 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4889 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004890 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4892 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004893 LOG(FATAL) << DebugName() << " is not defined for float values";
4894 UNREACHABLE();
4895 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004896 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4897 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004898 LOG(FATAL) << DebugName() << " is not defined for double values";
4899 UNREACHABLE();
4900 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004901
4902 DECLARE_INSTRUCTION(UShr);
4903
4904 private:
4905 DISALLOW_COPY_AND_ASSIGN(HUShr);
4906};
4907
Vladimir Markofcb503c2016-05-18 12:48:17 +01004908class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004909 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004910 HAnd(Primitive::Type result_type,
4911 HInstruction* left,
4912 HInstruction* right,
4913 uint32_t dex_pc = kNoDexPc)
4914 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004915
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004916 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004917
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004918 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004919
4920 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004921 return GetBlock()->GetGraph()->GetIntConstant(
4922 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004923 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004924 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 return GetBlock()->GetGraph()->GetLongConstant(
4926 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004927 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004928 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4929 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4930 LOG(FATAL) << DebugName() << " is not defined for float values";
4931 UNREACHABLE();
4932 }
4933 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4934 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4935 LOG(FATAL) << DebugName() << " is not defined for double values";
4936 UNREACHABLE();
4937 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004938
4939 DECLARE_INSTRUCTION(And);
4940
4941 private:
4942 DISALLOW_COPY_AND_ASSIGN(HAnd);
4943};
4944
Vladimir Markofcb503c2016-05-18 12:48:17 +01004945class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004946 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004947 HOr(Primitive::Type result_type,
4948 HInstruction* left,
4949 HInstruction* right,
4950 uint32_t dex_pc = kNoDexPc)
4951 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004952
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004953 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004954
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004955 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004956
4957 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004958 return GetBlock()->GetGraph()->GetIntConstant(
4959 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004960 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004961 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 return GetBlock()->GetGraph()->GetLongConstant(
4963 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004964 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004965 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4966 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4967 LOG(FATAL) << DebugName() << " is not defined for float values";
4968 UNREACHABLE();
4969 }
4970 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4971 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4972 LOG(FATAL) << DebugName() << " is not defined for double values";
4973 UNREACHABLE();
4974 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004975
4976 DECLARE_INSTRUCTION(Or);
4977
4978 private:
4979 DISALLOW_COPY_AND_ASSIGN(HOr);
4980};
4981
Vladimir Markofcb503c2016-05-18 12:48:17 +01004982class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004983 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 HXor(Primitive::Type result_type,
4985 HInstruction* left,
4986 HInstruction* right,
4987 uint32_t dex_pc = kNoDexPc)
4988 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004989
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004990 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004991
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004992 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004993
4994 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 return GetBlock()->GetGraph()->GetIntConstant(
4996 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004997 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004999 return GetBlock()->GetGraph()->GetLongConstant(
5000 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005001 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005002 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
5003 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5004 LOG(FATAL) << DebugName() << " is not defined for float values";
5005 UNREACHABLE();
5006 }
5007 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
5008 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
5009 LOG(FATAL) << DebugName() << " is not defined for double values";
5010 UNREACHABLE();
5011 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005012
5013 DECLARE_INSTRUCTION(Xor);
5014
5015 private:
5016 DISALLOW_COPY_AND_ASSIGN(HXor);
5017};
5018
Vladimir Markofcb503c2016-05-18 12:48:17 +01005019class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005020 public:
5021 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005022 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5024 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005025 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026
Roland Levillain5b5b9312016-03-22 14:57:31 +00005027 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005028 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 typedef typename std::make_unsigned<T>::type V;
5030 V ux = static_cast<V>(value);
5031 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005032 return static_cast<T>(ux);
5033 } else {
5034 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 return static_cast<T>(ux >> (distance & max_shift_value)) |
5036 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005037 }
5038 }
5039
Roland Levillain5b5b9312016-03-22 14:57:31 +00005040 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005041 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005042 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005043 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005044 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005045 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005046 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005047 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005048 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5049 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5050 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5051 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005052 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005053 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5054 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005055 LOG(FATAL) << DebugName() << " is not defined for float values";
5056 UNREACHABLE();
5057 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005058 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5059 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005060 LOG(FATAL) << DebugName() << " is not defined for double values";
5061 UNREACHABLE();
5062 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005063
5064 DECLARE_INSTRUCTION(Ror);
5065
5066 private:
5067 DISALLOW_COPY_AND_ASSIGN(HRor);
5068};
5069
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005070// The value of a parameter in this method. Its location depends on
5071// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005072class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005073 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005074 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005075 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005076 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005077 Primitive::Type parameter_type,
5078 bool is_this = false)
5079 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005080 dex_file_(dex_file),
5081 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005082 index_(index) {
5083 SetPackedFlag<kFlagIsThis>(is_this);
5084 SetPackedFlag<kFlagCanBeNull>(!is_this);
5085 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005086
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005087 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005088 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005089 uint8_t GetIndex() const { return index_; }
Igor Murashkind01745e2017-04-05 16:40:31 -07005090 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005091
Vladimir Markoa1de9182016-02-25 11:37:38 +00005092 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5093 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005094
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005095 DECLARE_INSTRUCTION(ParameterValue);
5096
5097 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005098 // Whether or not the parameter value corresponds to 'this' argument.
5099 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5100 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5101 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5102 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5103 "Too many packed fields.");
5104
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005105 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005106 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005107 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005108 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005109 const uint8_t index_;
5110
5111 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5112};
5113
Vladimir Markofcb503c2016-05-18 12:48:17 +01005114class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005115 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005116 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5117 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005118
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005119 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005120 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005121 return true;
5122 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005123
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005124 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005125
5126 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005128 }
5129 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005131 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005132 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5133 LOG(FATAL) << DebugName() << " is not defined for float values";
5134 UNREACHABLE();
5135 }
5136 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5137 LOG(FATAL) << DebugName() << " is not defined for double values";
5138 UNREACHABLE();
5139 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005140
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005141 DECLARE_INSTRUCTION(Not);
5142
5143 private:
5144 DISALLOW_COPY_AND_ASSIGN(HNot);
5145};
5146
Vladimir Markofcb503c2016-05-18 12:48:17 +01005147class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005148 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005149 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5150 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005151
5152 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005153 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005154 return true;
5155 }
5156
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005157 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005158 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005159 return !x;
5160 }
5161
Roland Levillain9867bc72015-08-05 10:21:34 +01005162 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005163 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005164 }
5165 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5166 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005167 UNREACHABLE();
5168 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005169 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5170 LOG(FATAL) << DebugName() << " is not defined for float values";
5171 UNREACHABLE();
5172 }
5173 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5174 LOG(FATAL) << DebugName() << " is not defined for double values";
5175 UNREACHABLE();
5176 }
David Brazdil66d126e2015-04-03 16:02:44 +01005177
5178 DECLARE_INSTRUCTION(BooleanNot);
5179
5180 private:
5181 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5182};
5183
Vladimir Markofcb503c2016-05-18 12:48:17 +01005184class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005185 public:
5186 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005187 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005188 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005189 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005190 // Invariant: We should never generate a conversion to a Boolean value.
5191 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005192 }
5193
5194 HInstruction* GetInput() const { return InputAt(0); }
5195 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5196 Primitive::Type GetResultType() const { return GetType(); }
5197
5198 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005199 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5200 return true;
5201 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005202
Mark Mendelle82549b2015-05-06 10:55:34 -04005203 // Try to statically evaluate the conversion and return a HConstant
5204 // containing the result. If the input cannot be converted, return nullptr.
5205 HConstant* TryStaticEvaluation() const;
5206
Roland Levillaindff1f282014-11-05 14:15:05 +00005207 DECLARE_INSTRUCTION(TypeConversion);
5208
5209 private:
5210 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5211};
5212
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005213static constexpr uint32_t kNoRegNumber = -1;
5214
Vladimir Markofcb503c2016-05-18 12:48:17 +01005215class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005217 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5218 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005219 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005220 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005221 SetRawInputAt(0, value);
5222 }
5223
Calin Juravle10e244f2015-01-26 18:54:32 +00005224 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005225 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005226 return true;
5227 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005228
Calin Juravle10e244f2015-01-26 18:54:32 +00005229 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005230
Calin Juravle10e244f2015-01-26 18:54:32 +00005231 bool CanThrow() const OVERRIDE { return true; }
5232
5233 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005234
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005235
5236 DECLARE_INSTRUCTION(NullCheck);
5237
5238 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005239 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5240};
5241
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005242// Embeds an ArtField and all the information required by the compiler. We cache
5243// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005244class FieldInfo : public ValueObject {
5245 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005246 FieldInfo(ArtField* field,
5247 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005248 Primitive::Type field_type,
5249 bool is_volatile,
5250 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005251 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005252 const DexFile& dex_file)
5253 : field_(field),
5254 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005255 field_type_(field_type),
5256 is_volatile_(is_volatile),
5257 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005258 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005259 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005260
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005261 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005262 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005263 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005264 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005265 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005266 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005267 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005268
5269 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005270 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005271 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005272 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005273 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005274 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005275 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005276 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005277};
5278
Vladimir Markofcb503c2016-05-18 12:48:17 +01005279class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005280 public:
5281 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005282 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005283 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005284 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005285 bool is_volatile,
5286 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005287 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005288 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005289 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005290 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005291 field_info_(field,
5292 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005293 field_type,
5294 is_volatile,
5295 field_idx,
5296 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005297 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298 SetRawInputAt(0, value);
5299 }
5300
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005301 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005302
Vladimir Marko372f10e2016-05-17 16:30:10 +01005303 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5304 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005305 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005306 }
5307
Calin Juravle641547a2015-04-21 22:08:51 +01005308 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005309 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005310 }
5311
5312 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005313 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5314 }
5315
Calin Juravle52c48962014-12-16 17:02:57 +00005316 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005317 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005318 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005319 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005320
5321 DECLARE_INSTRUCTION(InstanceFieldGet);
5322
5323 private:
5324 const FieldInfo field_info_;
5325
5326 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5327};
5328
Vladimir Markofcb503c2016-05-18 12:48:17 +01005329class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005330 public:
5331 HInstanceFieldSet(HInstruction* object,
5332 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005333 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005334 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005335 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005336 bool is_volatile,
5337 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005338 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005339 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005340 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005341 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005342 field_info_(field,
5343 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005344 field_type,
5345 is_volatile,
5346 field_idx,
5347 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005348 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005349 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005350 SetRawInputAt(0, object);
5351 SetRawInputAt(1, value);
5352 }
5353
Calin Juravle641547a2015-04-21 22:08:51 +01005354 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005355 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005356 }
5357
Calin Juravle52c48962014-12-16 17:02:57 +00005358 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005359 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005360 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005361 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005362 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005363 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5364 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005365
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005366 DECLARE_INSTRUCTION(InstanceFieldSet);
5367
5368 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005369 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5370 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5371 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5372 "Too many packed fields.");
5373
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005374 const FieldInfo field_info_;
5375
5376 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5377};
5378
Vladimir Markofcb503c2016-05-18 12:48:17 +01005379class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005380 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005381 HArrayGet(HInstruction* array,
5382 HInstruction* index,
5383 Primitive::Type type,
5384 uint32_t dex_pc,
5385 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005386 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005387 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 SetRawInputAt(0, array);
5389 SetRawInputAt(1, index);
5390 }
5391
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005392 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005393 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005394 return true;
5395 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005396 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005397 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005398 // Currently, unless the array is the result of NewArray, the array access is always
5399 // preceded by some form of null NullCheck necessary for the bounds check, usually
5400 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5401 // dynamic BCE. There are cases when these could be removed to produce better code.
5402 // If we ever add optimizations to do so we should allow an implicit check here
5403 // (as long as the address falls in the first page).
5404 //
5405 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5406 // a = cond ? new int[1] : null;
5407 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005408 return false;
5409 }
5410
David Brazdil4833f5a2015-12-16 10:37:39 +00005411 bool IsEquivalentOf(HArrayGet* other) const {
5412 bool result = (GetDexPc() == other->GetDexPc());
5413 if (kIsDebugBuild && result) {
5414 DCHECK_EQ(GetBlock(), other->GetBlock());
5415 DCHECK_EQ(GetArray(), other->GetArray());
5416 DCHECK_EQ(GetIndex(), other->GetIndex());
5417 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005418 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005419 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005420 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5421 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005422 }
5423 }
5424 return result;
5425 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005426
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005427 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5428
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005429 HInstruction* GetArray() const { return InputAt(0); }
5430 HInstruction* GetIndex() const { return InputAt(1); }
5431
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 DECLARE_INSTRUCTION(ArrayGet);
5433
5434 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005435 // We treat a String as an array, creating the HArrayGet from String.charAt()
5436 // intrinsic in the instruction simplifier. We can always determine whether
5437 // a particular HArrayGet is actually a String.charAt() by looking at the type
5438 // of the input but that requires holding the mutator lock, so we prefer to use
5439 // a flag, so that code generators don't need to do the locking.
5440 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5441 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5442 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5443 "Too many packed fields.");
5444
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5446};
5447
Vladimir Markofcb503c2016-05-18 12:48:17 +01005448class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005449 public:
5450 HArraySet(HInstruction* array,
5451 HInstruction* index,
5452 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005453 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005454 uint32_t dex_pc)
5455 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5457 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5458 SetPackedFlag<kFlagValueCanBeNull>(true);
5459 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005460 SetRawInputAt(0, array);
5461 SetRawInputAt(1, index);
5462 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005463 // Make a best guess now, may be refined during SSA building.
5464 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005465 }
5466
Calin Juravle77520bc2015-01-12 18:45:46 +00005467 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005468 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005469 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005470 }
5471
Mingyao Yang81014cb2015-06-02 03:16:27 -07005472 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005473 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005474
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005475 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005476 // TODO: Same as for ArrayGet.
5477 return false;
5478 }
5479
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005480 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005481 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005482 }
5483
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005484 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005485 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005486 }
5487
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005488 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005490 }
5491
Vladimir Markoa1de9182016-02-25 11:37:38 +00005492 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5493 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5494 bool StaticTypeOfArrayIsObjectArray() const {
5495 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5496 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005497
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005498 HInstruction* GetArray() const { return InputAt(0); }
5499 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005500 HInstruction* GetValue() const { return InputAt(2); }
5501
5502 Primitive::Type GetComponentType() const {
5503 // The Dex format does not type floating point index operations. Since the
5504 // `expected_component_type_` is set during building and can therefore not
5505 // be correct, we also check what is the value type. If it is a floating
5506 // point type, we must use that type.
5507 Primitive::Type value_type = GetValue()->GetType();
5508 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5509 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005511 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005512
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005513 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005514 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005515 }
5516
Aart Bik18b36ab2016-04-13 16:41:35 -07005517 void ComputeSideEffects() {
5518 Primitive::Type type = GetComponentType();
5519 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5520 SideEffectsForArchRuntimeCalls(type)));
5521 }
5522
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005523 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5524 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5525 }
5526
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527 DECLARE_INSTRUCTION(ArraySet);
5528
5529 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005530 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5531 static constexpr size_t kFieldExpectedComponentTypeSize =
5532 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5533 static constexpr size_t kFlagNeedsTypeCheck =
5534 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5535 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005536 // Cached information for the reference_type_info_ so that codegen
5537 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005538 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5539 static constexpr size_t kNumberOfArraySetPackedBits =
5540 kFlagStaticTypeOfArrayIsObjectArray + 1;
5541 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5542 using ExpectedComponentTypeField =
5543 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544
5545 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5546};
5547
Vladimir Markofcb503c2016-05-18 12:48:17 +01005548class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005549 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005550 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005551 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005552 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005553 // Note that arrays do not change length, so the instruction does not
5554 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555 SetRawInputAt(0, array);
5556 }
5557
Calin Juravle77520bc2015-01-12 18:45:46 +00005558 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005559 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005560 return true;
5561 }
Calin Juravle641547a2015-04-21 22:08:51 +01005562 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5563 return obj == InputAt(0);
5564 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005565
Vladimir Markodce016e2016-04-28 13:10:02 +01005566 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5567
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 DECLARE_INSTRUCTION(ArrayLength);
5569
5570 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005571 // We treat a String as an array, creating the HArrayLength from String.length()
5572 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5573 // determine whether a particular HArrayLength is actually a String.length() by
5574 // looking at the type of the input but that requires holding the mutator lock, so
5575 // we prefer to use a flag, so that code generators don't need to do the locking.
5576 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5577 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5578 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5579 "Too many packed fields.");
5580
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5582};
5583
Vladimir Markofcb503c2016-05-18 12:48:17 +01005584class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005586 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5587 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005588 HBoundsCheck(HInstruction* index,
5589 HInstruction* length,
5590 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005591 bool string_char_at = false)
5592 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005593 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005594 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595 SetRawInputAt(0, index);
5596 SetRawInputAt(1, length);
5597 }
5598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005599 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005600 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005601 return true;
5602 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005604 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005605
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005606 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005607
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005608 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005609
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005610 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611
5612 DECLARE_INSTRUCTION(BoundsCheck);
5613
5614 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005615 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005616
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005617 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5618};
5619
Vladimir Markofcb503c2016-05-18 12:48:17 +01005620class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005621 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005622 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005623 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005624
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005625 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005626 return true;
5627 }
5628
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005629 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5630 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005631
5632 DECLARE_INSTRUCTION(SuspendCheck);
5633
5634 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005635 // Only used for code generation, in order to share the same slow path between back edges
5636 // of a same loop.
5637 SlowPathCode* slow_path_;
5638
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005639 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5640};
5641
David Srbecky0cf44932015-12-09 14:09:59 +00005642// Pseudo-instruction which provides the native debugger with mapping information.
5643// It ensures that we can generate line number and local variables at this point.
5644class HNativeDebugInfo : public HTemplateInstruction<0> {
5645 public:
5646 explicit HNativeDebugInfo(uint32_t dex_pc)
5647 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5648
5649 bool NeedsEnvironment() const OVERRIDE {
5650 return true;
5651 }
5652
5653 DECLARE_INSTRUCTION(NativeDebugInfo);
5654
5655 private:
5656 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5657};
5658
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005659/**
5660 * Instruction to load a Class object.
5661 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005663 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005664 // Determines how to load the Class.
5665 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005666 // We cannot load this class. See HSharpening::SharpenLoadClass.
5667 kInvalid = -1,
5668
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005669 // Use the Class* from the method's own ArtMethod*.
5670 kReferrersClass,
5671
5672 // Use boot image Class* address that will be known at link time.
5673 // Used for boot image classes referenced by boot image code in non-PIC mode.
5674 kBootImageLinkTimeAddress,
5675
5676 // Use PC-relative boot image Class* address that will be known at link time.
5677 // Used for boot image classes referenced by boot image code in PIC mode.
5678 kBootImageLinkTimePcRelative,
5679
5680 // Use a known boot image Class* address, embedded in the code by the codegen.
5681 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 kBootImageAddress,
5683
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005684 // Load from an entry in the .bss section using a PC-relative load.
5685 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5686 kBssEntry,
5687
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005688 // Load from the root table associated with the JIT compiled method.
5689 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 // Load from resolved types array accessed through the class loaded from
5692 // the compiled method's own ArtMethod*. This is the default access type when
5693 // all other types are unavailable.
5694 kDexCacheViaMethod,
5695
5696 kLast = kDexCacheViaMethod
5697 };
5698
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005699 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005700 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005701 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005702 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005703 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005704 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005705 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5707 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005708 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005709 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005710 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005711 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005712 // Referrers class should not need access check. We never inline unverified
5713 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005714 DCHECK(!is_referrers_class || !needs_access_check);
5715
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005716 SetPackedField<LoadKindField>(
5717 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005718 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005719 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005721 }
5722
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005723 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725 LoadKind GetLoadKind() const {
5726 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005727 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005728
5729 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005731 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005732
Andreas Gampea5b09a62016-11-17 15:21:22 -08005733 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005735 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005737 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005738 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005739 }
5740
Calin Juravle0ba218d2015-05-19 18:46:01 +01005741 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005742 // The entrypoint the code generator is going to call does not do
5743 // clinit of the class.
5744 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005745 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005746 }
5747
5748 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005749 return NeedsAccessCheck() ||
5750 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005751 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5752 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005753 }
5754
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005755 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005756 return NeedsAccessCheck() ||
5757 MustGenerateClinitCheck() ||
5758 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5759 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5760 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005761 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5762 GetLoadKind() == LoadKind::kBssEntry) &&
5763 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005764 }
5765
Calin Juravleacf735c2015-02-12 15:25:22 +00005766 ReferenceTypeInfo GetLoadedClassRTI() {
5767 return loaded_class_rti_;
5768 }
5769
5770 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5771 // Make sure we only set exact types (the loaded class should never be merged).
5772 DCHECK(rti.IsExact());
5773 loaded_class_rti_ = rti;
5774 }
5775
Andreas Gampea5b09a62016-11-17 15:21:22 -08005776 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005777 const DexFile& GetDexFile() const { return dex_file_; }
5778
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005779 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005780 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005781 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005782
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005783 static SideEffects SideEffectsForArchRuntimeCalls() {
5784 return SideEffects::CanTriggerGC();
5785 }
5786
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005788 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005789 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005790 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005791
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005792 void MarkInBootImage() {
5793 SetPackedFlag<kFlagIsInBootImage>(true);
5794 }
5795
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005796 void AddSpecialInput(HInstruction* special_input);
5797
5798 using HInstruction::GetInputRecords; // Keep the const version visible.
5799 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5800 return ArrayRef<HUserRecord<HInstruction*>>(
5801 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5802 }
5803
5804 Primitive::Type GetType() const OVERRIDE {
5805 return Primitive::kPrimNot;
5806 }
5807
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005808 Handle<mirror::Class> GetClass() const {
5809 return klass_;
5810 }
5811
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005812 DECLARE_INSTRUCTION(LoadClass);
5813
5814 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005816 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005817 // Whether this instruction must generate the initialization check.
5818 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005819 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005820 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5821 static constexpr size_t kFieldLoadKindSize =
5822 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5823 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005824 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005825 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5826
5827 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005828 return load_kind == LoadKind::kReferrersClass ||
5829 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005831 load_kind == LoadKind::kBssEntry ||
5832 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 }
5834
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 void SetLoadKindInternal(LoadKind load_kind);
5836
5837 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5838 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005839 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005840 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005841
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005842 // A type index and dex file where the class can be accessed. The dex file can be:
5843 // - The compiling method's dex file if the class is defined there too.
5844 // - The compiling method's dex file if the class is referenced there.
5845 // - The dex file where the class is defined. When the load kind can only be
5846 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005847 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005848 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005849
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005850 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005851
Calin Juravleacf735c2015-02-12 15:25:22 +00005852 ReferenceTypeInfo loaded_class_rti_;
5853
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005854 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5855};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005856std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5857
5858// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005860 // The special input is used for PC-relative loads on some architectures,
5861 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005862 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005863 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005864 GetLoadKind() == LoadKind::kBootImageAddress ||
5865 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005866 DCHECK(special_input_.GetInstruction() == nullptr);
5867 special_input_ = HUserRecord<HInstruction*>(special_input);
5868 special_input->AddUseAt(this, 0);
5869}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005870
Vladimir Marko372f10e2016-05-17 16:30:10 +01005871class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005872 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005873 // Determines how to load the String.
5874 enum class LoadKind {
5875 // Use boot image String* address that will be known at link time.
5876 // Used for boot image strings referenced by boot image code in non-PIC mode.
5877 kBootImageLinkTimeAddress,
5878
5879 // Use PC-relative boot image String* address that will be known at link time.
5880 // Used for boot image strings referenced by boot image code in PIC mode.
5881 kBootImageLinkTimePcRelative,
5882
5883 // Use a known boot image String* address, embedded in the code by the codegen.
5884 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 kBootImageAddress,
5886
Vladimir Markoaad75c62016-10-03 08:46:48 +00005887 // Load from an entry in the .bss section using a PC-relative load.
5888 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5889 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005891 // Load from the root table associated with the JIT compiled method.
5892 kJitTableAddress,
5893
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 // Load from resolved strings array accessed through the class loaded from
5895 // the compiled method's own ArtMethod*. This is the default access type when
5896 // all other types are unavailable.
5897 kDexCacheViaMethod,
5898
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005899 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900 };
5901
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005902 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005903 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904 const DexFile& dex_file,
5905 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005906 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5907 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005908 string_index_(string_index),
5909 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005910 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005911 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005913 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005915 LoadKind GetLoadKind() const {
5916 return GetPackedField<LoadKindField>();
5917 }
5918
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005919 const DexFile& GetDexFile() const {
5920 return dex_file_;
5921 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005922
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005923 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 return string_index_;
5925 }
5926
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005927 Handle<mirror::String> GetString() const {
5928 return string_;
5929 }
5930
5931 void SetString(Handle<mirror::String> str) {
5932 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005933 }
5934
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005935 bool CanBeMoved() const OVERRIDE { return true; }
5936
Vladimir Marko372f10e2016-05-17 16:30:10 +01005937 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005938
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005939 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005940
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941 // Will call the runtime if we need to load the string through
5942 // the dex cache and the string is not guaranteed to be there yet.
5943 bool NeedsEnvironment() const OVERRIDE {
5944 LoadKind load_kind = GetLoadKind();
5945 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5946 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005947 load_kind == LoadKind::kBootImageAddress ||
5948 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 return false;
5950 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005951 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005953
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5955 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5956 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005957
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005958 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005960
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005961 static SideEffects SideEffectsForArchRuntimeCalls() {
5962 return SideEffects::CanTriggerGC();
5963 }
5964
Vladimir Marko372f10e2016-05-17 16:30:10 +01005965 void AddSpecialInput(HInstruction* special_input);
5966
5967 using HInstruction::GetInputRecords; // Keep the const version visible.
5968 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5969 return ArrayRef<HUserRecord<HInstruction*>>(
5970 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 }
5972
Vladimir Marko372f10e2016-05-17 16:30:10 +01005973 Primitive::Type GetType() const OVERRIDE {
5974 return Primitive::kPrimNot;
5975 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005977 DECLARE_INSTRUCTION(LoadString);
5978
5979 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005980 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981 static constexpr size_t kFieldLoadKindSize =
5982 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5983 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005984 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005986
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 void SetLoadKindInternal(LoadKind load_kind);
5988
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005989 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5990 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005991 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005992 HUserRecord<HInstruction*> special_input_;
5993
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005994 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005995 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005996
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005997 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005998
5999 DISALLOW_COPY_AND_ASSIGN(HLoadString);
6000};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
6002
6003// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006004inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006005 // The special input is used for PC-relative loads on some architectures,
6006 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006007 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006008 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07006009 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
6010 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01006011 // HLoadString::GetInputRecords() returns an empty array at this point,
6012 // so use the GetInputRecords() from the base class to set the input record.
6013 DCHECK(special_input_.GetInstruction() == nullptr);
6014 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015 special_input->AddUseAt(this, 0);
6016}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006017
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018/**
6019 * Performs an initialization check on its Class object input.
6020 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006021class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006023 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006024 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006025 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006026 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6027 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006028 SetRawInputAt(0, constant);
6029 }
6030
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006031 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006032 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006033 return true;
6034 }
6035
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006036 bool NeedsEnvironment() const OVERRIDE {
6037 // May call runtime to initialize the class.
6038 return true;
6039 }
6040
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006041 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006042
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006043 HLoadClass* GetLoadClass() const {
6044 DCHECK(InputAt(0)->IsLoadClass());
6045 return InputAt(0)->AsLoadClass();
6046 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006047
6048 DECLARE_INSTRUCTION(ClinitCheck);
6049
6050 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006051 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6052};
6053
Vladimir Markofcb503c2016-05-18 12:48:17 +01006054class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006055 public:
6056 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006057 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006059 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006060 bool is_volatile,
6061 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006062 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006063 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006064 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006065 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006066 field_info_(field,
6067 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006068 field_type,
6069 is_volatile,
6070 field_idx,
6071 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006072 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006073 SetRawInputAt(0, cls);
6074 }
6075
Calin Juravle52c48962014-12-16 17:02:57 +00006076
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006077 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006078
Vladimir Marko372f10e2016-05-17 16:30:10 +01006079 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6080 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006081 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006082 }
6083
6084 size_t ComputeHashCode() const OVERRIDE {
6085 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6086 }
6087
Calin Juravle52c48962014-12-16 17:02:57 +00006088 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6090 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006091 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006092
6093 DECLARE_INSTRUCTION(StaticFieldGet);
6094
6095 private:
6096 const FieldInfo field_info_;
6097
6098 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6099};
6100
Vladimir Markofcb503c2016-05-18 12:48:17 +01006101class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 public:
6103 HStaticFieldSet(HInstruction* cls,
6104 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006105 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006106 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006107 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006108 bool is_volatile,
6109 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006110 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006111 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006112 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006113 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006114 field_info_(field,
6115 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006116 field_type,
6117 is_volatile,
6118 field_idx,
6119 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006120 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006122 SetRawInputAt(0, cls);
6123 SetRawInputAt(1, value);
6124 }
6125
Calin Juravle52c48962014-12-16 17:02:57 +00006126 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006127 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6128 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006129 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006131 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006132 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6133 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006134
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006135 DECLARE_INSTRUCTION(StaticFieldSet);
6136
6137 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006138 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6139 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6140 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6141 "Too many packed fields.");
6142
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006143 const FieldInfo field_info_;
6144
6145 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6146};
6147
Vladimir Markofcb503c2016-05-18 12:48:17 +01006148class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006149 public:
6150 HUnresolvedInstanceFieldGet(HInstruction* obj,
6151 Primitive::Type field_type,
6152 uint32_t field_index,
6153 uint32_t dex_pc)
6154 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6155 field_index_(field_index) {
6156 SetRawInputAt(0, obj);
6157 }
6158
6159 bool NeedsEnvironment() const OVERRIDE { return true; }
6160 bool CanThrow() const OVERRIDE { return true; }
6161
6162 Primitive::Type GetFieldType() const { return GetType(); }
6163 uint32_t GetFieldIndex() const { return field_index_; }
6164
6165 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6166
6167 private:
6168 const uint32_t field_index_;
6169
6170 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6171};
6172
Vladimir Markofcb503c2016-05-18 12:48:17 +01006173class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006174 public:
6175 HUnresolvedInstanceFieldSet(HInstruction* obj,
6176 HInstruction* value,
6177 Primitive::Type field_type,
6178 uint32_t field_index,
6179 uint32_t dex_pc)
6180 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006181 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006182 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006183 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006184 SetRawInputAt(0, obj);
6185 SetRawInputAt(1, value);
6186 }
6187
6188 bool NeedsEnvironment() const OVERRIDE { return true; }
6189 bool CanThrow() const OVERRIDE { return true; }
6190
Vladimir Markoa1de9182016-02-25 11:37:38 +00006191 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006192 uint32_t GetFieldIndex() const { return field_index_; }
6193
6194 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6195
6196 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006197 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6198 static constexpr size_t kFieldFieldTypeSize =
6199 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6200 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6201 kFieldFieldType + kFieldFieldTypeSize;
6202 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6203 "Too many packed fields.");
6204 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6205
Calin Juravlee460d1d2015-09-29 04:52:17 +01006206 const uint32_t field_index_;
6207
6208 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6209};
6210
Vladimir Markofcb503c2016-05-18 12:48:17 +01006211class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006212 public:
6213 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6214 uint32_t field_index,
6215 uint32_t dex_pc)
6216 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6217 field_index_(field_index) {
6218 }
6219
6220 bool NeedsEnvironment() const OVERRIDE { return true; }
6221 bool CanThrow() const OVERRIDE { return true; }
6222
6223 Primitive::Type GetFieldType() const { return GetType(); }
6224 uint32_t GetFieldIndex() const { return field_index_; }
6225
6226 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6227
6228 private:
6229 const uint32_t field_index_;
6230
6231 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6232};
6233
Vladimir Markofcb503c2016-05-18 12:48:17 +01006234class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006235 public:
6236 HUnresolvedStaticFieldSet(HInstruction* value,
6237 Primitive::Type field_type,
6238 uint32_t field_index,
6239 uint32_t dex_pc)
6240 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006241 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006242 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006243 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006244 SetRawInputAt(0, value);
6245 }
6246
6247 bool NeedsEnvironment() const OVERRIDE { return true; }
6248 bool CanThrow() const OVERRIDE { return true; }
6249
Vladimir Markoa1de9182016-02-25 11:37:38 +00006250 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006251 uint32_t GetFieldIndex() const { return field_index_; }
6252
6253 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6254
6255 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006256 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6257 static constexpr size_t kFieldFieldTypeSize =
6258 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6259 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6260 kFieldFieldType + kFieldFieldTypeSize;
6261 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6262 "Too many packed fields.");
6263 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6264
Calin Juravlee460d1d2015-09-29 04:52:17 +01006265 const uint32_t field_index_;
6266
6267 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6268};
6269
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006270// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006271class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006272 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006273 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6274 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006275
David Brazdilbbd733e2015-08-18 17:48:17 +01006276 bool CanBeNull() const OVERRIDE { return false; }
6277
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006278 DECLARE_INSTRUCTION(LoadException);
6279
6280 private:
6281 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6282};
6283
David Brazdilcb1c0552015-08-04 16:22:25 +01006284// Implicit part of move-exception which clears thread-local exception storage.
6285// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006286class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006287 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006288 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6289 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006290
6291 DECLARE_INSTRUCTION(ClearException);
6292
6293 private:
6294 DISALLOW_COPY_AND_ASSIGN(HClearException);
6295};
6296
Vladimir Markofcb503c2016-05-18 12:48:17 +01006297class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006298 public:
6299 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006300 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006301 SetRawInputAt(0, exception);
6302 }
6303
6304 bool IsControlFlow() const OVERRIDE { return true; }
6305
6306 bool NeedsEnvironment() const OVERRIDE { return true; }
6307
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006308 bool CanThrow() const OVERRIDE { return true; }
6309
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006310
6311 DECLARE_INSTRUCTION(Throw);
6312
6313 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006314 DISALLOW_COPY_AND_ASSIGN(HThrow);
6315};
6316
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317/**
6318 * Implementation strategies for the code generator of a HInstanceOf
6319 * or `HCheckCast`.
6320 */
6321enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006322 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 kExactCheck, // Can do a single class compare.
6324 kClassHierarchyCheck, // Can just walk the super class chain.
6325 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6326 kInterfaceCheck, // No optimization yet when checking against an interface.
6327 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 kArrayCheck, // No optimization yet when checking against a generic array.
6329 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330};
6331
Roland Levillain86503782016-02-11 19:07:30 +00006332std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6333
Vladimir Markofcb503c2016-05-18 12:48:17 +01006334class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006336 HInstanceOf(HInstruction* object,
6337 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006339 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006340 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006342 dex_pc) {
6343 SetPackedField<TypeCheckKindField>(check_kind);
6344 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006345 SetRawInputAt(0, object);
6346 SetRawInputAt(1, constant);
6347 }
6348
6349 bool CanBeMoved() const OVERRIDE { return true; }
6350
Vladimir Marko372f10e2016-05-17 16:30:10 +01006351 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006352 return true;
6353 }
6354
6355 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006356 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006357 }
6358
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006359 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6361 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6362 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6363 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006364
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006365 static bool CanCallRuntime(TypeCheckKind check_kind) {
6366 // Mips currently does runtime calls for any other checks.
6367 return check_kind != TypeCheckKind::kExactCheck;
6368 }
6369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006371 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006372 }
6373
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006374 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006375
6376 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6378 static constexpr size_t kFieldTypeCheckKindSize =
6379 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6380 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6381 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6382 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6383 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006384
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006385 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6386};
6387
Vladimir Markofcb503c2016-05-18 12:48:17 +01006388class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006389 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006390 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006391 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6393 SetPackedFlag<kFlagUpperCanBeNull>(true);
6394 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006395 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006396 SetRawInputAt(0, input);
6397 }
6398
David Brazdilf5552582015-12-27 13:36:12 +00006399 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006400 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006401 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006402 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006403
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006404 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 DCHECK(GetUpperCanBeNull() || !can_be_null);
6406 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006407 }
6408
Vladimir Markoa1de9182016-02-25 11:37:38 +00006409 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006410
Calin Juravleb1498f62015-02-16 13:13:29 +00006411 DECLARE_INSTRUCTION(BoundType);
6412
6413 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6415 // is false then CanBeNull() cannot be true).
6416 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6417 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6418 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6419 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6420
Calin Juravleb1498f62015-02-16 13:13:29 +00006421 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006422 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6423 // It is used to bound the type in cases like:
6424 // if (x instanceof ClassX) {
6425 // // uper_bound_ will be ClassX
6426 // }
David Brazdilf5552582015-12-27 13:36:12 +00006427 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006428
6429 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6430};
6431
Vladimir Markofcb503c2016-05-18 12:48:17 +01006432class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006433 public:
6434 HCheckCast(HInstruction* object,
6435 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006436 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006437 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6439 SetPackedField<TypeCheckKindField>(check_kind);
6440 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006441 SetRawInputAt(0, object);
6442 SetRawInputAt(1, constant);
6443 }
6444
6445 bool CanBeMoved() const OVERRIDE { return true; }
6446
Vladimir Marko372f10e2016-05-17 16:30:10 +01006447 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006448 return true;
6449 }
6450
6451 bool NeedsEnvironment() const OVERRIDE {
6452 // Instruction may throw a CheckCastError.
6453 return true;
6454 }
6455
6456 bool CanThrow() const OVERRIDE { return true; }
6457
Vladimir Markoa1de9182016-02-25 11:37:38 +00006458 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6459 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6460 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6461 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006462
6463 DECLARE_INSTRUCTION(CheckCast);
6464
6465 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006466 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6467 static constexpr size_t kFieldTypeCheckKindSize =
6468 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6469 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6470 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6471 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6472 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006473
6474 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006475};
6476
Andreas Gampe26de38b2016-07-27 17:53:11 -07006477/**
6478 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6479 * @details We define the combined barrier types that are actually required
6480 * by the Java Memory Model, rather than using exactly the terminology from
6481 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6482 * primitives. Note that the JSR-133 cookbook generally does not deal with
6483 * store atomicity issues, and the recipes there are not always entirely sufficient.
6484 * The current recipe is as follows:
6485 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6486 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6487 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6488 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6489 * class has final fields.
6490 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6491 * store-to-memory instructions. Only generated together with non-temporal stores.
6492 */
6493enum MemBarrierKind {
6494 kAnyStore,
6495 kLoadAny,
6496 kStoreStore,
6497 kAnyAny,
6498 kNTStoreStore,
6499 kLastBarrierKind = kNTStoreStore
6500};
6501std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6502
Vladimir Markofcb503c2016-05-18 12:48:17 +01006503class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006504 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006505 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006506 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006507 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6508 SetPackedField<BarrierKindField>(barrier_kind);
6509 }
Calin Juravle27df7582015-04-17 19:12:31 +01006510
Vladimir Markoa1de9182016-02-25 11:37:38 +00006511 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006512
6513 DECLARE_INSTRUCTION(MemoryBarrier);
6514
6515 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006516 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6517 static constexpr size_t kFieldBarrierKindSize =
6518 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6519 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6520 kFieldBarrierKind + kFieldBarrierKindSize;
6521 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6522 "Too many packed fields.");
6523 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006524
6525 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6526};
6527
Igor Murashkind01745e2017-04-05 16:40:31 -07006528// A constructor fence orders all prior stores to fields that could be accessed via a final field of
6529// the specified object(s), with respect to any subsequent store that might "publish"
6530// (i.e. make visible) the specified object to another thread.
6531//
6532// JLS 17.5.1 "Semantics of final fields" states that a freeze action happens
6533// for all final fields (that were set) at the end of the invoked constructor.
6534//
6535// The constructor fence models the freeze actions for the final fields of an object
6536// being constructed (semantically at the end of the constructor). Constructor fences
6537// have a per-object affinity; two separate objects being constructed get two separate
6538// constructor fences.
6539//
6540// (Note: that if calling a super-constructor or forwarding to another constructor,
6541// the freezes would happen at the end of *that* constructor being invoked).
6542//
6543// The memory model guarantees that when the object being constructed is "published" after
6544// constructor completion (i.e. escapes the current thread via a store), then any final field
6545// writes must be observable on other threads (once they observe that publication).
6546//
6547// Further, anything written before the freeze, and read by dereferencing through the final field,
6548// must also be visible (so final object field could itself have an object with non-final fields;
6549// yet the freeze must also extend to them).
6550//
6551// Constructor example:
6552//
6553// class HasFinal {
6554// final int field; Optimizing IR for <init>()V:
6555// HasFinal() {
6556// field = 123; HInstanceFieldSet(this, HasFinal.field, 123)
6557// // freeze(this.field); HConstructorFence(this)
6558// } HReturn
6559// }
6560//
6561// HConstructorFence can serve double duty as a fence for new-instance/new-array allocations of
6562// already-initialized classes; in that case the allocation must act as a "default-initializer"
6563// of the object which effectively writes the class pointer "final field".
6564//
6565// For example, we can model default-initialiation as roughly the equivalent of the following:
6566//
6567// class Object {
6568// private final Class header;
6569// }
6570//
6571// Java code: Optimizing IR:
6572//
6573// T new_instance<T>() {
6574// Object obj = allocate_memory(T.class.size); obj = HInvoke(art_quick_alloc_object, T)
6575// obj.header = T.class; // header write is done by above call.
6576// // freeze(obj.header) HConstructorFence(obj)
6577// return (T)obj;
6578// }
6579//
6580// See also:
6581// * CompilerDriver::RequiresConstructorBarrier
6582// * QuasiAtomic::ThreadFenceForConstructor
6583//
6584class HConstructorFence FINAL : public HVariableInputSizeInstruction {
6585 // A fence has variable inputs because the inputs can be removed
6586 // after prepare_for_register_allocation phase.
6587 // (TODO: In the future a fence could freeze multiple objects
6588 // after merging two fences together.)
6589 public:
6590 // `fence_object` is the reference that needs to be protected for correct publication.
6591 //
6592 // It makes sense in the following situations:
6593 // * <init> constructors, it's the "this" parameter (i.e. HParameterValue, s.t. IsThis() == true).
6594 // * new-instance-like instructions, it's the return value (i.e. HNewInstance).
6595 //
6596 // After construction the `fence_object` becomes the 0th input.
6597 // This is not an input in a real sense, but just a convenient place to stash the information
6598 // about the associated object.
6599 HConstructorFence(HInstruction* fence_object,
6600 uint32_t dex_pc,
6601 ArenaAllocator* arena)
6602 // We strongly suspect there is not a more accurate way to describe the fine-grained reordering
6603 // constraints described in the class header. We claim that these SideEffects constraints
6604 // enforce a superset of the real constraints.
6605 //
6606 // The ordering described above is conservatively modeled with SideEffects as follows:
6607 //
6608 // * To prevent reordering of the publication stores:
6609 // ----> "Reads of objects" is the initial SideEffect.
6610 // * For every primitive final field store in the constructor:
6611 // ----> Union that field's type as a read (e.g. "Read of T") into the SideEffect.
6612 // * If there are any stores to reference final fields in the constructor:
6613 // ----> Use a more conservative "AllReads" SideEffect because any stores to any references
6614 // that are reachable from `fence_object` also need to be prevented for reordering
6615 // (and we do not want to do alias analysis to figure out what those stores are).
6616 //
6617 // In the implementation, this initially starts out as an "all reads" side effect; this is an
6618 // even more conservative approach than the one described above, and prevents all of the
6619 // above reordering without analyzing any of the instructions in the constructor.
6620 //
6621 // If in a later phase we discover that there are no writes to reference final fields,
6622 // we can refine the side effect to a smaller set of type reads (see above constraints).
6623 : HVariableInputSizeInstruction(SideEffects::AllReads(),
6624 dex_pc,
6625 arena,
6626 /* number_of_inputs */ 1,
6627 kArenaAllocConstructorFenceInputs) {
6628 DCHECK(fence_object != nullptr);
6629 SetRawInputAt(0, fence_object);
6630 }
6631
6632 // The object associated with this constructor fence.
6633 //
6634 // (Note: This will be null after the prepare_for_register_allocation phase,
6635 // as all constructor fence inputs are removed there).
6636 HInstruction* GetFenceObject() const {
6637 return InputAt(0);
6638 }
6639
6640 // Find all the HConstructorFence uses (`fence_use`) for `this` and:
6641 // - Delete `fence_use` from `this`'s use list.
6642 // - Delete `this` from `fence_use`'s inputs list.
6643 // - If the `fence_use` is dead, remove it from the graph.
6644 //
6645 // A fence is considered dead once it no longer has any uses
6646 // and all of the inputs are dead.
6647 //
6648 // This must *not* be called during/after prepare_for_register_allocation,
6649 // because that removes all the inputs to the fences but the fence is actually
6650 // still considered live.
6651 static void RemoveConstructorFences(HInstruction* instruction);
6652
6653 DECLARE_INSTRUCTION(ConstructorFence);
6654
6655 private:
6656 DISALLOW_COPY_AND_ASSIGN(HConstructorFence);
6657};
6658
Vladimir Markofcb503c2016-05-18 12:48:17 +01006659class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006660 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006661 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006662 kEnter,
6663 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006664 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006665 };
6666
6667 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006668 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006669 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6670 dex_pc) {
6671 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006672 SetRawInputAt(0, object);
6673 }
6674
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006675 // Instruction may go into runtime, so we need an environment.
6676 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006677
6678 bool CanThrow() const OVERRIDE {
6679 // Verifier guarantees that monitor-exit cannot throw.
6680 // This is important because it allows the HGraphBuilder to remove
6681 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6682 return IsEnter();
6683 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006684
Vladimir Markoa1de9182016-02-25 11:37:38 +00006685 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6686 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006687
6688 DECLARE_INSTRUCTION(MonitorOperation);
6689
Calin Juravle52c48962014-12-16 17:02:57 +00006690 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006691 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6692 static constexpr size_t kFieldOperationKindSize =
6693 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6694 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6695 kFieldOperationKind + kFieldOperationKindSize;
6696 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6697 "Too many packed fields.");
6698 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006699
6700 private:
6701 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6702};
6703
Vladimir Markofcb503c2016-05-18 12:48:17 +01006704class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006705 public:
6706 HSelect(HInstruction* condition,
6707 HInstruction* true_value,
6708 HInstruction* false_value,
6709 uint32_t dex_pc)
6710 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6711 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6712
6713 // First input must be `true_value` or `false_value` to allow codegens to
6714 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6715 // that architectures which implement HSelect as a conditional move also
6716 // will not need to invert the condition.
6717 SetRawInputAt(0, false_value);
6718 SetRawInputAt(1, true_value);
6719 SetRawInputAt(2, condition);
6720 }
6721
6722 HInstruction* GetFalseValue() const { return InputAt(0); }
6723 HInstruction* GetTrueValue() const { return InputAt(1); }
6724 HInstruction* GetCondition() const { return InputAt(2); }
6725
6726 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006727 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6728 return true;
6729 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006730
6731 bool CanBeNull() const OVERRIDE {
6732 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6733 }
6734
6735 DECLARE_INSTRUCTION(Select);
6736
6737 private:
6738 DISALLOW_COPY_AND_ASSIGN(HSelect);
6739};
6740
Vladimir Markof9f64412015-09-02 14:05:49 +01006741class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006742 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006743 MoveOperands(Location source,
6744 Location destination,
6745 Primitive::Type type,
6746 HInstruction* instruction)
6747 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006748
6749 Location GetSource() const { return source_; }
6750 Location GetDestination() const { return destination_; }
6751
6752 void SetSource(Location value) { source_ = value; }
6753 void SetDestination(Location value) { destination_ = value; }
6754
6755 // The parallel move resolver marks moves as "in-progress" by clearing the
6756 // destination (but not the source).
6757 Location MarkPending() {
6758 DCHECK(!IsPending());
6759 Location dest = destination_;
6760 destination_ = Location::NoLocation();
6761 return dest;
6762 }
6763
6764 void ClearPending(Location dest) {
6765 DCHECK(IsPending());
6766 destination_ = dest;
6767 }
6768
6769 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006770 DCHECK(source_.IsValid() || destination_.IsInvalid());
6771 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006772 }
6773
6774 // True if this blocks a move from the given location.
6775 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006776 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006777 }
6778
6779 // A move is redundant if it's been eliminated, if its source and
6780 // destination are the same, or if its destination is unneeded.
6781 bool IsRedundant() const {
6782 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6783 }
6784
6785 // We clear both operands to indicate move that's been eliminated.
6786 void Eliminate() {
6787 source_ = destination_ = Location::NoLocation();
6788 }
6789
6790 bool IsEliminated() const {
6791 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6792 return source_.IsInvalid();
6793 }
6794
Alexey Frunze4dda3372015-06-01 18:31:49 -07006795 Primitive::Type GetType() const { return type_; }
6796
Nicolas Geoffray90218252015-04-15 11:56:51 +01006797 bool Is64BitMove() const {
6798 return Primitive::Is64BitType(type_);
6799 }
6800
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006801 HInstruction* GetInstruction() const { return instruction_; }
6802
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006803 private:
6804 Location source_;
6805 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006806 // The type this move is for.
6807 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006808 // The instruction this move is assocatied with. Null when this move is
6809 // for moving an input in the expected locations of user (including a phi user).
6810 // This is only used in debug mode, to ensure we do not connect interval siblings
6811 // in the same parallel move.
6812 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006813};
6814
Roland Levillainc9285912015-12-18 10:38:42 +00006815std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6816
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006817static constexpr size_t kDefaultNumberOfMoves = 4;
6818
Vladimir Markofcb503c2016-05-18 12:48:17 +01006819class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006820 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006821 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006822 : HTemplateInstruction(SideEffects::None(), dex_pc),
6823 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6824 moves_.reserve(kDefaultNumberOfMoves);
6825 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006826
Nicolas Geoffray90218252015-04-15 11:56:51 +01006827 void AddMove(Location source,
6828 Location destination,
6829 Primitive::Type type,
6830 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006831 DCHECK(source.IsValid());
6832 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006833 if (kIsDebugBuild) {
6834 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006835 for (const MoveOperands& move : moves_) {
6836 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006837 // Special case the situation where the move is for the spill slot
6838 // of the instruction.
6839 if ((GetPrevious() == instruction)
6840 || ((GetPrevious() == nullptr)
6841 && instruction->IsPhi()
6842 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006843 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006844 << "Doing parallel moves for the same instruction.";
6845 } else {
6846 DCHECK(false) << "Doing parallel moves for the same instruction.";
6847 }
6848 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006849 }
6850 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006851 for (const MoveOperands& move : moves_) {
6852 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006853 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006854 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006855 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006856 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006857 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006858 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006859 }
6860
Vladimir Marko225b6462015-09-28 12:17:40 +01006861 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006862 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006863 }
6864
Vladimir Marko225b6462015-09-28 12:17:40 +01006865 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006866
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006867 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006868
6869 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006870 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006871
6872 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6873};
6874
Mark Mendell0616ae02015-04-17 12:49:27 -04006875} // namespace art
6876
Aart Bikf8f5a162017-02-06 15:35:29 -08006877#include "nodes_vector.h"
6878
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006879#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6880#include "nodes_shared.h"
6881#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006882#ifdef ART_ENABLE_CODEGEN_arm
6883#include "nodes_arm.h"
6884#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006885#ifdef ART_ENABLE_CODEGEN_mips
6886#include "nodes_mips.h"
6887#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006888#ifdef ART_ENABLE_CODEGEN_x86
6889#include "nodes_x86.h"
6890#endif
6891
6892namespace art {
6893
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006894class HGraphVisitor : public ValueObject {
6895 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006896 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6897 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006898
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006899 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006900 virtual void VisitBasicBlock(HBasicBlock* block);
6901
Roland Levillain633021e2014-10-01 14:12:25 +01006902 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006903 void VisitInsertionOrder();
6904
Roland Levillain633021e2014-10-01 14:12:25 +01006905 // Visit the graph following dominator tree reverse post-order.
6906 void VisitReversePostOrder();
6907
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006908 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006909
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006910 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006911#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006912 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6913
6914 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6915
6916#undef DECLARE_VISIT_INSTRUCTION
6917
6918 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006919 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006920
6921 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6922};
6923
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006924class HGraphDelegateVisitor : public HGraphVisitor {
6925 public:
6926 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6927 virtual ~HGraphDelegateVisitor() {}
6928
6929 // Visit functions that delegate to to super class.
6930#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006931 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006932
6933 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6934
6935#undef DECLARE_VISIT_INSTRUCTION
6936
6937 private:
6938 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6939};
6940
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006941// Iterator over the blocks that art part of the loop. Includes blocks part
6942// of an inner loop. The order in which the blocks are iterated is on their
6943// block id.
6944class HBlocksInLoopIterator : public ValueObject {
6945 public:
6946 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6947 : blocks_in_loop_(info.GetBlocks()),
6948 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6949 index_(0) {
6950 if (!blocks_in_loop_.IsBitSet(index_)) {
6951 Advance();
6952 }
6953 }
6954
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006955 bool Done() const { return index_ == blocks_.size(); }
6956 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006957 void Advance() {
6958 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006959 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006960 if (blocks_in_loop_.IsBitSet(index_)) {
6961 break;
6962 }
6963 }
6964 }
6965
6966 private:
6967 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006968 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006969 size_t index_;
6970
6971 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6972};
6973
Mingyao Yang3584bce2015-05-19 16:01:59 -07006974// Iterator over the blocks that art part of the loop. Includes blocks part
6975// of an inner loop. The order in which the blocks are iterated is reverse
6976// post order.
6977class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6978 public:
6979 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6980 : blocks_in_loop_(info.GetBlocks()),
6981 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6982 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006983 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006984 Advance();
6985 }
6986 }
6987
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006988 bool Done() const { return index_ == blocks_.size(); }
6989 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006990 void Advance() {
6991 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006992 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6993 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006994 break;
6995 }
6996 }
6997 }
6998
6999 private:
7000 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01007001 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07007002 size_t index_;
7003
7004 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
7005};
7006
Aart Bikf3e61ee2017-04-12 17:09:20 -07007007// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007008inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00007009 if (constant->IsIntConstant()) {
7010 return constant->AsIntConstant()->GetValue();
7011 } else if (constant->IsLongConstant()) {
7012 return constant->AsLongConstant()->GetValue();
7013 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00007014 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00007015 return 0;
7016 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00007017}
7018
Aart Bikf3e61ee2017-04-12 17:09:20 -07007019// Returns true iff instruction is an integral constant (and sets value on success).
7020inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
7021 if (instruction->IsIntConstant()) {
7022 *value = instruction->AsIntConstant()->GetValue();
7023 return true;
7024 } else if (instruction->IsLongConstant()) {
7025 *value = instruction->AsLongConstant()->GetValue();
7026 return true;
7027 } else if (instruction->IsNullConstant()) {
7028 *value = 0;
7029 return true;
7030 }
7031 return false;
7032}
7033
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00007034#define INSTRUCTION_TYPE_CHECK(type, super) \
7035 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
7036 inline const H##type* HInstruction::As##type() const { \
7037 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
7038 } \
7039 inline H##type* HInstruction::As##type() { \
7040 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
7041 }
7042
7043 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
7044#undef INSTRUCTION_TYPE_CHECK
7045
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00007046// Create space in `blocks` for adding `number_of_new_blocks` entries
7047// starting at location `at`. Blocks after `at` are moved accordingly.
7048inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
7049 size_t number_of_new_blocks,
7050 size_t after) {
7051 DCHECK_LT(after, blocks->size());
7052 size_t old_size = blocks->size();
7053 size_t new_size = old_size + number_of_new_blocks;
7054 blocks->resize(new_size);
7055 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
7056}
7057
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007058/*
7059 * Hunt "under the hood" of array lengths (leading to array references),
7060 * null checks (also leading to array references), and new arrays
7061 * (leading to the actual length). This makes it more likely related
7062 * instructions become actually comparable.
7063 */
7064inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
7065 while (instruction->IsArrayLength() ||
7066 instruction->IsNullCheck() ||
7067 instruction->IsNewArray()) {
7068 instruction = instruction->IsNewArray()
7069 ? instruction->AsNewArray()->GetLength()
7070 : instruction->InputAt(0);
7071 }
7072 return instruction;
7073}
7074
Nicolas Geoffray818f2102014-02-18 16:43:35 +00007075} // namespace art
7076
7077#endif // ART_COMPILER_OPTIMIZING_NODES_H_