blob: c109369106821c37f4b64f67d62bdd54fe2018bd [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) \
1365 M(VecSetScalars, VecUnaryOperation) \
1366 M(VecSumReduce, VecUnaryOperation) \
1367 M(VecCnv, VecUnaryOperation) \
1368 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001369 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001370 M(VecNot, VecUnaryOperation) \
1371 M(VecAdd, VecBinaryOperation) \
1372 M(VecSub, VecBinaryOperation) \
1373 M(VecMul, VecBinaryOperation) \
1374 M(VecDiv, VecBinaryOperation) \
1375 M(VecAnd, VecBinaryOperation) \
1376 M(VecAndNot, VecBinaryOperation) \
1377 M(VecOr, VecBinaryOperation) \
1378 M(VecXor, VecBinaryOperation) \
1379 M(VecShl, VecBinaryOperation) \
1380 M(VecShr, VecBinaryOperation) \
1381 M(VecUShr, VecBinaryOperation) \
1382 M(VecLoad, VecMemoryOperation) \
1383 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001384
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001385/*
1386 * Instructions, shared across several (not all) architectures.
1387 */
1388#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1389#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1390#else
1391#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001392 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001393 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001394 M(MultiplyAccumulate, Instruction) \
1395 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001396#endif
1397
Vladimir Markob4536b72015-11-24 13:45:23 +00001398#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001399#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001400#else
1401#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1402 M(ArmDexCacheArraysBase, Instruction)
1403#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001404
1405#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1406
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001407#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001408#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001409#else
1410#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1411 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001412 M(MipsDexCacheArraysBase, Instruction) \
1413 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001414#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001416#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1417
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001418#ifndef ART_ENABLE_CODEGEN_x86
1419#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1420#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001421#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1422 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001423 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001424 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001425 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001426#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001427
1428#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1429
1430#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1431 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001432 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001433 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1434 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001435 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001436 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001437 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1438 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1439
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001440#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1441 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001442 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001443 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001444 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001445 M(Invoke, Instruction) \
1446 M(VecOperation, Instruction) \
1447 M(VecUnaryOperation, VecOperation) \
1448 M(VecBinaryOperation, VecOperation) \
1449 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001450
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001451#define FOR_EACH_INSTRUCTION(M) \
1452 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1453 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1454
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001455#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001456FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1457#undef FORWARD_DECLARATION
1458
Vladimir Marko372f10e2016-05-17 16:30:10 +01001459#define DECLARE_INSTRUCTION(type) \
1460 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1461 const char* DebugName() const OVERRIDE { return #type; } \
1462 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1463 return other->Is##type(); \
1464 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001465 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001466
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001467#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1468 bool Is##type() const { return As##type() != nullptr; } \
1469 const H##type* As##type() const { return this; } \
1470 H##type* As##type() { return this; }
1471
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001472template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001473class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001474 public:
David Brazdiled596192015-01-23 10:39:45 +00001475 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001476 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001477 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001478
Vladimir Marko46817b82016-03-29 12:21:58 +01001479 // Hook for the IntrusiveForwardList<>.
1480 // TODO: Hide this better.
1481 IntrusiveForwardListHook hook;
1482
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001483 private:
David Brazdiled596192015-01-23 10:39:45 +00001484 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001485 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001486
1487 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001488 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001489
Vladimir Marko46817b82016-03-29 12:21:58 +01001490 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001491
1492 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1493};
1494
David Brazdiled596192015-01-23 10:39:45 +00001495template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001496using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001497
David Brazdil1abb4192015-02-17 18:33:36 +00001498// This class is used by HEnvironment and HInstruction classes to record the
1499// instructions they use and pointers to the corresponding HUseListNodes kept
1500// by the used instructions.
1501template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001502class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001503 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001504 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1505 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001506
Vladimir Marko46817b82016-03-29 12:21:58 +01001507 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1508 : HUserRecord(old_record.instruction_, before_use_node) {}
1509 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1510 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001511 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001512 }
1513
1514 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001515 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1516 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001517
1518 private:
1519 // Instruction used by the user.
1520 HInstruction* instruction_;
1521
Vladimir Marko46817b82016-03-29 12:21:58 +01001522 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1523 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001524};
1525
Vladimir Markoe9004912016-06-16 16:50:52 +01001526// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1527// This is used for HInstruction::GetInputs() to return a container wrapper providing
1528// HInstruction* values even though the underlying container has HUserRecord<>s.
1529struct HInputExtractor {
1530 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1531 return record.GetInstruction();
1532 }
1533 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1534 return record.GetInstruction();
1535 }
1536};
1537
1538using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1539using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1540
Aart Bik854a02b2015-07-14 16:07:00 -07001541/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001542 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001543 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001544 * For write/read dependences on fields/arrays, the dependence analysis uses
1545 * type disambiguation (e.g. a float field write cannot modify the value of an
1546 * integer field read) and the access type (e.g. a reference array write cannot
1547 * modify the value of a reference field read [although it may modify the
1548 * reference fetch prior to reading the field, which is represented by its own
1549 * write/read dependence]). The analysis makes conservative points-to
1550 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1551 * the same, and any reference read depends on any reference read without
1552 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001553 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001554 * The internal representation uses 38-bit and is described in the table below.
1555 * The first line indicates the side effect, and for field/array accesses the
1556 * second line indicates the type of the access (in the order of the
1557 * Primitive::Type enum).
1558 * The two numbered lines below indicate the bit position in the bitfield (read
1559 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001560 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001561 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1562 * +-------------+---------+---------+--------------+---------+---------+
1563 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1564 * | 3 |333333322|222222221| 1 |111111110|000000000|
1565 * | 7 |654321098|765432109| 8 |765432109|876543210|
1566 *
1567 * Note that, to ease the implementation, 'changes' bits are least significant
1568 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001569 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570class SideEffects : public ValueObject {
1571 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001572 SideEffects() : flags_(0) {}
1573
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574 static SideEffects None() {
1575 return SideEffects(0);
1576 }
1577
1578 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001579 return SideEffects(kAllChangeBits | kAllDependOnBits);
1580 }
1581
1582 static SideEffects AllChanges() {
1583 return SideEffects(kAllChangeBits);
1584 }
1585
1586 static SideEffects AllDependencies() {
1587 return SideEffects(kAllDependOnBits);
1588 }
1589
1590 static SideEffects AllExceptGCDependency() {
1591 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1592 }
1593
1594 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001595 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001596 }
1597
Aart Bik34c3ba92015-07-20 14:08:59 -07001598 static SideEffects AllWrites() {
1599 return SideEffects(kAllWrites);
1600 }
1601
1602 static SideEffects AllReads() {
1603 return SideEffects(kAllReads);
1604 }
1605
1606 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1607 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001608 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001609 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001610 }
1611
Aart Bik854a02b2015-07-14 16:07:00 -07001612 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001613 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 }
1615
Aart Bik34c3ba92015-07-20 14:08:59 -07001616 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1617 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001618 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001619 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001620 }
1621
1622 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001623 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001624 }
1625
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001626 static SideEffects CanTriggerGC() {
1627 return SideEffects(1ULL << kCanTriggerGCBit);
1628 }
1629
1630 static SideEffects DependsOnGC() {
1631 return SideEffects(1ULL << kDependsOnGCBit);
1632 }
1633
Aart Bik854a02b2015-07-14 16:07:00 -07001634 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001635 SideEffects Union(SideEffects other) const {
1636 return SideEffects(flags_ | other.flags_);
1637 }
1638
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001639 SideEffects Exclusion(SideEffects other) const {
1640 return SideEffects(flags_ & ~other.flags_);
1641 }
1642
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001643 void Add(SideEffects other) {
1644 flags_ |= other.flags_;
1645 }
1646
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 bool Includes(SideEffects other) const {
1648 return (other.flags_ & flags_) == other.flags_;
1649 }
1650
1651 bool HasSideEffects() const {
1652 return (flags_ & kAllChangeBits);
1653 }
1654
1655 bool HasDependencies() const {
1656 return (flags_ & kAllDependOnBits);
1657 }
1658
1659 // Returns true if there are no side effects or dependencies.
1660 bool DoesNothing() const {
1661 return flags_ == 0;
1662 }
1663
Aart Bik854a02b2015-07-14 16:07:00 -07001664 // Returns true if something is written.
1665 bool DoesAnyWrite() const {
1666 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001667 }
1668
Aart Bik854a02b2015-07-14 16:07:00 -07001669 // Returns true if something is read.
1670 bool DoesAnyRead() const {
1671 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001672 }
1673
Aart Bik854a02b2015-07-14 16:07:00 -07001674 // Returns true if potentially everything is written and read
1675 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001676 bool DoesAllReadWrite() const {
1677 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1678 }
1679
Aart Bik854a02b2015-07-14 16:07:00 -07001680 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001681 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001682 }
1683
Roland Levillain0d5a2812015-11-13 10:07:31 +00001684 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001685 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001686 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1687 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001688 }
1689
1690 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001691 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001692 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001693 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 for (int s = kLastBit; s >= 0; s--) {
1695 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1696 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1697 // This is a bit for the GC side effect.
1698 if (current_bit_is_set) {
1699 flags += "GC";
1700 }
Aart Bik854a02b2015-07-14 16:07:00 -07001701 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001702 } else {
1703 // This is a bit for the array/field analysis.
1704 // The underscore character stands for the 'can trigger GC' bit.
1705 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1706 if (current_bit_is_set) {
1707 flags += kDebug[s];
1708 }
1709 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1710 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1711 flags += "|";
1712 }
1713 }
Aart Bik854a02b2015-07-14 16:07:00 -07001714 }
1715 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001716 }
1717
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001718 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001719
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001720 private:
1721 static constexpr int kFieldArrayAnalysisBits = 9;
1722
1723 static constexpr int kFieldWriteOffset = 0;
1724 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1725 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1726 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1727
1728 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1729
1730 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1731 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1732 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1733 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1734
1735 static constexpr int kLastBit = kDependsOnGCBit;
1736 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1737
1738 // Aliases.
1739
1740 static_assert(kChangeBits == kDependOnBits,
1741 "the 'change' bits should match the 'depend on' bits.");
1742
1743 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1744 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1745 static constexpr uint64_t kAllWrites =
1746 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1747 static constexpr uint64_t kAllReads =
1748 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001749
Aart Bik854a02b2015-07-14 16:07:00 -07001750 // Translates type to bit flag.
1751 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1752 CHECK_NE(type, Primitive::kPrimVoid);
1753 const uint64_t one = 1;
1754 const int shift = type; // 0-based consecutive enum
1755 DCHECK_LE(kFieldWriteOffset, shift);
1756 DCHECK_LT(shift, kArrayWriteOffset);
1757 return one << (type + offset);
1758 }
1759
1760 // Private constructor on direct flags value.
1761 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1762
1763 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001764};
1765
David Brazdiled596192015-01-23 10:39:45 +00001766// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001767class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001768 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001769 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1770 size_t number_of_vregs,
1771 ArtMethod* method,
1772 uint32_t dex_pc,
1773 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001774 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1775 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001776 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001777 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001778 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001779 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001780 }
1781
Mingyao Yang01b47b02017-02-03 12:09:57 -08001782 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001783 : HEnvironment(arena,
1784 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001785 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001786 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001787 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001788
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001790 if (parent_ != nullptr) {
1791 parent_->SetAndCopyParentChain(allocator, parent);
1792 } else {
1793 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1794 parent_->CopyFrom(parent);
1795 if (parent->GetParent() != nullptr) {
1796 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1797 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001798 }
David Brazdiled596192015-01-23 10:39:45 +00001799 }
1800
Vladimir Marko71bf8092015-09-15 15:33:14 +01001801 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001802 void CopyFrom(HEnvironment* environment);
1803
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001804 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1805 // input to the loop phi instead. This is for inserting instructions that
1806 // require an environment (like HDeoptimization) in the loop pre-header.
1807 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001808
1809 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001810 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001811 }
1812
1813 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001814 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001815 }
1816
David Brazdil1abb4192015-02-17 18:33:36 +00001817 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001818
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001819 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001820
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001821 HEnvironment* GetParent() const { return parent_; }
1822
1823 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001824 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001825 }
1826
1827 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001829 }
1830
1831 uint32_t GetDexPc() const {
1832 return dex_pc_;
1833 }
1834
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001835 ArtMethod* GetMethod() const {
1836 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001837 }
1838
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001839 HInstruction* GetHolder() const {
1840 return holder_;
1841 }
1842
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001843
1844 bool IsFromInlinedInvoke() const {
1845 return GetParent() != nullptr;
1846 }
1847
David Brazdiled596192015-01-23 10:39:45 +00001848 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001849 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1850 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001851 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001852 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001853 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001854
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001855 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001856 HInstruction* const holder_;
1857
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001858 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001859
1860 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1861};
1862
Vladimir Markof9f64412015-09-02 14:05:49 +01001863class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001865 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001866 : previous_(nullptr),
1867 next_(nullptr),
1868 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001869 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001871 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001872 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001873 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001874 locations_(nullptr),
1875 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001876 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001877 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001878 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1879 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1880 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001881
Dave Allison20dfc792014-06-16 20:44:29 -07001882 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001883
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001884#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001885 enum InstructionKind {
1886 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1887 };
1888#undef DECLARE_KIND
1889
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001890 HInstruction* GetNext() const { return next_; }
1891 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892
Calin Juravle77520bc2015-01-12 18:45:46 +00001893 HInstruction* GetNextDisregardingMoves() const;
1894 HInstruction* GetPreviousDisregardingMoves() const;
1895
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001896 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001897 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001898 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001899 bool IsInBlock() const { return block_ != nullptr; }
1900 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001901 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1902 bool IsIrreducibleLoopHeaderPhi() const {
1903 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1904 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001905
Vladimir Marko372f10e2016-05-17 16:30:10 +01001906 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1907
1908 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1909 // One virtual method is enough, just const_cast<> and then re-add the const.
1910 return ArrayRef<const HUserRecord<HInstruction*>>(
1911 const_cast<HInstruction*>(this)->GetInputRecords());
1912 }
1913
Vladimir Markoe9004912016-06-16 16:50:52 +01001914 HInputsRef GetInputs() {
1915 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001916 }
1917
Vladimir Markoe9004912016-06-16 16:50:52 +01001918 HConstInputsRef GetInputs() const {
1919 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001920 }
1921
1922 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001923 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001924
Aart Bik2767f4b2016-10-28 15:03:53 -07001925 bool HasInput(HInstruction* input) const {
1926 for (const HInstruction* i : GetInputs()) {
1927 if (i == input) {
1928 return true;
1929 }
1930 }
1931 return false;
1932 }
1933
Vladimir Marko372f10e2016-05-17 16:30:10 +01001934 void SetRawInputAt(size_t index, HInstruction* input) {
1935 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1936 }
1937
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001938 virtual void Accept(HGraphVisitor* visitor) = 0;
1939 virtual const char* DebugName() const = 0;
1940
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001943 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001944
1945 uint32_t GetDexPc() const { return dex_pc_; }
1946
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001947 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001948
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001949 // Can the instruction throw?
1950 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1951 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001952 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001953 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001954
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001955 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001956 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957
Calin Juravle10e244f2015-01-26 18:54:32 +00001958 // Does not apply for all instructions, but having this at top level greatly
1959 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001960 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001961 virtual bool CanBeNull() const {
1962 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1963 return true;
1964 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001965
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001966 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001967 return false;
1968 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001969
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001970 virtual bool IsActualObject() const {
1971 return GetType() == Primitive::kPrimNot;
1972 }
1973
Calin Juravle2e768302015-07-28 14:41:11 +00001974 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001975
Calin Juravle61d544b2015-02-23 16:46:57 +00001976 ReferenceTypeInfo GetReferenceTypeInfo() const {
1977 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001978 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001979 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001980 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001981
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001982 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001983 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001984 // Note: fixup_end remains valid across push_front().
1985 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1986 HUseListNode<HInstruction*>* new_node =
1987 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1988 uses_.push_front(*new_node);
1989 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001990 }
1991
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001992 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001993 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001994 // Note: env_fixup_end remains valid across push_front().
1995 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1996 HUseListNode<HEnvironment*>* new_node =
1997 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1998 env_uses_.push_front(*new_node);
1999 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002000 }
2001
David Brazdil1abb4192015-02-17 18:33:36 +00002002 void RemoveAsUserOfInput(size_t input) {
2003 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002004 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2005 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2006 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002007 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002008
Vladimir Marko372f10e2016-05-17 16:30:10 +01002009 void RemoveAsUserOfAllInputs() {
2010 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2011 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2012 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2013 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2014 }
2015 }
2016
David Brazdil1abb4192015-02-17 18:33:36 +00002017 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2018 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019
Vladimir Marko46817b82016-03-29 12:21:58 +01002020 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2021 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2022 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002023 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002024 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002025 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002026
Aart Bikcc42be02016-10-20 16:14:16 -07002027 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002028 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002029 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002030 !CanThrow() &&
2031 !IsSuspendCheck() &&
2032 !IsControlFlow() &&
2033 !IsNativeDebugInfo() &&
2034 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002035 // If we added an explicit barrier then we should keep it.
2036 !IsMemoryBarrier();
2037 }
2038
Aart Bikcc42be02016-10-20 16:14:16 -07002039 bool IsDeadAndRemovable() const {
2040 return IsRemovable() && !HasUses();
2041 }
2042
Roland Levillain6c82d402014-10-13 16:10:27 +01002043 // Does this instruction strictly dominate `other_instruction`?
2044 // Returns false if this instruction and `other_instruction` are the same.
2045 // Aborts if this instruction and `other_instruction` are both phis.
2046 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002047
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002048 int GetId() const { return id_; }
2049 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002051 int GetSsaIndex() const { return ssa_index_; }
2052 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2053 bool HasSsaIndex() const { return ssa_index_ != -1; }
2054
2055 bool HasEnvironment() const { return environment_ != nullptr; }
2056 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002057 // Set the `environment_` field. Raw because this method does not
2058 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002059 void SetRawEnvironment(HEnvironment* environment) {
2060 DCHECK(environment_ == nullptr);
2061 DCHECK_EQ(environment->GetHolder(), this);
2062 environment_ = environment;
2063 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002064
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002065 void InsertRawEnvironment(HEnvironment* environment) {
2066 DCHECK(environment_ != nullptr);
2067 DCHECK_EQ(environment->GetHolder(), this);
2068 DCHECK(environment->GetParent() == nullptr);
2069 environment->parent_ = environment_;
2070 environment_ = environment;
2071 }
2072
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002073 void RemoveEnvironment();
2074
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002075 // Set the environment of this instruction, copying it from `environment`. While
2076 // copying, the uses lists are being updated.
2077 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002078 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002079 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002080 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002081 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002082 if (environment->GetParent() != nullptr) {
2083 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2084 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002085 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002086
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002087 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2088 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002089 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002090 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002091 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002092 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002093 if (environment->GetParent() != nullptr) {
2094 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2095 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002096 }
2097
Nicolas Geoffray39468442014-09-02 15:17:15 +01002098 // Returns the number of entries in the environment. Typically, that is the
2099 // number of dex registers in a method. It could be more in case of inlining.
2100 size_t EnvironmentSize() const;
2101
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002102 LocationSummary* GetLocations() const { return locations_; }
2103 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002104
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002105 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002106 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002107 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108
Alexandre Rames188d4312015-04-09 18:30:21 +01002109 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2110 // uses of this instruction by `other` are *not* updated.
2111 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2112 ReplaceWith(other);
2113 other->ReplaceInput(this, use_index);
2114 }
2115
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002116 // Move `this` instruction before `cursor`
2117 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002118
Vladimir Markofb337ea2015-11-25 15:25:10 +00002119 // Move `this` before its first user and out of any loops. If there is no
2120 // out-of-loop user that dominates all other users, move the instruction
2121 // to the end of the out-of-loop common dominator of the user's blocks.
2122 //
2123 // This can be used only on non-throwing instructions with no side effects that
2124 // have at least one use but no environment uses.
2125 void MoveBeforeFirstUserAndOutOfLoops();
2126
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002127#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 bool Is##type() const; \
2129 const H##type* As##type() const; \
2130 H##type* As##type();
2131
2132 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2133#undef INSTRUCTION_TYPE_CHECK
2134
2135#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002136 bool Is##type() const { return (As##type() != nullptr); } \
2137 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002139 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002140#undef INSTRUCTION_TYPE_CHECK
2141
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002142 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002143 // TODO: this method is used by LICM and GVN with possibly different
2144 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002145 virtual bool CanBeMoved() const { return false; }
2146
2147 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002148 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002149 return false;
2150 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002151
2152 // Returns whether any data encoded in the two instructions is equal.
2153 // This method does not look at the inputs. Both instructions must be
2154 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002155 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002156 return false;
2157 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002158
2159 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002160 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002162 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002163
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002164 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2165 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2166 // the virtual function because the __attribute__((__pure__)) doesn't really
2167 // apply the strong requirement for virtual functions, preventing optimizations.
2168 InstructionKind GetKind() const PURE;
2169 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002170
2171 virtual size_t ComputeHashCode() const {
2172 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002173 for (const HInstruction* input : GetInputs()) {
2174 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002175 }
2176 return result;
2177 }
2178
2179 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002180 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002181 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002182
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002183 size_t GetLifetimePosition() const { return lifetime_position_; }
2184 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2185 LiveInterval* GetLiveInterval() const { return live_interval_; }
2186 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2187 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2188
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002189 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2190
2191 // Returns whether the code generation of the instruction will require to have access
2192 // to the current method. Such instructions are:
2193 // (1): Instructions that require an environment, as calling the runtime requires
2194 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002195 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2196 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002197 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002198 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002199 }
2200
Vladimir Markodc151b22015-10-15 18:02:30 +01002201 // Returns whether the code generation of the instruction will require to have access
2202 // to the dex cache of the current method's declaring class via the current method.
2203 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002204
Mark Mendellc4701932015-04-10 13:18:51 -04002205 // Does this instruction have any use in an environment before
2206 // control flow hits 'other'?
2207 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2208
2209 // Remove all references to environment uses of this instruction.
2210 // The caller must ensure that this is safe to do.
2211 void RemoveEnvironmentUsers();
2212
Vladimir Markoa1de9182016-02-25 11:37:38 +00002213 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2214 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002215
David Brazdil1abb4192015-02-17 18:33:36 +00002216 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002217 // If set, the machine code for this instruction is assumed to be generated by
2218 // its users. Used by liveness analysis to compute use positions accordingly.
2219 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2220 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2221 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2222 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2223
Vladimir Marko372f10e2016-05-17 16:30:10 +01002224 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2225 return GetInputRecords()[i];
2226 }
2227
2228 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2229 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2230 input_records[index] = input;
2231 }
David Brazdil1abb4192015-02-17 18:33:36 +00002232
Vladimir Markoa1de9182016-02-25 11:37:38 +00002233 uint32_t GetPackedFields() const {
2234 return packed_fields_;
2235 }
2236
2237 template <size_t flag>
2238 bool GetPackedFlag() const {
2239 return (packed_fields_ & (1u << flag)) != 0u;
2240 }
2241
2242 template <size_t flag>
2243 void SetPackedFlag(bool value = true) {
2244 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2245 }
2246
2247 template <typename BitFieldType>
2248 typename BitFieldType::value_type GetPackedField() const {
2249 return BitFieldType::Decode(packed_fields_);
2250 }
2251
2252 template <typename BitFieldType>
2253 void SetPackedField(typename BitFieldType::value_type value) {
2254 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2255 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2256 }
2257
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002258 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002259 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2260 auto before_use_node = uses_.before_begin();
2261 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2262 HInstruction* user = use_node->GetUser();
2263 size_t input_index = use_node->GetIndex();
2264 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2265 before_use_node = use_node;
2266 }
2267 }
2268
2269 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2270 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2271 if (next != uses_.end()) {
2272 HInstruction* next_user = next->GetUser();
2273 size_t next_index = next->GetIndex();
2274 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2275 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2276 }
2277 }
2278
2279 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2280 auto before_env_use_node = env_uses_.before_begin();
2281 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2282 HEnvironment* user = env_use_node->GetUser();
2283 size_t input_index = env_use_node->GetIndex();
2284 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2285 before_env_use_node = env_use_node;
2286 }
2287 }
2288
2289 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2290 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2291 if (next != env_uses_.end()) {
2292 HEnvironment* next_user = next->GetUser();
2293 size_t next_index = next->GetIndex();
2294 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2295 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2296 }
2297 }
David Brazdil1abb4192015-02-17 18:33:36 +00002298
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002299 HInstruction* previous_;
2300 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002301 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002302 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002304 // An instruction gets an id when it is added to the graph.
2305 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002306 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002307 int id_;
2308
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002309 // When doing liveness analysis, instructions that have uses get an SSA index.
2310 int ssa_index_;
2311
Vladimir Markoa1de9182016-02-25 11:37:38 +00002312 // Packed fields.
2313 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002314
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002315 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002316 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002317
2318 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002319 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002320
Nicolas Geoffray39468442014-09-02 15:17:15 +01002321 // The environment associated with this instruction. Not null if the instruction
2322 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002323 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002324
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325 // Set by the code generator.
2326 LocationSummary* locations_;
2327
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002328 // Set by the liveness analysis.
2329 LiveInterval* live_interval_;
2330
2331 // Set by the liveness analysis, this is the position in a linear
2332 // order of blocks where this instruction's live interval start.
2333 size_t lifetime_position_;
2334
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002335 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002336
Vladimir Markoa1de9182016-02-25 11:37:38 +00002337 // The reference handle part of the reference type info.
2338 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002339 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002341
David Brazdil1abb4192015-02-17 18:33:36 +00002342 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002343 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002344 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002345 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002346 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002347
2348 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2349};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002350std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002352// Iterates over the instructions, while preserving the next instruction
2353// in case the current instruction gets removed from the list by the user
2354// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355class HInstructionIterator : public ValueObject {
2356 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002357 explicit HInstructionIterator(const HInstructionList& instructions)
2358 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002359 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002360 }
2361
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002362 bool Done() const { return instruction_ == nullptr; }
2363 HInstruction* Current() const { return instruction_; }
2364 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002365 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002366 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002367 }
2368
2369 private:
2370 HInstruction* instruction_;
2371 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002372
2373 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002374};
2375
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002376// Iterates over the instructions without saving the next instruction,
2377// therefore handling changes in the graph potentially made by the user
2378// of this iterator.
2379class HInstructionIteratorHandleChanges : public ValueObject {
2380 public:
2381 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2382 : instruction_(instructions.first_instruction_) {
2383 }
2384
2385 bool Done() const { return instruction_ == nullptr; }
2386 HInstruction* Current() const { return instruction_; }
2387 void Advance() {
2388 instruction_ = instruction_->GetNext();
2389 }
2390
2391 private:
2392 HInstruction* instruction_;
2393
2394 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2395};
2396
2397
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002398class HBackwardInstructionIterator : public ValueObject {
2399 public:
2400 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2401 : instruction_(instructions.last_instruction_) {
2402 next_ = Done() ? nullptr : instruction_->GetPrevious();
2403 }
2404
2405 bool Done() const { return instruction_ == nullptr; }
2406 HInstruction* Current() const { return instruction_; }
2407 void Advance() {
2408 instruction_ = next_;
2409 next_ = Done() ? nullptr : instruction_->GetPrevious();
2410 }
2411
2412 private:
2413 HInstruction* instruction_;
2414 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002415
2416 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002417};
2418
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002419class HVariableInputSizeInstruction : public HInstruction {
2420 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002421 using HInstruction::GetInputRecords; // Keep the const version visible.
2422 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2423 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2424 }
2425
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002426 void AddInput(HInstruction* input);
2427 void InsertInputAt(size_t index, HInstruction* input);
2428 void RemoveInputAt(size_t index);
2429
2430 protected:
2431 HVariableInputSizeInstruction(SideEffects side_effects,
2432 uint32_t dex_pc,
2433 ArenaAllocator* arena,
2434 size_t number_of_inputs,
2435 ArenaAllocKind kind)
2436 : HInstruction(side_effects, dex_pc),
2437 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2438
2439 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2440
2441 private:
2442 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2443};
2444
Vladimir Markof9f64412015-09-02 14:05:49 +01002445template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002446class HTemplateInstruction: public HInstruction {
2447 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002448 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002449 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002450 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002451
Vladimir Marko372f10e2016-05-17 16:30:10 +01002452 using HInstruction::GetInputRecords; // Keep the const version visible.
2453 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2454 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002455 }
2456
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002457 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002458 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002459
2460 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461};
2462
Vladimir Markof9f64412015-09-02 14:05:49 +01002463// HTemplateInstruction specialization for N=0.
2464template<>
2465class HTemplateInstruction<0>: public HInstruction {
2466 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002467 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002468 : HInstruction(side_effects, dex_pc) {}
2469
Vladimir Markof9f64412015-09-02 14:05:49 +01002470 virtual ~HTemplateInstruction() {}
2471
Vladimir Marko372f10e2016-05-17 16:30:10 +01002472 using HInstruction::GetInputRecords; // Keep the const version visible.
2473 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2474 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002475 }
2476
2477 private:
2478 friend class SsaBuilder;
2479};
2480
Dave Allison20dfc792014-06-16 20:44:29 -07002481template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002482class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002483 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002484 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002485 : HTemplateInstruction<N>(side_effects, dex_pc) {
2486 this->template SetPackedField<TypeField>(type);
2487 }
Dave Allison20dfc792014-06-16 20:44:29 -07002488 virtual ~HExpression() {}
2489
Vladimir Markoa1de9182016-02-25 11:37:38 +00002490 Primitive::Type GetType() const OVERRIDE {
2491 return TypeField::Decode(this->GetPackedFields());
2492 }
Dave Allison20dfc792014-06-16 20:44:29 -07002493
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002494 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002495 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2496 static constexpr size_t kFieldTypeSize =
2497 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2498 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2499 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2500 "Too many packed fields.");
2501 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002502};
2503
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002504// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2505// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002506class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002508 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2509 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002510
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002511 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002512
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002513 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002514
2515 private:
2516 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2517};
2518
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002519// Represents dex's RETURN opcodes. A HReturn is a control flow
2520// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002521class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002522 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002523 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2524 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002525 SetRawInputAt(0, value);
2526 }
2527
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002528 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002529
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002530 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002531
2532 private:
2533 DISALLOW_COPY_AND_ASSIGN(HReturn);
2534};
2535
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002536class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002537 public:
2538 HPhi(ArenaAllocator* arena,
2539 uint32_t reg_number,
2540 size_t number_of_inputs,
2541 Primitive::Type type,
2542 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002543 : HVariableInputSizeInstruction(
2544 SideEffects::None(),
2545 dex_pc,
2546 arena,
2547 number_of_inputs,
2548 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002549 reg_number_(reg_number) {
2550 SetPackedField<TypeField>(ToPhiType(type));
2551 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2552 // Phis are constructed live and marked dead if conflicting or unused.
2553 // Individual steps of SsaBuilder should assume that if a phi has been
2554 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2555 SetPackedFlag<kFlagIsLive>(true);
2556 SetPackedFlag<kFlagCanBeNull>(true);
2557 }
2558
2559 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2560 static Primitive::Type ToPhiType(Primitive::Type type) {
2561 return Primitive::PrimitiveKind(type);
2562 }
2563
2564 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2565
David Brazdildee58d62016-04-07 09:54:26 +00002566 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2567 void SetType(Primitive::Type new_type) {
2568 // Make sure that only valid type changes occur. The following are allowed:
2569 // (1) int -> float/ref (primitive type propagation),
2570 // (2) long -> double (primitive type propagation).
2571 DCHECK(GetType() == new_type ||
2572 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2573 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2574 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2575 SetPackedField<TypeField>(new_type);
2576 }
2577
2578 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2579 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2580
2581 uint32_t GetRegNumber() const { return reg_number_; }
2582
2583 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2584 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2585 bool IsDead() const { return !IsLive(); }
2586 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2587
Vladimir Markoe9004912016-06-16 16:50:52 +01002588 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002589 return other != nullptr
2590 && other->IsPhi()
2591 && other->AsPhi()->GetBlock() == GetBlock()
2592 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2593 }
2594
2595 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2596 // An equivalent phi is a phi having the same dex register and type.
2597 // It assumes that phis with the same dex register are adjacent.
2598 HPhi* GetNextEquivalentPhiWithSameType() {
2599 HInstruction* next = GetNext();
2600 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2601 if (next->GetType() == GetType()) {
2602 return next->AsPhi();
2603 }
2604 next = next->GetNext();
2605 }
2606 return nullptr;
2607 }
2608
2609 DECLARE_INSTRUCTION(Phi);
2610
David Brazdildee58d62016-04-07 09:54:26 +00002611 private:
2612 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2613 static constexpr size_t kFieldTypeSize =
2614 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2615 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2616 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2617 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2618 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2619 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2620
David Brazdildee58d62016-04-07 09:54:26 +00002621 const uint32_t reg_number_;
2622
2623 DISALLOW_COPY_AND_ASSIGN(HPhi);
2624};
2625
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002626// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002627// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002628// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002629class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002631 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002632
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002633 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002634
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002635 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002636
2637 private:
2638 DISALLOW_COPY_AND_ASSIGN(HExit);
2639};
2640
2641// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002642class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002643 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002644 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002645
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002646 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002647
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002648 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002649 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002650 }
2651
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002652 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002653
2654 private:
2655 DISALLOW_COPY_AND_ASSIGN(HGoto);
2656};
2657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658class HConstant : public HExpression<0> {
2659 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002660 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2661 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002662
2663 bool CanBeMoved() const OVERRIDE { return true; }
2664
Roland Levillain1a653882016-03-18 18:05:57 +00002665 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002666 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002667 // Is this constant 0 in the arithmetic sense?
2668 virtual bool IsArithmeticZero() const { return false; }
2669 // Is this constant a 0-bit pattern?
2670 virtual bool IsZeroBitPattern() const { return false; }
2671 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002672 virtual bool IsOne() const { return false; }
2673
David Brazdil77a48ae2015-09-15 12:34:04 +00002674 virtual uint64_t GetValueAsUint64() const = 0;
2675
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002676 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002677
2678 private:
2679 DISALLOW_COPY_AND_ASSIGN(HConstant);
2680};
2681
Vladimir Markofcb503c2016-05-18 12:48:17 +01002682class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002683 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002684 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002685 return true;
2686 }
2687
David Brazdil77a48ae2015-09-15 12:34:04 +00002688 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2689
Roland Levillain9867bc72015-08-05 10:21:34 +01002690 size_t ComputeHashCode() const OVERRIDE { return 0; }
2691
Roland Levillain1a653882016-03-18 18:05:57 +00002692 // The null constant representation is a 0-bit pattern.
2693 virtual bool IsZeroBitPattern() const { return true; }
2694
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 DECLARE_INSTRUCTION(NullConstant);
2696
2697 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002699
2700 friend class HGraph;
2701 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2702};
2703
2704// Constants of the type int. Those can be from Dex instructions, or
2705// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002706class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002707 public:
2708 int32_t GetValue() const { return value_; }
2709
David Brazdil9f389d42015-10-01 14:32:56 +01002710 uint64_t GetValueAsUint64() const OVERRIDE {
2711 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2712 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002713
Vladimir Marko372f10e2016-05-17 16:30:10 +01002714 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002715 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002716 return other->AsIntConstant()->value_ == value_;
2717 }
2718
2719 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2720
2721 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002722 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2723 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002724 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2725
Roland Levillain1a653882016-03-18 18:05:57 +00002726 // Integer constants are used to encode Boolean values as well,
2727 // where 1 means true and 0 means false.
2728 bool IsTrue() const { return GetValue() == 1; }
2729 bool IsFalse() const { return GetValue() == 0; }
2730
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 DECLARE_INSTRUCTION(IntConstant);
2732
2733 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002734 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2735 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2736 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2737 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002738
2739 const int32_t value_;
2740
2741 friend class HGraph;
2742 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2743 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2744 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2745};
2746
Vladimir Markofcb503c2016-05-18 12:48:17 +01002747class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002748 public:
2749 int64_t GetValue() const { return value_; }
2750
David Brazdil77a48ae2015-09-15 12:34:04 +00002751 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2752
Vladimir Marko372f10e2016-05-17 16:30:10 +01002753 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002754 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002755 return other->AsLongConstant()->value_ == value_;
2756 }
2757
2758 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2759
2760 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002761 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2762 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002763 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2764
2765 DECLARE_INSTRUCTION(LongConstant);
2766
2767 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002768 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2769 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002770
2771 const int64_t value_;
2772
2773 friend class HGraph;
2774 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2775};
Dave Allison20dfc792014-06-16 20:44:29 -07002776
Vladimir Markofcb503c2016-05-18 12:48:17 +01002777class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002778 public:
2779 float GetValue() const { return value_; }
2780
2781 uint64_t GetValueAsUint64() const OVERRIDE {
2782 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2783 }
2784
Vladimir Marko372f10e2016-05-17 16:30:10 +01002785 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002786 DCHECK(other->IsFloatConstant()) << other->DebugName();
2787 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2788 }
2789
2790 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2791
2792 bool IsMinusOne() const OVERRIDE {
2793 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2794 }
Roland Levillain1a653882016-03-18 18:05:57 +00002795 bool IsArithmeticZero() const OVERRIDE {
2796 return std::fpclassify(value_) == FP_ZERO;
2797 }
2798 bool IsArithmeticPositiveZero() const {
2799 return IsArithmeticZero() && !std::signbit(value_);
2800 }
2801 bool IsArithmeticNegativeZero() const {
2802 return IsArithmeticZero() && std::signbit(value_);
2803 }
2804 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002805 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002806 }
2807 bool IsOne() const OVERRIDE {
2808 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2809 }
2810 bool IsNaN() const {
2811 return std::isnan(value_);
2812 }
2813
2814 DECLARE_INSTRUCTION(FloatConstant);
2815
2816 private:
2817 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2818 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2819 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2820 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2821
2822 const float value_;
2823
2824 // Only the SsaBuilder and HGraph can create floating-point constants.
2825 friend class SsaBuilder;
2826 friend class HGraph;
2827 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2828};
2829
Vladimir Markofcb503c2016-05-18 12:48:17 +01002830class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002831 public:
2832 double GetValue() const { return value_; }
2833
2834 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2835
Vladimir Marko372f10e2016-05-17 16:30:10 +01002836 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002837 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2838 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2839 }
2840
2841 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2842
2843 bool IsMinusOne() const OVERRIDE {
2844 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2845 }
Roland Levillain1a653882016-03-18 18:05:57 +00002846 bool IsArithmeticZero() const OVERRIDE {
2847 return std::fpclassify(value_) == FP_ZERO;
2848 }
2849 bool IsArithmeticPositiveZero() const {
2850 return IsArithmeticZero() && !std::signbit(value_);
2851 }
2852 bool IsArithmeticNegativeZero() const {
2853 return IsArithmeticZero() && std::signbit(value_);
2854 }
2855 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002856 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002857 }
2858 bool IsOne() const OVERRIDE {
2859 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2860 }
2861 bool IsNaN() const {
2862 return std::isnan(value_);
2863 }
2864
2865 DECLARE_INSTRUCTION(DoubleConstant);
2866
2867 private:
2868 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2869 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2870 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2871 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2872
2873 const double value_;
2874
2875 // Only the SsaBuilder and HGraph can create floating-point constants.
2876 friend class SsaBuilder;
2877 friend class HGraph;
2878 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2879};
2880
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002881// Conditional branch. A block ending with an HIf instruction must have
2882// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002883class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002884 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002885 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2886 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002887 SetRawInputAt(0, input);
2888 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002889
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002890 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002891
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002892 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002893 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002894 }
2895
2896 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002897 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002898 }
2899
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002900 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002901
2902 private:
2903 DISALLOW_COPY_AND_ASSIGN(HIf);
2904};
2905
David Brazdilfc6a86a2015-06-26 10:33:45 +00002906
2907// Abstract instruction which marks the beginning and/or end of a try block and
2908// links it to the respective exception handlers. Behaves the same as a Goto in
2909// non-exceptional control flow.
2910// Normal-flow successor is stored at index zero, exception handlers under
2911// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002912class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002913 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002914 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002915 kEntry,
2916 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002917 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002918 };
2919
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002920 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2922 SetPackedField<BoundaryKindField>(kind);
2923 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002924
2925 bool IsControlFlow() const OVERRIDE { return true; }
2926
2927 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002928 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
David Brazdild26a4112015-11-10 11:07:31 +00002930 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2931 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2932 }
2933
David Brazdilfc6a86a2015-06-26 10:33:45 +00002934 // Returns whether `handler` is among its exception handlers (non-zero index
2935 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002936 bool HasExceptionHandler(const HBasicBlock& handler) const {
2937 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002938 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002939 }
2940
2941 // If not present already, adds `handler` to its block's list of exception
2942 // handlers.
2943 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002944 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002945 GetBlock()->AddSuccessor(handler);
2946 }
2947 }
2948
Vladimir Markoa1de9182016-02-25 11:37:38 +00002949 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2950 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002951
David Brazdilffee3d32015-07-06 11:48:53 +01002952 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2953
David Brazdilfc6a86a2015-06-26 10:33:45 +00002954 DECLARE_INSTRUCTION(TryBoundary);
2955
2956 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002957 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2958 static constexpr size_t kFieldBoundaryKindSize =
2959 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2960 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2961 kFieldBoundaryKind + kFieldBoundaryKindSize;
2962 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2963 "Too many packed fields.");
2964 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002965
2966 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2967};
2968
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002969// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002970class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002971 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002972 enum class Kind {
2973 kBCE,
2974 kInline,
2975 kLast = kInline
2976 };
2977
2978 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2979 // across.
2980 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2981 : HVariableInputSizeInstruction(
2982 SideEffects::All(),
2983 dex_pc,
2984 arena,
2985 /* number_of_inputs */ 1,
2986 kArenaAllocMisc) {
2987 SetPackedFlag<kFieldCanBeMoved>(false);
2988 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002989 SetRawInputAt(0, cond);
2990 }
2991
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002992 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
2993 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
2994 // instead of `guard`.
2995 // We set CanTriggerGC to prevent any intermediate address to be live
2996 // at the point of the `HDeoptimize`.
2997 HDeoptimize(ArenaAllocator* arena,
2998 HInstruction* cond,
2999 HInstruction* guard,
3000 Kind kind,
3001 uint32_t dex_pc)
3002 : HVariableInputSizeInstruction(
3003 SideEffects::CanTriggerGC(),
3004 dex_pc,
3005 arena,
3006 /* number_of_inputs */ 2,
3007 kArenaAllocMisc) {
3008 SetPackedFlag<kFieldCanBeMoved>(true);
3009 SetPackedField<DeoptimizeKindField>(kind);
3010 SetRawInputAt(0, cond);
3011 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003012 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003013
3014 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3015
3016 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3017 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3018 }
3019
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003020 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003021
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003022 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003023
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003024 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3025
3026 Primitive::Type GetType() const OVERRIDE {
3027 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3028 }
3029
3030 bool GuardsAnInput() const {
3031 return InputCount() == 2;
3032 }
3033
3034 HInstruction* GuardedInput() const {
3035 DCHECK(GuardsAnInput());
3036 return InputAt(1);
3037 }
3038
3039 void RemoveGuard() {
3040 RemoveInputAt(1);
3041 }
3042
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003043 DECLARE_INSTRUCTION(Deoptimize);
3044
3045 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003046 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3047 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3048 static constexpr size_t kFieldDeoptimizeKindSize =
3049 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3050 static constexpr size_t kNumberOfDeoptimizePackedBits =
3051 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3052 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3053 "Too many packed fields.");
3054 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3055
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003056 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3057};
3058
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003059std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3060
Mingyao Yang063fc772016-08-02 11:02:54 -07003061// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3062// The compiled code checks this flag value in a guard before devirtualized call and
3063// if it's true, starts to do deoptimization.
3064// It has a 4-byte slot on stack.
3065// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003066class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003067 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003068 // CHA guards are only optimized in a separate pass and it has no side effects
3069 // with regard to other passes.
3070 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3071 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003072 }
3073
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003074 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3075
3076 // We do all CHA guard elimination/motion in a single pass, after which there is no
3077 // further guard elimination/motion since a guard might have been used for justification
3078 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3079 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003080 bool CanBeMoved() const OVERRIDE { return false; }
3081
3082 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3083
3084 private:
3085 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3086};
3087
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003088// Represents the ArtMethod that was passed as a first argument to
3089// the method. It is used by instructions that depend on it, like
3090// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003091class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003092 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003093 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3094 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003095
3096 DECLARE_INSTRUCTION(CurrentMethod);
3097
3098 private:
3099 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3100};
3101
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003102// Fetches an ArtMethod from the virtual table or the interface method table
3103// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003104class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003105 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003106 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003107 kVTable,
3108 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003109 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003110 };
3111 HClassTableGet(HInstruction* cls,
3112 Primitive::Type type,
3113 TableKind kind,
3114 size_t index,
3115 uint32_t dex_pc)
3116 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003117 index_(index) {
3118 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003119 SetRawInputAt(0, cls);
3120 }
3121
3122 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003123 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003124 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003125 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003126 }
3127
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003129 size_t GetIndex() const { return index_; }
3130
3131 DECLARE_INSTRUCTION(ClassTableGet);
3132
3133 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003134 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3135 static constexpr size_t kFieldTableKindSize =
3136 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3137 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3138 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3139 "Too many packed fields.");
3140 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3141
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003142 // The index of the ArtMethod in the table.
3143 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003144
3145 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3146};
3147
Mark Mendellfe57faa2015-09-18 09:26:15 -04003148// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3149// have one successor for each entry in the switch table, and the final successor
3150// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003151class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003152 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003153 HPackedSwitch(int32_t start_value,
3154 uint32_t num_entries,
3155 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003156 uint32_t dex_pc = kNoDexPc)
3157 : HTemplateInstruction(SideEffects::None(), dex_pc),
3158 start_value_(start_value),
3159 num_entries_(num_entries) {
3160 SetRawInputAt(0, input);
3161 }
3162
3163 bool IsControlFlow() const OVERRIDE { return true; }
3164
3165 int32_t GetStartValue() const { return start_value_; }
3166
Vladimir Marko211c2112015-09-24 16:52:33 +01003167 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003168
3169 HBasicBlock* GetDefaultBlock() const {
3170 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003171 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003172 }
3173 DECLARE_INSTRUCTION(PackedSwitch);
3174
3175 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003176 const int32_t start_value_;
3177 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003178
3179 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3180};
3181
Roland Levillain88cb1752014-10-20 16:36:47 +01003182class HUnaryOperation : public HExpression<1> {
3183 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003184 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3185 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003186 SetRawInputAt(0, input);
3187 }
3188
3189 HInstruction* GetInput() const { return InputAt(0); }
3190 Primitive::Type GetResultType() const { return GetType(); }
3191
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003192 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003193 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003194 return true;
3195 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003196
Roland Levillain31dd3d62016-02-16 12:21:02 +00003197 // Try to statically evaluate `this` and return a HConstant
3198 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003199 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003200 HConstant* TryStaticEvaluation() const;
3201
3202 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003203 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3204 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003205 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3206 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003207
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003208 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003209
3210 private:
3211 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3212};
3213
Dave Allison20dfc792014-06-16 20:44:29 -07003214class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003215 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003216 HBinaryOperation(Primitive::Type result_type,
3217 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003218 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003219 SideEffects side_effects = SideEffects::None(),
3220 uint32_t dex_pc = kNoDexPc)
3221 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003222 SetRawInputAt(0, left);
3223 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003224 }
3225
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003226 HInstruction* GetLeft() const { return InputAt(0); }
3227 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003228 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003229
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003230 virtual bool IsCommutative() const { return false; }
3231
3232 // Put constant on the right.
3233 // Returns whether order is changed.
3234 bool OrderInputsWithConstantOnTheRight() {
3235 HInstruction* left = InputAt(0);
3236 HInstruction* right = InputAt(1);
3237 if (left->IsConstant() && !right->IsConstant()) {
3238 ReplaceInput(right, 0);
3239 ReplaceInput(left, 1);
3240 return true;
3241 }
3242 return false;
3243 }
3244
3245 // Order inputs by instruction id, but favor constant on the right side.
3246 // This helps GVN for commutative ops.
3247 void OrderInputs() {
3248 DCHECK(IsCommutative());
3249 HInstruction* left = InputAt(0);
3250 HInstruction* right = InputAt(1);
3251 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3252 return;
3253 }
3254 if (OrderInputsWithConstantOnTheRight()) {
3255 return;
3256 }
3257 // Order according to instruction id.
3258 if (left->GetId() > right->GetId()) {
3259 ReplaceInput(right, 0);
3260 ReplaceInput(left, 1);
3261 }
3262 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003263
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003264 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003265 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003266 return true;
3267 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003268
Roland Levillain31dd3d62016-02-16 12:21:02 +00003269 // Try to statically evaluate `this` and return a HConstant
3270 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003271 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003272 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003273
3274 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003275 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3276 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003277 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3278 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003280 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3281 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003282 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3283 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003284 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3285 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003286 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003287 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3288 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003289
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003290 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003291 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003292 HConstant* GetConstantRight() const;
3293
3294 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003295 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003296 HInstruction* GetLeastConstantLeft() const;
3297
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003298 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003299
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003300 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003301 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3302};
3303
Mark Mendellc4701932015-04-10 13:18:51 -04003304// The comparison bias applies for floating point operations and indicates how NaN
3305// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003306enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003307 kNoBias, // bias is not applicable (i.e. for long operation)
3308 kGtBias, // return 1 for NaN comparisons
3309 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003310 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003311};
3312
Roland Levillain31dd3d62016-02-16 12:21:02 +00003313std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3314
Dave Allison20dfc792014-06-16 20:44:29 -07003315class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003316 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003317 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003318 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3319 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3320 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003321
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003322 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003323 // `instruction`, and disregard moves in between.
3324 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003325
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003326 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003327
3328 virtual IfCondition GetCondition() const = 0;
3329
Mark Mendellc4701932015-04-10 13:18:51 -04003330 virtual IfCondition GetOppositeCondition() const = 0;
3331
Vladimir Markoa1de9182016-02-25 11:37:38 +00003332 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003333 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3334
Vladimir Markoa1de9182016-02-25 11:37:38 +00003335 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3336 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003337
Vladimir Marko372f10e2016-05-17 16:30:10 +01003338 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003339 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003340 }
3341
Roland Levillain4fa13f62015-07-06 18:11:54 +01003342 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003343 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003344 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003345 if (if_cond == kCondNE) {
3346 return true;
3347 } else if (if_cond == kCondEQ) {
3348 return false;
3349 }
3350 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003351 }
3352
3353 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003354 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003355 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003356 if (if_cond == kCondEQ) {
3357 return true;
3358 } else if (if_cond == kCondNE) {
3359 return false;
3360 }
3361 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003362 }
3363
Roland Levillain31dd3d62016-02-16 12:21:02 +00003364 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003365 // Needed if we merge a HCompare into a HCondition.
3366 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3367 static constexpr size_t kFieldComparisonBiasSize =
3368 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3369 static constexpr size_t kNumberOfConditionPackedBits =
3370 kFieldComparisonBias + kFieldComparisonBiasSize;
3371 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3372 using ComparisonBiasField =
3373 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3374
Roland Levillain31dd3d62016-02-16 12:21:02 +00003375 template <typename T>
3376 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3377
3378 template <typename T>
3379 int32_t CompareFP(T x, T y) const {
3380 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3381 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3382 // Handle the bias.
3383 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3384 }
3385
3386 // Return an integer constant containing the result of a condition evaluated at compile time.
3387 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3388 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3389 }
3390
Dave Allison20dfc792014-06-16 20:44:29 -07003391 private:
3392 DISALLOW_COPY_AND_ASSIGN(HCondition);
3393};
3394
3395// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003396class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003397 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003398 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3399 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003400
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003401 bool IsCommutative() const OVERRIDE { return true; }
3402
Vladimir Marko9e23df52015-11-10 17:14:35 +00003403 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3404 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003405 return MakeConstantCondition(true, GetDexPc());
3406 }
3407 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3408 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3409 }
3410 // In the following Evaluate methods, a HCompare instruction has
3411 // been merged into this HEqual instruction; evaluate it as
3412 // `Compare(x, y) == 0`.
3413 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3414 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3415 GetDexPc());
3416 }
3417 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3418 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3419 }
3420 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3421 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003422 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003423
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003424 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003425
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003426 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003427 return kCondEQ;
3428 }
3429
Mark Mendellc4701932015-04-10 13:18:51 -04003430 IfCondition GetOppositeCondition() const OVERRIDE {
3431 return kCondNE;
3432 }
3433
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003434 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003435 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003436
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003437 DISALLOW_COPY_AND_ASSIGN(HEqual);
3438};
3439
Vladimir Markofcb503c2016-05-18 12:48:17 +01003440class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003441 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003442 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3443 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003444
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003445 bool IsCommutative() const OVERRIDE { return true; }
3446
Vladimir Marko9e23df52015-11-10 17:14:35 +00003447 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3448 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003449 return MakeConstantCondition(false, GetDexPc());
3450 }
3451 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3452 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3453 }
3454 // In the following Evaluate methods, a HCompare instruction has
3455 // been merged into this HNotEqual instruction; evaluate it as
3456 // `Compare(x, y) != 0`.
3457 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3458 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3459 }
3460 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3461 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3462 }
3463 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3464 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003465 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003466
Dave Allison20dfc792014-06-16 20:44:29 -07003467 DECLARE_INSTRUCTION(NotEqual);
3468
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003469 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003470 return kCondNE;
3471 }
3472
Mark Mendellc4701932015-04-10 13:18:51 -04003473 IfCondition GetOppositeCondition() const OVERRIDE {
3474 return kCondEQ;
3475 }
3476
Dave Allison20dfc792014-06-16 20:44:29 -07003477 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003478 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003479
Dave Allison20dfc792014-06-16 20:44:29 -07003480 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3481};
3482
Vladimir Markofcb503c2016-05-18 12:48:17 +01003483class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003484 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003485 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3486 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003487
Roland Levillain9867bc72015-08-05 10:21:34 +01003488 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003489 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003490 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003491 // In the following Evaluate methods, a HCompare instruction has
3492 // been merged into this HLessThan instruction; evaluate it as
3493 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003494 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3496 }
3497 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3498 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3499 }
3500 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3501 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003502 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003503
Dave Allison20dfc792014-06-16 20:44:29 -07003504 DECLARE_INSTRUCTION(LessThan);
3505
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003506 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003507 return kCondLT;
3508 }
3509
Mark Mendellc4701932015-04-10 13:18:51 -04003510 IfCondition GetOppositeCondition() const OVERRIDE {
3511 return kCondGE;
3512 }
3513
Dave Allison20dfc792014-06-16 20:44:29 -07003514 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003515 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003516
Dave Allison20dfc792014-06-16 20:44:29 -07003517 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3518};
3519
Vladimir Markofcb503c2016-05-18 12:48:17 +01003520class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003521 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003522 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3523 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003524
Roland Levillain9867bc72015-08-05 10:21:34 +01003525 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003526 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003527 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003528 // In the following Evaluate methods, a HCompare instruction has
3529 // been merged into this HLessThanOrEqual instruction; evaluate it as
3530 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003531 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3533 }
3534 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3535 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3536 }
3537 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3538 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003539 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003540
Dave Allison20dfc792014-06-16 20:44:29 -07003541 DECLARE_INSTRUCTION(LessThanOrEqual);
3542
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003543 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003544 return kCondLE;
3545 }
3546
Mark Mendellc4701932015-04-10 13:18:51 -04003547 IfCondition GetOppositeCondition() const OVERRIDE {
3548 return kCondGT;
3549 }
3550
Dave Allison20dfc792014-06-16 20:44:29 -07003551 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003552 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003553
Dave Allison20dfc792014-06-16 20:44:29 -07003554 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3555};
3556
Vladimir Markofcb503c2016-05-18 12:48:17 +01003557class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003558 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003559 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3560 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003561
Roland Levillain9867bc72015-08-05 10:21:34 +01003562 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003563 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003564 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003565 // In the following Evaluate methods, a HCompare instruction has
3566 // been merged into this HGreaterThan instruction; evaluate it as
3567 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003568 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3570 }
3571 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3572 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3573 }
3574 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3575 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003576 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003577
Dave Allison20dfc792014-06-16 20:44:29 -07003578 DECLARE_INSTRUCTION(GreaterThan);
3579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003580 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003581 return kCondGT;
3582 }
3583
Mark Mendellc4701932015-04-10 13:18:51 -04003584 IfCondition GetOppositeCondition() const OVERRIDE {
3585 return kCondLE;
3586 }
3587
Dave Allison20dfc792014-06-16 20:44:29 -07003588 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003589 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003590
Dave Allison20dfc792014-06-16 20:44:29 -07003591 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3592};
3593
Vladimir Markofcb503c2016-05-18 12:48:17 +01003594class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003596 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3597 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003598
Roland Levillain9867bc72015-08-05 10:21:34 +01003599 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003601 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003602 // In the following Evaluate methods, a HCompare instruction has
3603 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3604 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003605 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3607 }
3608 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3609 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3610 }
3611 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3612 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003613 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003614
Dave Allison20dfc792014-06-16 20:44:29 -07003615 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3616
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003617 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003618 return kCondGE;
3619 }
3620
Mark Mendellc4701932015-04-10 13:18:51 -04003621 IfCondition GetOppositeCondition() const OVERRIDE {
3622 return kCondLT;
3623 }
3624
Dave Allison20dfc792014-06-16 20:44:29 -07003625 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003626 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003627
Dave Allison20dfc792014-06-16 20:44:29 -07003628 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3629};
3630
Vladimir Markofcb503c2016-05-18 12:48:17 +01003631class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003632 public:
3633 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3634 : HCondition(first, second, dex_pc) {}
3635
3636 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003637 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003638 }
3639 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3641 }
3642 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3643 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3644 LOG(FATAL) << DebugName() << " is not defined for float values";
3645 UNREACHABLE();
3646 }
3647 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3648 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3649 LOG(FATAL) << DebugName() << " is not defined for double values";
3650 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003651 }
3652
3653 DECLARE_INSTRUCTION(Below);
3654
3655 IfCondition GetCondition() const OVERRIDE {
3656 return kCondB;
3657 }
3658
3659 IfCondition GetOppositeCondition() const OVERRIDE {
3660 return kCondAE;
3661 }
3662
3663 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003664 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003665 return MakeUnsigned(x) < MakeUnsigned(y);
3666 }
Aart Bike9f37602015-10-09 11:15:55 -07003667
3668 DISALLOW_COPY_AND_ASSIGN(HBelow);
3669};
3670
Vladimir Markofcb503c2016-05-18 12:48:17 +01003671class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003672 public:
3673 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3674 : HCondition(first, second, dex_pc) {}
3675
3676 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003677 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003678 }
3679 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3681 }
3682 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3683 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3684 LOG(FATAL) << DebugName() << " is not defined for float values";
3685 UNREACHABLE();
3686 }
3687 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3688 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3689 LOG(FATAL) << DebugName() << " is not defined for double values";
3690 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003691 }
3692
3693 DECLARE_INSTRUCTION(BelowOrEqual);
3694
3695 IfCondition GetCondition() const OVERRIDE {
3696 return kCondBE;
3697 }
3698
3699 IfCondition GetOppositeCondition() const OVERRIDE {
3700 return kCondA;
3701 }
3702
3703 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003704 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003705 return MakeUnsigned(x) <= MakeUnsigned(y);
3706 }
Aart Bike9f37602015-10-09 11:15:55 -07003707
3708 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3709};
3710
Vladimir Markofcb503c2016-05-18 12:48:17 +01003711class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003712 public:
3713 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3714 : HCondition(first, second, dex_pc) {}
3715
3716 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003717 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003718 }
3719 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3721 }
3722 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3723 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3724 LOG(FATAL) << DebugName() << " is not defined for float values";
3725 UNREACHABLE();
3726 }
3727 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3728 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3729 LOG(FATAL) << DebugName() << " is not defined for double values";
3730 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003731 }
3732
3733 DECLARE_INSTRUCTION(Above);
3734
3735 IfCondition GetCondition() const OVERRIDE {
3736 return kCondA;
3737 }
3738
3739 IfCondition GetOppositeCondition() const OVERRIDE {
3740 return kCondBE;
3741 }
3742
3743 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003744 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 return MakeUnsigned(x) > MakeUnsigned(y);
3746 }
Aart Bike9f37602015-10-09 11:15:55 -07003747
3748 DISALLOW_COPY_AND_ASSIGN(HAbove);
3749};
3750
Vladimir Markofcb503c2016-05-18 12:48:17 +01003751class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003752 public:
3753 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3754 : HCondition(first, second, dex_pc) {}
3755
3756 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003758 }
3759 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3761 }
3762 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3763 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3764 LOG(FATAL) << DebugName() << " is not defined for float values";
3765 UNREACHABLE();
3766 }
3767 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3768 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3769 LOG(FATAL) << DebugName() << " is not defined for double values";
3770 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003771 }
3772
3773 DECLARE_INSTRUCTION(AboveOrEqual);
3774
3775 IfCondition GetCondition() const OVERRIDE {
3776 return kCondAE;
3777 }
3778
3779 IfCondition GetOppositeCondition() const OVERRIDE {
3780 return kCondB;
3781 }
3782
3783 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003784 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003785 return MakeUnsigned(x) >= MakeUnsigned(y);
3786 }
Aart Bike9f37602015-10-09 11:15:55 -07003787
3788 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3789};
Dave Allison20dfc792014-06-16 20:44:29 -07003790
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003791// Instruction to check how two inputs compare to each other.
3792// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003793class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003794 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003795 // Note that `comparison_type` is the type of comparison performed
3796 // between the comparison's inputs, not the type of the instantiated
3797 // HCompare instruction (which is always Primitive::kPrimInt).
3798 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003799 HInstruction* first,
3800 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003801 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003803 : HBinaryOperation(Primitive::kPrimInt,
3804 first,
3805 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003806 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003807 dex_pc) {
3808 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003809 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3810 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003811 }
3812
Roland Levillain9867bc72015-08-05 10:21:34 +01003813 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003814 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3815
3816 template <typename T>
3817 int32_t ComputeFP(T x, T y) const {
3818 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3819 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3820 // Handle the bias.
3821 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3822 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003823
Roland Levillain9867bc72015-08-05 10:21:34 +01003824 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003825 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3826 // reach this code path when processing a freshly built HIR
3827 // graph. However HCompare integer instructions can be synthesized
3828 // by the instruction simplifier to implement IntegerCompare and
3829 // IntegerSignum intrinsics, so we have to handle this case.
3830 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003831 }
3832 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003833 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3834 }
3835 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3836 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3837 }
3838 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3839 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003840 }
3841
Vladimir Marko372f10e2016-05-17 16:30:10 +01003842 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003843 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003844 }
3845
Vladimir Markoa1de9182016-02-25 11:37:38 +00003846 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003847
Roland Levillain31dd3d62016-02-16 12:21:02 +00003848 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003849 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003850 bool IsGtBias() const {
3851 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003852 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003853 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003854
Roland Levillain1693a1f2016-03-15 14:57:31 +00003855 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3856 // Comparisons do not require a runtime call in any back end.
3857 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003858 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003859
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003860 DECLARE_INSTRUCTION(Compare);
3861
Roland Levillain31dd3d62016-02-16 12:21:02 +00003862 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003863 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3864 static constexpr size_t kFieldComparisonBiasSize =
3865 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3866 static constexpr size_t kNumberOfComparePackedBits =
3867 kFieldComparisonBias + kFieldComparisonBiasSize;
3868 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3869 using ComparisonBiasField =
3870 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3871
Roland Levillain31dd3d62016-02-16 12:21:02 +00003872 // Return an integer constant containing the result of a comparison evaluated at compile time.
3873 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3874 DCHECK(value == -1 || value == 0 || value == 1) << value;
3875 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3876 }
3877
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003878 private:
3879 DISALLOW_COPY_AND_ASSIGN(HCompare);
3880};
3881
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003882class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003883 public:
3884 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003885 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003886 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003887 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003888 bool finalizable,
3889 QuickEntrypointEnum entrypoint)
3890 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3891 type_index_(type_index),
3892 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003893 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003894 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003895 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003896 }
3897
Andreas Gampea5b09a62016-11-17 15:21:22 -08003898 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003899 const DexFile& GetDexFile() const { return dex_file_; }
3900
3901 // Calls runtime so needs an environment.
3902 bool NeedsEnvironment() const OVERRIDE { return true; }
3903
Mingyao Yang062157f2016-03-02 10:15:36 -08003904 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3905 bool CanThrow() const OVERRIDE { return true; }
3906
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003907 bool NeedsChecks() const {
3908 return entrypoint_ == kQuickAllocObjectWithChecks;
3909 }
David Brazdil6de19382016-01-08 17:37:10 +00003910
Vladimir Markoa1de9182016-02-25 11:37:38 +00003911 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003912
3913 bool CanBeNull() const OVERRIDE { return false; }
3914
3915 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3916
3917 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3918 entrypoint_ = entrypoint;
3919 }
3920
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003921 HLoadClass* GetLoadClass() const {
3922 HInstruction* input = InputAt(0);
3923 if (input->IsClinitCheck()) {
3924 input = input->InputAt(0);
3925 }
3926 DCHECK(input->IsLoadClass());
3927 return input->AsLoadClass();
3928 }
3929
David Brazdil6de19382016-01-08 17:37:10 +00003930 bool IsStringAlloc() const;
3931
3932 DECLARE_INSTRUCTION(NewInstance);
3933
3934 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003935 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003936 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3937 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3938 "Too many packed fields.");
3939
Andreas Gampea5b09a62016-11-17 15:21:22 -08003940 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003941 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003942 QuickEntrypointEnum entrypoint_;
3943
3944 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3945};
3946
Agi Csaki05f20562015-08-19 14:58:14 -07003947enum IntrinsicNeedsEnvironmentOrCache {
3948 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3949 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003950};
3951
Aart Bik5d75afe2015-12-14 11:57:01 -08003952enum IntrinsicSideEffects {
3953 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3954 kReadSideEffects, // Intrinsic may read heap memory.
3955 kWriteSideEffects, // Intrinsic may write heap memory.
3956 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3957};
3958
3959enum IntrinsicExceptions {
3960 kNoThrow, // Intrinsic does not throw any exceptions.
3961 kCanThrow // Intrinsic may throw exceptions.
3962};
3963
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003964class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003965 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003966 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003967
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003968 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003969 SetRawInputAt(index, argument);
3970 }
3971
Roland Levillain3e3d7332015-04-28 11:00:54 +01003972 // Return the number of arguments. This number can be lower than
3973 // the number of inputs returned by InputCount(), as some invoke
3974 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3975 // inputs at the end of their list of inputs.
3976 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3977
Vladimir Markoa1de9182016-02-25 11:37:38 +00003978 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003979
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003980 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3981
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003982 InvokeType GetInvokeType() const {
3983 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003984 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003985
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003986 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003987 return intrinsic_;
3988 }
3989
Aart Bik5d75afe2015-12-14 11:57:01 -08003990 void SetIntrinsic(Intrinsics intrinsic,
3991 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3992 IntrinsicSideEffects side_effects,
3993 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003994
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003995 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003996 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003997 }
3998
Aart Bikff7d89c2016-11-07 08:49:28 -08003999 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4000
Vladimir Markoa1de9182016-02-25 11:37:38 +00004001 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004002
Aart Bik71bf7b42016-11-16 10:17:46 -08004003 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004004
Vladimir Marko372f10e2016-05-17 16:30:10 +01004005 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004006 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4007 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004008
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004009 uint32_t* GetIntrinsicOptimizations() {
4010 return &intrinsic_optimizations_;
4011 }
4012
4013 const uint32_t* GetIntrinsicOptimizations() const {
4014 return &intrinsic_optimizations_;
4015 }
4016
4017 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4018
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004019 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004020 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004021
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004022 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004023
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004024 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004025 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4026 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004027 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4028 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004029 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004030 static constexpr size_t kFieldReturnTypeSize =
4031 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4032 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4033 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4034 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004035 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004036 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4037
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004038 HInvoke(ArenaAllocator* arena,
4039 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004040 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004041 Primitive::Type return_type,
4042 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004043 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004044 ArtMethod* resolved_method,
4045 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004046 : HVariableInputSizeInstruction(
4047 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4048 dex_pc,
4049 arena,
4050 number_of_arguments + number_of_other_inputs,
4051 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004052 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004053 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004054 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004055 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004056 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004057 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004058 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004059 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004060 }
4061
Roland Levillain3e3d7332015-04-28 11:00:54 +01004062 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004063 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004064 const uint32_t dex_method_index_;
4065 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004066
4067 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4068 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004069
4070 private:
4071 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4072};
4073
Vladimir Markofcb503c2016-05-18 12:48:17 +01004074class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004075 public:
4076 HInvokeUnresolved(ArenaAllocator* arena,
4077 uint32_t number_of_arguments,
4078 Primitive::Type return_type,
4079 uint32_t dex_pc,
4080 uint32_t dex_method_index,
4081 InvokeType invoke_type)
4082 : HInvoke(arena,
4083 number_of_arguments,
4084 0u /* number_of_other_inputs */,
4085 return_type,
4086 dex_pc,
4087 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004088 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004089 invoke_type) {
4090 }
4091
4092 DECLARE_INSTRUCTION(InvokeUnresolved);
4093
4094 private:
4095 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4096};
4097
Orion Hodsonac141392017-01-13 11:53:47 +00004098class HInvokePolymorphic FINAL : public HInvoke {
4099 public:
4100 HInvokePolymorphic(ArenaAllocator* arena,
4101 uint32_t number_of_arguments,
4102 Primitive::Type return_type,
4103 uint32_t dex_pc,
4104 uint32_t dex_method_index)
4105 : HInvoke(arena,
4106 number_of_arguments,
4107 0u /* number_of_other_inputs */,
4108 return_type,
4109 dex_pc,
4110 dex_method_index,
4111 nullptr,
4112 kVirtual) {}
4113
4114 DECLARE_INSTRUCTION(InvokePolymorphic);
4115
4116 private:
4117 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4118};
4119
Vladimir Markofcb503c2016-05-18 12:48:17 +01004120class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004121 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004122 // Requirements of this method call regarding the class
4123 // initialization (clinit) check of its declaring class.
4124 enum class ClinitCheckRequirement {
4125 kNone, // Class already initialized.
4126 kExplicit, // Static call having explicit clinit check as last input.
4127 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004128 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004129 };
4130
Vladimir Marko58155012015-08-19 12:49:41 +00004131 // Determines how to load the target ArtMethod*.
4132 enum class MethodLoadKind {
4133 // Use a String init ArtMethod* loaded from Thread entrypoints.
4134 kStringInit,
4135
4136 // Use the method's own ArtMethod* loaded by the register allocator.
4137 kRecursive,
4138
4139 // Use ArtMethod* at a known address, embed the direct address in the code.
4140 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4141 kDirectAddress,
4142
Vladimir Markoa1de9182016-02-25 11:37:38 +00004143 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004144 // Used when we need to use the dex cache, for example for invoke-static that
4145 // may cause class initialization (the entry may point to a resolution method),
4146 // and we know that we can access the dex cache arrays using a PC-relative load.
4147 kDexCachePcRelative,
4148
4149 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4150 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4151 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4152 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4153 kDexCacheViaMethod,
4154 };
4155
4156 // Determines the location of the code pointer.
4157 enum class CodePtrLocation {
4158 // Recursive call, use local PC-relative call instruction.
4159 kCallSelf,
4160
Vladimir Marko58155012015-08-19 12:49:41 +00004161 // Use code pointer from the ArtMethod*.
4162 // Used when we don't know the target code. This is also the last-resort-kind used when
4163 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4164 kCallArtMethod,
4165 };
4166
4167 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004168 MethodLoadKind method_load_kind;
4169 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004170 // The method load data holds
4171 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4172 // Note that there are multiple string init methods, each having its own offset.
4173 // - the method address for kDirectAddress
4174 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004175 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004176 };
4177
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004178 HInvokeStaticOrDirect(ArenaAllocator* arena,
4179 uint32_t number_of_arguments,
4180 Primitive::Type return_type,
4181 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004182 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004183 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004184 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004185 InvokeType invoke_type,
4186 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004187 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004188 : HInvoke(arena,
4189 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004190 // There is potentially one extra argument for the HCurrentMethod node, and
4191 // potentially one other if the clinit check is explicit, and potentially
4192 // one other if the method is a string factory.
4193 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004194 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004195 return_type,
4196 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004197 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004198 resolved_method,
4199 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004200 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004201 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004202 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4203 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004204
Vladimir Markodc151b22015-10-15 18:02:30 +01004205 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004206 bool had_current_method_input = HasCurrentMethodInput();
4207 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4208
4209 // Using the current method is the default and once we find a better
4210 // method load kind, we should not go back to using the current method.
4211 DCHECK(had_current_method_input || !needs_current_method_input);
4212
4213 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004214 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4215 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004216 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004217 dispatch_info_ = dispatch_info;
4218 }
4219
Aart Bik6daebeb2017-04-03 14:35:41 -07004220 DispatchInfo GetDispatchInfo() const {
4221 return dispatch_info_;
4222 }
4223
Vladimir Markoc53c0792015-11-19 15:48:33 +00004224 void AddSpecialInput(HInstruction* input) {
4225 // We allow only one special input.
4226 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4227 DCHECK(InputCount() == GetSpecialInputIndex() ||
4228 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4229 InsertInputAt(GetSpecialInputIndex(), input);
4230 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004231
Vladimir Marko372f10e2016-05-17 16:30:10 +01004232 using HInstruction::GetInputRecords; // Keep the const version visible.
4233 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4234 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4235 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4236 DCHECK(!input_records.empty());
4237 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4238 HInstruction* last_input = input_records.back().GetInstruction();
4239 // Note: `last_input` may be null during arguments setup.
4240 if (last_input != nullptr) {
4241 // `last_input` is the last input of a static invoke marked as having
4242 // an explicit clinit check. It must either be:
4243 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4244 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4245 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4246 }
4247 }
4248 return input_records;
4249 }
4250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004251 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004252 // We access the method via the dex cache so we can't do an implicit null check.
4253 // TODO: for intrinsics we can generate implicit null checks.
4254 return false;
4255 }
4256
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004257 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004258 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004259 }
4260
Vladimir Markoc53c0792015-11-19 15:48:33 +00004261 // Get the index of the special input, if any.
4262 //
David Brazdil6de19382016-01-08 17:37:10 +00004263 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4264 // method pointer; otherwise there may be one platform-specific special input,
4265 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004266 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004267 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004268
Vladimir Marko58155012015-08-19 12:49:41 +00004269 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4270 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4271 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004272 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004273 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004274 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004275 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004276 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4277 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004278 bool HasCurrentMethodInput() const {
4279 // This function can be called only after the invoke has been fully initialized by the builder.
4280 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004281 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004282 return true;
4283 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004284 DCHECK(InputCount() == GetSpecialInputIndex() ||
4285 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004286 return false;
4287 }
4288 }
Vladimir Marko58155012015-08-19 12:49:41 +00004289
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004290 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004291 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004292 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004293 }
4294
4295 uint64_t GetMethodAddress() const {
4296 DCHECK(HasMethodAddress());
4297 return dispatch_info_.method_load_data;
4298 }
4299
4300 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004301 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004302 return dispatch_info_.method_load_data;
4303 }
4304
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004305 const DexFile& GetDexFileForPcRelativeDexCache() const;
4306
Vladimir Markoa1de9182016-02-25 11:37:38 +00004307 ClinitCheckRequirement GetClinitCheckRequirement() const {
4308 return GetPackedField<ClinitCheckRequirementField>();
4309 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004310
Roland Levillain4c0eb422015-04-24 16:43:49 +01004311 // Is this instruction a call to a static method?
4312 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004313 return GetInvokeType() == kStatic;
4314 }
4315
4316 MethodReference GetTargetMethod() const {
4317 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004318 }
4319
Vladimir Markofbb184a2015-11-13 14:47:00 +00004320 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4321 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4322 // instruction; only relevant for static calls with explicit clinit check.
4323 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004324 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004325 size_t last_input_index = inputs_.size() - 1u;
4326 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004327 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004328 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004329 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004330 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004331 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004332 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004333 }
4334
4335 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004336 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004337 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004338 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004339 }
4340
4341 // Is this a call to a static method whose declaring class has an
4342 // implicit intialization check requirement?
4343 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004344 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004345 }
4346
Vladimir Markob554b5a2015-11-06 12:57:55 +00004347 // Does this method load kind need the current method as an input?
4348 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4349 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4350 }
4351
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004352 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004353
4354 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004355 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004356 static constexpr size_t kFieldClinitCheckRequirementSize =
4357 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4358 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4359 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4360 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4361 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004362 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4363 kFieldClinitCheckRequirement,
4364 kFieldClinitCheckRequirementSize>;
4365
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004366 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004367 MethodReference target_method_;
4368 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004370 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004371};
Vladimir Markof64242a2015-12-01 14:58:23 +00004372std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004373std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374
Vladimir Markofcb503c2016-05-18 12:48:17 +01004375class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004376 public:
4377 HInvokeVirtual(ArenaAllocator* arena,
4378 uint32_t number_of_arguments,
4379 Primitive::Type return_type,
4380 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004381 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004382 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004383 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004384 : HInvoke(arena,
4385 number_of_arguments,
4386 0u,
4387 return_type,
4388 dex_pc,
4389 dex_method_index,
4390 resolved_method,
4391 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004392 vtable_index_(vtable_index) {}
4393
Aart Bik71bf7b42016-11-16 10:17:46 -08004394 bool CanBeNull() const OVERRIDE {
4395 switch (GetIntrinsic()) {
4396 case Intrinsics::kThreadCurrentThread:
4397 case Intrinsics::kStringBufferAppend:
4398 case Intrinsics::kStringBufferToString:
4399 case Intrinsics::kStringBuilderAppend:
4400 case Intrinsics::kStringBuilderToString:
4401 return false;
4402 default:
4403 return HInvoke::CanBeNull();
4404 }
4405 }
4406
Calin Juravle641547a2015-04-21 22:08:51 +01004407 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004408 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004409 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004410 }
4411
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004412 uint32_t GetVTableIndex() const { return vtable_index_; }
4413
4414 DECLARE_INSTRUCTION(InvokeVirtual);
4415
4416 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004417 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004418 const uint32_t vtable_index_;
4419
4420 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4421};
4422
Vladimir Markofcb503c2016-05-18 12:48:17 +01004423class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004424 public:
4425 HInvokeInterface(ArenaAllocator* arena,
4426 uint32_t number_of_arguments,
4427 Primitive::Type return_type,
4428 uint32_t dex_pc,
4429 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004430 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004431 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004432 : HInvoke(arena,
4433 number_of_arguments,
4434 0u,
4435 return_type,
4436 dex_pc,
4437 dex_method_index,
4438 resolved_method,
4439 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004440 imt_index_(imt_index) {}
4441
Calin Juravle641547a2015-04-21 22:08:51 +01004442 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004443 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004444 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004445 }
4446
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004447 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4448 // The assembly stub currently needs it.
4449 return true;
4450 }
4451
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004452 uint32_t GetImtIndex() const { return imt_index_; }
4453 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4454
4455 DECLARE_INSTRUCTION(InvokeInterface);
4456
4457 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004458 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004459 const uint32_t imt_index_;
4460
4461 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4462};
4463
Vladimir Markofcb503c2016-05-18 12:48:17 +01004464class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004465 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004466 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004467 : HUnaryOperation(result_type, input, dex_pc) {
4468 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4469 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004470
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004471 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004472
4473 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004474 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004475 }
4476 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004478 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004479 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4480 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4481 }
4482 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4483 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4484 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004485
Roland Levillain88cb1752014-10-20 16:36:47 +01004486 DECLARE_INSTRUCTION(Neg);
4487
4488 private:
4489 DISALLOW_COPY_AND_ASSIGN(HNeg);
4490};
4491
Vladimir Markofcb503c2016-05-18 12:48:17 +01004492class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004493 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004494 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4495 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4496 SetRawInputAt(0, cls);
4497 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004498 }
4499
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004500 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004501 bool NeedsEnvironment() const OVERRIDE { return true; }
4502
Mingyao Yang0c365e62015-03-31 15:09:29 -07004503 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4504 bool CanThrow() const OVERRIDE { return true; }
4505
Calin Juravle10e244f2015-01-26 18:54:32 +00004506 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004507
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004508 HLoadClass* GetLoadClass() const {
4509 DCHECK(InputAt(0)->IsLoadClass());
4510 return InputAt(0)->AsLoadClass();
4511 }
4512
4513 HInstruction* GetLength() const {
4514 return InputAt(1);
4515 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004516
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004517 DECLARE_INSTRUCTION(NewArray);
4518
4519 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004520 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4521};
4522
Vladimir Markofcb503c2016-05-18 12:48:17 +01004523class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004525 HAdd(Primitive::Type result_type,
4526 HInstruction* left,
4527 HInstruction* right,
4528 uint32_t dex_pc = kNoDexPc)
4529 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004531 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004532
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004533 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004534
4535 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004536 return GetBlock()->GetGraph()->GetIntConstant(
4537 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004538 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 return GetBlock()->GetGraph()->GetLongConstant(
4541 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004542 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004543 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4544 return GetBlock()->GetGraph()->GetFloatConstant(
4545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4546 }
4547 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4548 return GetBlock()->GetGraph()->GetDoubleConstant(
4549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4550 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004551
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004552 DECLARE_INSTRUCTION(Add);
4553
4554 private:
4555 DISALLOW_COPY_AND_ASSIGN(HAdd);
4556};
4557
Vladimir Markofcb503c2016-05-18 12:48:17 +01004558class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004559 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004560 HSub(Primitive::Type result_type,
4561 HInstruction* left,
4562 HInstruction* right,
4563 uint32_t dex_pc = kNoDexPc)
4564 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004565
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004566 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004567
4568 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004569 return GetBlock()->GetGraph()->GetIntConstant(
4570 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004571 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004573 return GetBlock()->GetGraph()->GetLongConstant(
4574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004575 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004576 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4577 return GetBlock()->GetGraph()->GetFloatConstant(
4578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4579 }
4580 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4581 return GetBlock()->GetGraph()->GetDoubleConstant(
4582 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4583 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004584
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004585 DECLARE_INSTRUCTION(Sub);
4586
4587 private:
4588 DISALLOW_COPY_AND_ASSIGN(HSub);
4589};
4590
Vladimir Markofcb503c2016-05-18 12:48:17 +01004591class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004592 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004593 HMul(Primitive::Type result_type,
4594 HInstruction* left,
4595 HInstruction* right,
4596 uint32_t dex_pc = kNoDexPc)
4597 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004598
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004599 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004600
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004601 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004602
4603 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004604 return GetBlock()->GetGraph()->GetIntConstant(
4605 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004606 }
4607 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 return GetBlock()->GetGraph()->GetLongConstant(
4609 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004611 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4612 return GetBlock()->GetGraph()->GetFloatConstant(
4613 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4614 }
4615 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4616 return GetBlock()->GetGraph()->GetDoubleConstant(
4617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4618 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004619
4620 DECLARE_INSTRUCTION(Mul);
4621
4622 private:
4623 DISALLOW_COPY_AND_ASSIGN(HMul);
4624};
4625
Vladimir Markofcb503c2016-05-18 12:48:17 +01004626class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004628 HDiv(Primitive::Type result_type,
4629 HInstruction* left,
4630 HInstruction* right,
4631 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004632 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004633
Roland Levillain9867bc72015-08-05 10:21:34 +01004634 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004635 T ComputeIntegral(T x, T y) const {
4636 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 // Our graph structure ensures we never have 0 for `y` during
4638 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004639 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004640 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004641 return (y == -1) ? -x : x / y;
4642 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004643
Roland Levillain31dd3d62016-02-16 12:21:02 +00004644 template <typename T>
4645 T ComputeFP(T x, T y) const {
4646 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4647 return x / y;
4648 }
4649
Roland Levillain9867bc72015-08-05 10:21:34 +01004650 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004651 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004652 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 }
4654 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004656 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4657 }
4658 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4659 return GetBlock()->GetGraph()->GetFloatConstant(
4660 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4661 }
4662 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4663 return GetBlock()->GetGraph()->GetDoubleConstant(
4664 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004665 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004666
4667 DECLARE_INSTRUCTION(Div);
4668
4669 private:
4670 DISALLOW_COPY_AND_ASSIGN(HDiv);
4671};
4672
Vladimir Markofcb503c2016-05-18 12:48:17 +01004673class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004674 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004675 HRem(Primitive::Type result_type,
4676 HInstruction* left,
4677 HInstruction* right,
4678 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004679 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004680
Roland Levillain9867bc72015-08-05 10:21:34 +01004681 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004682 T ComputeIntegral(T x, T y) const {
4683 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 // Our graph structure ensures we never have 0 for `y` during
4685 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004686 DCHECK_NE(y, 0);
4687 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4688 return (y == -1) ? 0 : x % y;
4689 }
4690
Roland Levillain31dd3d62016-02-16 12:21:02 +00004691 template <typename T>
4692 T ComputeFP(T x, T y) const {
4693 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4694 return std::fmod(x, y);
4695 }
4696
Roland Levillain9867bc72015-08-05 10:21:34 +01004697 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004698 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004699 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 }
4701 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004703 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004704 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004705 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4706 return GetBlock()->GetGraph()->GetFloatConstant(
4707 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4708 }
4709 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4710 return GetBlock()->GetGraph()->GetDoubleConstant(
4711 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4712 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004713
4714 DECLARE_INSTRUCTION(Rem);
4715
4716 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004717 DISALLOW_COPY_AND_ASSIGN(HRem);
4718};
4719
Vladimir Markofcb503c2016-05-18 12:48:17 +01004720class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004721 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004722 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4723 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004724 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004725 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004726 SetRawInputAt(0, value);
4727 }
4728
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004729 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4730
Calin Juravled0d48522014-11-04 16:40:20 +00004731 bool CanBeMoved() const OVERRIDE { return true; }
4732
Vladimir Marko372f10e2016-05-17 16:30:10 +01004733 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004734 return true;
4735 }
4736
4737 bool NeedsEnvironment() const OVERRIDE { return true; }
4738 bool CanThrow() const OVERRIDE { return true; }
4739
Calin Juravled0d48522014-11-04 16:40:20 +00004740 DECLARE_INSTRUCTION(DivZeroCheck);
4741
4742 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004743 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4744};
4745
Vladimir Markofcb503c2016-05-18 12:48:17 +01004746class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004747 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004748 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HInstruction* value,
4750 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4753 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4754 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004755 }
4756
Roland Levillain5b5b9312016-03-22 14:57:31 +00004757 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004758 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004759 return value << (distance & max_shift_distance);
4760 }
4761
4762 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004763 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004765 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004769 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4771 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4772 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4773 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004774 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004775 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4776 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004777 LOG(FATAL) << DebugName() << " is not defined for float values";
4778 UNREACHABLE();
4779 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4781 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004782 LOG(FATAL) << DebugName() << " is not defined for double values";
4783 UNREACHABLE();
4784 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004785
4786 DECLARE_INSTRUCTION(Shl);
4787
4788 private:
4789 DISALLOW_COPY_AND_ASSIGN(HShl);
4790};
4791
Vladimir Markofcb503c2016-05-18 12:48:17 +01004792class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004793 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004794 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004795 HInstruction* value,
4796 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4799 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4800 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004801 }
4802
Roland Levillain5b5b9312016-03-22 14:57:31 +00004803 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004804 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004805 return value >> (distance & max_shift_distance);
4806 }
4807
4808 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004809 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004810 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004811 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004812 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004814 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4817 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4818 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4819 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004820 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4822 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004823 LOG(FATAL) << DebugName() << " is not defined for float values";
4824 UNREACHABLE();
4825 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004826 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4827 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004828 LOG(FATAL) << DebugName() << " is not defined for double values";
4829 UNREACHABLE();
4830 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004831
4832 DECLARE_INSTRUCTION(Shr);
4833
4834 private:
4835 DISALLOW_COPY_AND_ASSIGN(HShr);
4836};
4837
Vladimir Markofcb503c2016-05-18 12:48:17 +01004838class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004839 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004840 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004841 HInstruction* value,
4842 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4845 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4846 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004847 }
4848
Roland Levillain5b5b9312016-03-22 14:57:31 +00004849 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004850 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004851 typedef typename std::make_unsigned<T>::type V;
4852 V ux = static_cast<V>(value);
4853 return static_cast<T>(ux >> (distance & max_shift_distance));
4854 }
4855
4856 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004857 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004858 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004859 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004860 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004863 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4865 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4866 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4867 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004868 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004869 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4870 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004871 LOG(FATAL) << DebugName() << " is not defined for float values";
4872 UNREACHABLE();
4873 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004874 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4875 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004876 LOG(FATAL) << DebugName() << " is not defined for double values";
4877 UNREACHABLE();
4878 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004879
4880 DECLARE_INSTRUCTION(UShr);
4881
4882 private:
4883 DISALLOW_COPY_AND_ASSIGN(HUShr);
4884};
4885
Vladimir Markofcb503c2016-05-18 12:48:17 +01004886class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004888 HAnd(Primitive::Type result_type,
4889 HInstruction* left,
4890 HInstruction* right,
4891 uint32_t dex_pc = kNoDexPc)
4892 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004893
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004894 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004895
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004896 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004897
4898 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004899 return GetBlock()->GetGraph()->GetIntConstant(
4900 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004901 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004902 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004903 return GetBlock()->GetGraph()->GetLongConstant(
4904 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004906 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4907 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4908 LOG(FATAL) << DebugName() << " is not defined for float values";
4909 UNREACHABLE();
4910 }
4911 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4912 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4913 LOG(FATAL) << DebugName() << " is not defined for double values";
4914 UNREACHABLE();
4915 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004916
4917 DECLARE_INSTRUCTION(And);
4918
4919 private:
4920 DISALLOW_COPY_AND_ASSIGN(HAnd);
4921};
4922
Vladimir Markofcb503c2016-05-18 12:48:17 +01004923class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004924 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004925 HOr(Primitive::Type result_type,
4926 HInstruction* left,
4927 HInstruction* right,
4928 uint32_t dex_pc = kNoDexPc)
4929 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004930
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004931 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004932
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004933 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004934
4935 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004936 return GetBlock()->GetGraph()->GetIntConstant(
4937 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004938 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004939 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004940 return GetBlock()->GetGraph()->GetLongConstant(
4941 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004943 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4944 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4945 LOG(FATAL) << DebugName() << " is not defined for float values";
4946 UNREACHABLE();
4947 }
4948 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4949 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4950 LOG(FATAL) << DebugName() << " is not defined for double values";
4951 UNREACHABLE();
4952 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004953
4954 DECLARE_INSTRUCTION(Or);
4955
4956 private:
4957 DISALLOW_COPY_AND_ASSIGN(HOr);
4958};
4959
Vladimir Markofcb503c2016-05-18 12:48:17 +01004960class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004961 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004962 HXor(Primitive::Type result_type,
4963 HInstruction* left,
4964 HInstruction* right,
4965 uint32_t dex_pc = kNoDexPc)
4966 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004967
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004968 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004969
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004970 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004971
4972 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004973 return GetBlock()->GetGraph()->GetIntConstant(
4974 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004975 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004976 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 return GetBlock()->GetGraph()->GetLongConstant(
4978 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004980 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4981 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4982 LOG(FATAL) << DebugName() << " is not defined for float values";
4983 UNREACHABLE();
4984 }
4985 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4986 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4987 LOG(FATAL) << DebugName() << " is not defined for double values";
4988 UNREACHABLE();
4989 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004990
4991 DECLARE_INSTRUCTION(Xor);
4992
4993 private:
4994 DISALLOW_COPY_AND_ASSIGN(HXor);
4995};
4996
Vladimir Markofcb503c2016-05-18 12:48:17 +01004997class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004998 public:
4999 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005000 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005001 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5002 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005003 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005004
Roland Levillain5b5b9312016-03-22 14:57:31 +00005005 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005006 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005007 typedef typename std::make_unsigned<T>::type V;
5008 V ux = static_cast<V>(value);
5009 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005010 return static_cast<T>(ux);
5011 } else {
5012 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005013 return static_cast<T>(ux >> (distance & max_shift_value)) |
5014 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015 }
5016 }
5017
Roland Levillain5b5b9312016-03-22 14:57:31 +00005018 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005019 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005020 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005021 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005022 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005023 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005024 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005025 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5027 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5028 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5029 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005030 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005031 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5032 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005033 LOG(FATAL) << DebugName() << " is not defined for float values";
5034 UNREACHABLE();
5035 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005036 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5037 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005038 LOG(FATAL) << DebugName() << " is not defined for double values";
5039 UNREACHABLE();
5040 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005041
5042 DECLARE_INSTRUCTION(Ror);
5043
5044 private:
5045 DISALLOW_COPY_AND_ASSIGN(HRor);
5046};
5047
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005048// The value of a parameter in this method. Its location depends on
5049// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005050class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005051 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005052 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005053 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005054 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005055 Primitive::Type parameter_type,
5056 bool is_this = false)
5057 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005058 dex_file_(dex_file),
5059 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005060 index_(index) {
5061 SetPackedFlag<kFlagIsThis>(is_this);
5062 SetPackedFlag<kFlagCanBeNull>(!is_this);
5063 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005064
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005065 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005066 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005067 uint8_t GetIndex() const { return index_; }
Igor Murashkin032cacd2017-04-06 14:40:08 -07005068 bool IsThis() const ATTRIBUTE_UNUSED { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005069
Vladimir Markoa1de9182016-02-25 11:37:38 +00005070 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5071 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005072
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005073 DECLARE_INSTRUCTION(ParameterValue);
5074
5075 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005076 // Whether or not the parameter value corresponds to 'this' argument.
5077 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5078 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5079 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5080 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5081 "Too many packed fields.");
5082
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005083 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005084 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005085 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005086 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005087 const uint8_t index_;
5088
5089 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5090};
5091
Vladimir Markofcb503c2016-05-18 12:48:17 +01005092class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005093 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005094 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5095 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005096
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005097 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005098 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005099 return true;
5100 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005101
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005102 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005103
5104 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005105 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005106 }
5107 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005108 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005110 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5111 LOG(FATAL) << DebugName() << " is not defined for float values";
5112 UNREACHABLE();
5113 }
5114 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5115 LOG(FATAL) << DebugName() << " is not defined for double values";
5116 UNREACHABLE();
5117 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005118
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005119 DECLARE_INSTRUCTION(Not);
5120
5121 private:
5122 DISALLOW_COPY_AND_ASSIGN(HNot);
5123};
5124
Vladimir Markofcb503c2016-05-18 12:48:17 +01005125class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005126 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005127 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5128 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005129
5130 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005131 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005132 return true;
5133 }
5134
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005135 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005136 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005137 return !x;
5138 }
5139
Roland Levillain9867bc72015-08-05 10:21:34 +01005140 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005141 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005142 }
5143 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5144 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005145 UNREACHABLE();
5146 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005147 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5148 LOG(FATAL) << DebugName() << " is not defined for float values";
5149 UNREACHABLE();
5150 }
5151 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5152 LOG(FATAL) << DebugName() << " is not defined for double values";
5153 UNREACHABLE();
5154 }
David Brazdil66d126e2015-04-03 16:02:44 +01005155
5156 DECLARE_INSTRUCTION(BooleanNot);
5157
5158 private:
5159 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5160};
5161
Vladimir Markofcb503c2016-05-18 12:48:17 +01005162class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005163 public:
5164 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005165 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005166 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005167 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005168 // Invariant: We should never generate a conversion to a Boolean value.
5169 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005170 }
5171
5172 HInstruction* GetInput() const { return InputAt(0); }
5173 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5174 Primitive::Type GetResultType() const { return GetType(); }
5175
5176 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005177 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5178 return true;
5179 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005180
Mark Mendelle82549b2015-05-06 10:55:34 -04005181 // Try to statically evaluate the conversion and return a HConstant
5182 // containing the result. If the input cannot be converted, return nullptr.
5183 HConstant* TryStaticEvaluation() const;
5184
Roland Levillaindff1f282014-11-05 14:15:05 +00005185 DECLARE_INSTRUCTION(TypeConversion);
5186
5187 private:
5188 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5189};
5190
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005191static constexpr uint32_t kNoRegNumber = -1;
5192
Vladimir Markofcb503c2016-05-18 12:48:17 +01005193class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005194 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005195 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5196 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005197 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005198 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005199 SetRawInputAt(0, value);
5200 }
5201
Calin Juravle10e244f2015-01-26 18:54:32 +00005202 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005203 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005204 return true;
5205 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005206
Calin Juravle10e244f2015-01-26 18:54:32 +00005207 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208
Calin Juravle10e244f2015-01-26 18:54:32 +00005209 bool CanThrow() const OVERRIDE { return true; }
5210
5211 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005212
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005213
5214 DECLARE_INSTRUCTION(NullCheck);
5215
5216 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5218};
5219
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005220// Embeds an ArtField and all the information required by the compiler. We cache
5221// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005222class FieldInfo : public ValueObject {
5223 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005224 FieldInfo(ArtField* field,
5225 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005226 Primitive::Type field_type,
5227 bool is_volatile,
5228 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005229 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005230 const DexFile& dex_file)
5231 : field_(field),
5232 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005233 field_type_(field_type),
5234 is_volatile_(is_volatile),
5235 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005236 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005237 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005239 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005240 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005241 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005242 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005243 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005244 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005245 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005246
5247 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005248 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005250 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005251 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005252 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005253 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005254 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005255};
5256
Vladimir Markofcb503c2016-05-18 12:48:17 +01005257class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258 public:
5259 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005260 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005262 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005263 bool is_volatile,
5264 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005265 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005266 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005267 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005268 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005269 field_info_(field,
5270 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005271 field_type,
5272 is_volatile,
5273 field_idx,
5274 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005275 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005276 SetRawInputAt(0, value);
5277 }
5278
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005279 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005280
Vladimir Marko372f10e2016-05-17 16:30:10 +01005281 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5282 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005283 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005284 }
5285
Calin Juravle641547a2015-04-21 22:08:51 +01005286 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005287 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 }
5289
5290 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005291 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5292 }
5293
Calin Juravle52c48962014-12-16 17:02:57 +00005294 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005295 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005296 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005297 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298
5299 DECLARE_INSTRUCTION(InstanceFieldGet);
5300
5301 private:
5302 const FieldInfo field_info_;
5303
5304 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5305};
5306
Vladimir Markofcb503c2016-05-18 12:48:17 +01005307class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005308 public:
5309 HInstanceFieldSet(HInstruction* object,
5310 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005311 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005312 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005313 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005314 bool is_volatile,
5315 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005316 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005317 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005318 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005319 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005320 field_info_(field,
5321 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005322 field_type,
5323 is_volatile,
5324 field_idx,
5325 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005326 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005327 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005328 SetRawInputAt(0, object);
5329 SetRawInputAt(1, value);
5330 }
5331
Calin Juravle641547a2015-04-21 22:08:51 +01005332 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005333 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005334 }
5335
Calin Juravle52c48962014-12-16 17:02:57 +00005336 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005337 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005338 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005339 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005340 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005341 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5342 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005343
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005344 DECLARE_INSTRUCTION(InstanceFieldSet);
5345
5346 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5348 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5349 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5350 "Too many packed fields.");
5351
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005352 const FieldInfo field_info_;
5353
5354 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5355};
5356
Vladimir Markofcb503c2016-05-18 12:48:17 +01005357class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005359 HArrayGet(HInstruction* array,
5360 HInstruction* index,
5361 Primitive::Type type,
5362 uint32_t dex_pc,
5363 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005364 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005365 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 SetRawInputAt(0, array);
5367 SetRawInputAt(1, index);
5368 }
5369
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005370 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005371 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005372 return true;
5373 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005374 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005375 // TODO: We can be smarter here.
5376 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5377 // which generates the implicit null check. There are cases when these can be removed
5378 // to produce better code. If we ever add optimizations to do so we should allow an
5379 // implicit check here (as long as the address falls in the first page).
5380 return false;
5381 }
5382
David Brazdil4833f5a2015-12-16 10:37:39 +00005383 bool IsEquivalentOf(HArrayGet* other) const {
5384 bool result = (GetDexPc() == other->GetDexPc());
5385 if (kIsDebugBuild && result) {
5386 DCHECK_EQ(GetBlock(), other->GetBlock());
5387 DCHECK_EQ(GetArray(), other->GetArray());
5388 DCHECK_EQ(GetIndex(), other->GetIndex());
5389 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005390 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005391 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005392 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5393 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005394 }
5395 }
5396 return result;
5397 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005398
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005399 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5400
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005401 HInstruction* GetArray() const { return InputAt(0); }
5402 HInstruction* GetIndex() const { return InputAt(1); }
5403
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 DECLARE_INSTRUCTION(ArrayGet);
5405
5406 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005407 // We treat a String as an array, creating the HArrayGet from String.charAt()
5408 // intrinsic in the instruction simplifier. We can always determine whether
5409 // a particular HArrayGet is actually a String.charAt() by looking at the type
5410 // of the input but that requires holding the mutator lock, so we prefer to use
5411 // a flag, so that code generators don't need to do the locking.
5412 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5413 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5414 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5415 "Too many packed fields.");
5416
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5418};
5419
Vladimir Markofcb503c2016-05-18 12:48:17 +01005420class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 public:
5422 HArraySet(HInstruction* array,
5423 HInstruction* index,
5424 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005425 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005426 uint32_t dex_pc)
5427 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005428 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5429 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5430 SetPackedFlag<kFlagValueCanBeNull>(true);
5431 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432 SetRawInputAt(0, array);
5433 SetRawInputAt(1, index);
5434 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005435 // Make a best guess now, may be refined during SSA building.
5436 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005437 }
5438
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005440 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005441 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005442 }
5443
Mingyao Yang81014cb2015-06-02 03:16:27 -07005444 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005445 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005447 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005448 // TODO: Same as for ArrayGet.
5449 return false;
5450 }
5451
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005452 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005453 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005454 }
5455
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005456 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005457 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005458 }
5459
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005460 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005462 }
5463
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5465 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5466 bool StaticTypeOfArrayIsObjectArray() const {
5467 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5468 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005469
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005470 HInstruction* GetArray() const { return InputAt(0); }
5471 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005472 HInstruction* GetValue() const { return InputAt(2); }
5473
5474 Primitive::Type GetComponentType() const {
5475 // The Dex format does not type floating point index operations. Since the
5476 // `expected_component_type_` is set during building and can therefore not
5477 // be correct, we also check what is the value type. If it is a floating
5478 // point type, we must use that type.
5479 Primitive::Type value_type = GetValue()->GetType();
5480 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5481 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005482 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005483 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005484
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005485 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005486 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005487 }
5488
Aart Bik18b36ab2016-04-13 16:41:35 -07005489 void ComputeSideEffects() {
5490 Primitive::Type type = GetComponentType();
5491 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5492 SideEffectsForArchRuntimeCalls(type)));
5493 }
5494
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005495 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5496 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5497 }
5498
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005499 DECLARE_INSTRUCTION(ArraySet);
5500
5501 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005502 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5503 static constexpr size_t kFieldExpectedComponentTypeSize =
5504 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5505 static constexpr size_t kFlagNeedsTypeCheck =
5506 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5507 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005508 // Cached information for the reference_type_info_ so that codegen
5509 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005510 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5511 static constexpr size_t kNumberOfArraySetPackedBits =
5512 kFlagStaticTypeOfArrayIsObjectArray + 1;
5513 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5514 using ExpectedComponentTypeField =
5515 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516
5517 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5518};
5519
Vladimir Markofcb503c2016-05-18 12:48:17 +01005520class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005522 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005523 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005524 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005525 // Note that arrays do not change length, so the instruction does not
5526 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005527 SetRawInputAt(0, array);
5528 }
5529
Calin Juravle77520bc2015-01-12 18:45:46 +00005530 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005531 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005532 return true;
5533 }
Calin Juravle641547a2015-04-21 22:08:51 +01005534 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5535 return obj == InputAt(0);
5536 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005537
Vladimir Markodce016e2016-04-28 13:10:02 +01005538 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5539
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540 DECLARE_INSTRUCTION(ArrayLength);
5541
5542 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005543 // We treat a String as an array, creating the HArrayLength from String.length()
5544 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5545 // determine whether a particular HArrayLength is actually a String.length() by
5546 // looking at the type of the input but that requires holding the mutator lock, so
5547 // we prefer to use a flag, so that code generators don't need to do the locking.
5548 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5549 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5550 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5551 "Too many packed fields.");
5552
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5554};
5555
Vladimir Markofcb503c2016-05-18 12:48:17 +01005556class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005558 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5559 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005560 HBoundsCheck(HInstruction* index,
5561 HInstruction* length,
5562 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005563 bool string_char_at = false)
5564 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005565 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005566 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005567 SetRawInputAt(0, index);
5568 SetRawInputAt(1, length);
5569 }
5570
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005571 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005572 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005573 return true;
5574 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005575
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005576 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005577
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005578 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005579
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005580 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005581
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005582 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583
5584 DECLARE_INSTRUCTION(BoundsCheck);
5585
5586 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005587 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005588
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005589 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5590};
5591
Vladimir Markofcb503c2016-05-18 12:48:17 +01005592class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005593 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005594 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005595 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005597 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005598 return true;
5599 }
5600
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005601 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5602 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005603
5604 DECLARE_INSTRUCTION(SuspendCheck);
5605
5606 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005607 // Only used for code generation, in order to share the same slow path between back edges
5608 // of a same loop.
5609 SlowPathCode* slow_path_;
5610
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005611 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5612};
5613
David Srbecky0cf44932015-12-09 14:09:59 +00005614// Pseudo-instruction which provides the native debugger with mapping information.
5615// It ensures that we can generate line number and local variables at this point.
5616class HNativeDebugInfo : public HTemplateInstruction<0> {
5617 public:
5618 explicit HNativeDebugInfo(uint32_t dex_pc)
5619 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5620
5621 bool NeedsEnvironment() const OVERRIDE {
5622 return true;
5623 }
5624
5625 DECLARE_INSTRUCTION(NativeDebugInfo);
5626
5627 private:
5628 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5629};
5630
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005631/**
5632 * Instruction to load a Class object.
5633 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005634class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005636 // Determines how to load the Class.
5637 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005638 // We cannot load this class. See HSharpening::SharpenLoadClass.
5639 kInvalid = -1,
5640
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005641 // Use the Class* from the method's own ArtMethod*.
5642 kReferrersClass,
5643
5644 // Use boot image Class* address that will be known at link time.
5645 // Used for boot image classes referenced by boot image code in non-PIC mode.
5646 kBootImageLinkTimeAddress,
5647
5648 // Use PC-relative boot image Class* address that will be known at link time.
5649 // Used for boot image classes referenced by boot image code in PIC mode.
5650 kBootImageLinkTimePcRelative,
5651
5652 // Use a known boot image Class* address, embedded in the code by the codegen.
5653 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005654 kBootImageAddress,
5655
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005656 // Load from an entry in the .bss section using a PC-relative load.
5657 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5658 kBssEntry,
5659
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005660 // Load from the root table associated with the JIT compiled method.
5661 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005662
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005663 // Load from resolved types array accessed through the class loaded from
5664 // the compiled method's own ArtMethod*. This is the default access type when
5665 // all other types are unavailable.
5666 kDexCacheViaMethod,
5667
5668 kLast = kDexCacheViaMethod
5669 };
5670
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005671 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005672 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005673 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005674 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005675 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005676 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005677 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005678 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5679 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005680 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005681 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005682 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005683 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005684 // Referrers class should not need access check. We never inline unverified
5685 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005686 DCHECK(!is_referrers_class || !needs_access_check);
5687
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005688 SetPackedField<LoadKindField>(
5689 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005690 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005691 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005692 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005693 }
5694
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005695 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 LoadKind GetLoadKind() const {
5698 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005699 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005700
5701 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005702
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005703 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005704
Andreas Gampea5b09a62016-11-17 15:21:22 -08005705 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005706
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005707 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005708
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005709 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005710 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711 }
5712
Calin Juravle0ba218d2015-05-19 18:46:01 +01005713 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005714 // The entrypoint the code generator is going to call does not do
5715 // clinit of the class.
5716 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005717 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005718 }
5719
5720 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005721 return NeedsAccessCheck() ||
5722 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005723 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5724 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005725 }
5726
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005727 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005728 return NeedsAccessCheck() ||
5729 MustGenerateClinitCheck() ||
5730 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5731 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5732 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005733 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5734 GetLoadKind() == LoadKind::kBssEntry) &&
5735 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005736 }
5737
Calin Juravleacf735c2015-02-12 15:25:22 +00005738 ReferenceTypeInfo GetLoadedClassRTI() {
5739 return loaded_class_rti_;
5740 }
5741
5742 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5743 // Make sure we only set exact types (the loaded class should never be merged).
5744 DCHECK(rti.IsExact());
5745 loaded_class_rti_ = rti;
5746 }
5747
Andreas Gampea5b09a62016-11-17 15:21:22 -08005748 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 const DexFile& GetDexFile() const { return dex_file_; }
5750
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005751 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005752 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005753 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005754
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005755 static SideEffects SideEffectsForArchRuntimeCalls() {
5756 return SideEffects::CanTriggerGC();
5757 }
5758
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005759 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005760 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005761 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005762 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005763
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005764 void MarkInBootImage() {
5765 SetPackedFlag<kFlagIsInBootImage>(true);
5766 }
5767
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 void AddSpecialInput(HInstruction* special_input);
5769
5770 using HInstruction::GetInputRecords; // Keep the const version visible.
5771 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5772 return ArrayRef<HUserRecord<HInstruction*>>(
5773 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5774 }
5775
5776 Primitive::Type GetType() const OVERRIDE {
5777 return Primitive::kPrimNot;
5778 }
5779
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005780 Handle<mirror::Class> GetClass() const {
5781 return klass_;
5782 }
5783
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005784 DECLARE_INSTRUCTION(LoadClass);
5785
5786 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005788 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005789 // Whether this instruction must generate the initialization check.
5790 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005791 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005792 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5793 static constexpr size_t kFieldLoadKindSize =
5794 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5795 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005796 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005797 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5798
5799 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005800 return load_kind == LoadKind::kReferrersClass ||
5801 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005802 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005803 load_kind == LoadKind::kBssEntry ||
5804 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 }
5806
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005807 void SetLoadKindInternal(LoadKind load_kind);
5808
5809 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5810 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005811 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005812 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005813
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005814 // A type index and dex file where the class can be accessed. The dex file can be:
5815 // - The compiling method's dex file if the class is defined there too.
5816 // - The compiling method's dex file if the class is referenced there.
5817 // - The dex file where the class is defined. When the load kind can only be
5818 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005819 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005820 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005821
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005822 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823
Calin Juravleacf735c2015-02-12 15:25:22 +00005824 ReferenceTypeInfo loaded_class_rti_;
5825
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005826 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5827};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005828std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5829
5830// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005832 // The special input is used for PC-relative loads on some architectures,
5833 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005835 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005836 GetLoadKind() == LoadKind::kBootImageAddress ||
5837 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005838 DCHECK(special_input_.GetInstruction() == nullptr);
5839 special_input_ = HUserRecord<HInstruction*>(special_input);
5840 special_input->AddUseAt(this, 0);
5841}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005842
Vladimir Marko372f10e2016-05-17 16:30:10 +01005843class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005844 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005845 // Determines how to load the String.
5846 enum class LoadKind {
5847 // Use boot image String* address that will be known at link time.
5848 // Used for boot image strings referenced by boot image code in non-PIC mode.
5849 kBootImageLinkTimeAddress,
5850
5851 // Use PC-relative boot image String* address that will be known at link time.
5852 // Used for boot image strings referenced by boot image code in PIC mode.
5853 kBootImageLinkTimePcRelative,
5854
5855 // Use a known boot image String* address, embedded in the code by the codegen.
5856 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005857 kBootImageAddress,
5858
Vladimir Markoaad75c62016-10-03 08:46:48 +00005859 // Load from an entry in the .bss section using a PC-relative load.
5860 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5861 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005863 // Load from the root table associated with the JIT compiled method.
5864 kJitTableAddress,
5865
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866 // Load from resolved strings array accessed through the class loaded from
5867 // the compiled method's own ArtMethod*. This is the default access type when
5868 // all other types are unavailable.
5869 kDexCacheViaMethod,
5870
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005871 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872 };
5873
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005874 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005875 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 const DexFile& dex_file,
5877 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005878 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5879 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005880 string_index_(string_index),
5881 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005883 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005884
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005885 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005887 LoadKind GetLoadKind() const {
5888 return GetPackedField<LoadKindField>();
5889 }
5890
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005891 const DexFile& GetDexFile() const {
5892 return dex_file_;
5893 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005895 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005896 return string_index_;
5897 }
5898
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005899 Handle<mirror::String> GetString() const {
5900 return string_;
5901 }
5902
5903 void SetString(Handle<mirror::String> str) {
5904 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005905 }
5906
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005907 bool CanBeMoved() const OVERRIDE { return true; }
5908
Vladimir Marko372f10e2016-05-17 16:30:10 +01005909 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005910
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005911 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005912
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913 // Will call the runtime if we need to load the string through
5914 // the dex cache and the string is not guaranteed to be there yet.
5915 bool NeedsEnvironment() const OVERRIDE {
5916 LoadKind load_kind = GetLoadKind();
5917 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5918 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005919 load_kind == LoadKind::kBootImageAddress ||
5920 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 return false;
5922 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005923 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005925
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005926 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5927 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5928 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005929
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005930 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005931 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005932
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005933 static SideEffects SideEffectsForArchRuntimeCalls() {
5934 return SideEffects::CanTriggerGC();
5935 }
5936
Vladimir Marko372f10e2016-05-17 16:30:10 +01005937 void AddSpecialInput(HInstruction* special_input);
5938
5939 using HInstruction::GetInputRecords; // Keep the const version visible.
5940 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5941 return ArrayRef<HUserRecord<HInstruction*>>(
5942 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 }
5944
Vladimir Marko372f10e2016-05-17 16:30:10 +01005945 Primitive::Type GetType() const OVERRIDE {
5946 return Primitive::kPrimNot;
5947 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005948
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005949 DECLARE_INSTRUCTION(LoadString);
5950
5951 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005952 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005953 static constexpr size_t kFieldLoadKindSize =
5954 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5955 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005956 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005958
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959 void SetLoadKindInternal(LoadKind load_kind);
5960
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005961 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5962 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005963 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005964 HUserRecord<HInstruction*> special_input_;
5965
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005966 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005967 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005968
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005969 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005970
5971 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5972};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005973std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5974
5975// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005977 // The special input is used for PC-relative loads on some architectures,
5978 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005980 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005981 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5982 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005983 // HLoadString::GetInputRecords() returns an empty array at this point,
5984 // so use the GetInputRecords() from the base class to set the input record.
5985 DCHECK(special_input_.GetInstruction() == nullptr);
5986 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005987 special_input->AddUseAt(this, 0);
5988}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005989
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005990/**
5991 * Performs an initialization check on its Class object input.
5992 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005993class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005995 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005996 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005997 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005998 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5999 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006000 SetRawInputAt(0, constant);
6001 }
6002
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006003 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006004 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006005 return true;
6006 }
6007
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006008 bool NeedsEnvironment() const OVERRIDE {
6009 // May call runtime to initialize the class.
6010 return true;
6011 }
6012
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006013 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006014
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006015 HLoadClass* GetLoadClass() const {
6016 DCHECK(InputAt(0)->IsLoadClass());
6017 return InputAt(0)->AsLoadClass();
6018 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006019
6020 DECLARE_INSTRUCTION(ClinitCheck);
6021
6022 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006023 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6024};
6025
Vladimir Markofcb503c2016-05-18 12:48:17 +01006026class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 public:
6028 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006029 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006031 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006032 bool is_volatile,
6033 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006034 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006035 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006036 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006037 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006038 field_info_(field,
6039 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006040 field_type,
6041 is_volatile,
6042 field_idx,
6043 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006044 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006045 SetRawInputAt(0, cls);
6046 }
6047
Calin Juravle52c48962014-12-16 17:02:57 +00006048
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006049 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006050
Vladimir Marko372f10e2016-05-17 16:30:10 +01006051 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6052 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006053 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006054 }
6055
6056 size_t ComputeHashCode() const OVERRIDE {
6057 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6058 }
6059
Calin Juravle52c48962014-12-16 17:02:57 +00006060 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006061 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6062 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006063 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064
6065 DECLARE_INSTRUCTION(StaticFieldGet);
6066
6067 private:
6068 const FieldInfo field_info_;
6069
6070 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6071};
6072
Vladimir Markofcb503c2016-05-18 12:48:17 +01006073class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074 public:
6075 HStaticFieldSet(HInstruction* cls,
6076 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006077 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006078 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006079 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006080 bool is_volatile,
6081 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006082 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006083 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006084 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006085 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006086 field_info_(field,
6087 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006088 field_type,
6089 is_volatile,
6090 field_idx,
6091 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006092 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006093 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006094 SetRawInputAt(0, cls);
6095 SetRawInputAt(1, value);
6096 }
6097
Calin Juravle52c48962014-12-16 17:02:57 +00006098 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006099 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6100 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006101 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006103 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006104 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6105 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006106
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006107 DECLARE_INSTRUCTION(StaticFieldSet);
6108
6109 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006110 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6111 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6112 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6113 "Too many packed fields.");
6114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006115 const FieldInfo field_info_;
6116
6117 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6118};
6119
Vladimir Markofcb503c2016-05-18 12:48:17 +01006120class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006121 public:
6122 HUnresolvedInstanceFieldGet(HInstruction* obj,
6123 Primitive::Type field_type,
6124 uint32_t field_index,
6125 uint32_t dex_pc)
6126 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6127 field_index_(field_index) {
6128 SetRawInputAt(0, obj);
6129 }
6130
6131 bool NeedsEnvironment() const OVERRIDE { return true; }
6132 bool CanThrow() const OVERRIDE { return true; }
6133
6134 Primitive::Type GetFieldType() const { return GetType(); }
6135 uint32_t GetFieldIndex() const { return field_index_; }
6136
6137 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6138
6139 private:
6140 const uint32_t field_index_;
6141
6142 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6143};
6144
Vladimir Markofcb503c2016-05-18 12:48:17 +01006145class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006146 public:
6147 HUnresolvedInstanceFieldSet(HInstruction* obj,
6148 HInstruction* value,
6149 Primitive::Type field_type,
6150 uint32_t field_index,
6151 uint32_t dex_pc)
6152 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006153 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006154 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006155 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006156 SetRawInputAt(0, obj);
6157 SetRawInputAt(1, value);
6158 }
6159
6160 bool NeedsEnvironment() const OVERRIDE { return true; }
6161 bool CanThrow() const OVERRIDE { return true; }
6162
Vladimir Markoa1de9182016-02-25 11:37:38 +00006163 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006164 uint32_t GetFieldIndex() const { return field_index_; }
6165
6166 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6167
6168 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006169 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6170 static constexpr size_t kFieldFieldTypeSize =
6171 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6172 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6173 kFieldFieldType + kFieldFieldTypeSize;
6174 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6175 "Too many packed fields.");
6176 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6177
Calin Juravlee460d1d2015-09-29 04:52:17 +01006178 const uint32_t field_index_;
6179
6180 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6181};
6182
Vladimir Markofcb503c2016-05-18 12:48:17 +01006183class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006184 public:
6185 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6186 uint32_t field_index,
6187 uint32_t dex_pc)
6188 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6189 field_index_(field_index) {
6190 }
6191
6192 bool NeedsEnvironment() const OVERRIDE { return true; }
6193 bool CanThrow() const OVERRIDE { return true; }
6194
6195 Primitive::Type GetFieldType() const { return GetType(); }
6196 uint32_t GetFieldIndex() const { return field_index_; }
6197
6198 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6199
6200 private:
6201 const uint32_t field_index_;
6202
6203 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6204};
6205
Vladimir Markofcb503c2016-05-18 12:48:17 +01006206class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006207 public:
6208 HUnresolvedStaticFieldSet(HInstruction* value,
6209 Primitive::Type field_type,
6210 uint32_t field_index,
6211 uint32_t dex_pc)
6212 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006213 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006215 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006216 SetRawInputAt(0, value);
6217 }
6218
6219 bool NeedsEnvironment() const OVERRIDE { return true; }
6220 bool CanThrow() const OVERRIDE { return true; }
6221
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006223 uint32_t GetFieldIndex() const { return field_index_; }
6224
6225 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6226
6227 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6229 static constexpr size_t kFieldFieldTypeSize =
6230 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6231 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6232 kFieldFieldType + kFieldFieldTypeSize;
6233 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6234 "Too many packed fields.");
6235 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6236
Calin Juravlee460d1d2015-09-29 04:52:17 +01006237 const uint32_t field_index_;
6238
6239 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6240};
6241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006242// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006243class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006244 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006245 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6246 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006247
David Brazdilbbd733e2015-08-18 17:48:17 +01006248 bool CanBeNull() const OVERRIDE { return false; }
6249
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250 DECLARE_INSTRUCTION(LoadException);
6251
6252 private:
6253 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6254};
6255
David Brazdilcb1c0552015-08-04 16:22:25 +01006256// Implicit part of move-exception which clears thread-local exception storage.
6257// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006258class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006259 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006260 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6261 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006262
6263 DECLARE_INSTRUCTION(ClearException);
6264
6265 private:
6266 DISALLOW_COPY_AND_ASSIGN(HClearException);
6267};
6268
Vladimir Markofcb503c2016-05-18 12:48:17 +01006269class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006270 public:
6271 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006272 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006273 SetRawInputAt(0, exception);
6274 }
6275
6276 bool IsControlFlow() const OVERRIDE { return true; }
6277
6278 bool NeedsEnvironment() const OVERRIDE { return true; }
6279
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006280 bool CanThrow() const OVERRIDE { return true; }
6281
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006282
6283 DECLARE_INSTRUCTION(Throw);
6284
6285 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006286 DISALLOW_COPY_AND_ASSIGN(HThrow);
6287};
6288
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289/**
6290 * Implementation strategies for the code generator of a HInstanceOf
6291 * or `HCheckCast`.
6292 */
6293enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006294 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295 kExactCheck, // Can do a single class compare.
6296 kClassHierarchyCheck, // Can just walk the super class chain.
6297 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6298 kInterfaceCheck, // No optimization yet when checking against an interface.
6299 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006300 kArrayCheck, // No optimization yet when checking against a generic array.
6301 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302};
6303
Roland Levillain86503782016-02-11 19:07:30 +00006304std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6305
Vladimir Markofcb503c2016-05-18 12:48:17 +01006306class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006308 HInstanceOf(HInstruction* object,
6309 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006310 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006311 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006312 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006314 dex_pc) {
6315 SetPackedField<TypeCheckKindField>(check_kind);
6316 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006317 SetRawInputAt(0, object);
6318 SetRawInputAt(1, constant);
6319 }
6320
6321 bool CanBeMoved() const OVERRIDE { return true; }
6322
Vladimir Marko372f10e2016-05-17 16:30:10 +01006323 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006324 return true;
6325 }
6326
6327 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006328 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006329 }
6330
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006331 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6333 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6334 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6335 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006336
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006337 static bool CanCallRuntime(TypeCheckKind check_kind) {
6338 // Mips currently does runtime calls for any other checks.
6339 return check_kind != TypeCheckKind::kExactCheck;
6340 }
6341
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006343 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006344 }
6345
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006346 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006347
6348 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006349 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6350 static constexpr size_t kFieldTypeCheckKindSize =
6351 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6352 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6353 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6354 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6355 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6358};
6359
Vladimir Markofcb503c2016-05-18 12:48:17 +01006360class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006361 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006362 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006363 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006364 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6365 SetPackedFlag<kFlagUpperCanBeNull>(true);
6366 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006367 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006368 SetRawInputAt(0, input);
6369 }
6370
David Brazdilf5552582015-12-27 13:36:12 +00006371 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006372 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006373 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006374 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006375
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006376 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 DCHECK(GetUpperCanBeNull() || !can_be_null);
6378 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006379 }
6380
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006382
Calin Juravleb1498f62015-02-16 13:13:29 +00006383 DECLARE_INSTRUCTION(BoundType);
6384
6385 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006386 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6387 // is false then CanBeNull() cannot be true).
6388 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6389 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6390 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6391 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6392
Calin Juravleb1498f62015-02-16 13:13:29 +00006393 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006394 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6395 // It is used to bound the type in cases like:
6396 // if (x instanceof ClassX) {
6397 // // uper_bound_ will be ClassX
6398 // }
David Brazdilf5552582015-12-27 13:36:12 +00006399 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006400
6401 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6402};
6403
Vladimir Markofcb503c2016-05-18 12:48:17 +01006404class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006405 public:
6406 HCheckCast(HInstruction* object,
6407 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006409 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6411 SetPackedField<TypeCheckKindField>(check_kind);
6412 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 SetRawInputAt(0, object);
6414 SetRawInputAt(1, constant);
6415 }
6416
6417 bool CanBeMoved() const OVERRIDE { return true; }
6418
Vladimir Marko372f10e2016-05-17 16:30:10 +01006419 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006420 return true;
6421 }
6422
6423 bool NeedsEnvironment() const OVERRIDE {
6424 // Instruction may throw a CheckCastError.
6425 return true;
6426 }
6427
6428 bool CanThrow() const OVERRIDE { return true; }
6429
Vladimir Markoa1de9182016-02-25 11:37:38 +00006430 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6431 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6432 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6433 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006434
6435 DECLARE_INSTRUCTION(CheckCast);
6436
6437 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006438 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6439 static constexpr size_t kFieldTypeCheckKindSize =
6440 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6441 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6442 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6443 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6444 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006445
6446 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006447};
6448
Andreas Gampe26de38b2016-07-27 17:53:11 -07006449/**
6450 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6451 * @details We define the combined barrier types that are actually required
6452 * by the Java Memory Model, rather than using exactly the terminology from
6453 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6454 * primitives. Note that the JSR-133 cookbook generally does not deal with
6455 * store atomicity issues, and the recipes there are not always entirely sufficient.
6456 * The current recipe is as follows:
6457 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6458 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6459 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6460 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6461 * class has final fields.
6462 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6463 * store-to-memory instructions. Only generated together with non-temporal stores.
6464 */
6465enum MemBarrierKind {
6466 kAnyStore,
6467 kLoadAny,
6468 kStoreStore,
6469 kAnyAny,
6470 kNTStoreStore,
6471 kLastBarrierKind = kNTStoreStore
6472};
6473std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6474
Vladimir Markofcb503c2016-05-18 12:48:17 +01006475class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006476 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006477 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006478 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006479 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6480 SetPackedField<BarrierKindField>(barrier_kind);
6481 }
Calin Juravle27df7582015-04-17 19:12:31 +01006482
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006484
6485 DECLARE_INSTRUCTION(MemoryBarrier);
6486
6487 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006488 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6489 static constexpr size_t kFieldBarrierKindSize =
6490 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6491 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6492 kFieldBarrierKind + kFieldBarrierKindSize;
6493 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6494 "Too many packed fields.");
6495 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006496
6497 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6498};
6499
Vladimir Markofcb503c2016-05-18 12:48:17 +01006500class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006501 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006502 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006503 kEnter,
6504 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006506 };
6507
6508 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006509 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006510 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6511 dex_pc) {
6512 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006513 SetRawInputAt(0, object);
6514 }
6515
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006516 // Instruction may go into runtime, so we need an environment.
6517 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006518
6519 bool CanThrow() const OVERRIDE {
6520 // Verifier guarantees that monitor-exit cannot throw.
6521 // This is important because it allows the HGraphBuilder to remove
6522 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6523 return IsEnter();
6524 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006525
Vladimir Markoa1de9182016-02-25 11:37:38 +00006526 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6527 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006528
6529 DECLARE_INSTRUCTION(MonitorOperation);
6530
Calin Juravle52c48962014-12-16 17:02:57 +00006531 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006532 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6533 static constexpr size_t kFieldOperationKindSize =
6534 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6535 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6536 kFieldOperationKind + kFieldOperationKindSize;
6537 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6538 "Too many packed fields.");
6539 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006540
6541 private:
6542 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6543};
6544
Vladimir Markofcb503c2016-05-18 12:48:17 +01006545class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006546 public:
6547 HSelect(HInstruction* condition,
6548 HInstruction* true_value,
6549 HInstruction* false_value,
6550 uint32_t dex_pc)
6551 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6552 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6553
6554 // First input must be `true_value` or `false_value` to allow codegens to
6555 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6556 // that architectures which implement HSelect as a conditional move also
6557 // will not need to invert the condition.
6558 SetRawInputAt(0, false_value);
6559 SetRawInputAt(1, true_value);
6560 SetRawInputAt(2, condition);
6561 }
6562
6563 HInstruction* GetFalseValue() const { return InputAt(0); }
6564 HInstruction* GetTrueValue() const { return InputAt(1); }
6565 HInstruction* GetCondition() const { return InputAt(2); }
6566
6567 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006568 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6569 return true;
6570 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006571
6572 bool CanBeNull() const OVERRIDE {
6573 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6574 }
6575
6576 DECLARE_INSTRUCTION(Select);
6577
6578 private:
6579 DISALLOW_COPY_AND_ASSIGN(HSelect);
6580};
6581
Vladimir Markof9f64412015-09-02 14:05:49 +01006582class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006583 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006584 MoveOperands(Location source,
6585 Location destination,
6586 Primitive::Type type,
6587 HInstruction* instruction)
6588 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006589
6590 Location GetSource() const { return source_; }
6591 Location GetDestination() const { return destination_; }
6592
6593 void SetSource(Location value) { source_ = value; }
6594 void SetDestination(Location value) { destination_ = value; }
6595
6596 // The parallel move resolver marks moves as "in-progress" by clearing the
6597 // destination (but not the source).
6598 Location MarkPending() {
6599 DCHECK(!IsPending());
6600 Location dest = destination_;
6601 destination_ = Location::NoLocation();
6602 return dest;
6603 }
6604
6605 void ClearPending(Location dest) {
6606 DCHECK(IsPending());
6607 destination_ = dest;
6608 }
6609
6610 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006611 DCHECK(source_.IsValid() || destination_.IsInvalid());
6612 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006613 }
6614
6615 // True if this blocks a move from the given location.
6616 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006617 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006618 }
6619
6620 // A move is redundant if it's been eliminated, if its source and
6621 // destination are the same, or if its destination is unneeded.
6622 bool IsRedundant() const {
6623 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6624 }
6625
6626 // We clear both operands to indicate move that's been eliminated.
6627 void Eliminate() {
6628 source_ = destination_ = Location::NoLocation();
6629 }
6630
6631 bool IsEliminated() const {
6632 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6633 return source_.IsInvalid();
6634 }
6635
Alexey Frunze4dda3372015-06-01 18:31:49 -07006636 Primitive::Type GetType() const { return type_; }
6637
Nicolas Geoffray90218252015-04-15 11:56:51 +01006638 bool Is64BitMove() const {
6639 return Primitive::Is64BitType(type_);
6640 }
6641
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006642 HInstruction* GetInstruction() const { return instruction_; }
6643
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006644 private:
6645 Location source_;
6646 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006647 // The type this move is for.
6648 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006649 // The instruction this move is assocatied with. Null when this move is
6650 // for moving an input in the expected locations of user (including a phi user).
6651 // This is only used in debug mode, to ensure we do not connect interval siblings
6652 // in the same parallel move.
6653 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006654};
6655
Roland Levillainc9285912015-12-18 10:38:42 +00006656std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6657
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006658static constexpr size_t kDefaultNumberOfMoves = 4;
6659
Vladimir Markofcb503c2016-05-18 12:48:17 +01006660class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006661 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006662 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006663 : HTemplateInstruction(SideEffects::None(), dex_pc),
6664 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6665 moves_.reserve(kDefaultNumberOfMoves);
6666 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006667
Nicolas Geoffray90218252015-04-15 11:56:51 +01006668 void AddMove(Location source,
6669 Location destination,
6670 Primitive::Type type,
6671 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006672 DCHECK(source.IsValid());
6673 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006674 if (kIsDebugBuild) {
6675 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006676 for (const MoveOperands& move : moves_) {
6677 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006678 // Special case the situation where the move is for the spill slot
6679 // of the instruction.
6680 if ((GetPrevious() == instruction)
6681 || ((GetPrevious() == nullptr)
6682 && instruction->IsPhi()
6683 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006684 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006685 << "Doing parallel moves for the same instruction.";
6686 } else {
6687 DCHECK(false) << "Doing parallel moves for the same instruction.";
6688 }
6689 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006690 }
6691 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006692 for (const MoveOperands& move : moves_) {
6693 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006694 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006695 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006696 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006697 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006698 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006699 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006700 }
6701
Vladimir Marko225b6462015-09-28 12:17:40 +01006702 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006703 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006704 }
6705
Vladimir Marko225b6462015-09-28 12:17:40 +01006706 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006707
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006708 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006709
6710 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006711 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006712
6713 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6714};
6715
Mark Mendell0616ae02015-04-17 12:49:27 -04006716} // namespace art
6717
Aart Bikf8f5a162017-02-06 15:35:29 -08006718#include "nodes_vector.h"
6719
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006720#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6721#include "nodes_shared.h"
6722#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006723#ifdef ART_ENABLE_CODEGEN_arm
6724#include "nodes_arm.h"
6725#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006726#ifdef ART_ENABLE_CODEGEN_mips
6727#include "nodes_mips.h"
6728#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006729#ifdef ART_ENABLE_CODEGEN_x86
6730#include "nodes_x86.h"
6731#endif
6732
6733namespace art {
6734
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006735class HGraphVisitor : public ValueObject {
6736 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006737 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6738 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006739
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006740 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006741 virtual void VisitBasicBlock(HBasicBlock* block);
6742
Roland Levillain633021e2014-10-01 14:12:25 +01006743 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006744 void VisitInsertionOrder();
6745
Roland Levillain633021e2014-10-01 14:12:25 +01006746 // Visit the graph following dominator tree reverse post-order.
6747 void VisitReversePostOrder();
6748
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006749 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006750
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006751 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006752#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006753 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6754
6755 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6756
6757#undef DECLARE_VISIT_INSTRUCTION
6758
6759 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006760 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006761
6762 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6763};
6764
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006765class HGraphDelegateVisitor : public HGraphVisitor {
6766 public:
6767 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6768 virtual ~HGraphDelegateVisitor() {}
6769
6770 // Visit functions that delegate to to super class.
6771#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006772 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006773
6774 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6775
6776#undef DECLARE_VISIT_INSTRUCTION
6777
6778 private:
6779 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6780};
6781
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006782// Iterator over the blocks that art part of the loop. Includes blocks part
6783// of an inner loop. The order in which the blocks are iterated is on their
6784// block id.
6785class HBlocksInLoopIterator : public ValueObject {
6786 public:
6787 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6788 : blocks_in_loop_(info.GetBlocks()),
6789 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6790 index_(0) {
6791 if (!blocks_in_loop_.IsBitSet(index_)) {
6792 Advance();
6793 }
6794 }
6795
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006796 bool Done() const { return index_ == blocks_.size(); }
6797 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006798 void Advance() {
6799 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006800 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006801 if (blocks_in_loop_.IsBitSet(index_)) {
6802 break;
6803 }
6804 }
6805 }
6806
6807 private:
6808 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006809 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006810 size_t index_;
6811
6812 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6813};
6814
Mingyao Yang3584bce2015-05-19 16:01:59 -07006815// Iterator over the blocks that art part of the loop. Includes blocks part
6816// of an inner loop. The order in which the blocks are iterated is reverse
6817// post order.
6818class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6819 public:
6820 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6821 : blocks_in_loop_(info.GetBlocks()),
6822 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6823 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006824 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006825 Advance();
6826 }
6827 }
6828
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006829 bool Done() const { return index_ == blocks_.size(); }
6830 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006831 void Advance() {
6832 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006833 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6834 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006835 break;
6836 }
6837 }
6838 }
6839
6840 private:
6841 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006842 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006843 size_t index_;
6844
6845 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6846};
6847
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006848inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006849 if (constant->IsIntConstant()) {
6850 return constant->AsIntConstant()->GetValue();
6851 } else if (constant->IsLongConstant()) {
6852 return constant->AsLongConstant()->GetValue();
6853 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006854 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006855 return 0;
6856 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006857}
6858
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006859#define INSTRUCTION_TYPE_CHECK(type, super) \
6860 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6861 inline const H##type* HInstruction::As##type() const { \
6862 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6863 } \
6864 inline H##type* HInstruction::As##type() { \
6865 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6866 }
6867
6868 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6869#undef INSTRUCTION_TYPE_CHECK
6870
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006871// Create space in `blocks` for adding `number_of_new_blocks` entries
6872// starting at location `at`. Blocks after `at` are moved accordingly.
6873inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6874 size_t number_of_new_blocks,
6875 size_t after) {
6876 DCHECK_LT(after, blocks->size());
6877 size_t old_size = blocks->size();
6878 size_t new_size = old_size + number_of_new_blocks;
6879 blocks->resize(new_size);
6880 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6881}
6882
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006883/*
6884 * Hunt "under the hood" of array lengths (leading to array references),
6885 * null checks (also leading to array references), and new arrays
6886 * (leading to the actual length). This makes it more likely related
6887 * instructions become actually comparable.
6888 */
6889inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6890 while (instruction->IsArrayLength() ||
6891 instruction->IsNullCheck() ||
6892 instruction->IsNewArray()) {
6893 instruction = instruction->IsNewArray()
6894 ? instruction->AsNewArray()->GetLength()
6895 : instruction->InputAt(0);
6896 }
6897 return instruction;
6898}
6899
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006900} // namespace art
6901
6902#endif // ART_COMPILER_OPTIMIZING_NODES_H_