blob: 00d298824eea1e1734b6a28071e4ef4de6c8917f [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100309 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100310 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 blocks_(arena->Adapter(kArenaAllocBlockList)),
315 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
316 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700317 entry_block_(nullptr),
318 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320 number_of_vregs_(0),
321 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000322 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400323 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000324 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700325 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800326 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000327 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100330 dex_file_(dex_file),
331 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100332 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100342 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000343 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700344 osr_(osr),
345 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100346 blocks_.reserve(kDefaultNumberOfBlocks);
347 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
David Brazdilbadd8262016-02-02 16:28:56 +0000349 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700350 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
354
David Brazdil69ba7b72015-06-23 18:27:30 +0100355 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000356 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100357
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 HBasicBlock* GetEntryBlock() const { return entry_block_; }
359 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100360 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000362 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
363 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100366
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100367 void ComputeDominanceInformation();
368 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000369 void ClearLoopInformation();
370 void FindBackEdges(ArenaBitVector* visited);
371 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100372 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100373 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374
David Brazdil4833f5a2015-12-16 10:37:39 +0000375 // Analyze all natural loops in this graph. Returns a code specifying that it
376 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000377 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379
David Brazdilffee3d32015-07-06 11:48:53 +0100380 // Iterate over blocks to compute try block membership. Needs reverse post
381 // order and loop information.
382 void ComputeTryBlockInformation();
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000385 // Returns the instruction to replace the invoke expression or null if the
386 // invoke is for a void method. Note that the caller is responsible for replacing
387 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000388 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000390 // Update the loop and try membership of `block`, which was spawned from `reference`.
391 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
392 // should be the new back edge.
393 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
394 HBasicBlock* reference,
395 bool replace_if_back_edge);
396
Mingyao Yang3584bce2015-05-19 16:01:59 -0700397 // Need to add a couple of blocks to test if the loop body is entered and
398 // put deoptimization instructions, etc.
399 void TransformLoopHeaderForBCE(HBasicBlock* header);
400
Aart Bikf8f5a162017-02-06 15:35:29 -0800401 // Adds a new loop directly after the loop with the given header and exit.
402 // Returns the new preheader.
403 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
404 HBasicBlock* body,
405 HBasicBlock* exit);
406
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000407 // Removes `block` from the graph. Assumes `block` has been disconnected from
408 // other blocks and has no instructions or phis.
409 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000410
David Brazdilfc6a86a2015-06-26 10:33:45 +0000411 // Splits the edge between `block` and `successor` while preserving the
412 // indices in the predecessor/successor lists. If there are multiple edges
413 // between the blocks, the lowest indices are used.
414 // Returns the new block which is empty and has the same dex pc as `successor`.
415 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100417 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
418 void SimplifyLoop(HBasicBlock* header);
419
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 int32_t GetNextInstructionId() {
421 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000422 return current_instruction_id_++;
423 }
424
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000425 int32_t GetCurrentInstructionId() const {
426 return current_instruction_id_;
427 }
428
429 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000430 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 current_instruction_id_ = id;
432 }
433
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100434 uint16_t GetMaximumNumberOfOutVRegs() const {
435 return maximum_number_of_out_vregs_;
436 }
437
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000438 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
439 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100440 }
441
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
443 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
444 }
445
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000446 void UpdateTemporariesVRegSlots(size_t slots) {
447 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 }
449
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000450 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000452 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100453 }
454
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100455 void SetNumberOfVRegs(uint16_t number_of_vregs) {
456 number_of_vregs_ = number_of_vregs;
457 }
458
459 uint16_t GetNumberOfVRegs() const {
460 return number_of_vregs_;
461 }
462
463 void SetNumberOfInVRegs(uint16_t value) {
464 number_of_in_vregs_ = value;
465 }
466
David Brazdildee58d62016-04-07 09:54:26 +0000467 uint16_t GetNumberOfInVRegs() const {
468 return number_of_in_vregs_;
469 }
470
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100471 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100473 return number_of_vregs_ - number_of_in_vregs_;
474 }
475
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100476 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100477 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100479
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100480 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
481 DCHECK(GetReversePostOrder()[0] == entry_block_);
482 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
483 }
484
485 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
486 return ReverseRange(GetReversePostOrder());
487 }
488
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100490 return linear_order_;
491 }
492
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100493 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
494 return ReverseRange(GetLinearOrder());
495 }
496
Mark Mendell1152c922015-04-24 17:06:35 -0400497 bool HasBoundsChecks() const {
498 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800499 }
500
Mark Mendell1152c922015-04-24 17:06:35 -0400501 void SetHasBoundsChecks(bool value) {
502 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800503 }
504
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000505 bool IsDebuggable() const { return debuggable_; }
506
David Brazdil8d5b8b22015-03-24 10:51:52 +0000507 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 // already, it is created and inserted into the graph. This method is only for
509 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600510 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000511
512 // TODO: This is problematic for the consistency of reference type propagation
513 // because it can be created anytime after the pass and thus it will be left
514 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000516
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000529
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100530 HCurrentMethod* GetCurrentMethod();
531
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100532 const DexFile& GetDexFile() const {
533 return dex_file_;
534 }
535
536 uint32_t GetMethodIdx() const {
537 return method_idx_;
538 }
539
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100540 InvokeType GetInvokeType() const {
541 return invoke_type_;
542 }
543
Mark Mendellc4701932015-04-10 13:18:51 -0400544 InstructionSet GetInstructionSet() const {
545 return instruction_set_;
546 }
547
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000548 bool IsCompilingOsr() const { return osr_; }
549
Mingyao Yang063fc772016-08-02 11:02:54 -0700550 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
551 return cha_single_implementation_list_;
552 }
553
554 void AddCHASingleImplementationDependency(ArtMethod* method) {
555 cha_single_implementation_list_.insert(method);
556 }
557
558 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800559 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 }
561
David Brazdil77a48ae2015-09-15 12:34:04 +0000562 bool HasTryCatch() const { return has_try_catch_; }
563 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100564
Aart Bikb13c65b2017-03-21 20:14:07 -0700565 bool HasSIMD() const { return has_simd_; }
566 void SetHasSIMD(bool value) { has_simd_ = value; }
567
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800568 bool HasLoops() const { return has_loops_; }
569 void SetHasLoops(bool value) { has_loops_ = value; }
570
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000571 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
572 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
573
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100574 ArtMethod* GetArtMethod() const { return art_method_; }
575 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
576
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100577 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500578 // The instruction has been inserted into the graph, either as a constant, or
579 // before cursor.
580 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
581
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000582 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
583
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
585 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
586 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
587
David Brazdil2d7352b2015-04-20 14:52:42 +0100588 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000589 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100590 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 template <class InstructionType, typename ValueType>
593 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600594 ArenaSafeMap<ValueType, InstructionType*>* cache,
595 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 // Try to find an existing constant of the given value.
597 InstructionType* constant = nullptr;
598 auto cached_constant = cache->find(value);
599 if (cached_constant != cache->end()) {
600 constant = cached_constant->second;
601 }
602
603 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100604 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600606 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000607 cache->Overwrite(value, constant);
608 InsertConstant(constant);
609 }
610 return constant;
611 }
612
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 void InsertConstant(HConstant* instruction);
614
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 // Cache a float constant into the graph. This method should only be
616 // called by the SsaBuilder when creating "equivalent" instructions.
617 void CacheFloatConstant(HFloatConstant* constant);
618
619 // See CacheFloatConstant comment.
620 void CacheDoubleConstant(HDoubleConstant* constant);
621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623
624 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100627 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100628 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
Aart Bik281c6812016-08-26 11:31:48 -0700630 // List of blocks to perform a linear order tree traversal. Unlike the reverse
631 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100633
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000634 HBasicBlock* entry_block_;
635 HBasicBlock* exit_block_;
636
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100637 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100638 uint16_t maximum_number_of_out_vregs_;
639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100640 // The number of virtual registers in this method. Contains the parameters.
641 uint16_t number_of_vregs_;
642
643 // The number of virtual registers used by parameters of this method.
644 uint16_t number_of_in_vregs_;
645
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000646 // Number of vreg size slots that the temporaries use (used in baseline compiler).
647 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100648
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800649 // Flag whether there are bounds checks in the graph. We can skip
650 // BCE if it's false. It's only best effort to keep it up to date in
651 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400652 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800653
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800654 // Flag whether there are try/catch blocks in the graph. We will skip
655 // try/catch-related passes if it's false. It's only best effort to keep
656 // it up to date in the presence of code elimination so there might be
657 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000658 bool has_try_catch_;
659
Aart Bikb13c65b2017-03-21 20:14:07 -0700660 // Flag whether SIMD instructions appear in the graph. If true, the
661 // code generators may have to be more careful spilling the wider
662 // contents of SIMD registers.
663 bool has_simd_;
664
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800665 // Flag whether there are any loops in the graph. We can skip loop
666 // optimization if it's false. It's only best effort to keep it up
667 // to date in the presence of code elimination so there might be false
668 // positives.
669 bool has_loops_;
670
671 // Flag whether there are any irreducible loops in the graph. It's only
672 // best effort to keep it up to date in the presence of code elimination
673 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000674 bool has_irreducible_loops_;
675
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000676 // Indicates whether the graph should be compiled in a way that
677 // ensures full debuggability. If false, we can apply more
678 // aggressive optimizations that may limit the level of debugging.
679 const bool debuggable_;
680
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000681 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000682 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100684 // The dex file from which the method is from.
685 const DexFile& dex_file_;
686
687 // The method index in the dex file.
688 const uint32_t method_idx_;
689
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100690 // If inlined, this encodes how the callee is being invoked.
691 const InvokeType invoke_type_;
692
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100693 // Whether the graph has been transformed to SSA form. Only used
694 // in debug mode to ensure we are not using properties only valid
695 // for non-SSA form (like the number of temporaries).
696 bool in_ssa_form_;
697
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800698 // Number of CHA guards in the graph. Used to short-circuit the
699 // CHA guard optimization pass when there is no CHA guard left.
700 uint32_t number_of_cha_guards_;
701
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 const InstructionSet instruction_set_;
703
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000704 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000705 HNullConstant* cached_null_constant_;
706 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000707 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000708 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000709 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000710
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100711 HCurrentMethod* cached_current_method_;
712
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100713 // The ArtMethod this graph is for. Note that for AOT, it may be null,
714 // for example for methods whose declaring class could not be resolved
715 // (such as when the superclass could not be found).
716 ArtMethod* art_method_;
717
David Brazdil4833f5a2015-12-16 10:37:39 +0000718 // Keep the RTI of inexact Object to avoid having to pass stack handle
719 // collection pointer to passes which may create NullConstant.
720 ReferenceTypeInfo inexact_object_rti_;
721
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000722 // Whether we are compiling this graph for on stack replacement: this will
723 // make all loops seen as irreducible and emit special stack maps to mark
724 // compiled code entries which the interpreter can directly jump to.
725 const bool osr_;
726
Mingyao Yang063fc772016-08-02 11:02:54 -0700727 // List of methods that are assumed to have single implementation.
728 ArenaSet<ArtMethod*> cha_single_implementation_list_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100731 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000732 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000733 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 DISALLOW_COPY_AND_ASSIGN(HGraph);
735};
736
Vladimir Markof9f64412015-09-02 14:05:49 +0100737class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738 public:
739 HLoopInformation(HBasicBlock* header, HGraph* graph)
740 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100741 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000742 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100743 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100745 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000746 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 back_edges_.reserve(kDefaultNumberOfBackEdges);
748 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000750 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100751 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752
753 void Dump(std::ostream& os);
754
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 HBasicBlock* GetHeader() const {
756 return header_;
757 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 void SetHeader(HBasicBlock* block) {
760 header_ = block;
761 }
762
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
764 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
765 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
766
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100768 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769 }
770
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100772 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 }
774
David Brazdil46e2a392015-03-16 17:31:52 +0000775 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000779 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000781 }
782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100787 }
788
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100789 // Returns the lifetime position of the back edge that has the
790 // greatest lifetime position.
791 size_t GetLifetimeEnd() const;
792
793 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100795 }
796
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000797 // Finds blocks that are part of this loop.
798 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 // Returns whether this loop information contains `block`.
801 // Note that this loop information *must* be populated before entering this function.
802 bool Contains(const HBasicBlock& block) const;
803
804 // Returns whether this loop information is an inner loop of `other`.
805 // Note that `other` *must* be populated before entering this function.
806 bool IsIn(const HLoopInformation& other) const;
807
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800808 // Returns true if instruction is not defined within this loop.
809 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700810
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100811 const ArenaBitVector& GetBlocks() const { return blocks_; }
812
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000813 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000814 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000815
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000816 void ClearAllBlocks() {
817 blocks_.ClearAllBits();
818 }
819
David Brazdil3f4a5222016-05-06 12:46:21 +0100820 bool HasBackEdgeNotDominatedByHeader() const;
821
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100822 bool IsPopulated() const {
823 return blocks_.GetHighestBitSet() != -1;
824 }
825
Anton Shaminf89381f2016-05-16 16:44:13 +0600826 bool DominatesAllBackEdges(HBasicBlock* block);
827
David Sehrc757dec2016-11-04 15:48:34 -0700828 bool HasExitEdge() const;
829
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000830 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 // Internal recursive implementation of `Populate`.
832 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100833 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100836 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000837 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100838 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100839 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000841
842 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
843};
844
David Brazdilec16f792015-08-19 15:04:01 +0100845// Stores try/catch information for basic blocks.
846// Note that HGraph is constructed so that catch blocks cannot simultaneously
847// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100848class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100849 public:
850 // Try block information constructor.
851 explicit TryCatchInformation(const HTryBoundary& try_entry)
852 : try_entry_(&try_entry),
853 catch_dex_file_(nullptr),
854 catch_type_index_(DexFile::kDexNoIndex16) {
855 DCHECK(try_entry_ != nullptr);
856 }
857
858 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800859 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100860 : try_entry_(nullptr),
861 catch_dex_file_(&dex_file),
862 catch_type_index_(catch_type_index) {}
863
864 bool IsTryBlock() const { return try_entry_ != nullptr; }
865
866 const HTryBoundary& GetTryEntry() const {
867 DCHECK(IsTryBlock());
868 return *try_entry_;
869 }
870
871 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
872
873 bool IsCatchAllTypeIndex() const {
874 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800875 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100876 }
877
Andreas Gampea5b09a62016-11-17 15:21:22 -0800878 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100879 DCHECK(IsCatchBlock());
880 return catch_type_index_;
881 }
882
883 const DexFile& GetCatchDexFile() const {
884 DCHECK(IsCatchBlock());
885 return *catch_dex_file_;
886 }
887
888 private:
889 // One of possibly several TryBoundary instructions entering the block's try.
890 // Only set for try blocks.
891 const HTryBoundary* try_entry_;
892
893 // Exception type information. Only set for catch blocks.
894 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800895 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100896};
897
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100898static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901// A block in a method. Contains the list of instructions represented
902// as a double linked list. Each block knows its predecessors and
903// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100904
Vladimir Markof9f64412015-09-02 14:05:49 +0100905class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700907 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
910 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 loop_information_(nullptr),
912 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000913 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100914 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000917 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000918 try_catch_information_(nullptr) {
919 predecessors_.reserve(kDefaultNumberOfPredecessors);
920 successors_.reserve(kDefaultNumberOfSuccessors);
921 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
922 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923
Vladimir Marko60584552015-09-03 13:35:12 +0000924 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 }
927
Vladimir Marko60584552015-09-03 13:35:12 +0000928 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930 }
931
David Brazdild26a4112015-11-10 11:07:31 +0000932 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
933 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
934
Vladimir Marko60584552015-09-03 13:35:12 +0000935 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
936 return ContainsElement(successors_, block, start_from);
937 }
938
939 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100940 return dominated_blocks_;
941 }
942
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100943 bool IsEntryBlock() const {
944 return graph_->GetEntryBlock() == this;
945 }
946
947 bool IsExitBlock() const {
948 return graph_->GetExitBlock() == this;
949 }
950
David Brazdil46e2a392015-03-16 17:31:52 +0000951 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 bool IsSingleTryBoundary() const;
953
954 // Returns true if this block emits nothing but a jump.
955 bool IsSingleJump() const {
956 HLoopInformation* loop_info = GetLoopInformation();
957 return (IsSingleGoto() || IsSingleTryBoundary())
958 // Back edges generate a suspend check.
959 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
960 }
David Brazdil46e2a392015-03-16 17:31:52 +0000961
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000962 void AddBackEdge(HBasicBlock* back_edge) {
963 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000964 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000965 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000967 loop_information_->AddBackEdge(back_edge);
968 }
969
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000971 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100973 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000974 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600975 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000977 HBasicBlock* GetDominator() const { return dominator_; }
978 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000979 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
980
981 void RemoveDominatedBlock(HBasicBlock* block) {
982 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100983 }
Vladimir Marko60584552015-09-03 13:35:12 +0000984
985 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
986 ReplaceElement(dominated_blocks_, existing, new_block);
987 }
988
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100989 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990
991 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100992 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 }
994
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100995 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
996 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100997 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100998 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100999 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1000 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001002 HInstruction* GetFirstInstructionDisregardMoves() const;
1003
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successors_.push_back(block);
1006 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007 }
1008
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001009 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1010 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001012 new_block->predecessors_.push_back(this);
1013 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001014 }
1015
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001016 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1017 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001019 new_block->successors_.push_back(this);
1020 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 }
1022
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001023 // Insert `this` between `predecessor` and `successor. This method
1024 // preserves the indicies, and will update the first edge found between
1025 // `predecessor` and `successor`.
1026 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1027 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001028 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001029 successor->predecessors_[predecessor_index] = this;
1030 predecessor->successors_[successor_index] = this;
1031 successors_.push_back(successor);
1032 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001033 }
1034
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001035 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001036 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001037 }
1038
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001040 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 }
1042
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001043 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001044 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 }
1046
1047 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001048 predecessors_.push_back(block);
1049 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001050 }
1051
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001052 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001053 DCHECK_EQ(predecessors_.size(), 2u);
1054 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001055 }
1056
David Brazdil769c9e52015-04-27 13:54:09 +01001057 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001058 DCHECK_EQ(successors_.size(), 2u);
1059 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001060 }
1061
David Brazdilfc6a86a2015-06-26 10:33:45 +00001062 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001063 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001064 }
1065
David Brazdilfc6a86a2015-06-26 10:33:45 +00001066 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068 }
1069
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001072 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 }
1074
1075 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001076 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001077 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001078 }
1079
1080 // Returns whether the first occurrence of `predecessor` in the list of
1081 // predecessors is at index `idx`.
1082 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001083 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001084 return GetPredecessorIndexOf(predecessor) == idx;
1085 }
1086
David Brazdild7558da2015-09-22 13:04:14 +01001087 // Create a new block between this block and its predecessors. The new block
1088 // is added to the graph, all predecessor edges are relinked to it and an edge
1089 // is created to `this`. Returns the new empty block. Reverse post order or
1090 // loop and try/catch information are not updated.
1091 HBasicBlock* CreateImmediateDominator();
1092
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001094 // created, latter block. Note that this method will add the block to the
1095 // graph, create a Goto at the end of the former block and will create an edge
1096 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001097 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 HBasicBlock* SplitBefore(HInstruction* cursor);
1099
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001100 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001101 // created block. Note that this method just updates raw block information,
1102 // like predecessors, successors, dominators, and instruction list. It does not
1103 // update the graph, reverse post order, loop information, nor make sure the
1104 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001105 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1106
1107 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1108 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109
1110 // Merge `other` at the end of `this`. Successors and dominated blocks of
1111 // `other` are changed to be successors and dominated blocks of `this`. Note
1112 // that this method does not update the graph, reverse post order, loop
1113 // information, nor make sure the blocks are consistent (for example ending
1114 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001115 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116
1117 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1118 // of `this` are moved to `other`.
1119 // Note that this method does not update the graph, reverse post order, loop
1120 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001121 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001122 void ReplaceWith(HBasicBlock* other);
1123
Aart Bik6b69e0a2017-01-11 10:20:43 -08001124 // Merges the instructions of `other` at the end of `this`.
1125 void MergeInstructionsWith(HBasicBlock* other);
1126
David Brazdil2d7352b2015-04-20 14:52:42 +01001127 // Merge `other` at the end of `this`. This method updates loops, reverse post
1128 // order, links to predecessors, successors, dominators and deletes the block
1129 // from the graph. The two blocks must be successive, i.e. `this` the only
1130 // predecessor of `other` and vice versa.
1131 void MergeWith(HBasicBlock* other);
1132
1133 // Disconnects `this` from all its predecessors, successors and dominator,
1134 // removes it from all loops it is included in and eventually from the graph.
1135 // The block must not dominate any other block. Predecessors and successors
1136 // are safely updated.
1137 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001140 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 // Replace instruction `initial` with `replacement` within this block.
1144 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1145 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001147 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001148 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1149 // instruction list. With 'ensure_safety' set to true, it verifies that the
1150 // instruction is not in use and removes it from the use lists of its inputs.
1151 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1152 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001153 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154
1155 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001156 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 }
1158
Roland Levillain6b879dd2014-09-22 17:13:44 +01001159 bool IsLoopPreHeaderFirstPredecessor() const {
1160 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001161 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001162 }
1163
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001164 bool IsFirstPredecessorBackEdge() const {
1165 DCHECK(IsLoopHeader());
1166 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1167 }
1168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169 HLoopInformation* GetLoopInformation() const {
1170 return loop_information_;
1171 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001174 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001176 void SetInLoop(HLoopInformation* info) {
1177 if (IsLoopHeader()) {
1178 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001179 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001180 loop_information_ = info;
1181 } else if (loop_information_->Contains(*info->GetHeader())) {
1182 // Block is currently part of an outer loop. Make it part of this inner loop.
1183 // Note that a non loop header having a loop information means this loop information
1184 // has already been populated
1185 loop_information_ = info;
1186 } else {
1187 // Block is part of an inner loop. Do not update the loop information.
1188 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1189 // at this point, because this method is being called while populating `info`.
1190 }
1191 }
1192
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 // Raw update of the loop information.
1194 void SetLoopInformation(HLoopInformation* info) {
1195 loop_information_ = info;
1196 }
1197
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001198 bool IsInLoop() const { return loop_information_ != nullptr; }
1199
David Brazdilec16f792015-08-19 15:04:01 +01001200 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1201
1202 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1203 try_catch_information_ = try_catch_information;
1204 }
1205
1206 bool IsTryBlock() const {
1207 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1208 }
1209
1210 bool IsCatchBlock() const {
1211 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1212 }
David Brazdilffee3d32015-07-06 11:48:53 +01001213
1214 // Returns the try entry that this block's successors should have. They will
1215 // be in the same try, unless the block ends in a try boundary. In that case,
1216 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001217 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001218
David Brazdild7558da2015-09-22 13:04:14 +01001219 bool HasThrowingInstructions() const;
1220
David Brazdila4b8c212015-05-07 09:59:30 +01001221 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001222 bool Dominates(HBasicBlock* block) const;
1223
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001224 size_t GetLifetimeStart() const { return lifetime_start_; }
1225 size_t GetLifetimeEnd() const { return lifetime_end_; }
1226
1227 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1228 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1229
David Brazdil8d5b8b22015-03-24 10:51:52 +00001230 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001231 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001232 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233 bool HasSinglePhi() const;
1234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001235 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001237 ArenaVector<HBasicBlock*> predecessors_;
1238 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001239 HInstructionList instructions_;
1240 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001241 HLoopInformation* loop_information_;
1242 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001243 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001244 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001245 // The dex program counter of the first instruction of this block.
1246 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001247 size_t lifetime_start_;
1248 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001249 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001251 friend class HGraph;
1252 friend class HInstruction;
1253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001254 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1255};
1256
David Brazdilb2bd1c52015-03-25 11:17:37 +00001257// Iterates over the LoopInformation of all loops which contain 'block'
1258// from the innermost to the outermost.
1259class HLoopInformationOutwardIterator : public ValueObject {
1260 public:
1261 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1262 : current_(block.GetLoopInformation()) {}
1263
1264 bool Done() const { return current_ == nullptr; }
1265
1266 void Advance() {
1267 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001268 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001269 }
1270
1271 HLoopInformation* Current() const {
1272 DCHECK(!Done());
1273 return current_;
1274 }
1275
1276 private:
1277 HLoopInformation* current_;
1278
1279 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1280};
1281
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001283 M(Above, Condition) \
1284 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001286 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001287 M(ArrayGet, Instruction) \
1288 M(ArrayLength, Instruction) \
1289 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001290 M(Below, Condition) \
1291 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001292 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001293 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001294 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001295 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001296 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001297 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001298 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001299 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001300 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001301 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001302 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001303 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001304 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(Exit, Instruction) \
1308 M(FloatConstant, Constant) \
1309 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 M(GreaterThan, Condition) \
1311 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(InstanceFieldGet, Instruction) \
1314 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001315 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001317 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001319 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001321 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001322 M(LessThan, Condition) \
1323 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001324 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001325 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001326 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001327 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001328 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001329 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001330 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001331 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(Neg, UnaryOperation) \
1333 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001335 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001336 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001337 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001338 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001339 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001340 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001344 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001345 M(Return, Instruction) \
1346 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001347 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001348 M(Shl, BinaryOperation) \
1349 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001350 M(StaticFieldGet, Instruction) \
1351 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001352 M(UnresolvedInstanceFieldGet, Instruction) \
1353 M(UnresolvedInstanceFieldSet, Instruction) \
1354 M(UnresolvedStaticFieldGet, Instruction) \
1355 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001356 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001359 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001360 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001361 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001362 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001363 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001364 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001365 M(VecSumReduce, VecUnaryOperation) \
1366 M(VecCnv, VecUnaryOperation) \
1367 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001368 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001369 M(VecNot, VecUnaryOperation) \
1370 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001371 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001372 M(VecSub, VecBinaryOperation) \
1373 M(VecMul, VecBinaryOperation) \
1374 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001375 M(VecMin, VecBinaryOperation) \
1376 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001377 M(VecAnd, VecBinaryOperation) \
1378 M(VecAndNot, VecBinaryOperation) \
1379 M(VecOr, VecBinaryOperation) \
1380 M(VecXor, VecBinaryOperation) \
1381 M(VecShl, VecBinaryOperation) \
1382 M(VecShr, VecBinaryOperation) \
1383 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001384 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001385 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001386 M(VecLoad, VecMemoryOperation) \
1387 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001389/*
1390 * Instructions, shared across several (not all) architectures.
1391 */
1392#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1393#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1394#else
1395#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001396 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001397 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001398 M(MultiplyAccumulate, Instruction) \
Artem Serove1811ed2017-04-27 16:50:47 +01001399 M(IntermediateAddress, Instruction) \
1400 M(IntermediateAddressIndex, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001401#endif
1402
Vladimir Markob4536b72015-11-24 13:45:23 +00001403#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001404#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001405#else
1406#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1407 M(ArmDexCacheArraysBase, Instruction)
1408#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001409
1410#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1411
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001412#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001413#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001414#else
1415#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1416 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001417 M(MipsDexCacheArraysBase, Instruction) \
1418 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001419#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001420
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001421#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1422
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001423#ifndef ART_ENABLE_CODEGEN_x86
1424#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1425#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001426#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1427 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001428 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001429 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001430 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001431#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001432
1433#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1434
1435#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1436 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001437 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001438 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1439 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001440 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001441 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001442 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1443 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1444
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001445#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1446 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001447 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001448 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001449 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001450 M(Invoke, Instruction) \
1451 M(VecOperation, Instruction) \
1452 M(VecUnaryOperation, VecOperation) \
1453 M(VecBinaryOperation, VecOperation) \
1454 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001455
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001456#define FOR_EACH_INSTRUCTION(M) \
1457 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1458 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1459
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001460#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001461FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1462#undef FORWARD_DECLARATION
1463
Vladimir Marko372f10e2016-05-17 16:30:10 +01001464#define DECLARE_INSTRUCTION(type) \
1465 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1466 const char* DebugName() const OVERRIDE { return #type; } \
1467 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1468 return other->Is##type(); \
1469 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001470 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001471
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001472#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1473 bool Is##type() const { return As##type() != nullptr; } \
1474 const H##type* As##type() const { return this; } \
1475 H##type* As##type() { return this; }
1476
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001477template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001478class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001479 public:
David Brazdiled596192015-01-23 10:39:45 +00001480 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001481 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001482 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001483
Vladimir Marko46817b82016-03-29 12:21:58 +01001484 // Hook for the IntrusiveForwardList<>.
1485 // TODO: Hide this better.
1486 IntrusiveForwardListHook hook;
1487
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001488 private:
David Brazdiled596192015-01-23 10:39:45 +00001489 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001490 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001491
1492 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001493 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001494
Vladimir Marko46817b82016-03-29 12:21:58 +01001495 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001496
1497 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1498};
1499
David Brazdiled596192015-01-23 10:39:45 +00001500template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001501using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001502
David Brazdil1abb4192015-02-17 18:33:36 +00001503// This class is used by HEnvironment and HInstruction classes to record the
1504// instructions they use and pointers to the corresponding HUseListNodes kept
1505// by the used instructions.
1506template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001507class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001508 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001509 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1510 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001511
Vladimir Marko46817b82016-03-29 12:21:58 +01001512 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1513 : HUserRecord(old_record.instruction_, before_use_node) {}
1514 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1515 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001516 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001517 }
1518
1519 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001520 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1521 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001522
1523 private:
1524 // Instruction used by the user.
1525 HInstruction* instruction_;
1526
Vladimir Marko46817b82016-03-29 12:21:58 +01001527 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1528 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001529};
1530
Vladimir Markoe9004912016-06-16 16:50:52 +01001531// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1532// This is used for HInstruction::GetInputs() to return a container wrapper providing
1533// HInstruction* values even though the underlying container has HUserRecord<>s.
1534struct HInputExtractor {
1535 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1536 return record.GetInstruction();
1537 }
1538 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1539 return record.GetInstruction();
1540 }
1541};
1542
1543using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1544using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1545
Aart Bik854a02b2015-07-14 16:07:00 -07001546/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001548 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001549 * For write/read dependences on fields/arrays, the dependence analysis uses
1550 * type disambiguation (e.g. a float field write cannot modify the value of an
1551 * integer field read) and the access type (e.g. a reference array write cannot
1552 * modify the value of a reference field read [although it may modify the
1553 * reference fetch prior to reading the field, which is represented by its own
1554 * write/read dependence]). The analysis makes conservative points-to
1555 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1556 * the same, and any reference read depends on any reference read without
1557 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001558 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001559 * The internal representation uses 38-bit and is described in the table below.
1560 * The first line indicates the side effect, and for field/array accesses the
1561 * second line indicates the type of the access (in the order of the
1562 * Primitive::Type enum).
1563 * The two numbered lines below indicate the bit position in the bitfield (read
1564 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001565 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001566 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1567 * +-------------+---------+---------+--------------+---------+---------+
1568 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1569 * | 3 |333333322|222222221| 1 |111111110|000000000|
1570 * | 7 |654321098|765432109| 8 |765432109|876543210|
1571 *
1572 * Note that, to ease the implementation, 'changes' bits are least significant
1573 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001574 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001575class SideEffects : public ValueObject {
1576 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001577 SideEffects() : flags_(0) {}
1578
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001579 static SideEffects None() {
1580 return SideEffects(0);
1581 }
1582
1583 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001584 return SideEffects(kAllChangeBits | kAllDependOnBits);
1585 }
1586
1587 static SideEffects AllChanges() {
1588 return SideEffects(kAllChangeBits);
1589 }
1590
1591 static SideEffects AllDependencies() {
1592 return SideEffects(kAllDependOnBits);
1593 }
1594
1595 static SideEffects AllExceptGCDependency() {
1596 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1597 }
1598
1599 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001600 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001601 }
1602
Aart Bik34c3ba92015-07-20 14:08:59 -07001603 static SideEffects AllWrites() {
1604 return SideEffects(kAllWrites);
1605 }
1606
1607 static SideEffects AllReads() {
1608 return SideEffects(kAllReads);
1609 }
1610
1611 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1612 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001613 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001614 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001615 }
1616
Aart Bik854a02b2015-07-14 16:07:00 -07001617 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001618 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001619 }
1620
Aart Bik34c3ba92015-07-20 14:08:59 -07001621 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1622 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001623 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001624 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001625 }
1626
1627 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001628 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001629 }
1630
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001631 static SideEffects CanTriggerGC() {
1632 return SideEffects(1ULL << kCanTriggerGCBit);
1633 }
1634
1635 static SideEffects DependsOnGC() {
1636 return SideEffects(1ULL << kDependsOnGCBit);
1637 }
1638
Aart Bik854a02b2015-07-14 16:07:00 -07001639 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001640 SideEffects Union(SideEffects other) const {
1641 return SideEffects(flags_ | other.flags_);
1642 }
1643
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 SideEffects Exclusion(SideEffects other) const {
1645 return SideEffects(flags_ & ~other.flags_);
1646 }
1647
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001648 void Add(SideEffects other) {
1649 flags_ |= other.flags_;
1650 }
1651
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001652 bool Includes(SideEffects other) const {
1653 return (other.flags_ & flags_) == other.flags_;
1654 }
1655
1656 bool HasSideEffects() const {
1657 return (flags_ & kAllChangeBits);
1658 }
1659
1660 bool HasDependencies() const {
1661 return (flags_ & kAllDependOnBits);
1662 }
1663
1664 // Returns true if there are no side effects or dependencies.
1665 bool DoesNothing() const {
1666 return flags_ == 0;
1667 }
1668
Aart Bik854a02b2015-07-14 16:07:00 -07001669 // Returns true if something is written.
1670 bool DoesAnyWrite() const {
1671 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001672 }
1673
Aart Bik854a02b2015-07-14 16:07:00 -07001674 // Returns true if something is read.
1675 bool DoesAnyRead() const {
1676 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001677 }
1678
Aart Bik854a02b2015-07-14 16:07:00 -07001679 // Returns true if potentially everything is written and read
1680 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 bool DoesAllReadWrite() const {
1682 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1683 }
1684
Aart Bik854a02b2015-07-14 16:07:00 -07001685 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001686 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001687 }
1688
Roland Levillain0d5a2812015-11-13 10:07:31 +00001689 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001690 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001691 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1692 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001693 }
1694
1695 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001696 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001697 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001698 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001699 for (int s = kLastBit; s >= 0; s--) {
1700 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1701 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1702 // This is a bit for the GC side effect.
1703 if (current_bit_is_set) {
1704 flags += "GC";
1705 }
Aart Bik854a02b2015-07-14 16:07:00 -07001706 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001707 } else {
1708 // This is a bit for the array/field analysis.
1709 // The underscore character stands for the 'can trigger GC' bit.
1710 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1711 if (current_bit_is_set) {
1712 flags += kDebug[s];
1713 }
1714 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1715 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1716 flags += "|";
1717 }
1718 }
Aart Bik854a02b2015-07-14 16:07:00 -07001719 }
1720 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001721 }
1722
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001724
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001725 private:
1726 static constexpr int kFieldArrayAnalysisBits = 9;
1727
1728 static constexpr int kFieldWriteOffset = 0;
1729 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1730 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1731 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1732
1733 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1734
1735 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1736 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1737 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1738 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1739
1740 static constexpr int kLastBit = kDependsOnGCBit;
1741 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1742
1743 // Aliases.
1744
1745 static_assert(kChangeBits == kDependOnBits,
1746 "the 'change' bits should match the 'depend on' bits.");
1747
1748 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1749 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1750 static constexpr uint64_t kAllWrites =
1751 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1752 static constexpr uint64_t kAllReads =
1753 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001754
Aart Bik854a02b2015-07-14 16:07:00 -07001755 // Translates type to bit flag.
1756 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1757 CHECK_NE(type, Primitive::kPrimVoid);
1758 const uint64_t one = 1;
1759 const int shift = type; // 0-based consecutive enum
1760 DCHECK_LE(kFieldWriteOffset, shift);
1761 DCHECK_LT(shift, kArrayWriteOffset);
1762 return one << (type + offset);
1763 }
1764
1765 // Private constructor on direct flags value.
1766 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1767
1768 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001769};
1770
David Brazdiled596192015-01-23 10:39:45 +00001771// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001772class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001773 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001774 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1775 size_t number_of_vregs,
1776 ArtMethod* method,
1777 uint32_t dex_pc,
1778 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001779 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1780 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001781 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001782 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001783 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001784 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001785 }
1786
Mingyao Yang01b47b02017-02-03 12:09:57 -08001787 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001788 : HEnvironment(arena,
1789 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001790 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001791 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001792 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001793
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001794 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001795 if (parent_ != nullptr) {
1796 parent_->SetAndCopyParentChain(allocator, parent);
1797 } else {
1798 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1799 parent_->CopyFrom(parent);
1800 if (parent->GetParent() != nullptr) {
1801 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1802 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001803 }
David Brazdiled596192015-01-23 10:39:45 +00001804 }
1805
Vladimir Marko71bf8092015-09-15 15:33:14 +01001806 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001807 void CopyFrom(HEnvironment* environment);
1808
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001809 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1810 // input to the loop phi instead. This is for inserting instructions that
1811 // require an environment (like HDeoptimization) in the loop pre-header.
1812 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001813
1814 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001815 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001816 }
1817
1818 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001819 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001820 }
1821
David Brazdil1abb4192015-02-17 18:33:36 +00001822 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001823
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001824 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001825
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001826 HEnvironment* GetParent() const { return parent_; }
1827
1828 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001829 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001830 }
1831
1832 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001833 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001834 }
1835
1836 uint32_t GetDexPc() const {
1837 return dex_pc_;
1838 }
1839
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001840 ArtMethod* GetMethod() const {
1841 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001842 }
1843
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001844 HInstruction* GetHolder() const {
1845 return holder_;
1846 }
1847
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001848
1849 bool IsFromInlinedInvoke() const {
1850 return GetParent() != nullptr;
1851 }
1852
David Brazdiled596192015-01-23 10:39:45 +00001853 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001854 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1855 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001856 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001857 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001858 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001859
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001860 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001861 HInstruction* const holder_;
1862
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001863 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001864
1865 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1866};
1867
Vladimir Markof9f64412015-09-02 14:05:49 +01001868class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001869 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001870 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001871 : previous_(nullptr),
1872 next_(nullptr),
1873 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001874 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001875 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001876 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001877 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001878 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001879 locations_(nullptr),
1880 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001881 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001882 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001883 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1884 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1885 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001886
Dave Allison20dfc792014-06-16 20:44:29 -07001887 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001888
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001889#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001890 enum InstructionKind {
1891 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1892 };
1893#undef DECLARE_KIND
1894
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001895 HInstruction* GetNext() const { return next_; }
1896 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001897
Calin Juravle77520bc2015-01-12 18:45:46 +00001898 HInstruction* GetNextDisregardingMoves() const;
1899 HInstruction* GetPreviousDisregardingMoves() const;
1900
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001901 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001902 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001903 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001904 bool IsInBlock() const { return block_ != nullptr; }
1905 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001906 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1907 bool IsIrreducibleLoopHeaderPhi() const {
1908 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1909 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001910
Vladimir Marko372f10e2016-05-17 16:30:10 +01001911 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1912
1913 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1914 // One virtual method is enough, just const_cast<> and then re-add the const.
1915 return ArrayRef<const HUserRecord<HInstruction*>>(
1916 const_cast<HInstruction*>(this)->GetInputRecords());
1917 }
1918
Vladimir Markoe9004912016-06-16 16:50:52 +01001919 HInputsRef GetInputs() {
1920 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001921 }
1922
Vladimir Markoe9004912016-06-16 16:50:52 +01001923 HConstInputsRef GetInputs() const {
1924 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001925 }
1926
1927 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001928 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001929
Aart Bik2767f4b2016-10-28 15:03:53 -07001930 bool HasInput(HInstruction* input) const {
1931 for (const HInstruction* i : GetInputs()) {
1932 if (i == input) {
1933 return true;
1934 }
1935 }
1936 return false;
1937 }
1938
Vladimir Marko372f10e2016-05-17 16:30:10 +01001939 void SetRawInputAt(size_t index, HInstruction* input) {
1940 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1941 }
1942
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001943 virtual void Accept(HGraphVisitor* visitor) = 0;
1944 virtual const char* DebugName() const = 0;
1945
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1947
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001948 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001949
1950 uint32_t GetDexPc() const { return dex_pc_; }
1951
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001952 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001953
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001954 // Can the instruction throw?
1955 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1956 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001957 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001958 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001959
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001960 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001961 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001962
Calin Juravle10e244f2015-01-26 18:54:32 +00001963 // Does not apply for all instructions, but having this at top level greatly
1964 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001965 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001966 virtual bool CanBeNull() const {
1967 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1968 return true;
1969 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001971 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001972 return false;
1973 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001974
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001975 virtual bool IsActualObject() const {
1976 return GetType() == Primitive::kPrimNot;
1977 }
1978
Calin Juravle2e768302015-07-28 14:41:11 +00001979 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001980
Calin Juravle61d544b2015-02-23 16:46:57 +00001981 ReferenceTypeInfo GetReferenceTypeInfo() const {
1982 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001983 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001984 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001985 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001986
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001987 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001988 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001989 // Note: fixup_end remains valid across push_front().
1990 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1991 HUseListNode<HInstruction*>* new_node =
1992 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1993 uses_.push_front(*new_node);
1994 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995 }
1996
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001997 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001998 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001999 // Note: env_fixup_end remains valid across push_front().
2000 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2001 HUseListNode<HEnvironment*>* new_node =
2002 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2003 env_uses_.push_front(*new_node);
2004 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002005 }
2006
David Brazdil1abb4192015-02-17 18:33:36 +00002007 void RemoveAsUserOfInput(size_t input) {
2008 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002009 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2010 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2011 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002012 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002013
Vladimir Marko372f10e2016-05-17 16:30:10 +01002014 void RemoveAsUserOfAllInputs() {
2015 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2016 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2017 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2018 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2019 }
2020 }
2021
David Brazdil1abb4192015-02-17 18:33:36 +00002022 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2023 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002024
Vladimir Marko46817b82016-03-29 12:21:58 +01002025 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2026 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2027 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002028 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002029 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002030 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002031
Aart Bikcc42be02016-10-20 16:14:16 -07002032 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002033 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002034 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002035 !CanThrow() &&
2036 !IsSuspendCheck() &&
2037 !IsControlFlow() &&
2038 !IsNativeDebugInfo() &&
2039 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002040 // If we added an explicit barrier then we should keep it.
2041 !IsMemoryBarrier();
2042 }
2043
Aart Bikcc42be02016-10-20 16:14:16 -07002044 bool IsDeadAndRemovable() const {
2045 return IsRemovable() && !HasUses();
2046 }
2047
Roland Levillain6c82d402014-10-13 16:10:27 +01002048 // Does this instruction strictly dominate `other_instruction`?
2049 // Returns false if this instruction and `other_instruction` are the same.
2050 // Aborts if this instruction and `other_instruction` are both phis.
2051 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002052
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002053 int GetId() const { return id_; }
2054 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002055
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002056 int GetSsaIndex() const { return ssa_index_; }
2057 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2058 bool HasSsaIndex() const { return ssa_index_ != -1; }
2059
2060 bool HasEnvironment() const { return environment_ != nullptr; }
2061 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002062 // Set the `environment_` field. Raw because this method does not
2063 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002064 void SetRawEnvironment(HEnvironment* environment) {
2065 DCHECK(environment_ == nullptr);
2066 DCHECK_EQ(environment->GetHolder(), this);
2067 environment_ = environment;
2068 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002069
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002070 void InsertRawEnvironment(HEnvironment* environment) {
2071 DCHECK(environment_ != nullptr);
2072 DCHECK_EQ(environment->GetHolder(), this);
2073 DCHECK(environment->GetParent() == nullptr);
2074 environment->parent_ = environment_;
2075 environment_ = environment;
2076 }
2077
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002078 void RemoveEnvironment();
2079
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002080 // Set the environment of this instruction, copying it from `environment`. While
2081 // copying, the uses lists are being updated.
2082 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002083 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002084 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002085 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002086 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002087 if (environment->GetParent() != nullptr) {
2088 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2089 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002090 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002091
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002092 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2093 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002094 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002095 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002096 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002097 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002098 if (environment->GetParent() != nullptr) {
2099 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2100 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002101 }
2102
Nicolas Geoffray39468442014-09-02 15:17:15 +01002103 // Returns the number of entries in the environment. Typically, that is the
2104 // number of dex registers in a method. It could be more in case of inlining.
2105 size_t EnvironmentSize() const;
2106
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002107 LocationSummary* GetLocations() const { return locations_; }
2108 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002110 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002111 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002112 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002113
Alexandre Rames188d4312015-04-09 18:30:21 +01002114 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2115 // uses of this instruction by `other` are *not* updated.
2116 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2117 ReplaceWith(other);
2118 other->ReplaceInput(this, use_index);
2119 }
2120
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002121 // Move `this` instruction before `cursor`
2122 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002123
Vladimir Markofb337ea2015-11-25 15:25:10 +00002124 // Move `this` before its first user and out of any loops. If there is no
2125 // out-of-loop user that dominates all other users, move the instruction
2126 // to the end of the out-of-loop common dominator of the user's blocks.
2127 //
2128 // This can be used only on non-throwing instructions with no side effects that
2129 // have at least one use but no environment uses.
2130 void MoveBeforeFirstUserAndOutOfLoops();
2131
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002132#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002133 bool Is##type() const; \
2134 const H##type* As##type() const; \
2135 H##type* As##type();
2136
2137 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2138#undef INSTRUCTION_TYPE_CHECK
2139
2140#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002141 bool Is##type() const { return (As##type() != nullptr); } \
2142 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145#undef INSTRUCTION_TYPE_CHECK
2146
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002147 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002148 // TODO: this method is used by LICM and GVN with possibly different
2149 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002150 virtual bool CanBeMoved() const { return false; }
2151
2152 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002153 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002154 return false;
2155 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002156
2157 // Returns whether any data encoded in the two instructions is equal.
2158 // This method does not look at the inputs. Both instructions must be
2159 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002160 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002161 return false;
2162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163
2164 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002165 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002167 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002168
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002169 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2170 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2171 // the virtual function because the __attribute__((__pure__)) doesn't really
2172 // apply the strong requirement for virtual functions, preventing optimizations.
2173 InstructionKind GetKind() const PURE;
2174 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002175
2176 virtual size_t ComputeHashCode() const {
2177 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002178 for (const HInstruction* input : GetInputs()) {
2179 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002180 }
2181 return result;
2182 }
2183
2184 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002185 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002186 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002187
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002188 size_t GetLifetimePosition() const { return lifetime_position_; }
2189 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2190 LiveInterval* GetLiveInterval() const { return live_interval_; }
2191 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2192 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2193
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002194 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2195
2196 // Returns whether the code generation of the instruction will require to have access
2197 // to the current method. Such instructions are:
2198 // (1): Instructions that require an environment, as calling the runtime requires
2199 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002200 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2201 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002202 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002203 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002204 }
2205
Vladimir Markodc151b22015-10-15 18:02:30 +01002206 // Returns whether the code generation of the instruction will require to have access
2207 // to the dex cache of the current method's declaring class via the current method.
2208 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002209
Mark Mendellc4701932015-04-10 13:18:51 -04002210 // Does this instruction have any use in an environment before
2211 // control flow hits 'other'?
2212 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2213
2214 // Remove all references to environment uses of this instruction.
2215 // The caller must ensure that this is safe to do.
2216 void RemoveEnvironmentUsers();
2217
Vladimir Markoa1de9182016-02-25 11:37:38 +00002218 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2219 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002220
David Brazdil1abb4192015-02-17 18:33:36 +00002221 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002222 // If set, the machine code for this instruction is assumed to be generated by
2223 // its users. Used by liveness analysis to compute use positions accordingly.
2224 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2225 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2226 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2227 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2228
Vladimir Marko372f10e2016-05-17 16:30:10 +01002229 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2230 return GetInputRecords()[i];
2231 }
2232
2233 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2234 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2235 input_records[index] = input;
2236 }
David Brazdil1abb4192015-02-17 18:33:36 +00002237
Vladimir Markoa1de9182016-02-25 11:37:38 +00002238 uint32_t GetPackedFields() const {
2239 return packed_fields_;
2240 }
2241
2242 template <size_t flag>
2243 bool GetPackedFlag() const {
2244 return (packed_fields_ & (1u << flag)) != 0u;
2245 }
2246
2247 template <size_t flag>
2248 void SetPackedFlag(bool value = true) {
2249 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2250 }
2251
2252 template <typename BitFieldType>
2253 typename BitFieldType::value_type GetPackedField() const {
2254 return BitFieldType::Decode(packed_fields_);
2255 }
2256
2257 template <typename BitFieldType>
2258 void SetPackedField(typename BitFieldType::value_type value) {
2259 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2260 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2261 }
2262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002264 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2265 auto before_use_node = uses_.before_begin();
2266 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2267 HInstruction* user = use_node->GetUser();
2268 size_t input_index = use_node->GetIndex();
2269 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2270 before_use_node = use_node;
2271 }
2272 }
2273
2274 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2275 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2276 if (next != uses_.end()) {
2277 HInstruction* next_user = next->GetUser();
2278 size_t next_index = next->GetIndex();
2279 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2280 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2281 }
2282 }
2283
2284 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2285 auto before_env_use_node = env_uses_.before_begin();
2286 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2287 HEnvironment* user = env_use_node->GetUser();
2288 size_t input_index = env_use_node->GetIndex();
2289 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2290 before_env_use_node = env_use_node;
2291 }
2292 }
2293
2294 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2295 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2296 if (next != env_uses_.end()) {
2297 HEnvironment* next_user = next->GetUser();
2298 size_t next_index = next->GetIndex();
2299 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2300 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2301 }
2302 }
David Brazdil1abb4192015-02-17 18:33:36 +00002303
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002304 HInstruction* previous_;
2305 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002306 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002307 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002308
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002309 // An instruction gets an id when it is added to the graph.
2310 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002311 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002312 int id_;
2313
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002314 // When doing liveness analysis, instructions that have uses get an SSA index.
2315 int ssa_index_;
2316
Vladimir Markoa1de9182016-02-25 11:37:38 +00002317 // Packed fields.
2318 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002319
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002320 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002321 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002322
2323 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002324 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002325
Nicolas Geoffray39468442014-09-02 15:17:15 +01002326 // The environment associated with this instruction. Not null if the instruction
2327 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002328 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002329
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002330 // Set by the code generator.
2331 LocationSummary* locations_;
2332
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002333 // Set by the liveness analysis.
2334 LiveInterval* live_interval_;
2335
2336 // Set by the liveness analysis, this is the position in a linear
2337 // order of blocks where this instruction's live interval start.
2338 size_t lifetime_position_;
2339
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002340 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002341
Vladimir Markoa1de9182016-02-25 11:37:38 +00002342 // The reference handle part of the reference type info.
2343 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002344 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002345 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002346
David Brazdil1abb4192015-02-17 18:33:36 +00002347 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002348 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002349 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002351 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002352
2353 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2354};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002355std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002356
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002357// Iterates over the instructions, while preserving the next instruction
2358// in case the current instruction gets removed from the list by the user
2359// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002360class HInstructionIterator : public ValueObject {
2361 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002362 explicit HInstructionIterator(const HInstructionList& instructions)
2363 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002364 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365 }
2366
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002367 bool Done() const { return instruction_ == nullptr; }
2368 HInstruction* Current() const { return instruction_; }
2369 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002371 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002372 }
2373
2374 private:
2375 HInstruction* instruction_;
2376 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002377
2378 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002379};
2380
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002381// Iterates over the instructions without saving the next instruction,
2382// therefore handling changes in the graph potentially made by the user
2383// of this iterator.
2384class HInstructionIteratorHandleChanges : public ValueObject {
2385 public:
2386 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2387 : instruction_(instructions.first_instruction_) {
2388 }
2389
2390 bool Done() const { return instruction_ == nullptr; }
2391 HInstruction* Current() const { return instruction_; }
2392 void Advance() {
2393 instruction_ = instruction_->GetNext();
2394 }
2395
2396 private:
2397 HInstruction* instruction_;
2398
2399 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2400};
2401
2402
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002403class HBackwardInstructionIterator : public ValueObject {
2404 public:
2405 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2406 : instruction_(instructions.last_instruction_) {
2407 next_ = Done() ? nullptr : instruction_->GetPrevious();
2408 }
2409
2410 bool Done() const { return instruction_ == nullptr; }
2411 HInstruction* Current() const { return instruction_; }
2412 void Advance() {
2413 instruction_ = next_;
2414 next_ = Done() ? nullptr : instruction_->GetPrevious();
2415 }
2416
2417 private:
2418 HInstruction* instruction_;
2419 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002420
2421 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002422};
2423
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002424class HVariableInputSizeInstruction : public HInstruction {
2425 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002426 using HInstruction::GetInputRecords; // Keep the const version visible.
2427 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2428 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2429 }
2430
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002431 void AddInput(HInstruction* input);
2432 void InsertInputAt(size_t index, HInstruction* input);
2433 void RemoveInputAt(size_t index);
2434
2435 protected:
2436 HVariableInputSizeInstruction(SideEffects side_effects,
2437 uint32_t dex_pc,
2438 ArenaAllocator* arena,
2439 size_t number_of_inputs,
2440 ArenaAllocKind kind)
2441 : HInstruction(side_effects, dex_pc),
2442 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2443
2444 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2445
2446 private:
2447 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2448};
2449
Vladimir Markof9f64412015-09-02 14:05:49 +01002450template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451class HTemplateInstruction: public HInstruction {
2452 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002453 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002454 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002455 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002456
Vladimir Marko372f10e2016-05-17 16:30:10 +01002457 using HInstruction::GetInputRecords; // Keep the const version visible.
2458 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2459 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002460 }
2461
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002462 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002463 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002464
2465 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002466};
2467
Vladimir Markof9f64412015-09-02 14:05:49 +01002468// HTemplateInstruction specialization for N=0.
2469template<>
2470class HTemplateInstruction<0>: public HInstruction {
2471 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002472 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002473 : HInstruction(side_effects, dex_pc) {}
2474
Vladimir Markof9f64412015-09-02 14:05:49 +01002475 virtual ~HTemplateInstruction() {}
2476
Vladimir Marko372f10e2016-05-17 16:30:10 +01002477 using HInstruction::GetInputRecords; // Keep the const version visible.
2478 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2479 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002480 }
2481
2482 private:
2483 friend class SsaBuilder;
2484};
2485
Dave Allison20dfc792014-06-16 20:44:29 -07002486template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002487class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002488 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002489 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002490 : HTemplateInstruction<N>(side_effects, dex_pc) {
2491 this->template SetPackedField<TypeField>(type);
2492 }
Dave Allison20dfc792014-06-16 20:44:29 -07002493 virtual ~HExpression() {}
2494
Vladimir Markoa1de9182016-02-25 11:37:38 +00002495 Primitive::Type GetType() const OVERRIDE {
2496 return TypeField::Decode(this->GetPackedFields());
2497 }
Dave Allison20dfc792014-06-16 20:44:29 -07002498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002499 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002500 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2501 static constexpr size_t kFieldTypeSize =
2502 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2503 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2504 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2505 "Too many packed fields.");
2506 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002507};
2508
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002509// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2510// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002511class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002513 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2514 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002515
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002516 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002518 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002519
2520 private:
2521 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2522};
2523
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002524// Represents dex's RETURN opcodes. A HReturn is a control flow
2525// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002526class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002528 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2529 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002530 SetRawInputAt(0, value);
2531 }
2532
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002533 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002535 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002536
2537 private:
2538 DISALLOW_COPY_AND_ASSIGN(HReturn);
2539};
2540
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002541class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002542 public:
2543 HPhi(ArenaAllocator* arena,
2544 uint32_t reg_number,
2545 size_t number_of_inputs,
2546 Primitive::Type type,
2547 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002548 : HVariableInputSizeInstruction(
2549 SideEffects::None(),
2550 dex_pc,
2551 arena,
2552 number_of_inputs,
2553 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002554 reg_number_(reg_number) {
2555 SetPackedField<TypeField>(ToPhiType(type));
2556 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2557 // Phis are constructed live and marked dead if conflicting or unused.
2558 // Individual steps of SsaBuilder should assume that if a phi has been
2559 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2560 SetPackedFlag<kFlagIsLive>(true);
2561 SetPackedFlag<kFlagCanBeNull>(true);
2562 }
2563
2564 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2565 static Primitive::Type ToPhiType(Primitive::Type type) {
2566 return Primitive::PrimitiveKind(type);
2567 }
2568
2569 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2570
David Brazdildee58d62016-04-07 09:54:26 +00002571 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2572 void SetType(Primitive::Type new_type) {
2573 // Make sure that only valid type changes occur. The following are allowed:
2574 // (1) int -> float/ref (primitive type propagation),
2575 // (2) long -> double (primitive type propagation).
2576 DCHECK(GetType() == new_type ||
2577 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2578 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2579 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2580 SetPackedField<TypeField>(new_type);
2581 }
2582
2583 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2584 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2585
2586 uint32_t GetRegNumber() const { return reg_number_; }
2587
2588 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2589 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2590 bool IsDead() const { return !IsLive(); }
2591 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2592
Vladimir Markoe9004912016-06-16 16:50:52 +01002593 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002594 return other != nullptr
2595 && other->IsPhi()
2596 && other->AsPhi()->GetBlock() == GetBlock()
2597 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2598 }
2599
2600 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2601 // An equivalent phi is a phi having the same dex register and type.
2602 // It assumes that phis with the same dex register are adjacent.
2603 HPhi* GetNextEquivalentPhiWithSameType() {
2604 HInstruction* next = GetNext();
2605 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2606 if (next->GetType() == GetType()) {
2607 return next->AsPhi();
2608 }
2609 next = next->GetNext();
2610 }
2611 return nullptr;
2612 }
2613
2614 DECLARE_INSTRUCTION(Phi);
2615
David Brazdildee58d62016-04-07 09:54:26 +00002616 private:
2617 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2618 static constexpr size_t kFieldTypeSize =
2619 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2620 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2621 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2622 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2623 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2624 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2625
David Brazdildee58d62016-04-07 09:54:26 +00002626 const uint32_t reg_number_;
2627
2628 DISALLOW_COPY_AND_ASSIGN(HPhi);
2629};
2630
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002631// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002632// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002634class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002635 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002636 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002637
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002638 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002639
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002640 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002641
2642 private:
2643 DISALLOW_COPY_AND_ASSIGN(HExit);
2644};
2645
2646// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002647class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002648 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002649 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002650
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002651 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002652
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002653 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002654 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002655 }
2656
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002657 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002658
2659 private:
2660 DISALLOW_COPY_AND_ASSIGN(HGoto);
2661};
2662
Roland Levillain9867bc72015-08-05 10:21:34 +01002663class HConstant : public HExpression<0> {
2664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002665 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2666 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002667
2668 bool CanBeMoved() const OVERRIDE { return true; }
2669
Roland Levillain1a653882016-03-18 18:05:57 +00002670 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002671 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002672 // Is this constant 0 in the arithmetic sense?
2673 virtual bool IsArithmeticZero() const { return false; }
2674 // Is this constant a 0-bit pattern?
2675 virtual bool IsZeroBitPattern() const { return false; }
2676 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002677 virtual bool IsOne() const { return false; }
2678
David Brazdil77a48ae2015-09-15 12:34:04 +00002679 virtual uint64_t GetValueAsUint64() const = 0;
2680
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002681 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002682
2683 private:
2684 DISALLOW_COPY_AND_ASSIGN(HConstant);
2685};
2686
Vladimir Markofcb503c2016-05-18 12:48:17 +01002687class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002689 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 return true;
2691 }
2692
David Brazdil77a48ae2015-09-15 12:34:04 +00002693 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2694
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 size_t ComputeHashCode() const OVERRIDE { return 0; }
2696
Roland Levillain1a653882016-03-18 18:05:57 +00002697 // The null constant representation is a 0-bit pattern.
2698 virtual bool IsZeroBitPattern() const { return true; }
2699
Roland Levillain9867bc72015-08-05 10:21:34 +01002700 DECLARE_INSTRUCTION(NullConstant);
2701
2702 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002703 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002704
2705 friend class HGraph;
2706 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2707};
2708
2709// Constants of the type int. Those can be from Dex instructions, or
2710// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002711class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 public:
2713 int32_t GetValue() const { return value_; }
2714
David Brazdil9f389d42015-10-01 14:32:56 +01002715 uint64_t GetValueAsUint64() const OVERRIDE {
2716 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2717 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002718
Vladimir Marko372f10e2016-05-17 16:30:10 +01002719 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002720 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002721 return other->AsIntConstant()->value_ == value_;
2722 }
2723
2724 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2725
2726 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002727 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2728 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002729 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2730
Roland Levillain1a653882016-03-18 18:05:57 +00002731 // Integer constants are used to encode Boolean values as well,
2732 // where 1 means true and 0 means false.
2733 bool IsTrue() const { return GetValue() == 1; }
2734 bool IsFalse() const { return GetValue() == 0; }
2735
Roland Levillain9867bc72015-08-05 10:21:34 +01002736 DECLARE_INSTRUCTION(IntConstant);
2737
2738 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002739 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2740 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2741 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2742 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002743
2744 const int32_t value_;
2745
2746 friend class HGraph;
2747 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2748 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2749 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2750};
2751
Vladimir Markofcb503c2016-05-18 12:48:17 +01002752class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002753 public:
2754 int64_t GetValue() const { return value_; }
2755
David Brazdil77a48ae2015-09-15 12:34:04 +00002756 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2757
Vladimir Marko372f10e2016-05-17 16:30:10 +01002758 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002759 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002760 return other->AsLongConstant()->value_ == value_;
2761 }
2762
2763 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2764
2765 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002766 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2767 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002768 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2769
2770 DECLARE_INSTRUCTION(LongConstant);
2771
2772 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002773 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2774 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002775
2776 const int64_t value_;
2777
2778 friend class HGraph;
2779 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2780};
Dave Allison20dfc792014-06-16 20:44:29 -07002781
Vladimir Markofcb503c2016-05-18 12:48:17 +01002782class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002783 public:
2784 float GetValue() const { return value_; }
2785
2786 uint64_t GetValueAsUint64() const OVERRIDE {
2787 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2788 }
2789
Vladimir Marko372f10e2016-05-17 16:30:10 +01002790 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002791 DCHECK(other->IsFloatConstant()) << other->DebugName();
2792 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2793 }
2794
2795 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2796
2797 bool IsMinusOne() const OVERRIDE {
2798 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2799 }
Roland Levillain1a653882016-03-18 18:05:57 +00002800 bool IsArithmeticZero() const OVERRIDE {
2801 return std::fpclassify(value_) == FP_ZERO;
2802 }
2803 bool IsArithmeticPositiveZero() const {
2804 return IsArithmeticZero() && !std::signbit(value_);
2805 }
2806 bool IsArithmeticNegativeZero() const {
2807 return IsArithmeticZero() && std::signbit(value_);
2808 }
2809 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002810 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002811 }
2812 bool IsOne() const OVERRIDE {
2813 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2814 }
2815 bool IsNaN() const {
2816 return std::isnan(value_);
2817 }
2818
2819 DECLARE_INSTRUCTION(FloatConstant);
2820
2821 private:
2822 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2823 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2824 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2825 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2826
2827 const float value_;
2828
2829 // Only the SsaBuilder and HGraph can create floating-point constants.
2830 friend class SsaBuilder;
2831 friend class HGraph;
2832 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2833};
2834
Vladimir Markofcb503c2016-05-18 12:48:17 +01002835class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002836 public:
2837 double GetValue() const { return value_; }
2838
2839 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2840
Vladimir Marko372f10e2016-05-17 16:30:10 +01002841 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002842 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2843 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2844 }
2845
2846 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2847
2848 bool IsMinusOne() const OVERRIDE {
2849 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2850 }
Roland Levillain1a653882016-03-18 18:05:57 +00002851 bool IsArithmeticZero() const OVERRIDE {
2852 return std::fpclassify(value_) == FP_ZERO;
2853 }
2854 bool IsArithmeticPositiveZero() const {
2855 return IsArithmeticZero() && !std::signbit(value_);
2856 }
2857 bool IsArithmeticNegativeZero() const {
2858 return IsArithmeticZero() && std::signbit(value_);
2859 }
2860 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002861 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002862 }
2863 bool IsOne() const OVERRIDE {
2864 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2865 }
2866 bool IsNaN() const {
2867 return std::isnan(value_);
2868 }
2869
2870 DECLARE_INSTRUCTION(DoubleConstant);
2871
2872 private:
2873 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2874 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2875 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2876 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2877
2878 const double value_;
2879
2880 // Only the SsaBuilder and HGraph can create floating-point constants.
2881 friend class SsaBuilder;
2882 friend class HGraph;
2883 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2884};
2885
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002886// Conditional branch. A block ending with an HIf instruction must have
2887// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002888class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002889 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002890 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2891 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002892 SetRawInputAt(0, input);
2893 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002894
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002895 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002896
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002897 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002898 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002899 }
2900
2901 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002902 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002903 }
2904
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002905 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002906
2907 private:
2908 DISALLOW_COPY_AND_ASSIGN(HIf);
2909};
2910
David Brazdilfc6a86a2015-06-26 10:33:45 +00002911
2912// Abstract instruction which marks the beginning and/or end of a try block and
2913// links it to the respective exception handlers. Behaves the same as a Goto in
2914// non-exceptional control flow.
2915// Normal-flow successor is stored at index zero, exception handlers under
2916// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002917class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002918 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002919 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002920 kEntry,
2921 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002922 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002923 };
2924
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002925 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002926 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2927 SetPackedField<BoundaryKindField>(kind);
2928 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
2930 bool IsControlFlow() const OVERRIDE { return true; }
2931
2932 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002933 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002934
David Brazdild26a4112015-11-10 11:07:31 +00002935 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2936 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2937 }
2938
David Brazdilfc6a86a2015-06-26 10:33:45 +00002939 // Returns whether `handler` is among its exception handlers (non-zero index
2940 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002941 bool HasExceptionHandler(const HBasicBlock& handler) const {
2942 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002943 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002944 }
2945
2946 // If not present already, adds `handler` to its block's list of exception
2947 // handlers.
2948 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002949 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002950 GetBlock()->AddSuccessor(handler);
2951 }
2952 }
2953
Vladimir Markoa1de9182016-02-25 11:37:38 +00002954 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2955 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002956
David Brazdilffee3d32015-07-06 11:48:53 +01002957 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2958
David Brazdilfc6a86a2015-06-26 10:33:45 +00002959 DECLARE_INSTRUCTION(TryBoundary);
2960
2961 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002962 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2963 static constexpr size_t kFieldBoundaryKindSize =
2964 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2965 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2966 kFieldBoundaryKind + kFieldBoundaryKindSize;
2967 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2968 "Too many packed fields.");
2969 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002970
2971 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2972};
2973
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002974// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002975class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002976 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002977 enum class Kind {
2978 kBCE,
2979 kInline,
2980 kLast = kInline
2981 };
2982
2983 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2984 // across.
2985 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2986 : HVariableInputSizeInstruction(
2987 SideEffects::All(),
2988 dex_pc,
2989 arena,
2990 /* number_of_inputs */ 1,
2991 kArenaAllocMisc) {
2992 SetPackedFlag<kFieldCanBeMoved>(false);
2993 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002994 SetRawInputAt(0, cond);
2995 }
2996
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002997 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
2998 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
2999 // instead of `guard`.
3000 // We set CanTriggerGC to prevent any intermediate address to be live
3001 // at the point of the `HDeoptimize`.
3002 HDeoptimize(ArenaAllocator* arena,
3003 HInstruction* cond,
3004 HInstruction* guard,
3005 Kind kind,
3006 uint32_t dex_pc)
3007 : HVariableInputSizeInstruction(
3008 SideEffects::CanTriggerGC(),
3009 dex_pc,
3010 arena,
3011 /* number_of_inputs */ 2,
3012 kArenaAllocMisc) {
3013 SetPackedFlag<kFieldCanBeMoved>(true);
3014 SetPackedField<DeoptimizeKindField>(kind);
3015 SetRawInputAt(0, cond);
3016 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003017 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003018
3019 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3020
3021 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3022 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3023 }
3024
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003025 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003026
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003028
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003029 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3030
3031 Primitive::Type GetType() const OVERRIDE {
3032 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3033 }
3034
3035 bool GuardsAnInput() const {
3036 return InputCount() == 2;
3037 }
3038
3039 HInstruction* GuardedInput() const {
3040 DCHECK(GuardsAnInput());
3041 return InputAt(1);
3042 }
3043
3044 void RemoveGuard() {
3045 RemoveInputAt(1);
3046 }
3047
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003048 DECLARE_INSTRUCTION(Deoptimize);
3049
3050 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003051 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3052 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3053 static constexpr size_t kFieldDeoptimizeKindSize =
3054 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3055 static constexpr size_t kNumberOfDeoptimizePackedBits =
3056 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3057 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3058 "Too many packed fields.");
3059 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3060
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003061 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3062};
3063
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003064std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3065
Mingyao Yang063fc772016-08-02 11:02:54 -07003066// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3067// The compiled code checks this flag value in a guard before devirtualized call and
3068// if it's true, starts to do deoptimization.
3069// It has a 4-byte slot on stack.
3070// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003071class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003072 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003073 // CHA guards are only optimized in a separate pass and it has no side effects
3074 // with regard to other passes.
3075 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3076 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003077 }
3078
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003079 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3080
3081 // We do all CHA guard elimination/motion in a single pass, after which there is no
3082 // further guard elimination/motion since a guard might have been used for justification
3083 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3084 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003085 bool CanBeMoved() const OVERRIDE { return false; }
3086
3087 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3088
3089 private:
3090 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3091};
3092
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003093// Represents the ArtMethod that was passed as a first argument to
3094// the method. It is used by instructions that depend on it, like
3095// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003096class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003098 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3099 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003100
3101 DECLARE_INSTRUCTION(CurrentMethod);
3102
3103 private:
3104 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3105};
3106
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003107// Fetches an ArtMethod from the virtual table or the interface method table
3108// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003109class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003110 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003111 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003112 kVTable,
3113 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003114 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003115 };
3116 HClassTableGet(HInstruction* cls,
3117 Primitive::Type type,
3118 TableKind kind,
3119 size_t index,
3120 uint32_t dex_pc)
3121 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003122 index_(index) {
3123 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003124 SetRawInputAt(0, cls);
3125 }
3126
3127 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003128 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003129 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003130 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003131 }
3132
Vladimir Markoa1de9182016-02-25 11:37:38 +00003133 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003134 size_t GetIndex() const { return index_; }
3135
3136 DECLARE_INSTRUCTION(ClassTableGet);
3137
3138 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003139 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3140 static constexpr size_t kFieldTableKindSize =
3141 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3142 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3143 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3144 "Too many packed fields.");
3145 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3146
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003147 // The index of the ArtMethod in the table.
3148 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003149
3150 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3151};
3152
Mark Mendellfe57faa2015-09-18 09:26:15 -04003153// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3154// have one successor for each entry in the switch table, and the final successor
3155// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003156class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003157 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003158 HPackedSwitch(int32_t start_value,
3159 uint32_t num_entries,
3160 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003161 uint32_t dex_pc = kNoDexPc)
3162 : HTemplateInstruction(SideEffects::None(), dex_pc),
3163 start_value_(start_value),
3164 num_entries_(num_entries) {
3165 SetRawInputAt(0, input);
3166 }
3167
3168 bool IsControlFlow() const OVERRIDE { return true; }
3169
3170 int32_t GetStartValue() const { return start_value_; }
3171
Vladimir Marko211c2112015-09-24 16:52:33 +01003172 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003173
3174 HBasicBlock* GetDefaultBlock() const {
3175 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003176 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003177 }
3178 DECLARE_INSTRUCTION(PackedSwitch);
3179
3180 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003181 const int32_t start_value_;
3182 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003183
3184 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3185};
3186
Roland Levillain88cb1752014-10-20 16:36:47 +01003187class HUnaryOperation : public HExpression<1> {
3188 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003189 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3190 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003191 SetRawInputAt(0, input);
3192 }
3193
3194 HInstruction* GetInput() const { return InputAt(0); }
3195 Primitive::Type GetResultType() const { return GetType(); }
3196
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003197 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003198 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003199 return true;
3200 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003201
Roland Levillain31dd3d62016-02-16 12:21:02 +00003202 // Try to statically evaluate `this` and return a HConstant
3203 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003204 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003205 HConstant* TryStaticEvaluation() const;
3206
3207 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003208 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3209 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003210 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3211 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003212
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003213 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003214
3215 private:
3216 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3217};
3218
Dave Allison20dfc792014-06-16 20:44:29 -07003219class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003220 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003221 HBinaryOperation(Primitive::Type result_type,
3222 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003223 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003224 SideEffects side_effects = SideEffects::None(),
3225 uint32_t dex_pc = kNoDexPc)
3226 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003227 SetRawInputAt(0, left);
3228 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003229 }
3230
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003231 HInstruction* GetLeft() const { return InputAt(0); }
3232 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003233 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003234
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003235 virtual bool IsCommutative() const { return false; }
3236
3237 // Put constant on the right.
3238 // Returns whether order is changed.
3239 bool OrderInputsWithConstantOnTheRight() {
3240 HInstruction* left = InputAt(0);
3241 HInstruction* right = InputAt(1);
3242 if (left->IsConstant() && !right->IsConstant()) {
3243 ReplaceInput(right, 0);
3244 ReplaceInput(left, 1);
3245 return true;
3246 }
3247 return false;
3248 }
3249
3250 // Order inputs by instruction id, but favor constant on the right side.
3251 // This helps GVN for commutative ops.
3252 void OrderInputs() {
3253 DCHECK(IsCommutative());
3254 HInstruction* left = InputAt(0);
3255 HInstruction* right = InputAt(1);
3256 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3257 return;
3258 }
3259 if (OrderInputsWithConstantOnTheRight()) {
3260 return;
3261 }
3262 // Order according to instruction id.
3263 if (left->GetId() > right->GetId()) {
3264 ReplaceInput(right, 0);
3265 ReplaceInput(left, 1);
3266 }
3267 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003268
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003269 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003270 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003271 return true;
3272 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003273
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 // Try to statically evaluate `this` and return a HConstant
3275 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003276 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003277 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003278
3279 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003280 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3281 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003282 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3283 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003284 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003285 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3286 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003287 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3288 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003289 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3290 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003291 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003292 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3293 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003294
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003295 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003296 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003297 HConstant* GetConstantRight() const;
3298
3299 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003300 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003301 HInstruction* GetLeastConstantLeft() const;
3302
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003303 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003304
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003305 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003306 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3307};
3308
Mark Mendellc4701932015-04-10 13:18:51 -04003309// The comparison bias applies for floating point operations and indicates how NaN
3310// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003311enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003312 kNoBias, // bias is not applicable (i.e. for long operation)
3313 kGtBias, // return 1 for NaN comparisons
3314 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003315 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003316};
3317
Roland Levillain31dd3d62016-02-16 12:21:02 +00003318std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3319
Dave Allison20dfc792014-06-16 20:44:29 -07003320class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003321 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003322 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003323 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3324 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3325 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003326
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003327 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003328 // `instruction`, and disregard moves in between.
3329 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003330
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003331 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003332
3333 virtual IfCondition GetCondition() const = 0;
3334
Mark Mendellc4701932015-04-10 13:18:51 -04003335 virtual IfCondition GetOppositeCondition() const = 0;
3336
Vladimir Markoa1de9182016-02-25 11:37:38 +00003337 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003338 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3339
Vladimir Markoa1de9182016-02-25 11:37:38 +00003340 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3341 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003342
Vladimir Marko372f10e2016-05-17 16:30:10 +01003343 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003344 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003345 }
3346
Roland Levillain4fa13f62015-07-06 18:11:54 +01003347 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003349 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003350 if (if_cond == kCondNE) {
3351 return true;
3352 } else if (if_cond == kCondEQ) {
3353 return false;
3354 }
3355 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003356 }
3357
3358 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003359 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003360 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003361 if (if_cond == kCondEQ) {
3362 return true;
3363 } else if (if_cond == kCondNE) {
3364 return false;
3365 }
3366 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003367 }
3368
Roland Levillain31dd3d62016-02-16 12:21:02 +00003369 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003370 // Needed if we merge a HCompare into a HCondition.
3371 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3372 static constexpr size_t kFieldComparisonBiasSize =
3373 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3374 static constexpr size_t kNumberOfConditionPackedBits =
3375 kFieldComparisonBias + kFieldComparisonBiasSize;
3376 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3377 using ComparisonBiasField =
3378 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3379
Roland Levillain31dd3d62016-02-16 12:21:02 +00003380 template <typename T>
3381 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3382
3383 template <typename T>
3384 int32_t CompareFP(T x, T y) const {
3385 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3386 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3387 // Handle the bias.
3388 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3389 }
3390
3391 // Return an integer constant containing the result of a condition evaluated at compile time.
3392 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3393 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3394 }
3395
Dave Allison20dfc792014-06-16 20:44:29 -07003396 private:
3397 DISALLOW_COPY_AND_ASSIGN(HCondition);
3398};
3399
3400// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003401class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003402 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003403 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3404 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003405
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003406 bool IsCommutative() const OVERRIDE { return true; }
3407
Vladimir Marko9e23df52015-11-10 17:14:35 +00003408 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3409 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003410 return MakeConstantCondition(true, GetDexPc());
3411 }
3412 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3413 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3414 }
3415 // In the following Evaluate methods, a HCompare instruction has
3416 // been merged into this HEqual instruction; evaluate it as
3417 // `Compare(x, y) == 0`.
3418 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3419 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3420 GetDexPc());
3421 }
3422 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3423 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3424 }
3425 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3426 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003427 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003428
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003429 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003430
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003431 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003432 return kCondEQ;
3433 }
3434
Mark Mendellc4701932015-04-10 13:18:51 -04003435 IfCondition GetOppositeCondition() const OVERRIDE {
3436 return kCondNE;
3437 }
3438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003439 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003440 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003441
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003442 DISALLOW_COPY_AND_ASSIGN(HEqual);
3443};
3444
Vladimir Markofcb503c2016-05-18 12:48:17 +01003445class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003446 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003447 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3448 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003449
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003450 bool IsCommutative() const OVERRIDE { return true; }
3451
Vladimir Marko9e23df52015-11-10 17:14:35 +00003452 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3453 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003454 return MakeConstantCondition(false, GetDexPc());
3455 }
3456 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3457 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3458 }
3459 // In the following Evaluate methods, a HCompare instruction has
3460 // been merged into this HNotEqual instruction; evaluate it as
3461 // `Compare(x, y) != 0`.
3462 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3463 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3464 }
3465 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3466 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3467 }
3468 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3469 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003470 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003471
Dave Allison20dfc792014-06-16 20:44:29 -07003472 DECLARE_INSTRUCTION(NotEqual);
3473
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003474 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003475 return kCondNE;
3476 }
3477
Mark Mendellc4701932015-04-10 13:18:51 -04003478 IfCondition GetOppositeCondition() const OVERRIDE {
3479 return kCondEQ;
3480 }
3481
Dave Allison20dfc792014-06-16 20:44:29 -07003482 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003483 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003484
Dave Allison20dfc792014-06-16 20:44:29 -07003485 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3486};
3487
Vladimir Markofcb503c2016-05-18 12:48:17 +01003488class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003489 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003490 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3491 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003492
Roland Levillain9867bc72015-08-05 10:21:34 +01003493 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003495 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003496 // In the following Evaluate methods, a HCompare instruction has
3497 // been merged into this HLessThan instruction; evaluate it as
3498 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003499 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003500 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3501 }
3502 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3503 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3504 }
3505 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3506 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003507 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003508
Dave Allison20dfc792014-06-16 20:44:29 -07003509 DECLARE_INSTRUCTION(LessThan);
3510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003511 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003512 return kCondLT;
3513 }
3514
Mark Mendellc4701932015-04-10 13:18:51 -04003515 IfCondition GetOppositeCondition() const OVERRIDE {
3516 return kCondGE;
3517 }
3518
Dave Allison20dfc792014-06-16 20:44:29 -07003519 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003520 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003521
Dave Allison20dfc792014-06-16 20:44:29 -07003522 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3523};
3524
Vladimir Markofcb503c2016-05-18 12:48:17 +01003525class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003527 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3528 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003529
Roland Levillain9867bc72015-08-05 10:21:34 +01003530 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003531 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003532 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003533 // In the following Evaluate methods, a HCompare instruction has
3534 // been merged into this HLessThanOrEqual instruction; evaluate it as
3535 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003536 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003537 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3538 }
3539 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3540 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3541 }
3542 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3543 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003544 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003545
Dave Allison20dfc792014-06-16 20:44:29 -07003546 DECLARE_INSTRUCTION(LessThanOrEqual);
3547
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003548 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003549 return kCondLE;
3550 }
3551
Mark Mendellc4701932015-04-10 13:18:51 -04003552 IfCondition GetOppositeCondition() const OVERRIDE {
3553 return kCondGT;
3554 }
3555
Dave Allison20dfc792014-06-16 20:44:29 -07003556 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003557 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003558
Dave Allison20dfc792014-06-16 20:44:29 -07003559 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3560};
3561
Vladimir Markofcb503c2016-05-18 12:48:17 +01003562class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003563 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003564 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3565 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003566
Roland Levillain9867bc72015-08-05 10:21:34 +01003567 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003569 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003570 // In the following Evaluate methods, a HCompare instruction has
3571 // been merged into this HGreaterThan instruction; evaluate it as
3572 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003573 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003574 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3575 }
3576 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3577 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3578 }
3579 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3580 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003581 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003582
Dave Allison20dfc792014-06-16 20:44:29 -07003583 DECLARE_INSTRUCTION(GreaterThan);
3584
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003585 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003586 return kCondGT;
3587 }
3588
Mark Mendellc4701932015-04-10 13:18:51 -04003589 IfCondition GetOppositeCondition() const OVERRIDE {
3590 return kCondLE;
3591 }
3592
Dave Allison20dfc792014-06-16 20:44:29 -07003593 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003594 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003595
Dave Allison20dfc792014-06-16 20:44:29 -07003596 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3597};
3598
Vladimir Markofcb503c2016-05-18 12:48:17 +01003599class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003600 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003601 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3602 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003603
Roland Levillain9867bc72015-08-05 10:21:34 +01003604 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003606 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003607 // In the following Evaluate methods, a HCompare instruction has
3608 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3609 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003611 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3612 }
3613 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3614 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3615 }
3616 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3617 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003618 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003619
Dave Allison20dfc792014-06-16 20:44:29 -07003620 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3621
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003622 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003623 return kCondGE;
3624 }
3625
Mark Mendellc4701932015-04-10 13:18:51 -04003626 IfCondition GetOppositeCondition() const OVERRIDE {
3627 return kCondLT;
3628 }
3629
Dave Allison20dfc792014-06-16 20:44:29 -07003630 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003631 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003632
Dave Allison20dfc792014-06-16 20:44:29 -07003633 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3634};
3635
Vladimir Markofcb503c2016-05-18 12:48:17 +01003636class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003637 public:
3638 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3639 : HCondition(first, second, dex_pc) {}
3640
3641 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003642 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003643 }
3644 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003645 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3646 }
3647 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3648 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3649 LOG(FATAL) << DebugName() << " is not defined for float values";
3650 UNREACHABLE();
3651 }
3652 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3653 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3654 LOG(FATAL) << DebugName() << " is not defined for double values";
3655 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003656 }
3657
3658 DECLARE_INSTRUCTION(Below);
3659
3660 IfCondition GetCondition() const OVERRIDE {
3661 return kCondB;
3662 }
3663
3664 IfCondition GetOppositeCondition() const OVERRIDE {
3665 return kCondAE;
3666 }
3667
3668 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003669 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003670 return MakeUnsigned(x) < MakeUnsigned(y);
3671 }
Aart Bike9f37602015-10-09 11:15:55 -07003672
3673 DISALLOW_COPY_AND_ASSIGN(HBelow);
3674};
3675
Vladimir Markofcb503c2016-05-18 12:48:17 +01003676class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003677 public:
3678 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3679 : HCondition(first, second, dex_pc) {}
3680
3681 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003682 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003683 }
3684 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003685 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3686 }
3687 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3688 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3689 LOG(FATAL) << DebugName() << " is not defined for float values";
3690 UNREACHABLE();
3691 }
3692 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3693 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3694 LOG(FATAL) << DebugName() << " is not defined for double values";
3695 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003696 }
3697
3698 DECLARE_INSTRUCTION(BelowOrEqual);
3699
3700 IfCondition GetCondition() const OVERRIDE {
3701 return kCondBE;
3702 }
3703
3704 IfCondition GetOppositeCondition() const OVERRIDE {
3705 return kCondA;
3706 }
3707
3708 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003709 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003710 return MakeUnsigned(x) <= MakeUnsigned(y);
3711 }
Aart Bike9f37602015-10-09 11:15:55 -07003712
3713 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3714};
3715
Vladimir Markofcb503c2016-05-18 12:48:17 +01003716class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003717 public:
3718 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3719 : HCondition(first, second, dex_pc) {}
3720
3721 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003722 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003723 }
3724 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003725 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3726 }
3727 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3728 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3729 LOG(FATAL) << DebugName() << " is not defined for float values";
3730 UNREACHABLE();
3731 }
3732 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3733 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3734 LOG(FATAL) << DebugName() << " is not defined for double values";
3735 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003736 }
3737
3738 DECLARE_INSTRUCTION(Above);
3739
3740 IfCondition GetCondition() const OVERRIDE {
3741 return kCondA;
3742 }
3743
3744 IfCondition GetOppositeCondition() const OVERRIDE {
3745 return kCondBE;
3746 }
3747
3748 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003749 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003750 return MakeUnsigned(x) > MakeUnsigned(y);
3751 }
Aart Bike9f37602015-10-09 11:15:55 -07003752
3753 DISALLOW_COPY_AND_ASSIGN(HAbove);
3754};
3755
Vladimir Markofcb503c2016-05-18 12:48:17 +01003756class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003757 public:
3758 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3759 : HCondition(first, second, dex_pc) {}
3760
3761 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003762 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003763 }
3764 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003765 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3766 }
3767 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3768 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3769 LOG(FATAL) << DebugName() << " is not defined for float values";
3770 UNREACHABLE();
3771 }
3772 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3773 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3774 LOG(FATAL) << DebugName() << " is not defined for double values";
3775 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003776 }
3777
3778 DECLARE_INSTRUCTION(AboveOrEqual);
3779
3780 IfCondition GetCondition() const OVERRIDE {
3781 return kCondAE;
3782 }
3783
3784 IfCondition GetOppositeCondition() const OVERRIDE {
3785 return kCondB;
3786 }
3787
3788 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003789 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003790 return MakeUnsigned(x) >= MakeUnsigned(y);
3791 }
Aart Bike9f37602015-10-09 11:15:55 -07003792
3793 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3794};
Dave Allison20dfc792014-06-16 20:44:29 -07003795
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003796// Instruction to check how two inputs compare to each other.
3797// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003798class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003799 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003800 // Note that `comparison_type` is the type of comparison performed
3801 // between the comparison's inputs, not the type of the instantiated
3802 // HCompare instruction (which is always Primitive::kPrimInt).
3803 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003804 HInstruction* first,
3805 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003806 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003807 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003808 : HBinaryOperation(Primitive::kPrimInt,
3809 first,
3810 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003811 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003812 dex_pc) {
3813 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003814 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3815 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003816 }
3817
Roland Levillain9867bc72015-08-05 10:21:34 +01003818 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003819 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3820
3821 template <typename T>
3822 int32_t ComputeFP(T x, T y) const {
3823 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3824 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3825 // Handle the bias.
3826 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3827 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003828
Roland Levillain9867bc72015-08-05 10:21:34 +01003829 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003830 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3831 // reach this code path when processing a freshly built HIR
3832 // graph. However HCompare integer instructions can be synthesized
3833 // by the instruction simplifier to implement IntegerCompare and
3834 // IntegerSignum intrinsics, so we have to handle this case.
3835 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003836 }
3837 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003838 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3839 }
3840 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3841 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3842 }
3843 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3844 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003845 }
3846
Vladimir Marko372f10e2016-05-17 16:30:10 +01003847 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003848 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003849 }
3850
Vladimir Markoa1de9182016-02-25 11:37:38 +00003851 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003852
Roland Levillain31dd3d62016-02-16 12:21:02 +00003853 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003854 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003855 bool IsGtBias() const {
3856 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003857 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003858 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003859
Roland Levillain1693a1f2016-03-15 14:57:31 +00003860 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3861 // Comparisons do not require a runtime call in any back end.
3862 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003863 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003864
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003865 DECLARE_INSTRUCTION(Compare);
3866
Roland Levillain31dd3d62016-02-16 12:21:02 +00003867 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003868 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3869 static constexpr size_t kFieldComparisonBiasSize =
3870 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3871 static constexpr size_t kNumberOfComparePackedBits =
3872 kFieldComparisonBias + kFieldComparisonBiasSize;
3873 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3874 using ComparisonBiasField =
3875 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3876
Roland Levillain31dd3d62016-02-16 12:21:02 +00003877 // Return an integer constant containing the result of a comparison evaluated at compile time.
3878 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3879 DCHECK(value == -1 || value == 0 || value == 1) << value;
3880 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3881 }
3882
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003883 private:
3884 DISALLOW_COPY_AND_ASSIGN(HCompare);
3885};
3886
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003887class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003888 public:
3889 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003890 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003891 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003892 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003893 bool finalizable,
3894 QuickEntrypointEnum entrypoint)
3895 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3896 type_index_(type_index),
3897 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003898 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003899 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003900 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003901 }
3902
Andreas Gampea5b09a62016-11-17 15:21:22 -08003903 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003904 const DexFile& GetDexFile() const { return dex_file_; }
3905
3906 // Calls runtime so needs an environment.
3907 bool NeedsEnvironment() const OVERRIDE { return true; }
3908
Mingyao Yang062157f2016-03-02 10:15:36 -08003909 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3910 bool CanThrow() const OVERRIDE { return true; }
3911
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003912 bool NeedsChecks() const {
3913 return entrypoint_ == kQuickAllocObjectWithChecks;
3914 }
David Brazdil6de19382016-01-08 17:37:10 +00003915
Vladimir Markoa1de9182016-02-25 11:37:38 +00003916 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003917
3918 bool CanBeNull() const OVERRIDE { return false; }
3919
3920 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3921
3922 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3923 entrypoint_ = entrypoint;
3924 }
3925
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003926 HLoadClass* GetLoadClass() const {
3927 HInstruction* input = InputAt(0);
3928 if (input->IsClinitCheck()) {
3929 input = input->InputAt(0);
3930 }
3931 DCHECK(input->IsLoadClass());
3932 return input->AsLoadClass();
3933 }
3934
David Brazdil6de19382016-01-08 17:37:10 +00003935 bool IsStringAlloc() const;
3936
3937 DECLARE_INSTRUCTION(NewInstance);
3938
3939 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003940 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003941 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3942 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3943 "Too many packed fields.");
3944
Andreas Gampea5b09a62016-11-17 15:21:22 -08003945 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003946 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003947 QuickEntrypointEnum entrypoint_;
3948
3949 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3950};
3951
Agi Csaki05f20562015-08-19 14:58:14 -07003952enum IntrinsicNeedsEnvironmentOrCache {
3953 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3954 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003955};
3956
Aart Bik5d75afe2015-12-14 11:57:01 -08003957enum IntrinsicSideEffects {
3958 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3959 kReadSideEffects, // Intrinsic may read heap memory.
3960 kWriteSideEffects, // Intrinsic may write heap memory.
3961 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3962};
3963
3964enum IntrinsicExceptions {
3965 kNoThrow, // Intrinsic does not throw any exceptions.
3966 kCanThrow // Intrinsic may throw exceptions.
3967};
3968
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003969class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003970 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003971 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003972
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003973 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003974 SetRawInputAt(index, argument);
3975 }
3976
Roland Levillain3e3d7332015-04-28 11:00:54 +01003977 // Return the number of arguments. This number can be lower than
3978 // the number of inputs returned by InputCount(), as some invoke
3979 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3980 // inputs at the end of their list of inputs.
3981 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3982
Vladimir Markoa1de9182016-02-25 11:37:38 +00003983 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003984
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003985 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3986
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003987 InvokeType GetInvokeType() const {
3988 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003989 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003990
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003991 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003992 return intrinsic_;
3993 }
3994
Aart Bik5d75afe2015-12-14 11:57:01 -08003995 void SetIntrinsic(Intrinsics intrinsic,
3996 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3997 IntrinsicSideEffects side_effects,
3998 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003999
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01004000 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004001 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004002 }
4003
Aart Bikff7d89c2016-11-07 08:49:28 -08004004 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4005
Vladimir Markoa1de9182016-02-25 11:37:38 +00004006 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004007
Aart Bik71bf7b42016-11-16 10:17:46 -08004008 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004009
Vladimir Marko372f10e2016-05-17 16:30:10 +01004010 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004011 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4012 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004013
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004014 uint32_t* GetIntrinsicOptimizations() {
4015 return &intrinsic_optimizations_;
4016 }
4017
4018 const uint32_t* GetIntrinsicOptimizations() const {
4019 return &intrinsic_optimizations_;
4020 }
4021
4022 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4023
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004024 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004025 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004026
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004027 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004028
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004029 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004030 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4031 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004032 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4033 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004034 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 static constexpr size_t kFieldReturnTypeSize =
4036 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4037 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4038 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4039 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004040 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004041 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4042
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004043 HInvoke(ArenaAllocator* arena,
4044 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004045 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004046 Primitive::Type return_type,
4047 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004048 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004049 ArtMethod* resolved_method,
4050 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004051 : HVariableInputSizeInstruction(
4052 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4053 dex_pc,
4054 arena,
4055 number_of_arguments + number_of_other_inputs,
4056 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004057 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004058 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004059 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004060 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004061 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004063 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004064 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004065 }
4066
Roland Levillain3e3d7332015-04-28 11:00:54 +01004067 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004068 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004069 const uint32_t dex_method_index_;
4070 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004071
4072 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4073 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004074
4075 private:
4076 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4077};
4078
Vladimir Markofcb503c2016-05-18 12:48:17 +01004079class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004080 public:
4081 HInvokeUnresolved(ArenaAllocator* arena,
4082 uint32_t number_of_arguments,
4083 Primitive::Type return_type,
4084 uint32_t dex_pc,
4085 uint32_t dex_method_index,
4086 InvokeType invoke_type)
4087 : HInvoke(arena,
4088 number_of_arguments,
4089 0u /* number_of_other_inputs */,
4090 return_type,
4091 dex_pc,
4092 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004093 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004094 invoke_type) {
4095 }
4096
4097 DECLARE_INSTRUCTION(InvokeUnresolved);
4098
4099 private:
4100 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4101};
4102
Orion Hodsonac141392017-01-13 11:53:47 +00004103class HInvokePolymorphic FINAL : public HInvoke {
4104 public:
4105 HInvokePolymorphic(ArenaAllocator* arena,
4106 uint32_t number_of_arguments,
4107 Primitive::Type return_type,
4108 uint32_t dex_pc,
4109 uint32_t dex_method_index)
4110 : HInvoke(arena,
4111 number_of_arguments,
4112 0u /* number_of_other_inputs */,
4113 return_type,
4114 dex_pc,
4115 dex_method_index,
4116 nullptr,
4117 kVirtual) {}
4118
4119 DECLARE_INSTRUCTION(InvokePolymorphic);
4120
4121 private:
4122 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4123};
4124
Vladimir Markofcb503c2016-05-18 12:48:17 +01004125class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004126 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004127 // Requirements of this method call regarding the class
4128 // initialization (clinit) check of its declaring class.
4129 enum class ClinitCheckRequirement {
4130 kNone, // Class already initialized.
4131 kExplicit, // Static call having explicit clinit check as last input.
4132 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004133 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004134 };
4135
Vladimir Marko58155012015-08-19 12:49:41 +00004136 // Determines how to load the target ArtMethod*.
4137 enum class MethodLoadKind {
4138 // Use a String init ArtMethod* loaded from Thread entrypoints.
4139 kStringInit,
4140
4141 // Use the method's own ArtMethod* loaded by the register allocator.
4142 kRecursive,
4143
4144 // Use ArtMethod* at a known address, embed the direct address in the code.
4145 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4146 kDirectAddress,
4147
Vladimir Markoa1de9182016-02-25 11:37:38 +00004148 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004149 // Used when we need to use the dex cache, for example for invoke-static that
4150 // may cause class initialization (the entry may point to a resolution method),
4151 // and we know that we can access the dex cache arrays using a PC-relative load.
4152 kDexCachePcRelative,
4153
4154 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4155 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4156 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4157 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4158 kDexCacheViaMethod,
4159 };
4160
4161 // Determines the location of the code pointer.
4162 enum class CodePtrLocation {
4163 // Recursive call, use local PC-relative call instruction.
4164 kCallSelf,
4165
Vladimir Marko58155012015-08-19 12:49:41 +00004166 // Use code pointer from the ArtMethod*.
4167 // Used when we don't know the target code. This is also the last-resort-kind used when
4168 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4169 kCallArtMethod,
4170 };
4171
4172 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004173 MethodLoadKind method_load_kind;
4174 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004175 // The method load data holds
4176 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4177 // Note that there are multiple string init methods, each having its own offset.
4178 // - the method address for kDirectAddress
4179 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004180 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004181 };
4182
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004183 HInvokeStaticOrDirect(ArenaAllocator* arena,
4184 uint32_t number_of_arguments,
4185 Primitive::Type return_type,
4186 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004187 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004188 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004189 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004190 InvokeType invoke_type,
4191 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004192 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004193 : HInvoke(arena,
4194 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004195 // There is potentially one extra argument for the HCurrentMethod node, and
4196 // potentially one other if the clinit check is explicit, and potentially
4197 // one other if the method is a string factory.
4198 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004199 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004200 return_type,
4201 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004202 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004203 resolved_method,
4204 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004205 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004207 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4208 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004209
Vladimir Markodc151b22015-10-15 18:02:30 +01004210 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004211 bool had_current_method_input = HasCurrentMethodInput();
4212 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4213
4214 // Using the current method is the default and once we find a better
4215 // method load kind, we should not go back to using the current method.
4216 DCHECK(had_current_method_input || !needs_current_method_input);
4217
4218 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004219 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4220 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004221 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004222 dispatch_info_ = dispatch_info;
4223 }
4224
Aart Bik6daebeb2017-04-03 14:35:41 -07004225 DispatchInfo GetDispatchInfo() const {
4226 return dispatch_info_;
4227 }
4228
Vladimir Markoc53c0792015-11-19 15:48:33 +00004229 void AddSpecialInput(HInstruction* input) {
4230 // We allow only one special input.
4231 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4232 DCHECK(InputCount() == GetSpecialInputIndex() ||
4233 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4234 InsertInputAt(GetSpecialInputIndex(), input);
4235 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004236
Vladimir Marko372f10e2016-05-17 16:30:10 +01004237 using HInstruction::GetInputRecords; // Keep the const version visible.
4238 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4239 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4240 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4241 DCHECK(!input_records.empty());
4242 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4243 HInstruction* last_input = input_records.back().GetInstruction();
4244 // Note: `last_input` may be null during arguments setup.
4245 if (last_input != nullptr) {
4246 // `last_input` is the last input of a static invoke marked as having
4247 // an explicit clinit check. It must either be:
4248 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4249 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4250 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4251 }
4252 }
4253 return input_records;
4254 }
4255
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004256 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004257 // We access the method via the dex cache so we can't do an implicit null check.
4258 // TODO: for intrinsics we can generate implicit null checks.
4259 return false;
4260 }
4261
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004262 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004263 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004264 }
4265
Vladimir Markoc53c0792015-11-19 15:48:33 +00004266 // Get the index of the special input, if any.
4267 //
David Brazdil6de19382016-01-08 17:37:10 +00004268 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4269 // method pointer; otherwise there may be one platform-specific special input,
4270 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004271 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004272 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004273
Vladimir Marko58155012015-08-19 12:49:41 +00004274 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4275 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4276 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004277 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004278 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004279 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004280 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4282 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004283 bool HasCurrentMethodInput() const {
4284 // This function can be called only after the invoke has been fully initialized by the builder.
4285 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004286 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004287 return true;
4288 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004289 DCHECK(InputCount() == GetSpecialInputIndex() ||
4290 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004291 return false;
4292 }
4293 }
Vladimir Marko58155012015-08-19 12:49:41 +00004294
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004295 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004296 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004297 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004298 }
4299
4300 uint64_t GetMethodAddress() const {
4301 DCHECK(HasMethodAddress());
4302 return dispatch_info_.method_load_data;
4303 }
4304
4305 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004306 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004307 return dispatch_info_.method_load_data;
4308 }
4309
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004310 const DexFile& GetDexFileForPcRelativeDexCache() const;
4311
Vladimir Markoa1de9182016-02-25 11:37:38 +00004312 ClinitCheckRequirement GetClinitCheckRequirement() const {
4313 return GetPackedField<ClinitCheckRequirementField>();
4314 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004315
Roland Levillain4c0eb422015-04-24 16:43:49 +01004316 // Is this instruction a call to a static method?
4317 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004318 return GetInvokeType() == kStatic;
4319 }
4320
4321 MethodReference GetTargetMethod() const {
4322 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004323 }
4324
Vladimir Markofbb184a2015-11-13 14:47:00 +00004325 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4326 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4327 // instruction; only relevant for static calls with explicit clinit check.
4328 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004329 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004330 size_t last_input_index = inputs_.size() - 1u;
4331 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004333 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004334 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004335 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004336 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004337 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004338 }
4339
4340 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004341 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004342 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004343 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004344 }
4345
4346 // Is this a call to a static method whose declaring class has an
4347 // implicit intialization check requirement?
4348 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004349 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004350 }
4351
Vladimir Markob554b5a2015-11-06 12:57:55 +00004352 // Does this method load kind need the current method as an input?
4353 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4354 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4355 }
4356
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004357 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004358
4359 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004360 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004361 static constexpr size_t kFieldClinitCheckRequirementSize =
4362 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4363 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4364 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4365 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4366 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004367 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4368 kFieldClinitCheckRequirement,
4369 kFieldClinitCheckRequirementSize>;
4370
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004371 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004372 MethodReference target_method_;
4373 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004375 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004376};
Vladimir Markof64242a2015-12-01 14:58:23 +00004377std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004378std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004379
Vladimir Markofcb503c2016-05-18 12:48:17 +01004380class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004381 public:
4382 HInvokeVirtual(ArenaAllocator* arena,
4383 uint32_t number_of_arguments,
4384 Primitive::Type return_type,
4385 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004386 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004388 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004389 : HInvoke(arena,
4390 number_of_arguments,
4391 0u,
4392 return_type,
4393 dex_pc,
4394 dex_method_index,
4395 resolved_method,
4396 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004397 vtable_index_(vtable_index) {}
4398
Aart Bik71bf7b42016-11-16 10:17:46 -08004399 bool CanBeNull() const OVERRIDE {
4400 switch (GetIntrinsic()) {
4401 case Intrinsics::kThreadCurrentThread:
4402 case Intrinsics::kStringBufferAppend:
4403 case Intrinsics::kStringBufferToString:
4404 case Intrinsics::kStringBuilderAppend:
4405 case Intrinsics::kStringBuilderToString:
4406 return false;
4407 default:
4408 return HInvoke::CanBeNull();
4409 }
4410 }
4411
Calin Juravle641547a2015-04-21 22:08:51 +01004412 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004414 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004415 }
4416
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004417 uint32_t GetVTableIndex() const { return vtable_index_; }
4418
4419 DECLARE_INSTRUCTION(InvokeVirtual);
4420
4421 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004422 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004423 const uint32_t vtable_index_;
4424
4425 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4426};
4427
Vladimir Markofcb503c2016-05-18 12:48:17 +01004428class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004429 public:
4430 HInvokeInterface(ArenaAllocator* arena,
4431 uint32_t number_of_arguments,
4432 Primitive::Type return_type,
4433 uint32_t dex_pc,
4434 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004436 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004437 : HInvoke(arena,
4438 number_of_arguments,
4439 0u,
4440 return_type,
4441 dex_pc,
4442 dex_method_index,
4443 resolved_method,
4444 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004445 imt_index_(imt_index) {}
4446
Calin Juravle641547a2015-04-21 22:08:51 +01004447 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004448 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004449 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004450 }
4451
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004452 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4453 // The assembly stub currently needs it.
4454 return true;
4455 }
4456
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004457 uint32_t GetImtIndex() const { return imt_index_; }
4458 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4459
4460 DECLARE_INSTRUCTION(InvokeInterface);
4461
4462 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004463 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004464 const uint32_t imt_index_;
4465
4466 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4467};
4468
Vladimir Markofcb503c2016-05-18 12:48:17 +01004469class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004470 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004471 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004472 : HUnaryOperation(result_type, input, dex_pc) {
4473 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4474 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004475
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004476 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004477
4478 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004479 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004480 }
4481 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004483 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004484 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4485 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4486 }
4487 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4488 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4489 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004490
Roland Levillain88cb1752014-10-20 16:36:47 +01004491 DECLARE_INSTRUCTION(Neg);
4492
4493 private:
4494 DISALLOW_COPY_AND_ASSIGN(HNeg);
4495};
4496
Vladimir Markofcb503c2016-05-18 12:48:17 +01004497class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004498 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004499 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4500 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4501 SetRawInputAt(0, cls);
4502 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004503 }
4504
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004505 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004506 bool NeedsEnvironment() const OVERRIDE { return true; }
4507
Mingyao Yang0c365e62015-03-31 15:09:29 -07004508 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4509 bool CanThrow() const OVERRIDE { return true; }
4510
Calin Juravle10e244f2015-01-26 18:54:32 +00004511 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004512
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004513 HLoadClass* GetLoadClass() const {
4514 DCHECK(InputAt(0)->IsLoadClass());
4515 return InputAt(0)->AsLoadClass();
4516 }
4517
4518 HInstruction* GetLength() const {
4519 return InputAt(1);
4520 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004521
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004522 DECLARE_INSTRUCTION(NewArray);
4523
4524 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004525 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4526};
4527
Vladimir Markofcb503c2016-05-18 12:48:17 +01004528class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004529 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004530 HAdd(Primitive::Type result_type,
4531 HInstruction* left,
4532 HInstruction* right,
4533 uint32_t dex_pc = kNoDexPc)
4534 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004535
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004536 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004537
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004538 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004539
4540 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 return GetBlock()->GetGraph()->GetIntConstant(
4542 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004543 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 return GetBlock()->GetGraph()->GetLongConstant(
4546 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004547 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004548 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4549 return GetBlock()->GetGraph()->GetFloatConstant(
4550 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4551 }
4552 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4553 return GetBlock()->GetGraph()->GetDoubleConstant(
4554 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4555 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004556
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004557 DECLARE_INSTRUCTION(Add);
4558
4559 private:
4560 DISALLOW_COPY_AND_ASSIGN(HAdd);
4561};
4562
Vladimir Markofcb503c2016-05-18 12:48:17 +01004563class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 HSub(Primitive::Type result_type,
4566 HInstruction* left,
4567 HInstruction* right,
4568 uint32_t dex_pc = kNoDexPc)
4569 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004570
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004571 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004572
4573 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004574 return GetBlock()->GetGraph()->GetIntConstant(
4575 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004576 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004577 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004578 return GetBlock()->GetGraph()->GetLongConstant(
4579 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004580 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004581 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4582 return GetBlock()->GetGraph()->GetFloatConstant(
4583 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4584 }
4585 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4586 return GetBlock()->GetGraph()->GetDoubleConstant(
4587 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4588 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004589
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004590 DECLARE_INSTRUCTION(Sub);
4591
4592 private:
4593 DISALLOW_COPY_AND_ASSIGN(HSub);
4594};
4595
Vladimir Markofcb503c2016-05-18 12:48:17 +01004596class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004597 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004598 HMul(Primitive::Type result_type,
4599 HInstruction* left,
4600 HInstruction* right,
4601 uint32_t dex_pc = kNoDexPc)
4602 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004603
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004604 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004605
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004606 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004607
4608 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004609 return GetBlock()->GetGraph()->GetIntConstant(
4610 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004611 }
4612 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004613 return GetBlock()->GetGraph()->GetLongConstant(
4614 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004615 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004616 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4617 return GetBlock()->GetGraph()->GetFloatConstant(
4618 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4619 }
4620 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4621 return GetBlock()->GetGraph()->GetDoubleConstant(
4622 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4623 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004624
4625 DECLARE_INSTRUCTION(Mul);
4626
4627 private:
4628 DISALLOW_COPY_AND_ASSIGN(HMul);
4629};
4630
Vladimir Markofcb503c2016-05-18 12:48:17 +01004631class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004632 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004633 HDiv(Primitive::Type result_type,
4634 HInstruction* left,
4635 HInstruction* right,
4636 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004637 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004638
Roland Levillain9867bc72015-08-05 10:21:34 +01004639 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004640 T ComputeIntegral(T x, T y) const {
4641 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004642 // Our graph structure ensures we never have 0 for `y` during
4643 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004644 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004645 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004646 return (y == -1) ? -x : x / y;
4647 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004648
Roland Levillain31dd3d62016-02-16 12:21:02 +00004649 template <typename T>
4650 T ComputeFP(T x, T y) const {
4651 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4652 return x / y;
4653 }
4654
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004656 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004657 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 }
4659 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004660 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004661 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4662 }
4663 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4664 return GetBlock()->GetGraph()->GetFloatConstant(
4665 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4666 }
4667 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4668 return GetBlock()->GetGraph()->GetDoubleConstant(
4669 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004670 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004671
4672 DECLARE_INSTRUCTION(Div);
4673
4674 private:
4675 DISALLOW_COPY_AND_ASSIGN(HDiv);
4676};
4677
Vladimir Markofcb503c2016-05-18 12:48:17 +01004678class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004679 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004680 HRem(Primitive::Type result_type,
4681 HInstruction* left,
4682 HInstruction* right,
4683 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004684 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004685
Roland Levillain9867bc72015-08-05 10:21:34 +01004686 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004687 T ComputeIntegral(T x, T y) const {
4688 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004689 // Our graph structure ensures we never have 0 for `y` during
4690 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004691 DCHECK_NE(y, 0);
4692 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4693 return (y == -1) ? 0 : x % y;
4694 }
4695
Roland Levillain31dd3d62016-02-16 12:21:02 +00004696 template <typename T>
4697 T ComputeFP(T x, T y) const {
4698 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4699 return std::fmod(x, y);
4700 }
4701
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004703 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004704 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 }
4706 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004707 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004708 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004709 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004710 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4711 return GetBlock()->GetGraph()->GetFloatConstant(
4712 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4713 }
4714 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4715 return GetBlock()->GetGraph()->GetDoubleConstant(
4716 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4717 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004718
4719 DECLARE_INSTRUCTION(Rem);
4720
4721 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004722 DISALLOW_COPY_AND_ASSIGN(HRem);
4723};
4724
Vladimir Markofcb503c2016-05-18 12:48:17 +01004725class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004726 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004727 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4728 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004729 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004730 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004731 SetRawInputAt(0, value);
4732 }
4733
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004734 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4735
Calin Juravled0d48522014-11-04 16:40:20 +00004736 bool CanBeMoved() const OVERRIDE { return true; }
4737
Vladimir Marko372f10e2016-05-17 16:30:10 +01004738 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004739 return true;
4740 }
4741
4742 bool NeedsEnvironment() const OVERRIDE { return true; }
4743 bool CanThrow() const OVERRIDE { return true; }
4744
Calin Juravled0d48522014-11-04 16:40:20 +00004745 DECLARE_INSTRUCTION(DivZeroCheck);
4746
4747 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004748 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4749};
4750
Vladimir Markofcb503c2016-05-18 12:48:17 +01004751class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004752 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004753 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 HInstruction* value,
4755 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004756 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4758 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4759 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004760 }
4761
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004763 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 return value << (distance & max_shift_distance);
4765 }
4766
4767 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004774 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4776 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4777 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4778 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004779 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4781 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 LOG(FATAL) << DebugName() << " is not defined for float values";
4783 UNREACHABLE();
4784 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004785 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4786 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004787 LOG(FATAL) << DebugName() << " is not defined for double values";
4788 UNREACHABLE();
4789 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004790
4791 DECLARE_INSTRUCTION(Shl);
4792
4793 private:
4794 DISALLOW_COPY_AND_ASSIGN(HShl);
4795};
4796
Vladimir Markofcb503c2016-05-18 12:48:17 +01004797class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004798 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004799 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004800 HInstruction* value,
4801 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004802 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4804 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4805 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004806 }
4807
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004809 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004810 return value >> (distance & max_shift_distance);
4811 }
4812
4813 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004817 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4822 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4823 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4824 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004825 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4827 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004828 LOG(FATAL) << DebugName() << " is not defined for float values";
4829 UNREACHABLE();
4830 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004831 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4832 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004833 LOG(FATAL) << DebugName() << " is not defined for double values";
4834 UNREACHABLE();
4835 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004836
4837 DECLARE_INSTRUCTION(Shr);
4838
4839 private:
4840 DISALLOW_COPY_AND_ASSIGN(HShr);
4841};
4842
Vladimir Markofcb503c2016-05-18 12:48:17 +01004843class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004844 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004845 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004846 HInstruction* value,
4847 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004848 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4850 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4851 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004852 }
4853
Roland Levillain5b5b9312016-03-22 14:57:31 +00004854 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004855 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004856 typedef typename std::make_unsigned<T>::type V;
4857 V ux = static_cast<V>(value);
4858 return static_cast<T>(ux >> (distance & max_shift_distance));
4859 }
4860
4861 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004864 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004866 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004868 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004869 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4870 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4871 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4872 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004873 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004874 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4875 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004876 LOG(FATAL) << DebugName() << " is not defined for float values";
4877 UNREACHABLE();
4878 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004879 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4880 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004881 LOG(FATAL) << DebugName() << " is not defined for double values";
4882 UNREACHABLE();
4883 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004884
4885 DECLARE_INSTRUCTION(UShr);
4886
4887 private:
4888 DISALLOW_COPY_AND_ASSIGN(HUShr);
4889};
4890
Vladimir Markofcb503c2016-05-18 12:48:17 +01004891class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004892 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004893 HAnd(Primitive::Type result_type,
4894 HInstruction* left,
4895 HInstruction* right,
4896 uint32_t dex_pc = kNoDexPc)
4897 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004898
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004899 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004900
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004901 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004902
4903 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004904 return GetBlock()->GetGraph()->GetIntConstant(
4905 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004906 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004907 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004908 return GetBlock()->GetGraph()->GetLongConstant(
4909 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004910 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004911 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4912 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4913 LOG(FATAL) << DebugName() << " is not defined for float values";
4914 UNREACHABLE();
4915 }
4916 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4917 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4918 LOG(FATAL) << DebugName() << " is not defined for double values";
4919 UNREACHABLE();
4920 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004921
4922 DECLARE_INSTRUCTION(And);
4923
4924 private:
4925 DISALLOW_COPY_AND_ASSIGN(HAnd);
4926};
4927
Vladimir Markofcb503c2016-05-18 12:48:17 +01004928class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004929 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004930 HOr(Primitive::Type result_type,
4931 HInstruction* left,
4932 HInstruction* right,
4933 uint32_t dex_pc = kNoDexPc)
4934 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004935
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004936 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004937
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004938 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004939
4940 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004941 return GetBlock()->GetGraph()->GetIntConstant(
4942 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004943 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004944 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 return GetBlock()->GetGraph()->GetLongConstant(
4946 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004947 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004948 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4949 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4950 LOG(FATAL) << DebugName() << " is not defined for float values";
4951 UNREACHABLE();
4952 }
4953 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4954 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4955 LOG(FATAL) << DebugName() << " is not defined for double values";
4956 UNREACHABLE();
4957 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004958
4959 DECLARE_INSTRUCTION(Or);
4960
4961 private:
4962 DISALLOW_COPY_AND_ASSIGN(HOr);
4963};
4964
Vladimir Markofcb503c2016-05-18 12:48:17 +01004965class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004966 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004967 HXor(Primitive::Type result_type,
4968 HInstruction* left,
4969 HInstruction* right,
4970 uint32_t dex_pc = kNoDexPc)
4971 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004972
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004973 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004974
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004975 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004976
4977 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004978 return GetBlock()->GetGraph()->GetIntConstant(
4979 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004980 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004981 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004982 return GetBlock()->GetGraph()->GetLongConstant(
4983 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004984 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004985 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4986 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4987 LOG(FATAL) << DebugName() << " is not defined for float values";
4988 UNREACHABLE();
4989 }
4990 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4991 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4992 LOG(FATAL) << DebugName() << " is not defined for double values";
4993 UNREACHABLE();
4994 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004995
4996 DECLARE_INSTRUCTION(Xor);
4997
4998 private:
4999 DISALLOW_COPY_AND_ASSIGN(HXor);
5000};
5001
Vladimir Markofcb503c2016-05-18 12:48:17 +01005002class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005003 public:
5004 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005005 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005006 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5007 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005008 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005009
Roland Levillain5b5b9312016-03-22 14:57:31 +00005010 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005011 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005012 typedef typename std::make_unsigned<T>::type V;
5013 V ux = static_cast<V>(value);
5014 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015 return static_cast<T>(ux);
5016 } else {
5017 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 return static_cast<T>(ux >> (distance & max_shift_value)) |
5019 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005020 }
5021 }
5022
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005024 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005025 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005027 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005028 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005030 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005031 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5032 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5033 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5034 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005035 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005036 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5037 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005038 LOG(FATAL) << DebugName() << " is not defined for float values";
5039 UNREACHABLE();
5040 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005041 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5042 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005043 LOG(FATAL) << DebugName() << " is not defined for double values";
5044 UNREACHABLE();
5045 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005046
5047 DECLARE_INSTRUCTION(Ror);
5048
5049 private:
5050 DISALLOW_COPY_AND_ASSIGN(HRor);
5051};
5052
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005053// The value of a parameter in this method. Its location depends on
5054// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005055class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005056 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005057 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005058 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005059 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005060 Primitive::Type parameter_type,
5061 bool is_this = false)
5062 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005063 dex_file_(dex_file),
5064 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005065 index_(index) {
5066 SetPackedFlag<kFlagIsThis>(is_this);
5067 SetPackedFlag<kFlagCanBeNull>(!is_this);
5068 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005069
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005070 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005071 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072 uint8_t GetIndex() const { return index_; }
Igor Murashkin032cacd2017-04-06 14:40:08 -07005073 bool IsThis() const ATTRIBUTE_UNUSED { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005074
Vladimir Markoa1de9182016-02-25 11:37:38 +00005075 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5076 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005077
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005078 DECLARE_INSTRUCTION(ParameterValue);
5079
5080 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005081 // Whether or not the parameter value corresponds to 'this' argument.
5082 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5083 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5084 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5085 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5086 "Too many packed fields.");
5087
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005088 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005089 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005090 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005091 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005092 const uint8_t index_;
5093
5094 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5095};
5096
Vladimir Markofcb503c2016-05-18 12:48:17 +01005097class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005098 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005099 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5100 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005101
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005102 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005103 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005104 return true;
5105 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005106
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005107 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005108
5109 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005110 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005111 }
5112 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005113 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005114 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005115 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5116 LOG(FATAL) << DebugName() << " is not defined for float values";
5117 UNREACHABLE();
5118 }
5119 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5120 LOG(FATAL) << DebugName() << " is not defined for double values";
5121 UNREACHABLE();
5122 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005123
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005124 DECLARE_INSTRUCTION(Not);
5125
5126 private:
5127 DISALLOW_COPY_AND_ASSIGN(HNot);
5128};
5129
Vladimir Markofcb503c2016-05-18 12:48:17 +01005130class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005131 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005132 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5133 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005134
5135 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005136 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005137 return true;
5138 }
5139
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005140 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005141 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005142 return !x;
5143 }
5144
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005146 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005147 }
5148 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5149 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005150 UNREACHABLE();
5151 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005152 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5153 LOG(FATAL) << DebugName() << " is not defined for float values";
5154 UNREACHABLE();
5155 }
5156 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5157 LOG(FATAL) << DebugName() << " is not defined for double values";
5158 UNREACHABLE();
5159 }
David Brazdil66d126e2015-04-03 16:02:44 +01005160
5161 DECLARE_INSTRUCTION(BooleanNot);
5162
5163 private:
5164 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5165};
5166
Vladimir Markofcb503c2016-05-18 12:48:17 +01005167class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005168 public:
5169 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005170 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005171 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005172 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005173 // Invariant: We should never generate a conversion to a Boolean value.
5174 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005175 }
5176
5177 HInstruction* GetInput() const { return InputAt(0); }
5178 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5179 Primitive::Type GetResultType() const { return GetType(); }
5180
5181 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005182 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5183 return true;
5184 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005185
Mark Mendelle82549b2015-05-06 10:55:34 -04005186 // Try to statically evaluate the conversion and return a HConstant
5187 // containing the result. If the input cannot be converted, return nullptr.
5188 HConstant* TryStaticEvaluation() const;
5189
Roland Levillaindff1f282014-11-05 14:15:05 +00005190 DECLARE_INSTRUCTION(TypeConversion);
5191
5192 private:
5193 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5194};
5195
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005196static constexpr uint32_t kNoRegNumber = -1;
5197
Vladimir Markofcb503c2016-05-18 12:48:17 +01005198class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005199 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005200 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5201 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005203 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005204 SetRawInputAt(0, value);
5205 }
5206
Calin Juravle10e244f2015-01-26 18:54:32 +00005207 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005208 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005209 return true;
5210 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005211
Calin Juravle10e244f2015-01-26 18:54:32 +00005212 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005213
Calin Juravle10e244f2015-01-26 18:54:32 +00005214 bool CanThrow() const OVERRIDE { return true; }
5215
5216 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005217
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005218
5219 DECLARE_INSTRUCTION(NullCheck);
5220
5221 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005222 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5223};
5224
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005225// Embeds an ArtField and all the information required by the compiler. We cache
5226// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005227class FieldInfo : public ValueObject {
5228 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005229 FieldInfo(ArtField* field,
5230 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005231 Primitive::Type field_type,
5232 bool is_volatile,
5233 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005234 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005235 const DexFile& dex_file)
5236 : field_(field),
5237 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005238 field_type_(field_type),
5239 is_volatile_(is_volatile),
5240 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005241 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005242 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005244 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005245 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005246 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005247 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005248 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005249 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005250 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005251
5252 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005253 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005254 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005255 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005256 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005257 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005258 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005259 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005260};
5261
Vladimir Markofcb503c2016-05-18 12:48:17 +01005262class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005263 public:
5264 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005265 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005266 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005267 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005268 bool is_volatile,
5269 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005270 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005271 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005272 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005273 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005274 field_info_(field,
5275 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005276 field_type,
5277 is_volatile,
5278 field_idx,
5279 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005280 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005281 SetRawInputAt(0, value);
5282 }
5283
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005284 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005285
Vladimir Marko372f10e2016-05-17 16:30:10 +01005286 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5287 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005288 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005289 }
5290
Calin Juravle641547a2015-04-21 22:08:51 +01005291 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005292 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005293 }
5294
5295 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005296 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5297 }
5298
Calin Juravle52c48962014-12-16 17:02:57 +00005299 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005300 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005301 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005302 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005303
5304 DECLARE_INSTRUCTION(InstanceFieldGet);
5305
5306 private:
5307 const FieldInfo field_info_;
5308
5309 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5310};
5311
Vladimir Markofcb503c2016-05-18 12:48:17 +01005312class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005313 public:
5314 HInstanceFieldSet(HInstruction* object,
5315 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005316 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005317 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005318 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005319 bool is_volatile,
5320 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005321 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005322 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005323 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005324 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005325 field_info_(field,
5326 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005327 field_type,
5328 is_volatile,
5329 field_idx,
5330 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005331 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005332 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005333 SetRawInputAt(0, object);
5334 SetRawInputAt(1, value);
5335 }
5336
Calin Juravle641547a2015-04-21 22:08:51 +01005337 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005338 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005339 }
5340
Calin Juravle52c48962014-12-16 17:02:57 +00005341 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005342 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005343 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005344 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005345 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005346 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5347 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005348
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005349 DECLARE_INSTRUCTION(InstanceFieldSet);
5350
5351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005352 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5353 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5354 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5355 "Too many packed fields.");
5356
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005357 const FieldInfo field_info_;
5358
5359 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5360};
5361
Vladimir Markofcb503c2016-05-18 12:48:17 +01005362class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005364 HArrayGet(HInstruction* array,
5365 HInstruction* index,
5366 Primitive::Type type,
5367 uint32_t dex_pc,
5368 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005369 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005370 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371 SetRawInputAt(0, array);
5372 SetRawInputAt(1, index);
5373 }
5374
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005375 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005376 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005377 return true;
5378 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005379 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005380 // TODO: We can be smarter here.
Vladimir Marko66d691d2017-04-07 17:53:39 +01005381 // Currently, unless the array is the result of NewArray, the array access is always
5382 // preceded by some form of null NullCheck necessary for the bounds check, usually
5383 // implicit null check on the ArrayLength input to BoundsCheck or Deoptimize for
5384 // dynamic BCE. There are cases when these could be removed to produce better code.
5385 // If we ever add optimizations to do so we should allow an implicit check here
5386 // (as long as the address falls in the first page).
5387 //
5388 // As an example of such fancy optimization, we could eliminate BoundsCheck for
5389 // a = cond ? new int[1] : null;
5390 // a[0]; // The Phi does not need bounds check for either input.
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 return false;
5392 }
5393
David Brazdil4833f5a2015-12-16 10:37:39 +00005394 bool IsEquivalentOf(HArrayGet* other) const {
5395 bool result = (GetDexPc() == other->GetDexPc());
5396 if (kIsDebugBuild && result) {
5397 DCHECK_EQ(GetBlock(), other->GetBlock());
5398 DCHECK_EQ(GetArray(), other->GetArray());
5399 DCHECK_EQ(GetIndex(), other->GetIndex());
5400 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005401 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005402 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005403 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5404 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005405 }
5406 }
5407 return result;
5408 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005409
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005410 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5411
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005412 HInstruction* GetArray() const { return InputAt(0); }
5413 HInstruction* GetIndex() const { return InputAt(1); }
5414
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 DECLARE_INSTRUCTION(ArrayGet);
5416
5417 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005418 // We treat a String as an array, creating the HArrayGet from String.charAt()
5419 // intrinsic in the instruction simplifier. We can always determine whether
5420 // a particular HArrayGet is actually a String.charAt() by looking at the type
5421 // of the input but that requires holding the mutator lock, so we prefer to use
5422 // a flag, so that code generators don't need to do the locking.
5423 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5424 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5425 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5426 "Too many packed fields.");
5427
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5429};
5430
Vladimir Markofcb503c2016-05-18 12:48:17 +01005431class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 public:
5433 HArraySet(HInstruction* array,
5434 HInstruction* index,
5435 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005436 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005437 uint32_t dex_pc)
5438 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005439 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5440 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5441 SetPackedFlag<kFlagValueCanBeNull>(true);
5442 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 SetRawInputAt(0, array);
5444 SetRawInputAt(1, index);
5445 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005446 // Make a best guess now, may be refined during SSA building.
5447 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 }
5449
Calin Juravle77520bc2015-01-12 18:45:46 +00005450 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005451 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005452 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453 }
5454
Mingyao Yang81014cb2015-06-02 03:16:27 -07005455 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005457
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005458 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005459 // TODO: Same as for ArrayGet.
5460 return false;
5461 }
5462
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005463 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005465 }
5466
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005467 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005469 }
5470
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005471 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005472 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005473 }
5474
Vladimir Markoa1de9182016-02-25 11:37:38 +00005475 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5476 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5477 bool StaticTypeOfArrayIsObjectArray() const {
5478 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5479 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005480
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005481 HInstruction* GetArray() const { return InputAt(0); }
5482 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005483 HInstruction* GetValue() const { return InputAt(2); }
5484
5485 Primitive::Type GetComponentType() const {
5486 // The Dex format does not type floating point index operations. Since the
5487 // `expected_component_type_` is set during building and can therefore not
5488 // be correct, we also check what is the value type. If it is a floating
5489 // point type, we must use that type.
5490 Primitive::Type value_type = GetValue()->GetType();
5491 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5492 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005493 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005494 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005495
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005496 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005497 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005498 }
5499
Aart Bik18b36ab2016-04-13 16:41:35 -07005500 void ComputeSideEffects() {
5501 Primitive::Type type = GetComponentType();
5502 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5503 SideEffectsForArchRuntimeCalls(type)));
5504 }
5505
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005506 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5507 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5508 }
5509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005510 DECLARE_INSTRUCTION(ArraySet);
5511
5512 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5514 static constexpr size_t kFieldExpectedComponentTypeSize =
5515 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5516 static constexpr size_t kFlagNeedsTypeCheck =
5517 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5518 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005519 // Cached information for the reference_type_info_ so that codegen
5520 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005521 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5522 static constexpr size_t kNumberOfArraySetPackedBits =
5523 kFlagStaticTypeOfArrayIsObjectArray + 1;
5524 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5525 using ExpectedComponentTypeField =
5526 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527
5528 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5529};
5530
Vladimir Markofcb503c2016-05-18 12:48:17 +01005531class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005532 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005533 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005534 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005535 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005536 // Note that arrays do not change length, so the instruction does not
5537 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005538 SetRawInputAt(0, array);
5539 }
5540
Calin Juravle77520bc2015-01-12 18:45:46 +00005541 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005542 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005543 return true;
5544 }
Calin Juravle641547a2015-04-21 22:08:51 +01005545 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5546 return obj == InputAt(0);
5547 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005548
Vladimir Markodce016e2016-04-28 13:10:02 +01005549 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5550
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005551 DECLARE_INSTRUCTION(ArrayLength);
5552
5553 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005554 // We treat a String as an array, creating the HArrayLength from String.length()
5555 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5556 // determine whether a particular HArrayLength is actually a String.length() by
5557 // looking at the type of the input but that requires holding the mutator lock, so
5558 // we prefer to use a flag, so that code generators don't need to do the locking.
5559 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5560 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5561 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5562 "Too many packed fields.");
5563
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005564 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5565};
5566
Vladimir Markofcb503c2016-05-18 12:48:17 +01005567class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005568 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005569 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5570 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005571 HBoundsCheck(HInstruction* index,
5572 HInstruction* length,
5573 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005574 bool string_char_at = false)
5575 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005576 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005577 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578 SetRawInputAt(0, index);
5579 SetRawInputAt(1, length);
5580 }
5581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005582 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005583 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005584 return true;
5585 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005586
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005587 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005588
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005589 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005590
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005591 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005592
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005593 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594
5595 DECLARE_INSTRUCTION(BoundsCheck);
5596
5597 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005598 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005599
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5601};
5602
Vladimir Markofcb503c2016-05-18 12:48:17 +01005603class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005604 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005605 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005606 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005607
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005608 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005609 return true;
5610 }
5611
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005612 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5613 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005614
5615 DECLARE_INSTRUCTION(SuspendCheck);
5616
5617 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005618 // Only used for code generation, in order to share the same slow path between back edges
5619 // of a same loop.
5620 SlowPathCode* slow_path_;
5621
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005622 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5623};
5624
David Srbecky0cf44932015-12-09 14:09:59 +00005625// Pseudo-instruction which provides the native debugger with mapping information.
5626// It ensures that we can generate line number and local variables at this point.
5627class HNativeDebugInfo : public HTemplateInstruction<0> {
5628 public:
5629 explicit HNativeDebugInfo(uint32_t dex_pc)
5630 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5631
5632 bool NeedsEnvironment() const OVERRIDE {
5633 return true;
5634 }
5635
5636 DECLARE_INSTRUCTION(NativeDebugInfo);
5637
5638 private:
5639 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5640};
5641
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005642/**
5643 * Instruction to load a Class object.
5644 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005645class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005646 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005647 // Determines how to load the Class.
5648 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005649 // We cannot load this class. See HSharpening::SharpenLoadClass.
5650 kInvalid = -1,
5651
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005652 // Use the Class* from the method's own ArtMethod*.
5653 kReferrersClass,
5654
5655 // Use boot image Class* address that will be known at link time.
5656 // Used for boot image classes referenced by boot image code in non-PIC mode.
5657 kBootImageLinkTimeAddress,
5658
5659 // Use PC-relative boot image Class* address that will be known at link time.
5660 // Used for boot image classes referenced by boot image code in PIC mode.
5661 kBootImageLinkTimePcRelative,
5662
5663 // Use a known boot image Class* address, embedded in the code by the codegen.
5664 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 kBootImageAddress,
5666
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005667 // Load from an entry in the .bss section using a PC-relative load.
5668 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5669 kBssEntry,
5670
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005671 // Load from the root table associated with the JIT compiled method.
5672 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005673
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005674 // Load from resolved types array accessed through the class loaded from
5675 // the compiled method's own ArtMethod*. This is the default access type when
5676 // all other types are unavailable.
5677 kDexCacheViaMethod,
5678
5679 kLast = kDexCacheViaMethod
5680 };
5681
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005682 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005683 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005684 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005685 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005686 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005687 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005688 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005689 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5690 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005691 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005692 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005693 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005694 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005695 // Referrers class should not need access check. We never inline unverified
5696 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005697 DCHECK(!is_referrers_class || !needs_access_check);
5698
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005699 SetPackedField<LoadKindField>(
5700 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005701 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005702 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005703 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005704 }
5705
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005706 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708 LoadKind GetLoadKind() const {
5709 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005710 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005711
5712 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005713
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005714 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715
Andreas Gampea5b09a62016-11-17 15:21:22 -08005716 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005717
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005718 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005720 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005721 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722 }
5723
Calin Juravle0ba218d2015-05-19 18:46:01 +01005724 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005725 // The entrypoint the code generator is going to call does not do
5726 // clinit of the class.
5727 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005728 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005729 }
5730
5731 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005732 return NeedsAccessCheck() ||
5733 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005734 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5735 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005736 }
5737
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005738 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005739 return NeedsAccessCheck() ||
5740 MustGenerateClinitCheck() ||
5741 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5742 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5743 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005744 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5745 GetLoadKind() == LoadKind::kBssEntry) &&
5746 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005747 }
5748
Calin Juravleacf735c2015-02-12 15:25:22 +00005749 ReferenceTypeInfo GetLoadedClassRTI() {
5750 return loaded_class_rti_;
5751 }
5752
5753 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5754 // Make sure we only set exact types (the loaded class should never be merged).
5755 DCHECK(rti.IsExact());
5756 loaded_class_rti_ = rti;
5757 }
5758
Andreas Gampea5b09a62016-11-17 15:21:22 -08005759 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005760 const DexFile& GetDexFile() const { return dex_file_; }
5761
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005762 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005763 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005764 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005765
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005766 static SideEffects SideEffectsForArchRuntimeCalls() {
5767 return SideEffects::CanTriggerGC();
5768 }
5769
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005770 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005771 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005772 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005773 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005774
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005775 void MarkInBootImage() {
5776 SetPackedFlag<kFlagIsInBootImage>(true);
5777 }
5778
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005779 void AddSpecialInput(HInstruction* special_input);
5780
5781 using HInstruction::GetInputRecords; // Keep the const version visible.
5782 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5783 return ArrayRef<HUserRecord<HInstruction*>>(
5784 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5785 }
5786
5787 Primitive::Type GetType() const OVERRIDE {
5788 return Primitive::kPrimNot;
5789 }
5790
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005791 Handle<mirror::Class> GetClass() const {
5792 return klass_;
5793 }
5794
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005795 DECLARE_INSTRUCTION(LoadClass);
5796
5797 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005798 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005799 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005800 // Whether this instruction must generate the initialization check.
5801 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005802 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005803 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5804 static constexpr size_t kFieldLoadKindSize =
5805 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5806 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005807 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5809
5810 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005811 return load_kind == LoadKind::kReferrersClass ||
5812 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005813 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005814 load_kind == LoadKind::kBssEntry ||
5815 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 }
5817
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 void SetLoadKindInternal(LoadKind load_kind);
5819
5820 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5821 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005822 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005824
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005825 // A type index and dex file where the class can be accessed. The dex file can be:
5826 // - The compiling method's dex file if the class is defined there too.
5827 // - The compiling method's dex file if the class is referenced there.
5828 // - The dex file where the class is defined. When the load kind can only be
5829 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005830 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005831 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005832
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005833 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834
Calin Juravleacf735c2015-02-12 15:25:22 +00005835 ReferenceTypeInfo loaded_class_rti_;
5836
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005837 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5838};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005839std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5840
5841// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005842inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005843 // The special input is used for PC-relative loads on some architectures,
5844 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005846 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005847 GetLoadKind() == LoadKind::kBootImageAddress ||
5848 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849 DCHECK(special_input_.GetInstruction() == nullptr);
5850 special_input_ = HUserRecord<HInstruction*>(special_input);
5851 special_input->AddUseAt(this, 0);
5852}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005853
Vladimir Marko372f10e2016-05-17 16:30:10 +01005854class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005855 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 // Determines how to load the String.
5857 enum class LoadKind {
5858 // Use boot image String* address that will be known at link time.
5859 // Used for boot image strings referenced by boot image code in non-PIC mode.
5860 kBootImageLinkTimeAddress,
5861
5862 // Use PC-relative boot image String* address that will be known at link time.
5863 // Used for boot image strings referenced by boot image code in PIC mode.
5864 kBootImageLinkTimePcRelative,
5865
5866 // Use a known boot image String* address, embedded in the code by the codegen.
5867 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005868 kBootImageAddress,
5869
Vladimir Markoaad75c62016-10-03 08:46:48 +00005870 // Load from an entry in the .bss section using a PC-relative load.
5871 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5872 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005873
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005874 // Load from the root table associated with the JIT compiled method.
5875 kJitTableAddress,
5876
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005877 // Load from resolved strings array accessed through the class loaded from
5878 // the compiled method's own ArtMethod*. This is the default access type when
5879 // all other types are unavailable.
5880 kDexCacheViaMethod,
5881
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005882 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005883 };
5884
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005885 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005886 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005887 const DexFile& dex_file,
5888 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005889 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5890 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005891 string_index_(string_index),
5892 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005894 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005895
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005896 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898 LoadKind GetLoadKind() const {
5899 return GetPackedField<LoadKindField>();
5900 }
5901
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 const DexFile& GetDexFile() const {
5903 return dex_file_;
5904 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005905
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005906 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005907 return string_index_;
5908 }
5909
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005910 Handle<mirror::String> GetString() const {
5911 return string_;
5912 }
5913
5914 void SetString(Handle<mirror::String> str) {
5915 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 }
5917
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005918 bool CanBeMoved() const OVERRIDE { return true; }
5919
Vladimir Marko372f10e2016-05-17 16:30:10 +01005920 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005921
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005922 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005923
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 // Will call the runtime if we need to load the string through
5925 // the dex cache and the string is not guaranteed to be there yet.
5926 bool NeedsEnvironment() const OVERRIDE {
5927 LoadKind load_kind = GetLoadKind();
5928 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5929 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005930 load_kind == LoadKind::kBootImageAddress ||
5931 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 return false;
5933 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005934 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005936
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005937 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5938 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5939 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005940
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005941 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005943
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005944 static SideEffects SideEffectsForArchRuntimeCalls() {
5945 return SideEffects::CanTriggerGC();
5946 }
5947
Vladimir Marko372f10e2016-05-17 16:30:10 +01005948 void AddSpecialInput(HInstruction* special_input);
5949
5950 using HInstruction::GetInputRecords; // Keep the const version visible.
5951 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5952 return ArrayRef<HUserRecord<HInstruction*>>(
5953 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954 }
5955
Vladimir Marko372f10e2016-05-17 16:30:10 +01005956 Primitive::Type GetType() const OVERRIDE {
5957 return Primitive::kPrimNot;
5958 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005960 DECLARE_INSTRUCTION(LoadString);
5961
5962 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005963 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005964 static constexpr size_t kFieldLoadKindSize =
5965 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5966 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005967 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005968 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005969
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970 void SetLoadKindInternal(LoadKind load_kind);
5971
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005972 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5973 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005974 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005975 HUserRecord<HInstruction*> special_input_;
5976
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005977 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005978 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005980 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005981
5982 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5983};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005984std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5985
5986// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005988 // The special input is used for PC-relative loads on some architectures,
5989 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005991 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005992 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5993 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005994 // HLoadString::GetInputRecords() returns an empty array at this point,
5995 // so use the GetInputRecords() from the base class to set the input record.
5996 DCHECK(special_input_.GetInstruction() == nullptr);
5997 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 special_input->AddUseAt(this, 0);
5999}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001/**
6002 * Performs an initialization check on its Class object input.
6003 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01006004class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006005 public:
Roland Levillain3887c462015-08-12 18:15:42 +01006006 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006007 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006008 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006009 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6010 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 SetRawInputAt(0, constant);
6012 }
6013
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006014 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006015 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006016 return true;
6017 }
6018
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019 bool NeedsEnvironment() const OVERRIDE {
6020 // May call runtime to initialize the class.
6021 return true;
6022 }
6023
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006024 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006025
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006026 HLoadClass* GetLoadClass() const {
6027 DCHECK(InputAt(0)->IsLoadClass());
6028 return InputAt(0)->AsLoadClass();
6029 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030
6031 DECLARE_INSTRUCTION(ClinitCheck);
6032
6033 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006034 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6035};
6036
Vladimir Markofcb503c2016-05-18 12:48:17 +01006037class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006038 public:
6039 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006040 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006041 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006042 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006043 bool is_volatile,
6044 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006045 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006046 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006047 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006048 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006049 field_info_(field,
6050 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006051 field_type,
6052 is_volatile,
6053 field_idx,
6054 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006055 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006056 SetRawInputAt(0, cls);
6057 }
6058
Calin Juravle52c48962014-12-16 17:02:57 +00006059
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006060 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006061
Vladimir Marko372f10e2016-05-17 16:30:10 +01006062 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6063 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006064 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065 }
6066
6067 size_t ComputeHashCode() const OVERRIDE {
6068 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6069 }
6070
Calin Juravle52c48962014-12-16 17:02:57 +00006071 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006072 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6073 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006074 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075
6076 DECLARE_INSTRUCTION(StaticFieldGet);
6077
6078 private:
6079 const FieldInfo field_info_;
6080
6081 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6082};
6083
Vladimir Markofcb503c2016-05-18 12:48:17 +01006084class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006085 public:
6086 HStaticFieldSet(HInstruction* cls,
6087 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006088 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006089 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006090 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006091 bool is_volatile,
6092 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006093 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006094 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006095 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006096 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006097 field_info_(field,
6098 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006099 field_type,
6100 is_volatile,
6101 field_idx,
6102 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006103 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105 SetRawInputAt(0, cls);
6106 SetRawInputAt(1, value);
6107 }
6108
Calin Juravle52c48962014-12-16 17:02:57 +00006109 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6111 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006112 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006113
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006114 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006115 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6116 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006118 DECLARE_INSTRUCTION(StaticFieldSet);
6119
6120 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006121 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6122 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6123 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6124 "Too many packed fields.");
6125
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 const FieldInfo field_info_;
6127
6128 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6129};
6130
Vladimir Markofcb503c2016-05-18 12:48:17 +01006131class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006132 public:
6133 HUnresolvedInstanceFieldGet(HInstruction* obj,
6134 Primitive::Type field_type,
6135 uint32_t field_index,
6136 uint32_t dex_pc)
6137 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6138 field_index_(field_index) {
6139 SetRawInputAt(0, obj);
6140 }
6141
6142 bool NeedsEnvironment() const OVERRIDE { return true; }
6143 bool CanThrow() const OVERRIDE { return true; }
6144
6145 Primitive::Type GetFieldType() const { return GetType(); }
6146 uint32_t GetFieldIndex() const { return field_index_; }
6147
6148 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6149
6150 private:
6151 const uint32_t field_index_;
6152
6153 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6154};
6155
Vladimir Markofcb503c2016-05-18 12:48:17 +01006156class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006157 public:
6158 HUnresolvedInstanceFieldSet(HInstruction* obj,
6159 HInstruction* value,
6160 Primitive::Type field_type,
6161 uint32_t field_index,
6162 uint32_t dex_pc)
6163 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006165 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006166 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 SetRawInputAt(0, obj);
6168 SetRawInputAt(1, value);
6169 }
6170
6171 bool NeedsEnvironment() const OVERRIDE { return true; }
6172 bool CanThrow() const OVERRIDE { return true; }
6173
Vladimir Markoa1de9182016-02-25 11:37:38 +00006174 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006175 uint32_t GetFieldIndex() const { return field_index_; }
6176
6177 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6178
6179 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006180 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6181 static constexpr size_t kFieldFieldTypeSize =
6182 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6183 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6184 kFieldFieldType + kFieldFieldTypeSize;
6185 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6186 "Too many packed fields.");
6187 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6188
Calin Juravlee460d1d2015-09-29 04:52:17 +01006189 const uint32_t field_index_;
6190
6191 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6192};
6193
Vladimir Markofcb503c2016-05-18 12:48:17 +01006194class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006195 public:
6196 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6197 uint32_t field_index,
6198 uint32_t dex_pc)
6199 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6200 field_index_(field_index) {
6201 }
6202
6203 bool NeedsEnvironment() const OVERRIDE { return true; }
6204 bool CanThrow() const OVERRIDE { return true; }
6205
6206 Primitive::Type GetFieldType() const { return GetType(); }
6207 uint32_t GetFieldIndex() const { return field_index_; }
6208
6209 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6210
6211 private:
6212 const uint32_t field_index_;
6213
6214 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6215};
6216
Vladimir Markofcb503c2016-05-18 12:48:17 +01006217class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006218 public:
6219 HUnresolvedStaticFieldSet(HInstruction* value,
6220 Primitive::Type field_type,
6221 uint32_t field_index,
6222 uint32_t dex_pc)
6223 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006224 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006226 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006227 SetRawInputAt(0, value);
6228 }
6229
6230 bool NeedsEnvironment() const OVERRIDE { return true; }
6231 bool CanThrow() const OVERRIDE { return true; }
6232
Vladimir Markoa1de9182016-02-25 11:37:38 +00006233 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006234 uint32_t GetFieldIndex() const { return field_index_; }
6235
6236 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6237
6238 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006239 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6240 static constexpr size_t kFieldFieldTypeSize =
6241 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6242 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6243 kFieldFieldType + kFieldFieldTypeSize;
6244 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6245 "Too many packed fields.");
6246 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6247
Calin Juravlee460d1d2015-09-29 04:52:17 +01006248 const uint32_t field_index_;
6249
6250 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6251};
6252
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006253// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006254class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006255 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006256 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6257 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006258
David Brazdilbbd733e2015-08-18 17:48:17 +01006259 bool CanBeNull() const OVERRIDE { return false; }
6260
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006261 DECLARE_INSTRUCTION(LoadException);
6262
6263 private:
6264 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6265};
6266
David Brazdilcb1c0552015-08-04 16:22:25 +01006267// Implicit part of move-exception which clears thread-local exception storage.
6268// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006269class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006270 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006271 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6272 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006273
6274 DECLARE_INSTRUCTION(ClearException);
6275
6276 private:
6277 DISALLOW_COPY_AND_ASSIGN(HClearException);
6278};
6279
Vladimir Markofcb503c2016-05-18 12:48:17 +01006280class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006281 public:
6282 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006283 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006284 SetRawInputAt(0, exception);
6285 }
6286
6287 bool IsControlFlow() const OVERRIDE { return true; }
6288
6289 bool NeedsEnvironment() const OVERRIDE { return true; }
6290
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006291 bool CanThrow() const OVERRIDE { return true; }
6292
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006293
6294 DECLARE_INSTRUCTION(Throw);
6295
6296 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006297 DISALLOW_COPY_AND_ASSIGN(HThrow);
6298};
6299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300/**
6301 * Implementation strategies for the code generator of a HInstanceOf
6302 * or `HCheckCast`.
6303 */
6304enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006305 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 kExactCheck, // Can do a single class compare.
6307 kClassHierarchyCheck, // Can just walk the super class chain.
6308 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6309 kInterfaceCheck, // No optimization yet when checking against an interface.
6310 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006311 kArrayCheck, // No optimization yet when checking against a generic array.
6312 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313};
6314
Roland Levillain86503782016-02-11 19:07:30 +00006315std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6316
Vladimir Markofcb503c2016-05-18 12:48:17 +01006317class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006319 HInstanceOf(HInstruction* object,
6320 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006322 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006323 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006325 dex_pc) {
6326 SetPackedField<TypeCheckKindField>(check_kind);
6327 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006328 SetRawInputAt(0, object);
6329 SetRawInputAt(1, constant);
6330 }
6331
6332 bool CanBeMoved() const OVERRIDE { return true; }
6333
Vladimir Marko372f10e2016-05-17 16:30:10 +01006334 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 return true;
6336 }
6337
6338 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006339 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006340 }
6341
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006342 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006343 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6344 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6345 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6346 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006347
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006348 static bool CanCallRuntime(TypeCheckKind check_kind) {
6349 // Mips currently does runtime calls for any other checks.
6350 return check_kind != TypeCheckKind::kExactCheck;
6351 }
6352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006354 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006355 }
6356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006358
6359 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006360 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6361 static constexpr size_t kFieldTypeCheckKindSize =
6362 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6363 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6364 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6365 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6366 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006367
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006368 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6369};
6370
Vladimir Markofcb503c2016-05-18 12:48:17 +01006371class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006372 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006373 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006374 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006375 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6376 SetPackedFlag<kFlagUpperCanBeNull>(true);
6377 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006378 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006379 SetRawInputAt(0, input);
6380 }
6381
David Brazdilf5552582015-12-27 13:36:12 +00006382 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006383 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006385 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006386
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006387 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006388 DCHECK(GetUpperCanBeNull() || !can_be_null);
6389 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006390 }
6391
Vladimir Markoa1de9182016-02-25 11:37:38 +00006392 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006393
Calin Juravleb1498f62015-02-16 13:13:29 +00006394 DECLARE_INSTRUCTION(BoundType);
6395
6396 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006397 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6398 // is false then CanBeNull() cannot be true).
6399 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6400 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6401 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6402 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6403
Calin Juravleb1498f62015-02-16 13:13:29 +00006404 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006405 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6406 // It is used to bound the type in cases like:
6407 // if (x instanceof ClassX) {
6408 // // uper_bound_ will be ClassX
6409 // }
David Brazdilf5552582015-12-27 13:36:12 +00006410 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006411
6412 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6413};
6414
Vladimir Markofcb503c2016-05-18 12:48:17 +01006415class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416 public:
6417 HCheckCast(HInstruction* object,
6418 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006419 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006420 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006421 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6422 SetPackedField<TypeCheckKindField>(check_kind);
6423 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006424 SetRawInputAt(0, object);
6425 SetRawInputAt(1, constant);
6426 }
6427
6428 bool CanBeMoved() const OVERRIDE { return true; }
6429
Vladimir Marko372f10e2016-05-17 16:30:10 +01006430 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006431 return true;
6432 }
6433
6434 bool NeedsEnvironment() const OVERRIDE {
6435 // Instruction may throw a CheckCastError.
6436 return true;
6437 }
6438
6439 bool CanThrow() const OVERRIDE { return true; }
6440
Vladimir Markoa1de9182016-02-25 11:37:38 +00006441 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6442 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6443 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6444 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006445
6446 DECLARE_INSTRUCTION(CheckCast);
6447
6448 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006449 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6450 static constexpr size_t kFieldTypeCheckKindSize =
6451 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6452 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6453 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6454 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6455 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006456
6457 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006458};
6459
Andreas Gampe26de38b2016-07-27 17:53:11 -07006460/**
6461 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6462 * @details We define the combined barrier types that are actually required
6463 * by the Java Memory Model, rather than using exactly the terminology from
6464 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6465 * primitives. Note that the JSR-133 cookbook generally does not deal with
6466 * store atomicity issues, and the recipes there are not always entirely sufficient.
6467 * The current recipe is as follows:
6468 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6469 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6470 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6471 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6472 * class has final fields.
6473 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6474 * store-to-memory instructions. Only generated together with non-temporal stores.
6475 */
6476enum MemBarrierKind {
6477 kAnyStore,
6478 kLoadAny,
6479 kStoreStore,
6480 kAnyAny,
6481 kNTStoreStore,
6482 kLastBarrierKind = kNTStoreStore
6483};
6484std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6485
Vladimir Markofcb503c2016-05-18 12:48:17 +01006486class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006487 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006488 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006489 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006490 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6491 SetPackedField<BarrierKindField>(barrier_kind);
6492 }
Calin Juravle27df7582015-04-17 19:12:31 +01006493
Vladimir Markoa1de9182016-02-25 11:37:38 +00006494 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006495
6496 DECLARE_INSTRUCTION(MemoryBarrier);
6497
6498 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006499 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6500 static constexpr size_t kFieldBarrierKindSize =
6501 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6502 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6503 kFieldBarrierKind + kFieldBarrierKindSize;
6504 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6505 "Too many packed fields.");
6506 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006507
6508 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6509};
6510
Vladimir Markofcb503c2016-05-18 12:48:17 +01006511class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006512 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006514 kEnter,
6515 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006516 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006517 };
6518
6519 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006520 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006521 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6522 dex_pc) {
6523 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006524 SetRawInputAt(0, object);
6525 }
6526
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006527 // Instruction may go into runtime, so we need an environment.
6528 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006529
6530 bool CanThrow() const OVERRIDE {
6531 // Verifier guarantees that monitor-exit cannot throw.
6532 // This is important because it allows the HGraphBuilder to remove
6533 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6534 return IsEnter();
6535 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006536
Vladimir Markoa1de9182016-02-25 11:37:38 +00006537 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6538 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006539
6540 DECLARE_INSTRUCTION(MonitorOperation);
6541
Calin Juravle52c48962014-12-16 17:02:57 +00006542 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006543 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6544 static constexpr size_t kFieldOperationKindSize =
6545 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6546 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6547 kFieldOperationKind + kFieldOperationKindSize;
6548 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6549 "Too many packed fields.");
6550 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006551
6552 private:
6553 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6554};
6555
Vladimir Markofcb503c2016-05-18 12:48:17 +01006556class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006557 public:
6558 HSelect(HInstruction* condition,
6559 HInstruction* true_value,
6560 HInstruction* false_value,
6561 uint32_t dex_pc)
6562 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6563 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6564
6565 // First input must be `true_value` or `false_value` to allow codegens to
6566 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6567 // that architectures which implement HSelect as a conditional move also
6568 // will not need to invert the condition.
6569 SetRawInputAt(0, false_value);
6570 SetRawInputAt(1, true_value);
6571 SetRawInputAt(2, condition);
6572 }
6573
6574 HInstruction* GetFalseValue() const { return InputAt(0); }
6575 HInstruction* GetTrueValue() const { return InputAt(1); }
6576 HInstruction* GetCondition() const { return InputAt(2); }
6577
6578 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006579 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6580 return true;
6581 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006582
6583 bool CanBeNull() const OVERRIDE {
6584 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6585 }
6586
6587 DECLARE_INSTRUCTION(Select);
6588
6589 private:
6590 DISALLOW_COPY_AND_ASSIGN(HSelect);
6591};
6592
Vladimir Markof9f64412015-09-02 14:05:49 +01006593class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006594 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006595 MoveOperands(Location source,
6596 Location destination,
6597 Primitive::Type type,
6598 HInstruction* instruction)
6599 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006600
6601 Location GetSource() const { return source_; }
6602 Location GetDestination() const { return destination_; }
6603
6604 void SetSource(Location value) { source_ = value; }
6605 void SetDestination(Location value) { destination_ = value; }
6606
6607 // The parallel move resolver marks moves as "in-progress" by clearing the
6608 // destination (but not the source).
6609 Location MarkPending() {
6610 DCHECK(!IsPending());
6611 Location dest = destination_;
6612 destination_ = Location::NoLocation();
6613 return dest;
6614 }
6615
6616 void ClearPending(Location dest) {
6617 DCHECK(IsPending());
6618 destination_ = dest;
6619 }
6620
6621 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006622 DCHECK(source_.IsValid() || destination_.IsInvalid());
6623 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006624 }
6625
6626 // True if this blocks a move from the given location.
6627 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006628 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006629 }
6630
6631 // A move is redundant if it's been eliminated, if its source and
6632 // destination are the same, or if its destination is unneeded.
6633 bool IsRedundant() const {
6634 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6635 }
6636
6637 // We clear both operands to indicate move that's been eliminated.
6638 void Eliminate() {
6639 source_ = destination_ = Location::NoLocation();
6640 }
6641
6642 bool IsEliminated() const {
6643 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6644 return source_.IsInvalid();
6645 }
6646
Alexey Frunze4dda3372015-06-01 18:31:49 -07006647 Primitive::Type GetType() const { return type_; }
6648
Nicolas Geoffray90218252015-04-15 11:56:51 +01006649 bool Is64BitMove() const {
6650 return Primitive::Is64BitType(type_);
6651 }
6652
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006653 HInstruction* GetInstruction() const { return instruction_; }
6654
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006655 private:
6656 Location source_;
6657 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006658 // The type this move is for.
6659 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006660 // The instruction this move is assocatied with. Null when this move is
6661 // for moving an input in the expected locations of user (including a phi user).
6662 // This is only used in debug mode, to ensure we do not connect interval siblings
6663 // in the same parallel move.
6664 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006665};
6666
Roland Levillainc9285912015-12-18 10:38:42 +00006667std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6668
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006669static constexpr size_t kDefaultNumberOfMoves = 4;
6670
Vladimir Markofcb503c2016-05-18 12:48:17 +01006671class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006672 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006673 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006674 : HTemplateInstruction(SideEffects::None(), dex_pc),
6675 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6676 moves_.reserve(kDefaultNumberOfMoves);
6677 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006678
Nicolas Geoffray90218252015-04-15 11:56:51 +01006679 void AddMove(Location source,
6680 Location destination,
6681 Primitive::Type type,
6682 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006683 DCHECK(source.IsValid());
6684 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006685 if (kIsDebugBuild) {
6686 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006687 for (const MoveOperands& move : moves_) {
6688 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006689 // Special case the situation where the move is for the spill slot
6690 // of the instruction.
6691 if ((GetPrevious() == instruction)
6692 || ((GetPrevious() == nullptr)
6693 && instruction->IsPhi()
6694 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006695 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006696 << "Doing parallel moves for the same instruction.";
6697 } else {
6698 DCHECK(false) << "Doing parallel moves for the same instruction.";
6699 }
6700 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006701 }
6702 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006703 for (const MoveOperands& move : moves_) {
6704 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006705 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006706 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006707 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006708 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006709 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006710 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006711 }
6712
Vladimir Marko225b6462015-09-28 12:17:40 +01006713 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006714 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006715 }
6716
Vladimir Marko225b6462015-09-28 12:17:40 +01006717 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006718
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006719 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006720
6721 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006722 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006723
6724 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6725};
6726
Mark Mendell0616ae02015-04-17 12:49:27 -04006727} // namespace art
6728
Aart Bikf8f5a162017-02-06 15:35:29 -08006729#include "nodes_vector.h"
6730
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006731#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6732#include "nodes_shared.h"
6733#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006734#ifdef ART_ENABLE_CODEGEN_arm
6735#include "nodes_arm.h"
6736#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006737#ifdef ART_ENABLE_CODEGEN_mips
6738#include "nodes_mips.h"
6739#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006740#ifdef ART_ENABLE_CODEGEN_x86
6741#include "nodes_x86.h"
6742#endif
6743
6744namespace art {
6745
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006746class HGraphVisitor : public ValueObject {
6747 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006748 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6749 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006750
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006751 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006752 virtual void VisitBasicBlock(HBasicBlock* block);
6753
Roland Levillain633021e2014-10-01 14:12:25 +01006754 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006755 void VisitInsertionOrder();
6756
Roland Levillain633021e2014-10-01 14:12:25 +01006757 // Visit the graph following dominator tree reverse post-order.
6758 void VisitReversePostOrder();
6759
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006760 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006761
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006762 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006763#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006764 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6765
6766 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6767
6768#undef DECLARE_VISIT_INSTRUCTION
6769
6770 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006771 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006772
6773 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6774};
6775
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006776class HGraphDelegateVisitor : public HGraphVisitor {
6777 public:
6778 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6779 virtual ~HGraphDelegateVisitor() {}
6780
6781 // Visit functions that delegate to to super class.
6782#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006783 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006784
6785 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6786
6787#undef DECLARE_VISIT_INSTRUCTION
6788
6789 private:
6790 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6791};
6792
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006793// Iterator over the blocks that art part of the loop. Includes blocks part
6794// of an inner loop. The order in which the blocks are iterated is on their
6795// block id.
6796class HBlocksInLoopIterator : public ValueObject {
6797 public:
6798 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6799 : blocks_in_loop_(info.GetBlocks()),
6800 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6801 index_(0) {
6802 if (!blocks_in_loop_.IsBitSet(index_)) {
6803 Advance();
6804 }
6805 }
6806
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006807 bool Done() const { return index_ == blocks_.size(); }
6808 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006809 void Advance() {
6810 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006811 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006812 if (blocks_in_loop_.IsBitSet(index_)) {
6813 break;
6814 }
6815 }
6816 }
6817
6818 private:
6819 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006820 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006821 size_t index_;
6822
6823 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6824};
6825
Mingyao Yang3584bce2015-05-19 16:01:59 -07006826// Iterator over the blocks that art part of the loop. Includes blocks part
6827// of an inner loop. The order in which the blocks are iterated is reverse
6828// post order.
6829class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6830 public:
6831 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6832 : blocks_in_loop_(info.GetBlocks()),
6833 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6834 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006835 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006836 Advance();
6837 }
6838 }
6839
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006840 bool Done() const { return index_ == blocks_.size(); }
6841 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006842 void Advance() {
6843 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006844 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6845 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006846 break;
6847 }
6848 }
6849 }
6850
6851 private:
6852 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006853 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006854 size_t index_;
6855
6856 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6857};
6858
Aart Bikf3e61ee2017-04-12 17:09:20 -07006859// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006860inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006861 if (constant->IsIntConstant()) {
6862 return constant->AsIntConstant()->GetValue();
6863 } else if (constant->IsLongConstant()) {
6864 return constant->AsLongConstant()->GetValue();
6865 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006866 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006867 return 0;
6868 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006869}
6870
Aart Bikf3e61ee2017-04-12 17:09:20 -07006871// Returns true iff instruction is an integral constant (and sets value on success).
6872inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
6873 if (instruction->IsIntConstant()) {
6874 *value = instruction->AsIntConstant()->GetValue();
6875 return true;
6876 } else if (instruction->IsLongConstant()) {
6877 *value = instruction->AsLongConstant()->GetValue();
6878 return true;
6879 } else if (instruction->IsNullConstant()) {
6880 *value = 0;
6881 return true;
6882 }
6883 return false;
6884}
6885
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006886#define INSTRUCTION_TYPE_CHECK(type, super) \
6887 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6888 inline const H##type* HInstruction::As##type() const { \
6889 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6890 } \
6891 inline H##type* HInstruction::As##type() { \
6892 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6893 }
6894
6895 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6896#undef INSTRUCTION_TYPE_CHECK
6897
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006898// Create space in `blocks` for adding `number_of_new_blocks` entries
6899// starting at location `at`. Blocks after `at` are moved accordingly.
6900inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6901 size_t number_of_new_blocks,
6902 size_t after) {
6903 DCHECK_LT(after, blocks->size());
6904 size_t old_size = blocks->size();
6905 size_t new_size = old_size + number_of_new_blocks;
6906 blocks->resize(new_size);
6907 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6908}
6909
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006910/*
6911 * Hunt "under the hood" of array lengths (leading to array references),
6912 * null checks (also leading to array references), and new arrays
6913 * (leading to the actual length). This makes it more likely related
6914 * instructions become actually comparable.
6915 */
6916inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6917 while (instruction->IsArrayLength() ||
6918 instruction->IsNullCheck() ||
6919 instruction->IsNewArray()) {
6920 instruction = instruction->IsNewArray()
6921 ? instruction->AsNewArray()->GetLength()
6922 : instruction->InputAt(0);
6923 }
6924 return instruction;
6925}
6926
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006927} // namespace art
6928
6929#endif // ART_COMPILER_OPTIMIZING_NODES_H_