blob: 6be237e612929cc4908860e660a4e6e9049e550f [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) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001372 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001373 M(VecSub, VecBinaryOperation) \
1374 M(VecMul, VecBinaryOperation) \
1375 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001376 M(VecMin, VecBinaryOperation) \
1377 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001378 M(VecAnd, VecBinaryOperation) \
1379 M(VecAndNot, VecBinaryOperation) \
1380 M(VecOr, VecBinaryOperation) \
1381 M(VecXor, VecBinaryOperation) \
1382 M(VecShl, VecBinaryOperation) \
1383 M(VecShr, VecBinaryOperation) \
1384 M(VecUShr, VecBinaryOperation) \
1385 M(VecLoad, VecMemoryOperation) \
1386 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001387
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001388/*
1389 * Instructions, shared across several (not all) architectures.
1390 */
1391#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1392#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1393#else
1394#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001395 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001396 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001397 M(MultiplyAccumulate, Instruction) \
1398 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001399#endif
1400
Vladimir Markob4536b72015-11-24 13:45:23 +00001401#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001402#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001403#else
1404#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1405 M(ArmDexCacheArraysBase, Instruction)
1406#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001407
1408#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1409
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001410#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001412#else
1413#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1414 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001415 M(MipsDexCacheArraysBase, Instruction) \
1416 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001417#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001418
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001419#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1420
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001421#ifndef ART_ENABLE_CODEGEN_x86
1422#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1423#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001424#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1425 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001426 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001427 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001428 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001429#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001430
1431#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1432
1433#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1434 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001435 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001436 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1437 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001438 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001439 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001440 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1441 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1442
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001443#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1444 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001445 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001446 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001447 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001448 M(Invoke, Instruction) \
1449 M(VecOperation, Instruction) \
1450 M(VecUnaryOperation, VecOperation) \
1451 M(VecBinaryOperation, VecOperation) \
1452 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001453
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001454#define FOR_EACH_INSTRUCTION(M) \
1455 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1456 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1457
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001458#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001459FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1460#undef FORWARD_DECLARATION
1461
Vladimir Marko372f10e2016-05-17 16:30:10 +01001462#define DECLARE_INSTRUCTION(type) \
1463 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1464 const char* DebugName() const OVERRIDE { return #type; } \
1465 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1466 return other->Is##type(); \
1467 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001468 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001469
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001470#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1471 bool Is##type() const { return As##type() != nullptr; } \
1472 const H##type* As##type() const { return this; } \
1473 H##type* As##type() { return this; }
1474
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001475template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001476class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001477 public:
David Brazdiled596192015-01-23 10:39:45 +00001478 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001479 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001480 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001481
Vladimir Marko46817b82016-03-29 12:21:58 +01001482 // Hook for the IntrusiveForwardList<>.
1483 // TODO: Hide this better.
1484 IntrusiveForwardListHook hook;
1485
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001486 private:
David Brazdiled596192015-01-23 10:39:45 +00001487 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001488 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001489
1490 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001491 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001492
Vladimir Marko46817b82016-03-29 12:21:58 +01001493 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001494
1495 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1496};
1497
David Brazdiled596192015-01-23 10:39:45 +00001498template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001499using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001500
David Brazdil1abb4192015-02-17 18:33:36 +00001501// This class is used by HEnvironment and HInstruction classes to record the
1502// instructions they use and pointers to the corresponding HUseListNodes kept
1503// by the used instructions.
1504template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001505class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001506 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001507 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1508 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001509
Vladimir Marko46817b82016-03-29 12:21:58 +01001510 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1511 : HUserRecord(old_record.instruction_, before_use_node) {}
1512 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1513 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001514 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001515 }
1516
1517 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001518 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1519 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001520
1521 private:
1522 // Instruction used by the user.
1523 HInstruction* instruction_;
1524
Vladimir Marko46817b82016-03-29 12:21:58 +01001525 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1526 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001527};
1528
Vladimir Markoe9004912016-06-16 16:50:52 +01001529// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1530// This is used for HInstruction::GetInputs() to return a container wrapper providing
1531// HInstruction* values even though the underlying container has HUserRecord<>s.
1532struct HInputExtractor {
1533 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1534 return record.GetInstruction();
1535 }
1536 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1537 return record.GetInstruction();
1538 }
1539};
1540
1541using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1542using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1543
Aart Bik854a02b2015-07-14 16:07:00 -07001544/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001545 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001546 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 * For write/read dependences on fields/arrays, the dependence analysis uses
1548 * type disambiguation (e.g. a float field write cannot modify the value of an
1549 * integer field read) and the access type (e.g. a reference array write cannot
1550 * modify the value of a reference field read [although it may modify the
1551 * reference fetch prior to reading the field, which is represented by its own
1552 * write/read dependence]). The analysis makes conservative points-to
1553 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1554 * the same, and any reference read depends on any reference read without
1555 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001556 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001557 * The internal representation uses 38-bit and is described in the table below.
1558 * The first line indicates the side effect, and for field/array accesses the
1559 * second line indicates the type of the access (in the order of the
1560 * Primitive::Type enum).
1561 * The two numbered lines below indicate the bit position in the bitfield (read
1562 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001563 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1565 * +-------------+---------+---------+--------------+---------+---------+
1566 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1567 * | 3 |333333322|222222221| 1 |111111110|000000000|
1568 * | 7 |654321098|765432109| 8 |765432109|876543210|
1569 *
1570 * Note that, to ease the implementation, 'changes' bits are least significant
1571 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001572 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001573class SideEffects : public ValueObject {
1574 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001575 SideEffects() : flags_(0) {}
1576
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001577 static SideEffects None() {
1578 return SideEffects(0);
1579 }
1580
1581 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 return SideEffects(kAllChangeBits | kAllDependOnBits);
1583 }
1584
1585 static SideEffects AllChanges() {
1586 return SideEffects(kAllChangeBits);
1587 }
1588
1589 static SideEffects AllDependencies() {
1590 return SideEffects(kAllDependOnBits);
1591 }
1592
1593 static SideEffects AllExceptGCDependency() {
1594 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1595 }
1596
1597 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001598 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001599 }
1600
Aart Bik34c3ba92015-07-20 14:08:59 -07001601 static SideEffects AllWrites() {
1602 return SideEffects(kAllWrites);
1603 }
1604
1605 static SideEffects AllReads() {
1606 return SideEffects(kAllReads);
1607 }
1608
1609 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1610 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001611 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001612 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001613 }
1614
Aart Bik854a02b2015-07-14 16:07:00 -07001615 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001616 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001617 }
1618
Aart Bik34c3ba92015-07-20 14:08:59 -07001619 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1620 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001621 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001622 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001623 }
1624
1625 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001626 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001627 }
1628
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001629 static SideEffects CanTriggerGC() {
1630 return SideEffects(1ULL << kCanTriggerGCBit);
1631 }
1632
1633 static SideEffects DependsOnGC() {
1634 return SideEffects(1ULL << kDependsOnGCBit);
1635 }
1636
Aart Bik854a02b2015-07-14 16:07:00 -07001637 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001638 SideEffects Union(SideEffects other) const {
1639 return SideEffects(flags_ | other.flags_);
1640 }
1641
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 SideEffects Exclusion(SideEffects other) const {
1643 return SideEffects(flags_ & ~other.flags_);
1644 }
1645
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001646 void Add(SideEffects other) {
1647 flags_ |= other.flags_;
1648 }
1649
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 bool Includes(SideEffects other) const {
1651 return (other.flags_ & flags_) == other.flags_;
1652 }
1653
1654 bool HasSideEffects() const {
1655 return (flags_ & kAllChangeBits);
1656 }
1657
1658 bool HasDependencies() const {
1659 return (flags_ & kAllDependOnBits);
1660 }
1661
1662 // Returns true if there are no side effects or dependencies.
1663 bool DoesNothing() const {
1664 return flags_ == 0;
1665 }
1666
Aart Bik854a02b2015-07-14 16:07:00 -07001667 // Returns true if something is written.
1668 bool DoesAnyWrite() const {
1669 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001670 }
1671
Aart Bik854a02b2015-07-14 16:07:00 -07001672 // Returns true if something is read.
1673 bool DoesAnyRead() const {
1674 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001675 }
1676
Aart Bik854a02b2015-07-14 16:07:00 -07001677 // Returns true if potentially everything is written and read
1678 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001679 bool DoesAllReadWrite() const {
1680 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1681 }
1682
Aart Bik854a02b2015-07-14 16:07:00 -07001683 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001684 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001685 }
1686
Roland Levillain0d5a2812015-11-13 10:07:31 +00001687 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001688 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001689 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1690 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001691 }
1692
1693 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001694 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001695 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001696 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001697 for (int s = kLastBit; s >= 0; s--) {
1698 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1699 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1700 // This is a bit for the GC side effect.
1701 if (current_bit_is_set) {
1702 flags += "GC";
1703 }
Aart Bik854a02b2015-07-14 16:07:00 -07001704 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001705 } else {
1706 // This is a bit for the array/field analysis.
1707 // The underscore character stands for the 'can trigger GC' bit.
1708 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1709 if (current_bit_is_set) {
1710 flags += kDebug[s];
1711 }
1712 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1713 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1714 flags += "|";
1715 }
1716 }
Aart Bik854a02b2015-07-14 16:07:00 -07001717 }
1718 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001719 }
1720
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001721 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001722
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001723 private:
1724 static constexpr int kFieldArrayAnalysisBits = 9;
1725
1726 static constexpr int kFieldWriteOffset = 0;
1727 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1728 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1729 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1730
1731 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1732
1733 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1734 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1735 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1736 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1737
1738 static constexpr int kLastBit = kDependsOnGCBit;
1739 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1740
1741 // Aliases.
1742
1743 static_assert(kChangeBits == kDependOnBits,
1744 "the 'change' bits should match the 'depend on' bits.");
1745
1746 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1747 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1748 static constexpr uint64_t kAllWrites =
1749 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1750 static constexpr uint64_t kAllReads =
1751 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001752
Aart Bik854a02b2015-07-14 16:07:00 -07001753 // Translates type to bit flag.
1754 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1755 CHECK_NE(type, Primitive::kPrimVoid);
1756 const uint64_t one = 1;
1757 const int shift = type; // 0-based consecutive enum
1758 DCHECK_LE(kFieldWriteOffset, shift);
1759 DCHECK_LT(shift, kArrayWriteOffset);
1760 return one << (type + offset);
1761 }
1762
1763 // Private constructor on direct flags value.
1764 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1765
1766 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001767};
1768
David Brazdiled596192015-01-23 10:39:45 +00001769// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001770class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001771 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001772 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1773 size_t number_of_vregs,
1774 ArtMethod* method,
1775 uint32_t dex_pc,
1776 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001777 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1778 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001779 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001780 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001781 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001782 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 }
1784
Mingyao Yang01b47b02017-02-03 12:09:57 -08001785 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001786 : HEnvironment(arena,
1787 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001788 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001789 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001790 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001791
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001792 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001793 if (parent_ != nullptr) {
1794 parent_->SetAndCopyParentChain(allocator, parent);
1795 } else {
1796 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1797 parent_->CopyFrom(parent);
1798 if (parent->GetParent() != nullptr) {
1799 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1800 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001801 }
David Brazdiled596192015-01-23 10:39:45 +00001802 }
1803
Vladimir Marko71bf8092015-09-15 15:33:14 +01001804 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001805 void CopyFrom(HEnvironment* environment);
1806
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001807 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1808 // input to the loop phi instead. This is for inserting instructions that
1809 // require an environment (like HDeoptimization) in the loop pre-header.
1810 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001811
1812 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001813 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001814 }
1815
1816 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001817 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001818 }
1819
David Brazdil1abb4192015-02-17 18:33:36 +00001820 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001821
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001822 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001823
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001824 HEnvironment* GetParent() const { return parent_; }
1825
1826 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001827 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001828 }
1829
1830 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001831 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001832 }
1833
1834 uint32_t GetDexPc() const {
1835 return dex_pc_;
1836 }
1837
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001838 ArtMethod* GetMethod() const {
1839 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001840 }
1841
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001842 HInstruction* GetHolder() const {
1843 return holder_;
1844 }
1845
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001846
1847 bool IsFromInlinedInvoke() const {
1848 return GetParent() != nullptr;
1849 }
1850
David Brazdiled596192015-01-23 10:39:45 +00001851 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001852 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1853 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001854 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001855 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001856 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001857
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001858 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001859 HInstruction* const holder_;
1860
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001861 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001862
1863 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1864};
1865
Vladimir Markof9f64412015-09-02 14:05:49 +01001866class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001867 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001868 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001869 : previous_(nullptr),
1870 next_(nullptr),
1871 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001872 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001874 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001875 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001876 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001877 locations_(nullptr),
1878 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001879 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001880 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001881 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1882 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1883 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001884
Dave Allison20dfc792014-06-16 20:44:29 -07001885 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001886
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001887#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001888 enum InstructionKind {
1889 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1890 };
1891#undef DECLARE_KIND
1892
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001893 HInstruction* GetNext() const { return next_; }
1894 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001895
Calin Juravle77520bc2015-01-12 18:45:46 +00001896 HInstruction* GetNextDisregardingMoves() const;
1897 HInstruction* GetPreviousDisregardingMoves() const;
1898
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001899 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001900 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001901 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001902 bool IsInBlock() const { return block_ != nullptr; }
1903 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001904 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1905 bool IsIrreducibleLoopHeaderPhi() const {
1906 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1907 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001908
Vladimir Marko372f10e2016-05-17 16:30:10 +01001909 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1910
1911 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1912 // One virtual method is enough, just const_cast<> and then re-add the const.
1913 return ArrayRef<const HUserRecord<HInstruction*>>(
1914 const_cast<HInstruction*>(this)->GetInputRecords());
1915 }
1916
Vladimir Markoe9004912016-06-16 16:50:52 +01001917 HInputsRef GetInputs() {
1918 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001919 }
1920
Vladimir Markoe9004912016-06-16 16:50:52 +01001921 HConstInputsRef GetInputs() const {
1922 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001923 }
1924
1925 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001926 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001927
Aart Bik2767f4b2016-10-28 15:03:53 -07001928 bool HasInput(HInstruction* input) const {
1929 for (const HInstruction* i : GetInputs()) {
1930 if (i == input) {
1931 return true;
1932 }
1933 }
1934 return false;
1935 }
1936
Vladimir Marko372f10e2016-05-17 16:30:10 +01001937 void SetRawInputAt(size_t index, HInstruction* input) {
1938 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1939 }
1940
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001941 virtual void Accept(HGraphVisitor* visitor) = 0;
1942 virtual const char* DebugName() const = 0;
1943
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001944 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1945
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001946 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001947
1948 uint32_t GetDexPc() const { return dex_pc_; }
1949
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001950 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001951
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001952 // Can the instruction throw?
1953 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1954 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001955 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001956 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001957
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001958 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001959 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001960
Calin Juravle10e244f2015-01-26 18:54:32 +00001961 // Does not apply for all instructions, but having this at top level greatly
1962 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001963 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001964 virtual bool CanBeNull() const {
1965 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1966 return true;
1967 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001968
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001969 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001970 return false;
1971 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001972
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001973 virtual bool IsActualObject() const {
1974 return GetType() == Primitive::kPrimNot;
1975 }
1976
Calin Juravle2e768302015-07-28 14:41:11 +00001977 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001978
Calin Juravle61d544b2015-02-23 16:46:57 +00001979 ReferenceTypeInfo GetReferenceTypeInfo() const {
1980 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001981 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001982 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001983 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001984
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001985 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001986 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001987 // Note: fixup_end remains valid across push_front().
1988 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1989 HUseListNode<HInstruction*>* new_node =
1990 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1991 uses_.push_front(*new_node);
1992 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993 }
1994
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001995 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001996 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001997 // Note: env_fixup_end remains valid across push_front().
1998 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1999 HUseListNode<HEnvironment*>* new_node =
2000 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2001 env_uses_.push_front(*new_node);
2002 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002003 }
2004
David Brazdil1abb4192015-02-17 18:33:36 +00002005 void RemoveAsUserOfInput(size_t input) {
2006 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002007 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2008 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2009 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002010 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002011
Vladimir Marko372f10e2016-05-17 16:30:10 +01002012 void RemoveAsUserOfAllInputs() {
2013 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2014 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2015 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2016 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2017 }
2018 }
2019
David Brazdil1abb4192015-02-17 18:33:36 +00002020 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2021 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002022
Vladimir Marko46817b82016-03-29 12:21:58 +01002023 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2024 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2025 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002026 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002027 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002028 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002029
Aart Bikcc42be02016-10-20 16:14:16 -07002030 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002031 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002032 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002033 !CanThrow() &&
2034 !IsSuspendCheck() &&
2035 !IsControlFlow() &&
2036 !IsNativeDebugInfo() &&
2037 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002038 // If we added an explicit barrier then we should keep it.
2039 !IsMemoryBarrier();
2040 }
2041
Aart Bikcc42be02016-10-20 16:14:16 -07002042 bool IsDeadAndRemovable() const {
2043 return IsRemovable() && !HasUses();
2044 }
2045
Roland Levillain6c82d402014-10-13 16:10:27 +01002046 // Does this instruction strictly dominate `other_instruction`?
2047 // Returns false if this instruction and `other_instruction` are the same.
2048 // Aborts if this instruction and `other_instruction` are both phis.
2049 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002050
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002051 int GetId() const { return id_; }
2052 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002053
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002054 int GetSsaIndex() const { return ssa_index_; }
2055 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2056 bool HasSsaIndex() const { return ssa_index_ != -1; }
2057
2058 bool HasEnvironment() const { return environment_ != nullptr; }
2059 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002060 // Set the `environment_` field. Raw because this method does not
2061 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002062 void SetRawEnvironment(HEnvironment* environment) {
2063 DCHECK(environment_ == nullptr);
2064 DCHECK_EQ(environment->GetHolder(), this);
2065 environment_ = environment;
2066 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002067
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002068 void InsertRawEnvironment(HEnvironment* environment) {
2069 DCHECK(environment_ != nullptr);
2070 DCHECK_EQ(environment->GetHolder(), this);
2071 DCHECK(environment->GetParent() == nullptr);
2072 environment->parent_ = environment_;
2073 environment_ = environment;
2074 }
2075
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002076 void RemoveEnvironment();
2077
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002078 // Set the environment of this instruction, copying it from `environment`. While
2079 // copying, the uses lists are being updated.
2080 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002081 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002082 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002083 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002084 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002085 if (environment->GetParent() != nullptr) {
2086 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2087 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002088 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002089
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002090 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2091 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002092 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002093 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002094 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002095 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002096 if (environment->GetParent() != nullptr) {
2097 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2098 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002099 }
2100
Nicolas Geoffray39468442014-09-02 15:17:15 +01002101 // Returns the number of entries in the environment. Typically, that is the
2102 // number of dex registers in a method. It could be more in case of inlining.
2103 size_t EnvironmentSize() const;
2104
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002105 LocationSummary* GetLocations() const { return locations_; }
2106 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002107
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002108 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002109 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002110 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002111
Alexandre Rames188d4312015-04-09 18:30:21 +01002112 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2113 // uses of this instruction by `other` are *not* updated.
2114 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2115 ReplaceWith(other);
2116 other->ReplaceInput(this, use_index);
2117 }
2118
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002119 // Move `this` instruction before `cursor`
2120 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002121
Vladimir Markofb337ea2015-11-25 15:25:10 +00002122 // Move `this` before its first user and out of any loops. If there is no
2123 // out-of-loop user that dominates all other users, move the instruction
2124 // to the end of the out-of-loop common dominator of the user's blocks.
2125 //
2126 // This can be used only on non-throwing instructions with no side effects that
2127 // have at least one use but no environment uses.
2128 void MoveBeforeFirstUserAndOutOfLoops();
2129
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002130#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002131 bool Is##type() const; \
2132 const H##type* As##type() const; \
2133 H##type* As##type();
2134
2135 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2136#undef INSTRUCTION_TYPE_CHECK
2137
2138#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002139 bool Is##type() const { return (As##type() != nullptr); } \
2140 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002141 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002142 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002143#undef INSTRUCTION_TYPE_CHECK
2144
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002145 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002146 // TODO: this method is used by LICM and GVN with possibly different
2147 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002148 virtual bool CanBeMoved() const { return false; }
2149
2150 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002151 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002152 return false;
2153 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002154
2155 // Returns whether any data encoded in the two instructions is equal.
2156 // This method does not look at the inputs. Both instructions must be
2157 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002158 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002159 return false;
2160 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002161
2162 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002163 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002164 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002165 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002166
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002167 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2168 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2169 // the virtual function because the __attribute__((__pure__)) doesn't really
2170 // apply the strong requirement for virtual functions, preventing optimizations.
2171 InstructionKind GetKind() const PURE;
2172 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002173
2174 virtual size_t ComputeHashCode() const {
2175 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002176 for (const HInstruction* input : GetInputs()) {
2177 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002178 }
2179 return result;
2180 }
2181
2182 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002183 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002184 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002185
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002186 size_t GetLifetimePosition() const { return lifetime_position_; }
2187 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2188 LiveInterval* GetLiveInterval() const { return live_interval_; }
2189 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2190 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2191
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002192 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2193
2194 // Returns whether the code generation of the instruction will require to have access
2195 // to the current method. Such instructions are:
2196 // (1): Instructions that require an environment, as calling the runtime requires
2197 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002198 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2199 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002200 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002201 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002202 }
2203
Vladimir Markodc151b22015-10-15 18:02:30 +01002204 // Returns whether the code generation of the instruction will require to have access
2205 // to the dex cache of the current method's declaring class via the current method.
2206 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002207
Mark Mendellc4701932015-04-10 13:18:51 -04002208 // Does this instruction have any use in an environment before
2209 // control flow hits 'other'?
2210 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2211
2212 // Remove all references to environment uses of this instruction.
2213 // The caller must ensure that this is safe to do.
2214 void RemoveEnvironmentUsers();
2215
Vladimir Markoa1de9182016-02-25 11:37:38 +00002216 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2217 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002218
David Brazdil1abb4192015-02-17 18:33:36 +00002219 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002220 // If set, the machine code for this instruction is assumed to be generated by
2221 // its users. Used by liveness analysis to compute use positions accordingly.
2222 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2223 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2224 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2225 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2226
Vladimir Marko372f10e2016-05-17 16:30:10 +01002227 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2228 return GetInputRecords()[i];
2229 }
2230
2231 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2232 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2233 input_records[index] = input;
2234 }
David Brazdil1abb4192015-02-17 18:33:36 +00002235
Vladimir Markoa1de9182016-02-25 11:37:38 +00002236 uint32_t GetPackedFields() const {
2237 return packed_fields_;
2238 }
2239
2240 template <size_t flag>
2241 bool GetPackedFlag() const {
2242 return (packed_fields_ & (1u << flag)) != 0u;
2243 }
2244
2245 template <size_t flag>
2246 void SetPackedFlag(bool value = true) {
2247 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2248 }
2249
2250 template <typename BitFieldType>
2251 typename BitFieldType::value_type GetPackedField() const {
2252 return BitFieldType::Decode(packed_fields_);
2253 }
2254
2255 template <typename BitFieldType>
2256 void SetPackedField(typename BitFieldType::value_type value) {
2257 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2258 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2259 }
2260
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002261 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002262 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2263 auto before_use_node = uses_.before_begin();
2264 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2265 HInstruction* user = use_node->GetUser();
2266 size_t input_index = use_node->GetIndex();
2267 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2268 before_use_node = use_node;
2269 }
2270 }
2271
2272 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2273 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2274 if (next != uses_.end()) {
2275 HInstruction* next_user = next->GetUser();
2276 size_t next_index = next->GetIndex();
2277 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2278 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2279 }
2280 }
2281
2282 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2283 auto before_env_use_node = env_uses_.before_begin();
2284 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2285 HEnvironment* user = env_use_node->GetUser();
2286 size_t input_index = env_use_node->GetIndex();
2287 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2288 before_env_use_node = env_use_node;
2289 }
2290 }
2291
2292 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2293 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2294 if (next != env_uses_.end()) {
2295 HEnvironment* next_user = next->GetUser();
2296 size_t next_index = next->GetIndex();
2297 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2298 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2299 }
2300 }
David Brazdil1abb4192015-02-17 18:33:36 +00002301
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002302 HInstruction* previous_;
2303 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002304 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002305 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002306
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002307 // An instruction gets an id when it is added to the graph.
2308 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002309 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002310 int id_;
2311
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002312 // When doing liveness analysis, instructions that have uses get an SSA index.
2313 int ssa_index_;
2314
Vladimir Markoa1de9182016-02-25 11:37:38 +00002315 // Packed fields.
2316 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002317
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002318 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002319 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002320
2321 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002322 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002323
Nicolas Geoffray39468442014-09-02 15:17:15 +01002324 // The environment associated with this instruction. Not null if the instruction
2325 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002326 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002327
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002328 // Set by the code generator.
2329 LocationSummary* locations_;
2330
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002331 // Set by the liveness analysis.
2332 LiveInterval* live_interval_;
2333
2334 // Set by the liveness analysis, this is the position in a linear
2335 // order of blocks where this instruction's live interval start.
2336 size_t lifetime_position_;
2337
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002338 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002339
Vladimir Markoa1de9182016-02-25 11:37:38 +00002340 // The reference handle part of the reference type info.
2341 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002342 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002343 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002344
David Brazdil1abb4192015-02-17 18:33:36 +00002345 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002346 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002347 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002348 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002349 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002350
2351 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2352};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002353std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002354
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002355// Iterates over the instructions, while preserving the next instruction
2356// in case the current instruction gets removed from the list by the user
2357// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002358class HInstructionIterator : public ValueObject {
2359 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002360 explicit HInstructionIterator(const HInstructionList& instructions)
2361 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002362 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002363 }
2364
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002365 bool Done() const { return instruction_ == nullptr; }
2366 HInstruction* Current() const { return instruction_; }
2367 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002368 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002369 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002370 }
2371
2372 private:
2373 HInstruction* instruction_;
2374 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002375
2376 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002377};
2378
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002379// Iterates over the instructions without saving the next instruction,
2380// therefore handling changes in the graph potentially made by the user
2381// of this iterator.
2382class HInstructionIteratorHandleChanges : public ValueObject {
2383 public:
2384 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2385 : instruction_(instructions.first_instruction_) {
2386 }
2387
2388 bool Done() const { return instruction_ == nullptr; }
2389 HInstruction* Current() const { return instruction_; }
2390 void Advance() {
2391 instruction_ = instruction_->GetNext();
2392 }
2393
2394 private:
2395 HInstruction* instruction_;
2396
2397 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2398};
2399
2400
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002401class HBackwardInstructionIterator : public ValueObject {
2402 public:
2403 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2404 : instruction_(instructions.last_instruction_) {
2405 next_ = Done() ? nullptr : instruction_->GetPrevious();
2406 }
2407
2408 bool Done() const { return instruction_ == nullptr; }
2409 HInstruction* Current() const { return instruction_; }
2410 void Advance() {
2411 instruction_ = next_;
2412 next_ = Done() ? nullptr : instruction_->GetPrevious();
2413 }
2414
2415 private:
2416 HInstruction* instruction_;
2417 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002418
2419 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002420};
2421
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002422class HVariableInputSizeInstruction : public HInstruction {
2423 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002424 using HInstruction::GetInputRecords; // Keep the const version visible.
2425 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2426 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2427 }
2428
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002429 void AddInput(HInstruction* input);
2430 void InsertInputAt(size_t index, HInstruction* input);
2431 void RemoveInputAt(size_t index);
2432
2433 protected:
2434 HVariableInputSizeInstruction(SideEffects side_effects,
2435 uint32_t dex_pc,
2436 ArenaAllocator* arena,
2437 size_t number_of_inputs,
2438 ArenaAllocKind kind)
2439 : HInstruction(side_effects, dex_pc),
2440 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2441
2442 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2443
2444 private:
2445 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2446};
2447
Vladimir Markof9f64412015-09-02 14:05:49 +01002448template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002449class HTemplateInstruction: public HInstruction {
2450 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002451 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002452 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002453 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002454
Vladimir Marko372f10e2016-05-17 16:30:10 +01002455 using HInstruction::GetInputRecords; // Keep the const version visible.
2456 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2457 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002458 }
2459
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002460 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002461 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002462
2463 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002464};
2465
Vladimir Markof9f64412015-09-02 14:05:49 +01002466// HTemplateInstruction specialization for N=0.
2467template<>
2468class HTemplateInstruction<0>: public HInstruction {
2469 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002470 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002471 : HInstruction(side_effects, dex_pc) {}
2472
Vladimir Markof9f64412015-09-02 14:05:49 +01002473 virtual ~HTemplateInstruction() {}
2474
Vladimir Marko372f10e2016-05-17 16:30:10 +01002475 using HInstruction::GetInputRecords; // Keep the const version visible.
2476 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2477 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002478 }
2479
2480 private:
2481 friend class SsaBuilder;
2482};
2483
Dave Allison20dfc792014-06-16 20:44:29 -07002484template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002485class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002486 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002487 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002488 : HTemplateInstruction<N>(side_effects, dex_pc) {
2489 this->template SetPackedField<TypeField>(type);
2490 }
Dave Allison20dfc792014-06-16 20:44:29 -07002491 virtual ~HExpression() {}
2492
Vladimir Markoa1de9182016-02-25 11:37:38 +00002493 Primitive::Type GetType() const OVERRIDE {
2494 return TypeField::Decode(this->GetPackedFields());
2495 }
Dave Allison20dfc792014-06-16 20:44:29 -07002496
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002497 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002498 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2499 static constexpr size_t kFieldTypeSize =
2500 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2501 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2502 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2503 "Too many packed fields.");
2504 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002505};
2506
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002507// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2508// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002509class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002510 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002511 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2512 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002513
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002514 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002515
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002516 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002517
2518 private:
2519 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2520};
2521
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002522// Represents dex's RETURN opcodes. A HReturn is a control flow
2523// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002524class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002526 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2527 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002528 SetRawInputAt(0, value);
2529 }
2530
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002531 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002532
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002533 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002534
2535 private:
2536 DISALLOW_COPY_AND_ASSIGN(HReturn);
2537};
2538
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002539class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002540 public:
2541 HPhi(ArenaAllocator* arena,
2542 uint32_t reg_number,
2543 size_t number_of_inputs,
2544 Primitive::Type type,
2545 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002546 : HVariableInputSizeInstruction(
2547 SideEffects::None(),
2548 dex_pc,
2549 arena,
2550 number_of_inputs,
2551 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002552 reg_number_(reg_number) {
2553 SetPackedField<TypeField>(ToPhiType(type));
2554 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2555 // Phis are constructed live and marked dead if conflicting or unused.
2556 // Individual steps of SsaBuilder should assume that if a phi has been
2557 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2558 SetPackedFlag<kFlagIsLive>(true);
2559 SetPackedFlag<kFlagCanBeNull>(true);
2560 }
2561
2562 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2563 static Primitive::Type ToPhiType(Primitive::Type type) {
2564 return Primitive::PrimitiveKind(type);
2565 }
2566
2567 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2568
David Brazdildee58d62016-04-07 09:54:26 +00002569 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2570 void SetType(Primitive::Type new_type) {
2571 // Make sure that only valid type changes occur. The following are allowed:
2572 // (1) int -> float/ref (primitive type propagation),
2573 // (2) long -> double (primitive type propagation).
2574 DCHECK(GetType() == new_type ||
2575 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2576 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2577 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2578 SetPackedField<TypeField>(new_type);
2579 }
2580
2581 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2582 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2583
2584 uint32_t GetRegNumber() const { return reg_number_; }
2585
2586 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2587 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2588 bool IsDead() const { return !IsLive(); }
2589 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2590
Vladimir Markoe9004912016-06-16 16:50:52 +01002591 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002592 return other != nullptr
2593 && other->IsPhi()
2594 && other->AsPhi()->GetBlock() == GetBlock()
2595 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2596 }
2597
2598 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2599 // An equivalent phi is a phi having the same dex register and type.
2600 // It assumes that phis with the same dex register are adjacent.
2601 HPhi* GetNextEquivalentPhiWithSameType() {
2602 HInstruction* next = GetNext();
2603 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2604 if (next->GetType() == GetType()) {
2605 return next->AsPhi();
2606 }
2607 next = next->GetNext();
2608 }
2609 return nullptr;
2610 }
2611
2612 DECLARE_INSTRUCTION(Phi);
2613
David Brazdildee58d62016-04-07 09:54:26 +00002614 private:
2615 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2616 static constexpr size_t kFieldTypeSize =
2617 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2618 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2619 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2620 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2621 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2622 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2623
David Brazdildee58d62016-04-07 09:54:26 +00002624 const uint32_t reg_number_;
2625
2626 DISALLOW_COPY_AND_ASSIGN(HPhi);
2627};
2628
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002629// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002630// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002631// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002632class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002633 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002634 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002635
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002636 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002637
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002638 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002639
2640 private:
2641 DISALLOW_COPY_AND_ASSIGN(HExit);
2642};
2643
2644// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002645class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002646 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002647 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002648
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002649 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002650
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002651 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002652 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002653 }
2654
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002655 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002656
2657 private:
2658 DISALLOW_COPY_AND_ASSIGN(HGoto);
2659};
2660
Roland Levillain9867bc72015-08-05 10:21:34 +01002661class HConstant : public HExpression<0> {
2662 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002663 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2664 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002665
2666 bool CanBeMoved() const OVERRIDE { return true; }
2667
Roland Levillain1a653882016-03-18 18:05:57 +00002668 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002669 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002670 // Is this constant 0 in the arithmetic sense?
2671 virtual bool IsArithmeticZero() const { return false; }
2672 // Is this constant a 0-bit pattern?
2673 virtual bool IsZeroBitPattern() const { return false; }
2674 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002675 virtual bool IsOne() const { return false; }
2676
David Brazdil77a48ae2015-09-15 12:34:04 +00002677 virtual uint64_t GetValueAsUint64() const = 0;
2678
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002679 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002680
2681 private:
2682 DISALLOW_COPY_AND_ASSIGN(HConstant);
2683};
2684
Vladimir Markofcb503c2016-05-18 12:48:17 +01002685class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002686 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002687 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 return true;
2689 }
2690
David Brazdil77a48ae2015-09-15 12:34:04 +00002691 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2692
Roland Levillain9867bc72015-08-05 10:21:34 +01002693 size_t ComputeHashCode() const OVERRIDE { return 0; }
2694
Roland Levillain1a653882016-03-18 18:05:57 +00002695 // The null constant representation is a 0-bit pattern.
2696 virtual bool IsZeroBitPattern() const { return true; }
2697
Roland Levillain9867bc72015-08-05 10:21:34 +01002698 DECLARE_INSTRUCTION(NullConstant);
2699
2700 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002701 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002702
2703 friend class HGraph;
2704 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2705};
2706
2707// Constants of the type int. Those can be from Dex instructions, or
2708// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002709class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002710 public:
2711 int32_t GetValue() const { return value_; }
2712
David Brazdil9f389d42015-10-01 14:32:56 +01002713 uint64_t GetValueAsUint64() const OVERRIDE {
2714 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2715 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002716
Vladimir Marko372f10e2016-05-17 16:30:10 +01002717 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002718 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 return other->AsIntConstant()->value_ == value_;
2720 }
2721
2722 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2723
2724 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002725 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2726 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2728
Roland Levillain1a653882016-03-18 18:05:57 +00002729 // Integer constants are used to encode Boolean values as well,
2730 // where 1 means true and 0 means false.
2731 bool IsTrue() const { return GetValue() == 1; }
2732 bool IsFalse() const { return GetValue() == 0; }
2733
Roland Levillain9867bc72015-08-05 10:21:34 +01002734 DECLARE_INSTRUCTION(IntConstant);
2735
2736 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002737 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2738 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2739 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2740 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002741
2742 const int32_t value_;
2743
2744 friend class HGraph;
2745 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2746 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2747 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2748};
2749
Vladimir Markofcb503c2016-05-18 12:48:17 +01002750class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002751 public:
2752 int64_t GetValue() const { return value_; }
2753
David Brazdil77a48ae2015-09-15 12:34:04 +00002754 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2755
Vladimir Marko372f10e2016-05-17 16:30:10 +01002756 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002757 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002758 return other->AsLongConstant()->value_ == value_;
2759 }
2760
2761 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2762
2763 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002764 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2765 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002766 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2767
2768 DECLARE_INSTRUCTION(LongConstant);
2769
2770 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002771 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2772 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002773
2774 const int64_t value_;
2775
2776 friend class HGraph;
2777 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2778};
Dave Allison20dfc792014-06-16 20:44:29 -07002779
Vladimir Markofcb503c2016-05-18 12:48:17 +01002780class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002781 public:
2782 float GetValue() const { return value_; }
2783
2784 uint64_t GetValueAsUint64() const OVERRIDE {
2785 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2786 }
2787
Vladimir Marko372f10e2016-05-17 16:30:10 +01002788 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002789 DCHECK(other->IsFloatConstant()) << other->DebugName();
2790 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2791 }
2792
2793 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2794
2795 bool IsMinusOne() const OVERRIDE {
2796 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2797 }
Roland Levillain1a653882016-03-18 18:05:57 +00002798 bool IsArithmeticZero() const OVERRIDE {
2799 return std::fpclassify(value_) == FP_ZERO;
2800 }
2801 bool IsArithmeticPositiveZero() const {
2802 return IsArithmeticZero() && !std::signbit(value_);
2803 }
2804 bool IsArithmeticNegativeZero() const {
2805 return IsArithmeticZero() && std::signbit(value_);
2806 }
2807 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002808 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002809 }
2810 bool IsOne() const OVERRIDE {
2811 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2812 }
2813 bool IsNaN() const {
2814 return std::isnan(value_);
2815 }
2816
2817 DECLARE_INSTRUCTION(FloatConstant);
2818
2819 private:
2820 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2821 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2822 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2823 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2824
2825 const float value_;
2826
2827 // Only the SsaBuilder and HGraph can create floating-point constants.
2828 friend class SsaBuilder;
2829 friend class HGraph;
2830 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2831};
2832
Vladimir Markofcb503c2016-05-18 12:48:17 +01002833class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002834 public:
2835 double GetValue() const { return value_; }
2836
2837 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2838
Vladimir Marko372f10e2016-05-17 16:30:10 +01002839 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002840 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2841 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2842 }
2843
2844 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2845
2846 bool IsMinusOne() const OVERRIDE {
2847 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2848 }
Roland Levillain1a653882016-03-18 18:05:57 +00002849 bool IsArithmeticZero() const OVERRIDE {
2850 return std::fpclassify(value_) == FP_ZERO;
2851 }
2852 bool IsArithmeticPositiveZero() const {
2853 return IsArithmeticZero() && !std::signbit(value_);
2854 }
2855 bool IsArithmeticNegativeZero() const {
2856 return IsArithmeticZero() && std::signbit(value_);
2857 }
2858 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002859 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002860 }
2861 bool IsOne() const OVERRIDE {
2862 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2863 }
2864 bool IsNaN() const {
2865 return std::isnan(value_);
2866 }
2867
2868 DECLARE_INSTRUCTION(DoubleConstant);
2869
2870 private:
2871 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2872 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2873 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2874 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2875
2876 const double value_;
2877
2878 // Only the SsaBuilder and HGraph can create floating-point constants.
2879 friend class SsaBuilder;
2880 friend class HGraph;
2881 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2882};
2883
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002884// Conditional branch. A block ending with an HIf instruction must have
2885// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002886class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002887 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2889 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002890 SetRawInputAt(0, input);
2891 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002892
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002893 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002894
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002895 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002896 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002897 }
2898
2899 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002900 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002901 }
2902
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002903 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002904
2905 private:
2906 DISALLOW_COPY_AND_ASSIGN(HIf);
2907};
2908
David Brazdilfc6a86a2015-06-26 10:33:45 +00002909
2910// Abstract instruction which marks the beginning and/or end of a try block and
2911// links it to the respective exception handlers. Behaves the same as a Goto in
2912// non-exceptional control flow.
2913// Normal-flow successor is stored at index zero, exception handlers under
2914// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002915class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002916 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002917 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002918 kEntry,
2919 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002920 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002921 };
2922
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002923 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002924 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2925 SetPackedField<BoundaryKindField>(kind);
2926 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002927
2928 bool IsControlFlow() const OVERRIDE { return true; }
2929
2930 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002931 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002932
David Brazdild26a4112015-11-10 11:07:31 +00002933 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2934 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2935 }
2936
David Brazdilfc6a86a2015-06-26 10:33:45 +00002937 // Returns whether `handler` is among its exception handlers (non-zero index
2938 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002939 bool HasExceptionHandler(const HBasicBlock& handler) const {
2940 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002941 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002942 }
2943
2944 // If not present already, adds `handler` to its block's list of exception
2945 // handlers.
2946 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002947 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002948 GetBlock()->AddSuccessor(handler);
2949 }
2950 }
2951
Vladimir Markoa1de9182016-02-25 11:37:38 +00002952 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2953 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002954
David Brazdilffee3d32015-07-06 11:48:53 +01002955 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2956
David Brazdilfc6a86a2015-06-26 10:33:45 +00002957 DECLARE_INSTRUCTION(TryBoundary);
2958
2959 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002960 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2961 static constexpr size_t kFieldBoundaryKindSize =
2962 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2963 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2964 kFieldBoundaryKind + kFieldBoundaryKindSize;
2965 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2966 "Too many packed fields.");
2967 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002968
2969 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2970};
2971
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002972// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002973class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002974 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002975 enum class Kind {
2976 kBCE,
2977 kInline,
2978 kLast = kInline
2979 };
2980
2981 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2982 // across.
2983 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2984 : HVariableInputSizeInstruction(
2985 SideEffects::All(),
2986 dex_pc,
2987 arena,
2988 /* number_of_inputs */ 1,
2989 kArenaAllocMisc) {
2990 SetPackedFlag<kFieldCanBeMoved>(false);
2991 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002992 SetRawInputAt(0, cond);
2993 }
2994
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002995 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
2996 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
2997 // instead of `guard`.
2998 // We set CanTriggerGC to prevent any intermediate address to be live
2999 // at the point of the `HDeoptimize`.
3000 HDeoptimize(ArenaAllocator* arena,
3001 HInstruction* cond,
3002 HInstruction* guard,
3003 Kind kind,
3004 uint32_t dex_pc)
3005 : HVariableInputSizeInstruction(
3006 SideEffects::CanTriggerGC(),
3007 dex_pc,
3008 arena,
3009 /* number_of_inputs */ 2,
3010 kArenaAllocMisc) {
3011 SetPackedFlag<kFieldCanBeMoved>(true);
3012 SetPackedField<DeoptimizeKindField>(kind);
3013 SetRawInputAt(0, cond);
3014 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003015 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003016
3017 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3018
3019 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3020 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3021 }
3022
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003023 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003024
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003025 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003026
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003027 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3028
3029 Primitive::Type GetType() const OVERRIDE {
3030 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3031 }
3032
3033 bool GuardsAnInput() const {
3034 return InputCount() == 2;
3035 }
3036
3037 HInstruction* GuardedInput() const {
3038 DCHECK(GuardsAnInput());
3039 return InputAt(1);
3040 }
3041
3042 void RemoveGuard() {
3043 RemoveInputAt(1);
3044 }
3045
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003046 DECLARE_INSTRUCTION(Deoptimize);
3047
3048 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003049 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3050 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3051 static constexpr size_t kFieldDeoptimizeKindSize =
3052 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3053 static constexpr size_t kNumberOfDeoptimizePackedBits =
3054 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3055 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3056 "Too many packed fields.");
3057 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3058
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003059 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3060};
3061
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003062std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3063
Mingyao Yang063fc772016-08-02 11:02:54 -07003064// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3065// The compiled code checks this flag value in a guard before devirtualized call and
3066// if it's true, starts to do deoptimization.
3067// It has a 4-byte slot on stack.
3068// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003069class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003070 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003071 // CHA guards are only optimized in a separate pass and it has no side effects
3072 // with regard to other passes.
3073 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3074 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003075 }
3076
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003077 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3078
3079 // We do all CHA guard elimination/motion in a single pass, after which there is no
3080 // further guard elimination/motion since a guard might have been used for justification
3081 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3082 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003083 bool CanBeMoved() const OVERRIDE { return false; }
3084
3085 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3086
3087 private:
3088 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3089};
3090
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003091// Represents the ArtMethod that was passed as a first argument to
3092// the method. It is used by instructions that depend on it, like
3093// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003094class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003095 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003096 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3097 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003098
3099 DECLARE_INSTRUCTION(CurrentMethod);
3100
3101 private:
3102 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3103};
3104
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003105// Fetches an ArtMethod from the virtual table or the interface method table
3106// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003107class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003108 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003109 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003110 kVTable,
3111 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003112 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003113 };
3114 HClassTableGet(HInstruction* cls,
3115 Primitive::Type type,
3116 TableKind kind,
3117 size_t index,
3118 uint32_t dex_pc)
3119 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003120 index_(index) {
3121 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003122 SetRawInputAt(0, cls);
3123 }
3124
3125 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003126 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003127 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003128 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003129 }
3130
Vladimir Markoa1de9182016-02-25 11:37:38 +00003131 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003132 size_t GetIndex() const { return index_; }
3133
3134 DECLARE_INSTRUCTION(ClassTableGet);
3135
3136 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003137 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3138 static constexpr size_t kFieldTableKindSize =
3139 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3140 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3141 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3142 "Too many packed fields.");
3143 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3144
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003145 // The index of the ArtMethod in the table.
3146 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003147
3148 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3149};
3150
Mark Mendellfe57faa2015-09-18 09:26:15 -04003151// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3152// have one successor for each entry in the switch table, and the final successor
3153// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003154class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003155 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003156 HPackedSwitch(int32_t start_value,
3157 uint32_t num_entries,
3158 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003159 uint32_t dex_pc = kNoDexPc)
3160 : HTemplateInstruction(SideEffects::None(), dex_pc),
3161 start_value_(start_value),
3162 num_entries_(num_entries) {
3163 SetRawInputAt(0, input);
3164 }
3165
3166 bool IsControlFlow() const OVERRIDE { return true; }
3167
3168 int32_t GetStartValue() const { return start_value_; }
3169
Vladimir Marko211c2112015-09-24 16:52:33 +01003170 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003171
3172 HBasicBlock* GetDefaultBlock() const {
3173 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003174 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003175 }
3176 DECLARE_INSTRUCTION(PackedSwitch);
3177
3178 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003179 const int32_t start_value_;
3180 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003181
3182 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3183};
3184
Roland Levillain88cb1752014-10-20 16:36:47 +01003185class HUnaryOperation : public HExpression<1> {
3186 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003187 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3188 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003189 SetRawInputAt(0, input);
3190 }
3191
3192 HInstruction* GetInput() const { return InputAt(0); }
3193 Primitive::Type GetResultType() const { return GetType(); }
3194
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003195 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003196 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003197 return true;
3198 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003199
Roland Levillain31dd3d62016-02-16 12:21:02 +00003200 // Try to statically evaluate `this` and return a HConstant
3201 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003202 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003203 HConstant* TryStaticEvaluation() const;
3204
3205 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003206 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3207 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3209 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003210
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003211 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003212
3213 private:
3214 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3215};
3216
Dave Allison20dfc792014-06-16 20:44:29 -07003217class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003218 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003219 HBinaryOperation(Primitive::Type result_type,
3220 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003221 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003222 SideEffects side_effects = SideEffects::None(),
3223 uint32_t dex_pc = kNoDexPc)
3224 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003225 SetRawInputAt(0, left);
3226 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003227 }
3228
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003229 HInstruction* GetLeft() const { return InputAt(0); }
3230 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003231 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003232
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003233 virtual bool IsCommutative() const { return false; }
3234
3235 // Put constant on the right.
3236 // Returns whether order is changed.
3237 bool OrderInputsWithConstantOnTheRight() {
3238 HInstruction* left = InputAt(0);
3239 HInstruction* right = InputAt(1);
3240 if (left->IsConstant() && !right->IsConstant()) {
3241 ReplaceInput(right, 0);
3242 ReplaceInput(left, 1);
3243 return true;
3244 }
3245 return false;
3246 }
3247
3248 // Order inputs by instruction id, but favor constant on the right side.
3249 // This helps GVN for commutative ops.
3250 void OrderInputs() {
3251 DCHECK(IsCommutative());
3252 HInstruction* left = InputAt(0);
3253 HInstruction* right = InputAt(1);
3254 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3255 return;
3256 }
3257 if (OrderInputsWithConstantOnTheRight()) {
3258 return;
3259 }
3260 // Order according to instruction id.
3261 if (left->GetId() > right->GetId()) {
3262 ReplaceInput(right, 0);
3263 ReplaceInput(left, 1);
3264 }
3265 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003266
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003267 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003268 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003269 return true;
3270 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003271
Roland Levillain31dd3d62016-02-16 12:21:02 +00003272 // Try to statically evaluate `this` and return a HConstant
3273 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003274 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003275 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003276
3277 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003278 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3279 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003280 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3281 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003282 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003283 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3284 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003285 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3286 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003287 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3288 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003289 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003290 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3291 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003292
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003293 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003294 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003295 HConstant* GetConstantRight() const;
3296
3297 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003298 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003299 HInstruction* GetLeastConstantLeft() const;
3300
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003301 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003302
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003303 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003304 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3305};
3306
Mark Mendellc4701932015-04-10 13:18:51 -04003307// The comparison bias applies for floating point operations and indicates how NaN
3308// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003309enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003310 kNoBias, // bias is not applicable (i.e. for long operation)
3311 kGtBias, // return 1 for NaN comparisons
3312 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003313 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003314};
3315
Roland Levillain31dd3d62016-02-16 12:21:02 +00003316std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3317
Dave Allison20dfc792014-06-16 20:44:29 -07003318class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003319 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003320 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003321 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3322 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3323 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003324
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003325 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003326 // `instruction`, and disregard moves in between.
3327 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003328
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003329 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003330
3331 virtual IfCondition GetCondition() const = 0;
3332
Mark Mendellc4701932015-04-10 13:18:51 -04003333 virtual IfCondition GetOppositeCondition() const = 0;
3334
Vladimir Markoa1de9182016-02-25 11:37:38 +00003335 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003336 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3337
Vladimir Markoa1de9182016-02-25 11:37:38 +00003338 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3339 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003340
Vladimir Marko372f10e2016-05-17 16:30:10 +01003341 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003342 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003343 }
3344
Roland Levillain4fa13f62015-07-06 18:11:54 +01003345 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003346 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003347 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003348 if (if_cond == kCondNE) {
3349 return true;
3350 } else if (if_cond == kCondEQ) {
3351 return false;
3352 }
3353 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003354 }
3355
3356 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003357 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003358 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003359 if (if_cond == kCondEQ) {
3360 return true;
3361 } else if (if_cond == kCondNE) {
3362 return false;
3363 }
3364 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003365 }
3366
Roland Levillain31dd3d62016-02-16 12:21:02 +00003367 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003368 // Needed if we merge a HCompare into a HCondition.
3369 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3370 static constexpr size_t kFieldComparisonBiasSize =
3371 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3372 static constexpr size_t kNumberOfConditionPackedBits =
3373 kFieldComparisonBias + kFieldComparisonBiasSize;
3374 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3375 using ComparisonBiasField =
3376 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3377
Roland Levillain31dd3d62016-02-16 12:21:02 +00003378 template <typename T>
3379 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3380
3381 template <typename T>
3382 int32_t CompareFP(T x, T y) const {
3383 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3384 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3385 // Handle the bias.
3386 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3387 }
3388
3389 // Return an integer constant containing the result of a condition evaluated at compile time.
3390 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3391 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3392 }
3393
Dave Allison20dfc792014-06-16 20:44:29 -07003394 private:
3395 DISALLOW_COPY_AND_ASSIGN(HCondition);
3396};
3397
3398// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003399class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003400 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003401 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3402 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003403
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003404 bool IsCommutative() const OVERRIDE { return true; }
3405
Vladimir Marko9e23df52015-11-10 17:14:35 +00003406 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3407 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003408 return MakeConstantCondition(true, GetDexPc());
3409 }
3410 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3411 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3412 }
3413 // In the following Evaluate methods, a HCompare instruction has
3414 // been merged into this HEqual instruction; evaluate it as
3415 // `Compare(x, y) == 0`.
3416 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3417 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3418 GetDexPc());
3419 }
3420 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3421 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3422 }
3423 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3424 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003425 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003426
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003427 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003428
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003429 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003430 return kCondEQ;
3431 }
3432
Mark Mendellc4701932015-04-10 13:18:51 -04003433 IfCondition GetOppositeCondition() const OVERRIDE {
3434 return kCondNE;
3435 }
3436
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003437 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003438 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003439
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003440 DISALLOW_COPY_AND_ASSIGN(HEqual);
3441};
3442
Vladimir Markofcb503c2016-05-18 12:48:17 +01003443class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003444 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003445 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3446 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003447
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003448 bool IsCommutative() const OVERRIDE { return true; }
3449
Vladimir Marko9e23df52015-11-10 17:14:35 +00003450 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3451 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003452 return MakeConstantCondition(false, GetDexPc());
3453 }
3454 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3455 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3456 }
3457 // In the following Evaluate methods, a HCompare instruction has
3458 // been merged into this HNotEqual instruction; evaluate it as
3459 // `Compare(x, y) != 0`.
3460 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3461 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3462 }
3463 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3464 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3465 }
3466 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3467 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003468 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003469
Dave Allison20dfc792014-06-16 20:44:29 -07003470 DECLARE_INSTRUCTION(NotEqual);
3471
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003472 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003473 return kCondNE;
3474 }
3475
Mark Mendellc4701932015-04-10 13:18:51 -04003476 IfCondition GetOppositeCondition() const OVERRIDE {
3477 return kCondEQ;
3478 }
3479
Dave Allison20dfc792014-06-16 20:44:29 -07003480 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003481 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003482
Dave Allison20dfc792014-06-16 20:44:29 -07003483 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3484};
3485
Vladimir Markofcb503c2016-05-18 12:48:17 +01003486class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003487 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003488 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3489 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003490
Roland Levillain9867bc72015-08-05 10:21:34 +01003491 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003492 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003493 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003494 // In the following Evaluate methods, a HCompare instruction has
3495 // been merged into this HLessThan instruction; evaluate it as
3496 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003497 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003498 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3499 }
3500 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3501 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3502 }
3503 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3504 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003505 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003506
Dave Allison20dfc792014-06-16 20:44:29 -07003507 DECLARE_INSTRUCTION(LessThan);
3508
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003509 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003510 return kCondLT;
3511 }
3512
Mark Mendellc4701932015-04-10 13:18:51 -04003513 IfCondition GetOppositeCondition() const OVERRIDE {
3514 return kCondGE;
3515 }
3516
Dave Allison20dfc792014-06-16 20:44:29 -07003517 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003518 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003519
Dave Allison20dfc792014-06-16 20:44:29 -07003520 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3521};
3522
Vladimir Markofcb503c2016-05-18 12:48:17 +01003523class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003524 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003525 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3526 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003527
Roland Levillain9867bc72015-08-05 10:21:34 +01003528 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003529 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003530 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003531 // In the following Evaluate methods, a HCompare instruction has
3532 // been merged into this HLessThanOrEqual instruction; evaluate it as
3533 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3536 }
3537 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3538 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3539 }
3540 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3541 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003542 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003543
Dave Allison20dfc792014-06-16 20:44:29 -07003544 DECLARE_INSTRUCTION(LessThanOrEqual);
3545
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003546 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003547 return kCondLE;
3548 }
3549
Mark Mendellc4701932015-04-10 13:18:51 -04003550 IfCondition GetOppositeCondition() const OVERRIDE {
3551 return kCondGT;
3552 }
3553
Dave Allison20dfc792014-06-16 20:44:29 -07003554 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003555 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003556
Dave Allison20dfc792014-06-16 20:44:29 -07003557 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3558};
3559
Vladimir Markofcb503c2016-05-18 12:48:17 +01003560class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003562 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3563 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003564
Roland Levillain9867bc72015-08-05 10:21:34 +01003565 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003566 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003567 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003568 // In the following Evaluate methods, a HCompare instruction has
3569 // been merged into this HGreaterThan instruction; evaluate it as
3570 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003571 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3573 }
3574 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3575 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3576 }
3577 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3578 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003579 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003580
Dave Allison20dfc792014-06-16 20:44:29 -07003581 DECLARE_INSTRUCTION(GreaterThan);
3582
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003583 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003584 return kCondGT;
3585 }
3586
Mark Mendellc4701932015-04-10 13:18:51 -04003587 IfCondition GetOppositeCondition() const OVERRIDE {
3588 return kCondLE;
3589 }
3590
Dave Allison20dfc792014-06-16 20:44:29 -07003591 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003592 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003593
Dave Allison20dfc792014-06-16 20:44:29 -07003594 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3595};
3596
Vladimir Markofcb503c2016-05-18 12:48:17 +01003597class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003598 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003599 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3600 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003601
Roland Levillain9867bc72015-08-05 10:21:34 +01003602 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003603 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003604 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003605 // In the following Evaluate methods, a HCompare instruction has
3606 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3607 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003608 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003609 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3610 }
3611 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3612 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3613 }
3614 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3615 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003616 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003617
Dave Allison20dfc792014-06-16 20:44:29 -07003618 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3619
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003620 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003621 return kCondGE;
3622 }
3623
Mark Mendellc4701932015-04-10 13:18:51 -04003624 IfCondition GetOppositeCondition() const OVERRIDE {
3625 return kCondLT;
3626 }
3627
Dave Allison20dfc792014-06-16 20:44:29 -07003628 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003629 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003630
Dave Allison20dfc792014-06-16 20:44:29 -07003631 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3632};
3633
Vladimir Markofcb503c2016-05-18 12:48:17 +01003634class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003635 public:
3636 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3637 : HCondition(first, second, dex_pc) {}
3638
3639 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003641 }
3642 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003643 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3644 }
3645 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3646 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3647 LOG(FATAL) << DebugName() << " is not defined for float values";
3648 UNREACHABLE();
3649 }
3650 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3651 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3652 LOG(FATAL) << DebugName() << " is not defined for double values";
3653 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003654 }
3655
3656 DECLARE_INSTRUCTION(Below);
3657
3658 IfCondition GetCondition() const OVERRIDE {
3659 return kCondB;
3660 }
3661
3662 IfCondition GetOppositeCondition() const OVERRIDE {
3663 return kCondAE;
3664 }
3665
3666 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003667 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003668 return MakeUnsigned(x) < MakeUnsigned(y);
3669 }
Aart Bike9f37602015-10-09 11:15:55 -07003670
3671 DISALLOW_COPY_AND_ASSIGN(HBelow);
3672};
3673
Vladimir Markofcb503c2016-05-18 12:48:17 +01003674class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003675 public:
3676 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3677 : HCondition(first, second, dex_pc) {}
3678
3679 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003681 }
3682 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003683 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3684 }
3685 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3686 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3687 LOG(FATAL) << DebugName() << " is not defined for float values";
3688 UNREACHABLE();
3689 }
3690 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3691 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3692 LOG(FATAL) << DebugName() << " is not defined for double values";
3693 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003694 }
3695
3696 DECLARE_INSTRUCTION(BelowOrEqual);
3697
3698 IfCondition GetCondition() const OVERRIDE {
3699 return kCondBE;
3700 }
3701
3702 IfCondition GetOppositeCondition() const OVERRIDE {
3703 return kCondA;
3704 }
3705
3706 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003707 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003708 return MakeUnsigned(x) <= MakeUnsigned(y);
3709 }
Aart Bike9f37602015-10-09 11:15:55 -07003710
3711 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3712};
3713
Vladimir Markofcb503c2016-05-18 12:48:17 +01003714class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003715 public:
3716 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3717 : HCondition(first, second, dex_pc) {}
3718
3719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003721 }
3722 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003723 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3724 }
3725 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3726 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3727 LOG(FATAL) << DebugName() << " is not defined for float values";
3728 UNREACHABLE();
3729 }
3730 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3731 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3732 LOG(FATAL) << DebugName() << " is not defined for double values";
3733 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003734 }
3735
3736 DECLARE_INSTRUCTION(Above);
3737
3738 IfCondition GetCondition() const OVERRIDE {
3739 return kCondA;
3740 }
3741
3742 IfCondition GetOppositeCondition() const OVERRIDE {
3743 return kCondBE;
3744 }
3745
3746 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003747 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 return MakeUnsigned(x) > MakeUnsigned(y);
3749 }
Aart Bike9f37602015-10-09 11:15:55 -07003750
3751 DISALLOW_COPY_AND_ASSIGN(HAbove);
3752};
3753
Vladimir Markofcb503c2016-05-18 12:48:17 +01003754class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003755 public:
3756 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3757 : HCondition(first, second, dex_pc) {}
3758
3759 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003760 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003761 }
3762 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003763 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3764 }
3765 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3766 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3767 LOG(FATAL) << DebugName() << " is not defined for float values";
3768 UNREACHABLE();
3769 }
3770 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3771 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3772 LOG(FATAL) << DebugName() << " is not defined for double values";
3773 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003774 }
3775
3776 DECLARE_INSTRUCTION(AboveOrEqual);
3777
3778 IfCondition GetCondition() const OVERRIDE {
3779 return kCondAE;
3780 }
3781
3782 IfCondition GetOppositeCondition() const OVERRIDE {
3783 return kCondB;
3784 }
3785
3786 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003787 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003788 return MakeUnsigned(x) >= MakeUnsigned(y);
3789 }
Aart Bike9f37602015-10-09 11:15:55 -07003790
3791 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3792};
Dave Allison20dfc792014-06-16 20:44:29 -07003793
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003794// Instruction to check how two inputs compare to each other.
3795// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003796class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003797 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003798 // Note that `comparison_type` is the type of comparison performed
3799 // between the comparison's inputs, not the type of the instantiated
3800 // HCompare instruction (which is always Primitive::kPrimInt).
3801 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003802 HInstruction* first,
3803 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003804 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003805 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003806 : HBinaryOperation(Primitive::kPrimInt,
3807 first,
3808 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003809 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003810 dex_pc) {
3811 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003812 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3813 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003814 }
3815
Roland Levillain9867bc72015-08-05 10:21:34 +01003816 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003817 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3818
3819 template <typename T>
3820 int32_t ComputeFP(T x, T y) const {
3821 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3822 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3823 // Handle the bias.
3824 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3825 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003826
Roland Levillain9867bc72015-08-05 10:21:34 +01003827 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003828 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3829 // reach this code path when processing a freshly built HIR
3830 // graph. However HCompare integer instructions can be synthesized
3831 // by the instruction simplifier to implement IntegerCompare and
3832 // IntegerSignum intrinsics, so we have to handle this case.
3833 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003834 }
3835 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003836 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3837 }
3838 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3839 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3840 }
3841 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3842 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003843 }
3844
Vladimir Marko372f10e2016-05-17 16:30:10 +01003845 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003846 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003847 }
3848
Vladimir Markoa1de9182016-02-25 11:37:38 +00003849 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003850
Roland Levillain31dd3d62016-02-16 12:21:02 +00003851 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003852 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003853 bool IsGtBias() const {
3854 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003855 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003856 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003857
Roland Levillain1693a1f2016-03-15 14:57:31 +00003858 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3859 // Comparisons do not require a runtime call in any back end.
3860 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003861 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003862
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003863 DECLARE_INSTRUCTION(Compare);
3864
Roland Levillain31dd3d62016-02-16 12:21:02 +00003865 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003866 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3867 static constexpr size_t kFieldComparisonBiasSize =
3868 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3869 static constexpr size_t kNumberOfComparePackedBits =
3870 kFieldComparisonBias + kFieldComparisonBiasSize;
3871 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3872 using ComparisonBiasField =
3873 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3874
Roland Levillain31dd3d62016-02-16 12:21:02 +00003875 // Return an integer constant containing the result of a comparison evaluated at compile time.
3876 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3877 DCHECK(value == -1 || value == 0 || value == 1) << value;
3878 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3879 }
3880
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003881 private:
3882 DISALLOW_COPY_AND_ASSIGN(HCompare);
3883};
3884
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003885class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003886 public:
3887 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003888 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003889 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003890 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003891 bool finalizable,
3892 QuickEntrypointEnum entrypoint)
3893 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3894 type_index_(type_index),
3895 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003896 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003897 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003898 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003899 }
3900
Andreas Gampea5b09a62016-11-17 15:21:22 -08003901 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003902 const DexFile& GetDexFile() const { return dex_file_; }
3903
3904 // Calls runtime so needs an environment.
3905 bool NeedsEnvironment() const OVERRIDE { return true; }
3906
Mingyao Yang062157f2016-03-02 10:15:36 -08003907 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3908 bool CanThrow() const OVERRIDE { return true; }
3909
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003910 bool NeedsChecks() const {
3911 return entrypoint_ == kQuickAllocObjectWithChecks;
3912 }
David Brazdil6de19382016-01-08 17:37:10 +00003913
Vladimir Markoa1de9182016-02-25 11:37:38 +00003914 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003915
3916 bool CanBeNull() const OVERRIDE { return false; }
3917
3918 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3919
3920 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3921 entrypoint_ = entrypoint;
3922 }
3923
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003924 HLoadClass* GetLoadClass() const {
3925 HInstruction* input = InputAt(0);
3926 if (input->IsClinitCheck()) {
3927 input = input->InputAt(0);
3928 }
3929 DCHECK(input->IsLoadClass());
3930 return input->AsLoadClass();
3931 }
3932
David Brazdil6de19382016-01-08 17:37:10 +00003933 bool IsStringAlloc() const;
3934
3935 DECLARE_INSTRUCTION(NewInstance);
3936
3937 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003938 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003939 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3940 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3941 "Too many packed fields.");
3942
Andreas Gampea5b09a62016-11-17 15:21:22 -08003943 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003944 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003945 QuickEntrypointEnum entrypoint_;
3946
3947 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3948};
3949
Agi Csaki05f20562015-08-19 14:58:14 -07003950enum IntrinsicNeedsEnvironmentOrCache {
3951 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3952 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003953};
3954
Aart Bik5d75afe2015-12-14 11:57:01 -08003955enum IntrinsicSideEffects {
3956 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3957 kReadSideEffects, // Intrinsic may read heap memory.
3958 kWriteSideEffects, // Intrinsic may write heap memory.
3959 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3960};
3961
3962enum IntrinsicExceptions {
3963 kNoThrow, // Intrinsic does not throw any exceptions.
3964 kCanThrow // Intrinsic may throw exceptions.
3965};
3966
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003967class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003968 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003969 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003970
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003971 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003972 SetRawInputAt(index, argument);
3973 }
3974
Roland Levillain3e3d7332015-04-28 11:00:54 +01003975 // Return the number of arguments. This number can be lower than
3976 // the number of inputs returned by InputCount(), as some invoke
3977 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3978 // inputs at the end of their list of inputs.
3979 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3980
Vladimir Markoa1de9182016-02-25 11:37:38 +00003981 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003982
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003983 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3984
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003985 InvokeType GetInvokeType() const {
3986 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003987 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003988
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003989 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003990 return intrinsic_;
3991 }
3992
Aart Bik5d75afe2015-12-14 11:57:01 -08003993 void SetIntrinsic(Intrinsics intrinsic,
3994 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3995 IntrinsicSideEffects side_effects,
3996 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003997
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003998 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003999 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004000 }
4001
Aart Bikff7d89c2016-11-07 08:49:28 -08004002 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4003
Vladimir Markoa1de9182016-02-25 11:37:38 +00004004 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004005
Aart Bik71bf7b42016-11-16 10:17:46 -08004006 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004007
Vladimir Marko372f10e2016-05-17 16:30:10 +01004008 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004009 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4010 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004011
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004012 uint32_t* GetIntrinsicOptimizations() {
4013 return &intrinsic_optimizations_;
4014 }
4015
4016 const uint32_t* GetIntrinsicOptimizations() const {
4017 return &intrinsic_optimizations_;
4018 }
4019
4020 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4021
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004022 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004023 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004024
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004025 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004026
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004027 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004028 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4029 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004030 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4031 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004032 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004033 static constexpr size_t kFieldReturnTypeSize =
4034 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4035 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4036 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4037 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004038 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004039 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4040
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004041 HInvoke(ArenaAllocator* arena,
4042 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004043 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004044 Primitive::Type return_type,
4045 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004046 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004047 ArtMethod* resolved_method,
4048 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004049 : HVariableInputSizeInstruction(
4050 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4051 dex_pc,
4052 arena,
4053 number_of_arguments + number_of_other_inputs,
4054 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004055 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004056 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004057 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004058 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004059 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004060 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004061 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004063 }
4064
Roland Levillain3e3d7332015-04-28 11:00:54 +01004065 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004066 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004067 const uint32_t dex_method_index_;
4068 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004069
4070 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4071 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004072
4073 private:
4074 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4075};
4076
Vladimir Markofcb503c2016-05-18 12:48:17 +01004077class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004078 public:
4079 HInvokeUnresolved(ArenaAllocator* arena,
4080 uint32_t number_of_arguments,
4081 Primitive::Type return_type,
4082 uint32_t dex_pc,
4083 uint32_t dex_method_index,
4084 InvokeType invoke_type)
4085 : HInvoke(arena,
4086 number_of_arguments,
4087 0u /* number_of_other_inputs */,
4088 return_type,
4089 dex_pc,
4090 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004091 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004092 invoke_type) {
4093 }
4094
4095 DECLARE_INSTRUCTION(InvokeUnresolved);
4096
4097 private:
4098 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4099};
4100
Orion Hodsonac141392017-01-13 11:53:47 +00004101class HInvokePolymorphic FINAL : public HInvoke {
4102 public:
4103 HInvokePolymorphic(ArenaAllocator* arena,
4104 uint32_t number_of_arguments,
4105 Primitive::Type return_type,
4106 uint32_t dex_pc,
4107 uint32_t dex_method_index)
4108 : HInvoke(arena,
4109 number_of_arguments,
4110 0u /* number_of_other_inputs */,
4111 return_type,
4112 dex_pc,
4113 dex_method_index,
4114 nullptr,
4115 kVirtual) {}
4116
4117 DECLARE_INSTRUCTION(InvokePolymorphic);
4118
4119 private:
4120 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4121};
4122
Vladimir Markofcb503c2016-05-18 12:48:17 +01004123class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004124 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004125 // Requirements of this method call regarding the class
4126 // initialization (clinit) check of its declaring class.
4127 enum class ClinitCheckRequirement {
4128 kNone, // Class already initialized.
4129 kExplicit, // Static call having explicit clinit check as last input.
4130 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004131 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004132 };
4133
Vladimir Marko58155012015-08-19 12:49:41 +00004134 // Determines how to load the target ArtMethod*.
4135 enum class MethodLoadKind {
4136 // Use a String init ArtMethod* loaded from Thread entrypoints.
4137 kStringInit,
4138
4139 // Use the method's own ArtMethod* loaded by the register allocator.
4140 kRecursive,
4141
4142 // Use ArtMethod* at a known address, embed the direct address in the code.
4143 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4144 kDirectAddress,
4145
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004147 // Used when we need to use the dex cache, for example for invoke-static that
4148 // may cause class initialization (the entry may point to a resolution method),
4149 // and we know that we can access the dex cache arrays using a PC-relative load.
4150 kDexCachePcRelative,
4151
4152 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4153 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4154 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4155 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4156 kDexCacheViaMethod,
4157 };
4158
4159 // Determines the location of the code pointer.
4160 enum class CodePtrLocation {
4161 // Recursive call, use local PC-relative call instruction.
4162 kCallSelf,
4163
Vladimir Marko58155012015-08-19 12:49:41 +00004164 // Use code pointer from the ArtMethod*.
4165 // Used when we don't know the target code. This is also the last-resort-kind used when
4166 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4167 kCallArtMethod,
4168 };
4169
4170 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004171 MethodLoadKind method_load_kind;
4172 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004173 // The method load data holds
4174 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4175 // Note that there are multiple string init methods, each having its own offset.
4176 // - the method address for kDirectAddress
4177 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004178 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004179 };
4180
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004181 HInvokeStaticOrDirect(ArenaAllocator* arena,
4182 uint32_t number_of_arguments,
4183 Primitive::Type return_type,
4184 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004185 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004186 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004187 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004188 InvokeType invoke_type,
4189 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004190 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004191 : HInvoke(arena,
4192 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004193 // There is potentially one extra argument for the HCurrentMethod node, and
4194 // potentially one other if the clinit check is explicit, and potentially
4195 // one other if the method is a string factory.
4196 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004197 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004198 return_type,
4199 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004200 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004201 resolved_method,
4202 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004203 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004204 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004205 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4206 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004207
Vladimir Markodc151b22015-10-15 18:02:30 +01004208 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004209 bool had_current_method_input = HasCurrentMethodInput();
4210 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4211
4212 // Using the current method is the default and once we find a better
4213 // method load kind, we should not go back to using the current method.
4214 DCHECK(had_current_method_input || !needs_current_method_input);
4215
4216 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004217 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4218 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004219 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004220 dispatch_info_ = dispatch_info;
4221 }
4222
Aart Bik6daebeb2017-04-03 14:35:41 -07004223 DispatchInfo GetDispatchInfo() const {
4224 return dispatch_info_;
4225 }
4226
Vladimir Markoc53c0792015-11-19 15:48:33 +00004227 void AddSpecialInput(HInstruction* input) {
4228 // We allow only one special input.
4229 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4230 DCHECK(InputCount() == GetSpecialInputIndex() ||
4231 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4232 InsertInputAt(GetSpecialInputIndex(), input);
4233 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004234
Vladimir Marko372f10e2016-05-17 16:30:10 +01004235 using HInstruction::GetInputRecords; // Keep the const version visible.
4236 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4237 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4238 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4239 DCHECK(!input_records.empty());
4240 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4241 HInstruction* last_input = input_records.back().GetInstruction();
4242 // Note: `last_input` may be null during arguments setup.
4243 if (last_input != nullptr) {
4244 // `last_input` is the last input of a static invoke marked as having
4245 // an explicit clinit check. It must either be:
4246 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4247 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4248 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4249 }
4250 }
4251 return input_records;
4252 }
4253
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004254 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004255 // We access the method via the dex cache so we can't do an implicit null check.
4256 // TODO: for intrinsics we can generate implicit null checks.
4257 return false;
4258 }
4259
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004260 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004261 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004262 }
4263
Vladimir Markoc53c0792015-11-19 15:48:33 +00004264 // Get the index of the special input, if any.
4265 //
David Brazdil6de19382016-01-08 17:37:10 +00004266 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4267 // method pointer; otherwise there may be one platform-specific special input,
4268 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004269 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004270 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004271
Vladimir Marko58155012015-08-19 12:49:41 +00004272 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4273 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4274 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004275 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004276 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004277 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004278 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004279 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4280 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004281 bool HasCurrentMethodInput() const {
4282 // This function can be called only after the invoke has been fully initialized by the builder.
4283 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004284 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004285 return true;
4286 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004287 DCHECK(InputCount() == GetSpecialInputIndex() ||
4288 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004289 return false;
4290 }
4291 }
Vladimir Marko58155012015-08-19 12:49:41 +00004292
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004293 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004294 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004295 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004296 }
4297
4298 uint64_t GetMethodAddress() const {
4299 DCHECK(HasMethodAddress());
4300 return dispatch_info_.method_load_data;
4301 }
4302
4303 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004304 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004305 return dispatch_info_.method_load_data;
4306 }
4307
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004308 const DexFile& GetDexFileForPcRelativeDexCache() const;
4309
Vladimir Markoa1de9182016-02-25 11:37:38 +00004310 ClinitCheckRequirement GetClinitCheckRequirement() const {
4311 return GetPackedField<ClinitCheckRequirementField>();
4312 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004313
Roland Levillain4c0eb422015-04-24 16:43:49 +01004314 // Is this instruction a call to a static method?
4315 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 return GetInvokeType() == kStatic;
4317 }
4318
4319 MethodReference GetTargetMethod() const {
4320 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004321 }
4322
Vladimir Markofbb184a2015-11-13 14:47:00 +00004323 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4324 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4325 // instruction; only relevant for static calls with explicit clinit check.
4326 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004327 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004328 size_t last_input_index = inputs_.size() - 1u;
4329 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004330 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004331 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004332 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004333 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004334 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004335 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004336 }
4337
4338 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004339 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004340 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004341 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004342 }
4343
4344 // Is this a call to a static method whose declaring class has an
4345 // implicit intialization check requirement?
4346 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004347 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004348 }
4349
Vladimir Markob554b5a2015-11-06 12:57:55 +00004350 // Does this method load kind need the current method as an input?
4351 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4352 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4353 }
4354
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004355 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004356
4357 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004358 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004359 static constexpr size_t kFieldClinitCheckRequirementSize =
4360 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4361 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4362 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4363 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4364 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004365 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4366 kFieldClinitCheckRequirement,
4367 kFieldClinitCheckRequirementSize>;
4368
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004369 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004370 MethodReference target_method_;
4371 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004372
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004373 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004374};
Vladimir Markof64242a2015-12-01 14:58:23 +00004375std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004376std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004377
Vladimir Markofcb503c2016-05-18 12:48:17 +01004378class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004379 public:
4380 HInvokeVirtual(ArenaAllocator* arena,
4381 uint32_t number_of_arguments,
4382 Primitive::Type return_type,
4383 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004384 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004385 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004386 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004387 : HInvoke(arena,
4388 number_of_arguments,
4389 0u,
4390 return_type,
4391 dex_pc,
4392 dex_method_index,
4393 resolved_method,
4394 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004395 vtable_index_(vtable_index) {}
4396
Aart Bik71bf7b42016-11-16 10:17:46 -08004397 bool CanBeNull() const OVERRIDE {
4398 switch (GetIntrinsic()) {
4399 case Intrinsics::kThreadCurrentThread:
4400 case Intrinsics::kStringBufferAppend:
4401 case Intrinsics::kStringBufferToString:
4402 case Intrinsics::kStringBuilderAppend:
4403 case Intrinsics::kStringBuilderToString:
4404 return false;
4405 default:
4406 return HInvoke::CanBeNull();
4407 }
4408 }
4409
Calin Juravle641547a2015-04-21 22:08:51 +01004410 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004411 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004412 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004413 }
4414
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004415 uint32_t GetVTableIndex() const { return vtable_index_; }
4416
4417 DECLARE_INSTRUCTION(InvokeVirtual);
4418
4419 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004420 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004421 const uint32_t vtable_index_;
4422
4423 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4424};
4425
Vladimir Markofcb503c2016-05-18 12:48:17 +01004426class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004427 public:
4428 HInvokeInterface(ArenaAllocator* arena,
4429 uint32_t number_of_arguments,
4430 Primitive::Type return_type,
4431 uint32_t dex_pc,
4432 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004433 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004434 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004435 : HInvoke(arena,
4436 number_of_arguments,
4437 0u,
4438 return_type,
4439 dex_pc,
4440 dex_method_index,
4441 resolved_method,
4442 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004443 imt_index_(imt_index) {}
4444
Calin Juravle641547a2015-04-21 22:08:51 +01004445 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004447 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004448 }
4449
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004450 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4451 // The assembly stub currently needs it.
4452 return true;
4453 }
4454
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004455 uint32_t GetImtIndex() const { return imt_index_; }
4456 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4457
4458 DECLARE_INSTRUCTION(InvokeInterface);
4459
4460 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004461 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004462 const uint32_t imt_index_;
4463
4464 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4465};
4466
Vladimir Markofcb503c2016-05-18 12:48:17 +01004467class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004468 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004469 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004470 : HUnaryOperation(result_type, input, dex_pc) {
4471 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4472 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004473
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004474 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004475
4476 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004477 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004478 }
4479 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004480 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004481 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004482 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4483 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4484 }
4485 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4486 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4487 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004488
Roland Levillain88cb1752014-10-20 16:36:47 +01004489 DECLARE_INSTRUCTION(Neg);
4490
4491 private:
4492 DISALLOW_COPY_AND_ASSIGN(HNeg);
4493};
4494
Vladimir Markofcb503c2016-05-18 12:48:17 +01004495class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004496 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004497 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4498 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4499 SetRawInputAt(0, cls);
4500 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004501 }
4502
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004503 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004504 bool NeedsEnvironment() const OVERRIDE { return true; }
4505
Mingyao Yang0c365e62015-03-31 15:09:29 -07004506 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4507 bool CanThrow() const OVERRIDE { return true; }
4508
Calin Juravle10e244f2015-01-26 18:54:32 +00004509 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004510
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004511 HLoadClass* GetLoadClass() const {
4512 DCHECK(InputAt(0)->IsLoadClass());
4513 return InputAt(0)->AsLoadClass();
4514 }
4515
4516 HInstruction* GetLength() const {
4517 return InputAt(1);
4518 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004519
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004520 DECLARE_INSTRUCTION(NewArray);
4521
4522 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004523 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4524};
4525
Vladimir Markofcb503c2016-05-18 12:48:17 +01004526class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004527 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004528 HAdd(Primitive::Type result_type,
4529 HInstruction* left,
4530 HInstruction* right,
4531 uint32_t dex_pc = kNoDexPc)
4532 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004533
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004534 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004535
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004536 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004537
4538 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004539 return GetBlock()->GetGraph()->GetIntConstant(
4540 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004541 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004543 return GetBlock()->GetGraph()->GetLongConstant(
4544 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004545 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004546 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4547 return GetBlock()->GetGraph()->GetFloatConstant(
4548 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4549 }
4550 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4551 return GetBlock()->GetGraph()->GetDoubleConstant(
4552 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4553 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004554
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004555 DECLARE_INSTRUCTION(Add);
4556
4557 private:
4558 DISALLOW_COPY_AND_ASSIGN(HAdd);
4559};
4560
Vladimir Markofcb503c2016-05-18 12:48:17 +01004561class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004563 HSub(Primitive::Type result_type,
4564 HInstruction* left,
4565 HInstruction* right,
4566 uint32_t dex_pc = kNoDexPc)
4567 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004568
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004569 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004570
4571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004572 return GetBlock()->GetGraph()->GetIntConstant(
4573 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004574 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004575 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004576 return GetBlock()->GetGraph()->GetLongConstant(
4577 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004578 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004579 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4580 return GetBlock()->GetGraph()->GetFloatConstant(
4581 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4582 }
4583 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4584 return GetBlock()->GetGraph()->GetDoubleConstant(
4585 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4586 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004587
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004588 DECLARE_INSTRUCTION(Sub);
4589
4590 private:
4591 DISALLOW_COPY_AND_ASSIGN(HSub);
4592};
4593
Vladimir Markofcb503c2016-05-18 12:48:17 +01004594class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004595 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004596 HMul(Primitive::Type result_type,
4597 HInstruction* left,
4598 HInstruction* right,
4599 uint32_t dex_pc = kNoDexPc)
4600 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004601
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004602 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004603
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004604 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004605
4606 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004607 return GetBlock()->GetGraph()->GetIntConstant(
4608 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004609 }
4610 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004611 return GetBlock()->GetGraph()->GetLongConstant(
4612 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004613 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004614 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4615 return GetBlock()->GetGraph()->GetFloatConstant(
4616 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4617 }
4618 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4619 return GetBlock()->GetGraph()->GetDoubleConstant(
4620 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4621 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004622
4623 DECLARE_INSTRUCTION(Mul);
4624
4625 private:
4626 DISALLOW_COPY_AND_ASSIGN(HMul);
4627};
4628
Vladimir Markofcb503c2016-05-18 12:48:17 +01004629class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004630 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004631 HDiv(Primitive::Type result_type,
4632 HInstruction* left,
4633 HInstruction* right,
4634 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004635 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004636
Roland Levillain9867bc72015-08-05 10:21:34 +01004637 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004638 T ComputeIntegral(T x, T y) const {
4639 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004640 // Our graph structure ensures we never have 0 for `y` during
4641 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004642 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004643 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004644 return (y == -1) ? -x : x / y;
4645 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004646
Roland Levillain31dd3d62016-02-16 12:21:02 +00004647 template <typename T>
4648 T ComputeFP(T x, T y) const {
4649 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4650 return x / y;
4651 }
4652
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004654 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004655 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 }
4657 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004658 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004659 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4660 }
4661 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4662 return GetBlock()->GetGraph()->GetFloatConstant(
4663 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4664 }
4665 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4666 return GetBlock()->GetGraph()->GetDoubleConstant(
4667 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004668 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004669
4670 DECLARE_INSTRUCTION(Div);
4671
4672 private:
4673 DISALLOW_COPY_AND_ASSIGN(HDiv);
4674};
4675
Vladimir Markofcb503c2016-05-18 12:48:17 +01004676class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004677 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004678 HRem(Primitive::Type result_type,
4679 HInstruction* left,
4680 HInstruction* right,
4681 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004682 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004683
Roland Levillain9867bc72015-08-05 10:21:34 +01004684 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004685 T ComputeIntegral(T x, T y) const {
4686 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004687 // Our graph structure ensures we never have 0 for `y` during
4688 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004689 DCHECK_NE(y, 0);
4690 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4691 return (y == -1) ? 0 : x % y;
4692 }
4693
Roland Levillain31dd3d62016-02-16 12:21:02 +00004694 template <typename T>
4695 T ComputeFP(T x, T y) const {
4696 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4697 return std::fmod(x, y);
4698 }
4699
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004701 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004702 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 }
4704 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004705 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004706 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004707 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004708 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4709 return GetBlock()->GetGraph()->GetFloatConstant(
4710 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4711 }
4712 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4713 return GetBlock()->GetGraph()->GetDoubleConstant(
4714 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4715 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004716
4717 DECLARE_INSTRUCTION(Rem);
4718
4719 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004720 DISALLOW_COPY_AND_ASSIGN(HRem);
4721};
4722
Vladimir Markofcb503c2016-05-18 12:48:17 +01004723class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004724 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004725 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4726 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004727 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004728 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004729 SetRawInputAt(0, value);
4730 }
4731
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004732 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4733
Calin Juravled0d48522014-11-04 16:40:20 +00004734 bool CanBeMoved() const OVERRIDE { return true; }
4735
Vladimir Marko372f10e2016-05-17 16:30:10 +01004736 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004737 return true;
4738 }
4739
4740 bool NeedsEnvironment() const OVERRIDE { return true; }
4741 bool CanThrow() const OVERRIDE { return true; }
4742
Calin Juravled0d48522014-11-04 16:40:20 +00004743 DECLARE_INSTRUCTION(DivZeroCheck);
4744
4745 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004746 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4747};
4748
Vladimir Markofcb503c2016-05-18 12:48:17 +01004749class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004750 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004751 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 HInstruction* value,
4753 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004754 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4756 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4757 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004758 }
4759
Roland Levillain5b5b9312016-03-22 14:57:31 +00004760 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004761 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 return value << (distance & max_shift_distance);
4763 }
4764
4765 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004766 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004767 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004768 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004769 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004770 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004771 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004772 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004773 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4774 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4775 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4776 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004777 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4779 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004780 LOG(FATAL) << DebugName() << " is not defined for float values";
4781 UNREACHABLE();
4782 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4784 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004785 LOG(FATAL) << DebugName() << " is not defined for double values";
4786 UNREACHABLE();
4787 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004788
4789 DECLARE_INSTRUCTION(Shl);
4790
4791 private:
4792 DISALLOW_COPY_AND_ASSIGN(HShl);
4793};
4794
Vladimir Markofcb503c2016-05-18 12:48:17 +01004795class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004796 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004797 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 HInstruction* value,
4799 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004800 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004801 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4802 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4803 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004804 }
4805
Roland Levillain5b5b9312016-03-22 14:57:31 +00004806 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004807 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004808 return value >> (distance & max_shift_distance);
4809 }
4810
4811 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004812 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004813 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004814 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004815 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004816 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004817 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004818 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004819 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4820 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4821 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4822 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004823 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004824 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4825 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004826 LOG(FATAL) << DebugName() << " is not defined for float values";
4827 UNREACHABLE();
4828 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004829 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4830 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004831 LOG(FATAL) << DebugName() << " is not defined for double values";
4832 UNREACHABLE();
4833 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004834
4835 DECLARE_INSTRUCTION(Shr);
4836
4837 private:
4838 DISALLOW_COPY_AND_ASSIGN(HShr);
4839};
4840
Vladimir Markofcb503c2016-05-18 12:48:17 +01004841class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004842 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004843 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004844 HInstruction* value,
4845 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004846 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004847 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4848 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4849 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004850 }
4851
Roland Levillain5b5b9312016-03-22 14:57:31 +00004852 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004853 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004854 typedef typename std::make_unsigned<T>::type V;
4855 V ux = static_cast<V>(value);
4856 return static_cast<T>(ux >> (distance & max_shift_distance));
4857 }
4858
4859 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004860 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004861 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004862 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004863 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004864 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004865 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004866 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004867 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4868 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4869 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4870 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004871 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004872 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4873 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004874 LOG(FATAL) << DebugName() << " is not defined for float values";
4875 UNREACHABLE();
4876 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004877 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4878 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004879 LOG(FATAL) << DebugName() << " is not defined for double values";
4880 UNREACHABLE();
4881 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004882
4883 DECLARE_INSTRUCTION(UShr);
4884
4885 private:
4886 DISALLOW_COPY_AND_ASSIGN(HUShr);
4887};
4888
Vladimir Markofcb503c2016-05-18 12:48:17 +01004889class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004890 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004891 HAnd(Primitive::Type result_type,
4892 HInstruction* left,
4893 HInstruction* right,
4894 uint32_t dex_pc = kNoDexPc)
4895 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004896
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004897 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004898
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004899 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004900
4901 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004902 return GetBlock()->GetGraph()->GetIntConstant(
4903 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004904 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004906 return GetBlock()->GetGraph()->GetLongConstant(
4907 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004908 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004909 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4910 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4911 LOG(FATAL) << DebugName() << " is not defined for float values";
4912 UNREACHABLE();
4913 }
4914 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4915 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4916 LOG(FATAL) << DebugName() << " is not defined for double values";
4917 UNREACHABLE();
4918 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004919
4920 DECLARE_INSTRUCTION(And);
4921
4922 private:
4923 DISALLOW_COPY_AND_ASSIGN(HAnd);
4924};
4925
Vladimir Markofcb503c2016-05-18 12:48:17 +01004926class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004927 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004928 HOr(Primitive::Type result_type,
4929 HInstruction* left,
4930 HInstruction* right,
4931 uint32_t dex_pc = kNoDexPc)
4932 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004933
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004934 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004935
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004936 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004937
4938 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004939 return GetBlock()->GetGraph()->GetIntConstant(
4940 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004941 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004943 return GetBlock()->GetGraph()->GetLongConstant(
4944 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004945 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004946 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4947 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4948 LOG(FATAL) << DebugName() << " is not defined for float values";
4949 UNREACHABLE();
4950 }
4951 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4952 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4953 LOG(FATAL) << DebugName() << " is not defined for double values";
4954 UNREACHABLE();
4955 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004956
4957 DECLARE_INSTRUCTION(Or);
4958
4959 private:
4960 DISALLOW_COPY_AND_ASSIGN(HOr);
4961};
4962
Vladimir Markofcb503c2016-05-18 12:48:17 +01004963class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004964 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004965 HXor(Primitive::Type result_type,
4966 HInstruction* left,
4967 HInstruction* right,
4968 uint32_t dex_pc = kNoDexPc)
4969 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004970
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004971 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004972
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004973 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004974
4975 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004976 return GetBlock()->GetGraph()->GetIntConstant(
4977 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004978 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004980 return GetBlock()->GetGraph()->GetLongConstant(
4981 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004982 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004983 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4984 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4985 LOG(FATAL) << DebugName() << " is not defined for float values";
4986 UNREACHABLE();
4987 }
4988 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4989 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4990 LOG(FATAL) << DebugName() << " is not defined for double values";
4991 UNREACHABLE();
4992 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004993
4994 DECLARE_INSTRUCTION(Xor);
4995
4996 private:
4997 DISALLOW_COPY_AND_ASSIGN(HXor);
4998};
4999
Vladimir Markofcb503c2016-05-18 12:48:17 +01005000class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005001 public:
5002 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005003 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005004 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5005 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005006 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005007
Roland Levillain5b5b9312016-03-22 14:57:31 +00005008 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005009 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005010 typedef typename std::make_unsigned<T>::type V;
5011 V ux = static_cast<V>(value);
5012 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005013 return static_cast<T>(ux);
5014 } else {
5015 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005016 return static_cast<T>(ux >> (distance & max_shift_value)) |
5017 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005018 }
5019 }
5020
Roland Levillain5b5b9312016-03-22 14:57:31 +00005021 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005022 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005023 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005024 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005025 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005026 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005027 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005028 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005029 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5030 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5031 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5032 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005033 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005034 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5035 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 LOG(FATAL) << DebugName() << " is not defined for float values";
5037 UNREACHABLE();
5038 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005039 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5040 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005041 LOG(FATAL) << DebugName() << " is not defined for double values";
5042 UNREACHABLE();
5043 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005044
5045 DECLARE_INSTRUCTION(Ror);
5046
5047 private:
5048 DISALLOW_COPY_AND_ASSIGN(HRor);
5049};
5050
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005051// The value of a parameter in this method. Its location depends on
5052// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005053class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005054 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005055 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005056 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005057 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005058 Primitive::Type parameter_type,
5059 bool is_this = false)
5060 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005061 dex_file_(dex_file),
5062 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005063 index_(index) {
5064 SetPackedFlag<kFlagIsThis>(is_this);
5065 SetPackedFlag<kFlagCanBeNull>(!is_this);
5066 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005067
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005068 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005069 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005070 uint8_t GetIndex() const { return index_; }
Igor Murashkin032cacd2017-04-06 14:40:08 -07005071 bool IsThis() const ATTRIBUTE_UNUSED { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005072
Vladimir Markoa1de9182016-02-25 11:37:38 +00005073 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5074 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005075
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005076 DECLARE_INSTRUCTION(ParameterValue);
5077
5078 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005079 // Whether or not the parameter value corresponds to 'this' argument.
5080 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5081 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5082 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5083 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5084 "Too many packed fields.");
5085
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005086 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005087 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005088 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005089 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005090 const uint8_t index_;
5091
5092 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5093};
5094
Vladimir Markofcb503c2016-05-18 12:48:17 +01005095class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005097 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5098 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005099
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005100 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005101 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005102 return true;
5103 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005104
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005105 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005106
5107 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005108 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005109 }
5110 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005111 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005112 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005113 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5114 LOG(FATAL) << DebugName() << " is not defined for float values";
5115 UNREACHABLE();
5116 }
5117 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5118 LOG(FATAL) << DebugName() << " is not defined for double values";
5119 UNREACHABLE();
5120 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005121
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005122 DECLARE_INSTRUCTION(Not);
5123
5124 private:
5125 DISALLOW_COPY_AND_ASSIGN(HNot);
5126};
5127
Vladimir Markofcb503c2016-05-18 12:48:17 +01005128class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005129 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005130 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5131 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005132
5133 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005134 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005135 return true;
5136 }
5137
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005138 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005139 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005140 return !x;
5141 }
5142
Roland Levillain9867bc72015-08-05 10:21:34 +01005143 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005144 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005145 }
5146 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5147 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005148 UNREACHABLE();
5149 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005150 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5151 LOG(FATAL) << DebugName() << " is not defined for float values";
5152 UNREACHABLE();
5153 }
5154 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5155 LOG(FATAL) << DebugName() << " is not defined for double values";
5156 UNREACHABLE();
5157 }
David Brazdil66d126e2015-04-03 16:02:44 +01005158
5159 DECLARE_INSTRUCTION(BooleanNot);
5160
5161 private:
5162 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5163};
5164
Vladimir Markofcb503c2016-05-18 12:48:17 +01005165class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005166 public:
5167 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005168 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005169 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005170 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005171 // Invariant: We should never generate a conversion to a Boolean value.
5172 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005173 }
5174
5175 HInstruction* GetInput() const { return InputAt(0); }
5176 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5177 Primitive::Type GetResultType() const { return GetType(); }
5178
5179 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005180 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5181 return true;
5182 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005183
Mark Mendelle82549b2015-05-06 10:55:34 -04005184 // Try to statically evaluate the conversion and return a HConstant
5185 // containing the result. If the input cannot be converted, return nullptr.
5186 HConstant* TryStaticEvaluation() const;
5187
Roland Levillaindff1f282014-11-05 14:15:05 +00005188 DECLARE_INSTRUCTION(TypeConversion);
5189
5190 private:
5191 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5192};
5193
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005194static constexpr uint32_t kNoRegNumber = -1;
5195
Vladimir Markofcb503c2016-05-18 12:48:17 +01005196class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005197 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005198 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5199 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005201 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202 SetRawInputAt(0, value);
5203 }
5204
Calin Juravle10e244f2015-01-26 18:54:32 +00005205 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005206 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005207 return true;
5208 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005209
Calin Juravle10e244f2015-01-26 18:54:32 +00005210 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211
Calin Juravle10e244f2015-01-26 18:54:32 +00005212 bool CanThrow() const OVERRIDE { return true; }
5213
5214 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005215
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216
5217 DECLARE_INSTRUCTION(NullCheck);
5218
5219 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005220 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5221};
5222
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005223// Embeds an ArtField and all the information required by the compiler. We cache
5224// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225class FieldInfo : public ValueObject {
5226 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005227 FieldInfo(ArtField* field,
5228 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005229 Primitive::Type field_type,
5230 bool is_volatile,
5231 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005232 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005233 const DexFile& dex_file)
5234 : field_(field),
5235 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005236 field_type_(field_type),
5237 is_volatile_(is_volatile),
5238 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005239 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005240 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005242 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005244 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005245 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005246 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005247 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005248 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005249
5250 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005251 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005252 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005253 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005254 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005255 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005256 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005257 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258};
5259
Vladimir Markofcb503c2016-05-18 12:48:17 +01005260class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005261 public:
5262 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005263 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005264 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005265 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005266 bool is_volatile,
5267 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005268 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005269 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005270 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005271 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005272 field_info_(field,
5273 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005274 field_type,
5275 is_volatile,
5276 field_idx,
5277 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005278 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005279 SetRawInputAt(0, value);
5280 }
5281
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005282 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005283
Vladimir Marko372f10e2016-05-17 16:30:10 +01005284 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5285 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005286 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005287 }
5288
Calin Juravle641547a2015-04-21 22:08:51 +01005289 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005290 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005291 }
5292
5293 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005294 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5295 }
5296
Calin Juravle52c48962014-12-16 17:02:57 +00005297 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005298 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005299 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005300 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005301
5302 DECLARE_INSTRUCTION(InstanceFieldGet);
5303
5304 private:
5305 const FieldInfo field_info_;
5306
5307 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5308};
5309
Vladimir Markofcb503c2016-05-18 12:48:17 +01005310class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005311 public:
5312 HInstanceFieldSet(HInstruction* object,
5313 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005314 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005315 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005316 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005317 bool is_volatile,
5318 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005319 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005320 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005321 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005322 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005323 field_info_(field,
5324 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005325 field_type,
5326 is_volatile,
5327 field_idx,
5328 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005329 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005330 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331 SetRawInputAt(0, object);
5332 SetRawInputAt(1, value);
5333 }
5334
Calin Juravle641547a2015-04-21 22:08:51 +01005335 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005336 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 }
5338
Calin Juravle52c48962014-12-16 17:02:57 +00005339 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005340 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005341 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005342 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005343 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005344 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5345 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005346
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347 DECLARE_INSTRUCTION(InstanceFieldSet);
5348
5349 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005350 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5351 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5352 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5353 "Too many packed fields.");
5354
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005355 const FieldInfo field_info_;
5356
5357 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5358};
5359
Vladimir Markofcb503c2016-05-18 12:48:17 +01005360class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005362 HArrayGet(HInstruction* array,
5363 HInstruction* index,
5364 Primitive::Type type,
5365 uint32_t dex_pc,
5366 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005367 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005368 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369 SetRawInputAt(0, array);
5370 SetRawInputAt(1, index);
5371 }
5372
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005373 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005374 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005375 return true;
5376 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005377 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005378 // TODO: We can be smarter here.
5379 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5380 // which generates the implicit null check. There are cases when these can be removed
5381 // to produce better code. If we ever add optimizations to do so we should allow an
5382 // implicit check here (as long as the address falls in the first page).
5383 return false;
5384 }
5385
David Brazdil4833f5a2015-12-16 10:37:39 +00005386 bool IsEquivalentOf(HArrayGet* other) const {
5387 bool result = (GetDexPc() == other->GetDexPc());
5388 if (kIsDebugBuild && result) {
5389 DCHECK_EQ(GetBlock(), other->GetBlock());
5390 DCHECK_EQ(GetArray(), other->GetArray());
5391 DCHECK_EQ(GetIndex(), other->GetIndex());
5392 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005393 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005394 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005395 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5396 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005397 }
5398 }
5399 return result;
5400 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005401
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005402 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5403
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005404 HInstruction* GetArray() const { return InputAt(0); }
5405 HInstruction* GetIndex() const { return InputAt(1); }
5406
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005407 DECLARE_INSTRUCTION(ArrayGet);
5408
5409 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005410 // We treat a String as an array, creating the HArrayGet from String.charAt()
5411 // intrinsic in the instruction simplifier. We can always determine whether
5412 // a particular HArrayGet is actually a String.charAt() by looking at the type
5413 // of the input but that requires holding the mutator lock, so we prefer to use
5414 // a flag, so that code generators don't need to do the locking.
5415 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5416 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5417 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5418 "Too many packed fields.");
5419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5421};
5422
Vladimir Markofcb503c2016-05-18 12:48:17 +01005423class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 public:
5425 HArraySet(HInstruction* array,
5426 HInstruction* index,
5427 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005428 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005429 uint32_t dex_pc)
5430 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005431 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5432 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5433 SetPackedFlag<kFlagValueCanBeNull>(true);
5434 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 SetRawInputAt(0, array);
5436 SetRawInputAt(1, index);
5437 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005438 // Make a best guess now, may be refined during SSA building.
5439 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 }
5441
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005443 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005444 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445 }
5446
Mingyao Yang81014cb2015-06-02 03:16:27 -07005447 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005448 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005449
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005450 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005451 // TODO: Same as for ArrayGet.
5452 return false;
5453 }
5454
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005455 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005456 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005457 }
5458
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005460 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005461 }
5462
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005463 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005464 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005465 }
5466
Vladimir Markoa1de9182016-02-25 11:37:38 +00005467 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5468 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5469 bool StaticTypeOfArrayIsObjectArray() const {
5470 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5471 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005472
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005473 HInstruction* GetArray() const { return InputAt(0); }
5474 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005475 HInstruction* GetValue() const { return InputAt(2); }
5476
5477 Primitive::Type GetComponentType() const {
5478 // The Dex format does not type floating point index operations. Since the
5479 // `expected_component_type_` is set during building and can therefore not
5480 // be correct, we also check what is the value type. If it is a floating
5481 // point type, we must use that type.
5482 Primitive::Type value_type = GetValue()->GetType();
5483 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5484 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005485 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005486 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005487
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005488 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005489 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005490 }
5491
Aart Bik18b36ab2016-04-13 16:41:35 -07005492 void ComputeSideEffects() {
5493 Primitive::Type type = GetComponentType();
5494 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5495 SideEffectsForArchRuntimeCalls(type)));
5496 }
5497
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005498 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5499 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5500 }
5501
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005502 DECLARE_INSTRUCTION(ArraySet);
5503
5504 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005505 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5506 static constexpr size_t kFieldExpectedComponentTypeSize =
5507 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5508 static constexpr size_t kFlagNeedsTypeCheck =
5509 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5510 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005511 // Cached information for the reference_type_info_ so that codegen
5512 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005513 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5514 static constexpr size_t kNumberOfArraySetPackedBits =
5515 kFlagStaticTypeOfArrayIsObjectArray + 1;
5516 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5517 using ExpectedComponentTypeField =
5518 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519
5520 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5521};
5522
Vladimir Markofcb503c2016-05-18 12:48:17 +01005523class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005525 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005526 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005527 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005528 // Note that arrays do not change length, so the instruction does not
5529 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005530 SetRawInputAt(0, array);
5531 }
5532
Calin Juravle77520bc2015-01-12 18:45:46 +00005533 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005534 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005535 return true;
5536 }
Calin Juravle641547a2015-04-21 22:08:51 +01005537 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5538 return obj == InputAt(0);
5539 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005540
Vladimir Markodce016e2016-04-28 13:10:02 +01005541 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5542
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005543 DECLARE_INSTRUCTION(ArrayLength);
5544
5545 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005546 // We treat a String as an array, creating the HArrayLength from String.length()
5547 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5548 // determine whether a particular HArrayLength is actually a String.length() by
5549 // looking at the type of the input but that requires holding the mutator lock, so
5550 // we prefer to use a flag, so that code generators don't need to do the locking.
5551 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5552 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5553 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5554 "Too many packed fields.");
5555
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5557};
5558
Vladimir Markofcb503c2016-05-18 12:48:17 +01005559class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005560 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005561 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5562 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005563 HBoundsCheck(HInstruction* index,
5564 HInstruction* length,
5565 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005566 bool string_char_at = false)
5567 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005568 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005569 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005570 SetRawInputAt(0, index);
5571 SetRawInputAt(1, length);
5572 }
5573
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005574 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005575 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005576 return true;
5577 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005578
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005579 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005580
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005581 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005582
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005583 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005584
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005585 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586
5587 DECLARE_INSTRUCTION(BoundsCheck);
5588
5589 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005590 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005591
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005592 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5593};
5594
Vladimir Markofcb503c2016-05-18 12:48:17 +01005595class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005596 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005597 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005598 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005599
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005600 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005601 return true;
5602 }
5603
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005604 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5605 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005606
5607 DECLARE_INSTRUCTION(SuspendCheck);
5608
5609 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005610 // Only used for code generation, in order to share the same slow path between back edges
5611 // of a same loop.
5612 SlowPathCode* slow_path_;
5613
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005614 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5615};
5616
David Srbecky0cf44932015-12-09 14:09:59 +00005617// Pseudo-instruction which provides the native debugger with mapping information.
5618// It ensures that we can generate line number and local variables at this point.
5619class HNativeDebugInfo : public HTemplateInstruction<0> {
5620 public:
5621 explicit HNativeDebugInfo(uint32_t dex_pc)
5622 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5623
5624 bool NeedsEnvironment() const OVERRIDE {
5625 return true;
5626 }
5627
5628 DECLARE_INSTRUCTION(NativeDebugInfo);
5629
5630 private:
5631 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5632};
5633
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005634/**
5635 * Instruction to load a Class object.
5636 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005637class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005638 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005639 // Determines how to load the Class.
5640 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005641 // We cannot load this class. See HSharpening::SharpenLoadClass.
5642 kInvalid = -1,
5643
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005644 // Use the Class* from the method's own ArtMethod*.
5645 kReferrersClass,
5646
5647 // Use boot image Class* address that will be known at link time.
5648 // Used for boot image classes referenced by boot image code in non-PIC mode.
5649 kBootImageLinkTimeAddress,
5650
5651 // Use PC-relative boot image Class* address that will be known at link time.
5652 // Used for boot image classes referenced by boot image code in PIC mode.
5653 kBootImageLinkTimePcRelative,
5654
5655 // Use a known boot image Class* address, embedded in the code by the codegen.
5656 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005657 kBootImageAddress,
5658
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005659 // Load from an entry in the .bss section using a PC-relative load.
5660 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5661 kBssEntry,
5662
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005663 // Load from the root table associated with the JIT compiled method.
5664 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666 // Load from resolved types array accessed through the class loaded from
5667 // the compiled method's own ArtMethod*. This is the default access type when
5668 // all other types are unavailable.
5669 kDexCacheViaMethod,
5670
5671 kLast = kDexCacheViaMethod
5672 };
5673
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005674 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005675 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005676 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005677 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005678 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005679 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005680 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005681 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5682 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005683 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005684 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005685 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005686 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005687 // Referrers class should not need access check. We never inline unverified
5688 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005689 DCHECK(!is_referrers_class || !needs_access_check);
5690
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005691 SetPackedField<LoadKindField>(
5692 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005693 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005694 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005695 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 }
5697
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005698 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005699
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 LoadKind GetLoadKind() const {
5701 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005702 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005703
5704 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005705
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005706 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005707
Andreas Gampea5b09a62016-11-17 15:21:22 -08005708 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005709
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005710 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005711
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005712 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005713 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005714 }
5715
Calin Juravle0ba218d2015-05-19 18:46:01 +01005716 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005717 // The entrypoint the code generator is going to call does not do
5718 // clinit of the class.
5719 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005721 }
5722
5723 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005724 return NeedsAccessCheck() ||
5725 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005726 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5727 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005728 }
5729
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005730 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005731 return NeedsAccessCheck() ||
5732 MustGenerateClinitCheck() ||
5733 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5734 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5735 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005736 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5737 GetLoadKind() == LoadKind::kBssEntry) &&
5738 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005739 }
5740
Calin Juravleacf735c2015-02-12 15:25:22 +00005741 ReferenceTypeInfo GetLoadedClassRTI() {
5742 return loaded_class_rti_;
5743 }
5744
5745 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5746 // Make sure we only set exact types (the loaded class should never be merged).
5747 DCHECK(rti.IsExact());
5748 loaded_class_rti_ = rti;
5749 }
5750
Andreas Gampea5b09a62016-11-17 15:21:22 -08005751 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005752 const DexFile& GetDexFile() const { return dex_file_; }
5753
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005754 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005755 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005756 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005757
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005758 static SideEffects SideEffectsForArchRuntimeCalls() {
5759 return SideEffects::CanTriggerGC();
5760 }
5761
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005762 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005763 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005764 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005765 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005766
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005767 void MarkInBootImage() {
5768 SetPackedFlag<kFlagIsInBootImage>(true);
5769 }
5770
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 void AddSpecialInput(HInstruction* special_input);
5772
5773 using HInstruction::GetInputRecords; // Keep the const version visible.
5774 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5775 return ArrayRef<HUserRecord<HInstruction*>>(
5776 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5777 }
5778
5779 Primitive::Type GetType() const OVERRIDE {
5780 return Primitive::kPrimNot;
5781 }
5782
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005783 Handle<mirror::Class> GetClass() const {
5784 return klass_;
5785 }
5786
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005787 DECLARE_INSTRUCTION(LoadClass);
5788
5789 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005791 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005792 // Whether this instruction must generate the initialization check.
5793 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005794 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005795 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5796 static constexpr size_t kFieldLoadKindSize =
5797 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5798 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005799 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5801
5802 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005803 return load_kind == LoadKind::kReferrersClass ||
5804 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005806 load_kind == LoadKind::kBssEntry ||
5807 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 }
5809
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005810 void SetLoadKindInternal(LoadKind load_kind);
5811
5812 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5813 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005814 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005816
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005817 // A type index and dex file where the class can be accessed. The dex file can be:
5818 // - The compiling method's dex file if the class is defined there too.
5819 // - The compiling method's dex file if the class is referenced there.
5820 // - The dex file where the class is defined. When the load kind can only be
5821 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005822 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005823 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005824
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005825 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005826
Calin Juravleacf735c2015-02-12 15:25:22 +00005827 ReferenceTypeInfo loaded_class_rti_;
5828
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005829 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5830};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005831std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5832
5833// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005834inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005835 // The special input is used for PC-relative loads on some architectures,
5836 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005837 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005838 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005839 GetLoadKind() == LoadKind::kBootImageAddress ||
5840 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005841 DCHECK(special_input_.GetInstruction() == nullptr);
5842 special_input_ = HUserRecord<HInstruction*>(special_input);
5843 special_input->AddUseAt(this, 0);
5844}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005845
Vladimir Marko372f10e2016-05-17 16:30:10 +01005846class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005847 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005848 // Determines how to load the String.
5849 enum class LoadKind {
5850 // Use boot image String* address that will be known at link time.
5851 // Used for boot image strings referenced by boot image code in non-PIC mode.
5852 kBootImageLinkTimeAddress,
5853
5854 // Use PC-relative boot image String* address that will be known at link time.
5855 // Used for boot image strings referenced by boot image code in PIC mode.
5856 kBootImageLinkTimePcRelative,
5857
5858 // Use a known boot image String* address, embedded in the code by the codegen.
5859 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 kBootImageAddress,
5861
Vladimir Markoaad75c62016-10-03 08:46:48 +00005862 // Load from an entry in the .bss section using a PC-relative load.
5863 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5864 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005866 // Load from the root table associated with the JIT compiled method.
5867 kJitTableAddress,
5868
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005869 // Load from resolved strings array accessed through the class loaded from
5870 // the compiled method's own ArtMethod*. This is the default access type when
5871 // all other types are unavailable.
5872 kDexCacheViaMethod,
5873
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005874 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005875 };
5876
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005877 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005878 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 const DexFile& dex_file,
5880 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005881 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5882 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005883 string_index_(string_index),
5884 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005886 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005887
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005888 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005889
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 LoadKind GetLoadKind() const {
5891 return GetPackedField<LoadKindField>();
5892 }
5893
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005894 const DexFile& GetDexFile() const {
5895 return dex_file_;
5896 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005898 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005899 return string_index_;
5900 }
5901
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005902 Handle<mirror::String> GetString() const {
5903 return string_;
5904 }
5905
5906 void SetString(Handle<mirror::String> str) {
5907 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005908 }
5909
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005910 bool CanBeMoved() const OVERRIDE { return true; }
5911
Vladimir Marko372f10e2016-05-17 16:30:10 +01005912 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005913
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005914 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005915
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 // Will call the runtime if we need to load the string through
5917 // the dex cache and the string is not guaranteed to be there yet.
5918 bool NeedsEnvironment() const OVERRIDE {
5919 LoadKind load_kind = GetLoadKind();
5920 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5921 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005922 load_kind == LoadKind::kBootImageAddress ||
5923 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 return false;
5925 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005926 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005928
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5930 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5931 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005932
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005933 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005935
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005936 static SideEffects SideEffectsForArchRuntimeCalls() {
5937 return SideEffects::CanTriggerGC();
5938 }
5939
Vladimir Marko372f10e2016-05-17 16:30:10 +01005940 void AddSpecialInput(HInstruction* special_input);
5941
5942 using HInstruction::GetInputRecords; // Keep the const version visible.
5943 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5944 return ArrayRef<HUserRecord<HInstruction*>>(
5945 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005946 }
5947
Vladimir Marko372f10e2016-05-17 16:30:10 +01005948 Primitive::Type GetType() const OVERRIDE {
5949 return Primitive::kPrimNot;
5950 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005951
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005952 DECLARE_INSTRUCTION(LoadString);
5953
5954 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005955 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956 static constexpr size_t kFieldLoadKindSize =
5957 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5958 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005959 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005961
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005962 void SetLoadKindInternal(LoadKind load_kind);
5963
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005964 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5965 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005966 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005967 HUserRecord<HInstruction*> special_input_;
5968
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005969 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005970 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005972 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005973
5974 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5975};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5977
5978// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005980 // The special input is used for PC-relative loads on some architectures,
5981 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005983 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005984 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5985 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005986 // HLoadString::GetInputRecords() returns an empty array at this point,
5987 // so use the GetInputRecords() from the base class to set the input record.
5988 DCHECK(special_input_.GetInstruction() == nullptr);
5989 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 special_input->AddUseAt(this, 0);
5991}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005992
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005993/**
5994 * Performs an initialization check on its Class object input.
5995 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005996class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005997 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005998 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005999 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006000 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006001 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6002 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006003 SetRawInputAt(0, constant);
6004 }
6005
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006006 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006007 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006008 return true;
6009 }
6010
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006011 bool NeedsEnvironment() const OVERRIDE {
6012 // May call runtime to initialize the class.
6013 return true;
6014 }
6015
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006016 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006017
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006018 HLoadClass* GetLoadClass() const {
6019 DCHECK(InputAt(0)->IsLoadClass());
6020 return InputAt(0)->AsLoadClass();
6021 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022
6023 DECLARE_INSTRUCTION(ClinitCheck);
6024
6025 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006026 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6027};
6028
Vladimir Markofcb503c2016-05-18 12:48:17 +01006029class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006030 public:
6031 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006032 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006033 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006034 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006035 bool is_volatile,
6036 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006037 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006038 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006039 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006040 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006041 field_info_(field,
6042 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006043 field_type,
6044 is_volatile,
6045 field_idx,
6046 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006047 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006048 SetRawInputAt(0, cls);
6049 }
6050
Calin Juravle52c48962014-12-16 17:02:57 +00006051
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006052 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006053
Vladimir Marko372f10e2016-05-17 16:30:10 +01006054 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6055 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006056 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006057 }
6058
6059 size_t ComputeHashCode() const OVERRIDE {
6060 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6061 }
6062
Calin Juravle52c48962014-12-16 17:02:57 +00006063 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006064 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6065 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006066 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006067
6068 DECLARE_INSTRUCTION(StaticFieldGet);
6069
6070 private:
6071 const FieldInfo field_info_;
6072
6073 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6074};
6075
Vladimir Markofcb503c2016-05-18 12:48:17 +01006076class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006077 public:
6078 HStaticFieldSet(HInstruction* cls,
6079 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006080 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006081 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006082 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006083 bool is_volatile,
6084 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006085 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006086 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006087 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006088 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006089 field_info_(field,
6090 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006091 field_type,
6092 is_volatile,
6093 field_idx,
6094 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006095 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006096 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006097 SetRawInputAt(0, cls);
6098 SetRawInputAt(1, value);
6099 }
6100
Calin Juravle52c48962014-12-16 17:02:57 +00006101 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006102 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6103 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006104 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006105
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006106 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006107 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6108 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006109
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110 DECLARE_INSTRUCTION(StaticFieldSet);
6111
6112 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006113 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6114 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6115 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6116 "Too many packed fields.");
6117
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006118 const FieldInfo field_info_;
6119
6120 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6121};
6122
Vladimir Markofcb503c2016-05-18 12:48:17 +01006123class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006124 public:
6125 HUnresolvedInstanceFieldGet(HInstruction* obj,
6126 Primitive::Type field_type,
6127 uint32_t field_index,
6128 uint32_t dex_pc)
6129 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6130 field_index_(field_index) {
6131 SetRawInputAt(0, obj);
6132 }
6133
6134 bool NeedsEnvironment() const OVERRIDE { return true; }
6135 bool CanThrow() const OVERRIDE { return true; }
6136
6137 Primitive::Type GetFieldType() const { return GetType(); }
6138 uint32_t GetFieldIndex() const { return field_index_; }
6139
6140 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6141
6142 private:
6143 const uint32_t field_index_;
6144
6145 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6146};
6147
Vladimir Markofcb503c2016-05-18 12:48:17 +01006148class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006149 public:
6150 HUnresolvedInstanceFieldSet(HInstruction* obj,
6151 HInstruction* value,
6152 Primitive::Type field_type,
6153 uint32_t field_index,
6154 uint32_t dex_pc)
6155 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006156 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006157 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006158 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006159 SetRawInputAt(0, obj);
6160 SetRawInputAt(1, value);
6161 }
6162
6163 bool NeedsEnvironment() const OVERRIDE { return true; }
6164 bool CanThrow() const OVERRIDE { return true; }
6165
Vladimir Markoa1de9182016-02-25 11:37:38 +00006166 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006167 uint32_t GetFieldIndex() const { return field_index_; }
6168
6169 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6170
6171 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006172 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6173 static constexpr size_t kFieldFieldTypeSize =
6174 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6175 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6176 kFieldFieldType + kFieldFieldTypeSize;
6177 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6178 "Too many packed fields.");
6179 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6180
Calin Juravlee460d1d2015-09-29 04:52:17 +01006181 const uint32_t field_index_;
6182
6183 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6184};
6185
Vladimir Markofcb503c2016-05-18 12:48:17 +01006186class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006187 public:
6188 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6189 uint32_t field_index,
6190 uint32_t dex_pc)
6191 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6192 field_index_(field_index) {
6193 }
6194
6195 bool NeedsEnvironment() const OVERRIDE { return true; }
6196 bool CanThrow() const OVERRIDE { return true; }
6197
6198 Primitive::Type GetFieldType() const { return GetType(); }
6199 uint32_t GetFieldIndex() const { return field_index_; }
6200
6201 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6202
6203 private:
6204 const uint32_t field_index_;
6205
6206 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6207};
6208
Vladimir Markofcb503c2016-05-18 12:48:17 +01006209class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006210 public:
6211 HUnresolvedStaticFieldSet(HInstruction* value,
6212 Primitive::Type field_type,
6213 uint32_t field_index,
6214 uint32_t dex_pc)
6215 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006216 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006217 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006218 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006219 SetRawInputAt(0, value);
6220 }
6221
6222 bool NeedsEnvironment() const OVERRIDE { return true; }
6223 bool CanThrow() const OVERRIDE { return true; }
6224
Vladimir Markoa1de9182016-02-25 11:37:38 +00006225 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006226 uint32_t GetFieldIndex() const { return field_index_; }
6227
6228 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6229
6230 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006231 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6232 static constexpr size_t kFieldFieldTypeSize =
6233 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6234 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6235 kFieldFieldType + kFieldFieldTypeSize;
6236 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6237 "Too many packed fields.");
6238 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6239
Calin Juravlee460d1d2015-09-29 04:52:17 +01006240 const uint32_t field_index_;
6241
6242 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6243};
6244
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006245// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006246class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006247 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006248 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6249 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250
David Brazdilbbd733e2015-08-18 17:48:17 +01006251 bool CanBeNull() const OVERRIDE { return false; }
6252
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006253 DECLARE_INSTRUCTION(LoadException);
6254
6255 private:
6256 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6257};
6258
David Brazdilcb1c0552015-08-04 16:22:25 +01006259// Implicit part of move-exception which clears thread-local exception storage.
6260// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006261class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006262 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006263 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6264 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006265
6266 DECLARE_INSTRUCTION(ClearException);
6267
6268 private:
6269 DISALLOW_COPY_AND_ASSIGN(HClearException);
6270};
6271
Vladimir Markofcb503c2016-05-18 12:48:17 +01006272class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006273 public:
6274 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006275 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006276 SetRawInputAt(0, exception);
6277 }
6278
6279 bool IsControlFlow() const OVERRIDE { return true; }
6280
6281 bool NeedsEnvironment() const OVERRIDE { return true; }
6282
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006283 bool CanThrow() const OVERRIDE { return true; }
6284
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006285
6286 DECLARE_INSTRUCTION(Throw);
6287
6288 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006289 DISALLOW_COPY_AND_ASSIGN(HThrow);
6290};
6291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292/**
6293 * Implementation strategies for the code generator of a HInstanceOf
6294 * or `HCheckCast`.
6295 */
6296enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006297 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 kExactCheck, // Can do a single class compare.
6299 kClassHierarchyCheck, // Can just walk the super class chain.
6300 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6301 kInterfaceCheck, // No optimization yet when checking against an interface.
6302 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006303 kArrayCheck, // No optimization yet when checking against a generic array.
6304 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305};
6306
Roland Levillain86503782016-02-11 19:07:30 +00006307std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6308
Vladimir Markofcb503c2016-05-18 12:48:17 +01006309class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006311 HInstanceOf(HInstruction* object,
6312 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006314 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006315 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006317 dex_pc) {
6318 SetPackedField<TypeCheckKindField>(check_kind);
6319 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006320 SetRawInputAt(0, object);
6321 SetRawInputAt(1, constant);
6322 }
6323
6324 bool CanBeMoved() const OVERRIDE { return true; }
6325
Vladimir Marko372f10e2016-05-17 16:30:10 +01006326 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006327 return true;
6328 }
6329
6330 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006331 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006332 }
6333
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006334 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006335 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6336 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6337 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6338 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006339
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006340 static bool CanCallRuntime(TypeCheckKind check_kind) {
6341 // Mips currently does runtime calls for any other checks.
6342 return check_kind != TypeCheckKind::kExactCheck;
6343 }
6344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006346 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006347 }
6348
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006349 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006350
6351 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006352 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6353 static constexpr size_t kFieldTypeCheckKindSize =
6354 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6355 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6356 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6357 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6358 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006360 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6361};
6362
Vladimir Markofcb503c2016-05-18 12:48:17 +01006363class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006364 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006365 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006366 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006367 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6368 SetPackedFlag<kFlagUpperCanBeNull>(true);
6369 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006370 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006371 SetRawInputAt(0, input);
6372 }
6373
David Brazdilf5552582015-12-27 13:36:12 +00006374 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006375 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006376 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006377 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006378
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006379 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006380 DCHECK(GetUpperCanBeNull() || !can_be_null);
6381 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006382 }
6383
Vladimir Markoa1de9182016-02-25 11:37:38 +00006384 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006385
Calin Juravleb1498f62015-02-16 13:13:29 +00006386 DECLARE_INSTRUCTION(BoundType);
6387
6388 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006389 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6390 // is false then CanBeNull() cannot be true).
6391 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6392 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6393 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6394 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6395
Calin Juravleb1498f62015-02-16 13:13:29 +00006396 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006397 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6398 // It is used to bound the type in cases like:
6399 // if (x instanceof ClassX) {
6400 // // uper_bound_ will be ClassX
6401 // }
David Brazdilf5552582015-12-27 13:36:12 +00006402 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006403
6404 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6405};
6406
Vladimir Markofcb503c2016-05-18 12:48:17 +01006407class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006408 public:
6409 HCheckCast(HInstruction* object,
6410 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006412 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006413 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6414 SetPackedField<TypeCheckKindField>(check_kind);
6415 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006416 SetRawInputAt(0, object);
6417 SetRawInputAt(1, constant);
6418 }
6419
6420 bool CanBeMoved() const OVERRIDE { return true; }
6421
Vladimir Marko372f10e2016-05-17 16:30:10 +01006422 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006423 return true;
6424 }
6425
6426 bool NeedsEnvironment() const OVERRIDE {
6427 // Instruction may throw a CheckCastError.
6428 return true;
6429 }
6430
6431 bool CanThrow() const OVERRIDE { return true; }
6432
Vladimir Markoa1de9182016-02-25 11:37:38 +00006433 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6434 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6435 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6436 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006437
6438 DECLARE_INSTRUCTION(CheckCast);
6439
6440 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006441 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6442 static constexpr size_t kFieldTypeCheckKindSize =
6443 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6444 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6445 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6446 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6447 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006448
6449 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450};
6451
Andreas Gampe26de38b2016-07-27 17:53:11 -07006452/**
6453 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6454 * @details We define the combined barrier types that are actually required
6455 * by the Java Memory Model, rather than using exactly the terminology from
6456 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6457 * primitives. Note that the JSR-133 cookbook generally does not deal with
6458 * store atomicity issues, and the recipes there are not always entirely sufficient.
6459 * The current recipe is as follows:
6460 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6461 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6462 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6463 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6464 * class has final fields.
6465 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6466 * store-to-memory instructions. Only generated together with non-temporal stores.
6467 */
6468enum MemBarrierKind {
6469 kAnyStore,
6470 kLoadAny,
6471 kStoreStore,
6472 kAnyAny,
6473 kNTStoreStore,
6474 kLastBarrierKind = kNTStoreStore
6475};
6476std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6477
Vladimir Markofcb503c2016-05-18 12:48:17 +01006478class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006479 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006480 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006481 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006482 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6483 SetPackedField<BarrierKindField>(barrier_kind);
6484 }
Calin Juravle27df7582015-04-17 19:12:31 +01006485
Vladimir Markoa1de9182016-02-25 11:37:38 +00006486 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006487
6488 DECLARE_INSTRUCTION(MemoryBarrier);
6489
6490 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006491 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6492 static constexpr size_t kFieldBarrierKindSize =
6493 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6494 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6495 kFieldBarrierKind + kFieldBarrierKindSize;
6496 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6497 "Too many packed fields.");
6498 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006499
6500 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6501};
6502
Vladimir Markofcb503c2016-05-18 12:48:17 +01006503class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006504 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006505 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006506 kEnter,
6507 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006508 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006509 };
6510
6511 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006512 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006513 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6514 dex_pc) {
6515 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006516 SetRawInputAt(0, object);
6517 }
6518
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006519 // Instruction may go into runtime, so we need an environment.
6520 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006521
6522 bool CanThrow() const OVERRIDE {
6523 // Verifier guarantees that monitor-exit cannot throw.
6524 // This is important because it allows the HGraphBuilder to remove
6525 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6526 return IsEnter();
6527 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006528
Vladimir Markoa1de9182016-02-25 11:37:38 +00006529 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6530 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006531
6532 DECLARE_INSTRUCTION(MonitorOperation);
6533
Calin Juravle52c48962014-12-16 17:02:57 +00006534 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006535 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6536 static constexpr size_t kFieldOperationKindSize =
6537 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6538 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6539 kFieldOperationKind + kFieldOperationKindSize;
6540 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6541 "Too many packed fields.");
6542 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006543
6544 private:
6545 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6546};
6547
Vladimir Markofcb503c2016-05-18 12:48:17 +01006548class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006549 public:
6550 HSelect(HInstruction* condition,
6551 HInstruction* true_value,
6552 HInstruction* false_value,
6553 uint32_t dex_pc)
6554 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6555 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6556
6557 // First input must be `true_value` or `false_value` to allow codegens to
6558 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6559 // that architectures which implement HSelect as a conditional move also
6560 // will not need to invert the condition.
6561 SetRawInputAt(0, false_value);
6562 SetRawInputAt(1, true_value);
6563 SetRawInputAt(2, condition);
6564 }
6565
6566 HInstruction* GetFalseValue() const { return InputAt(0); }
6567 HInstruction* GetTrueValue() const { return InputAt(1); }
6568 HInstruction* GetCondition() const { return InputAt(2); }
6569
6570 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006571 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6572 return true;
6573 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006574
6575 bool CanBeNull() const OVERRIDE {
6576 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6577 }
6578
6579 DECLARE_INSTRUCTION(Select);
6580
6581 private:
6582 DISALLOW_COPY_AND_ASSIGN(HSelect);
6583};
6584
Vladimir Markof9f64412015-09-02 14:05:49 +01006585class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006586 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006587 MoveOperands(Location source,
6588 Location destination,
6589 Primitive::Type type,
6590 HInstruction* instruction)
6591 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006592
6593 Location GetSource() const { return source_; }
6594 Location GetDestination() const { return destination_; }
6595
6596 void SetSource(Location value) { source_ = value; }
6597 void SetDestination(Location value) { destination_ = value; }
6598
6599 // The parallel move resolver marks moves as "in-progress" by clearing the
6600 // destination (but not the source).
6601 Location MarkPending() {
6602 DCHECK(!IsPending());
6603 Location dest = destination_;
6604 destination_ = Location::NoLocation();
6605 return dest;
6606 }
6607
6608 void ClearPending(Location dest) {
6609 DCHECK(IsPending());
6610 destination_ = dest;
6611 }
6612
6613 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006614 DCHECK(source_.IsValid() || destination_.IsInvalid());
6615 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006616 }
6617
6618 // True if this blocks a move from the given location.
6619 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006620 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006621 }
6622
6623 // A move is redundant if it's been eliminated, if its source and
6624 // destination are the same, or if its destination is unneeded.
6625 bool IsRedundant() const {
6626 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6627 }
6628
6629 // We clear both operands to indicate move that's been eliminated.
6630 void Eliminate() {
6631 source_ = destination_ = Location::NoLocation();
6632 }
6633
6634 bool IsEliminated() const {
6635 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6636 return source_.IsInvalid();
6637 }
6638
Alexey Frunze4dda3372015-06-01 18:31:49 -07006639 Primitive::Type GetType() const { return type_; }
6640
Nicolas Geoffray90218252015-04-15 11:56:51 +01006641 bool Is64BitMove() const {
6642 return Primitive::Is64BitType(type_);
6643 }
6644
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006645 HInstruction* GetInstruction() const { return instruction_; }
6646
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006647 private:
6648 Location source_;
6649 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006650 // The type this move is for.
6651 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006652 // The instruction this move is assocatied with. Null when this move is
6653 // for moving an input in the expected locations of user (including a phi user).
6654 // This is only used in debug mode, to ensure we do not connect interval siblings
6655 // in the same parallel move.
6656 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006657};
6658
Roland Levillainc9285912015-12-18 10:38:42 +00006659std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6660
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006661static constexpr size_t kDefaultNumberOfMoves = 4;
6662
Vladimir Markofcb503c2016-05-18 12:48:17 +01006663class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006664 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006665 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006666 : HTemplateInstruction(SideEffects::None(), dex_pc),
6667 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6668 moves_.reserve(kDefaultNumberOfMoves);
6669 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006670
Nicolas Geoffray90218252015-04-15 11:56:51 +01006671 void AddMove(Location source,
6672 Location destination,
6673 Primitive::Type type,
6674 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006675 DCHECK(source.IsValid());
6676 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006677 if (kIsDebugBuild) {
6678 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006679 for (const MoveOperands& move : moves_) {
6680 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006681 // Special case the situation where the move is for the spill slot
6682 // of the instruction.
6683 if ((GetPrevious() == instruction)
6684 || ((GetPrevious() == nullptr)
6685 && instruction->IsPhi()
6686 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006687 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006688 << "Doing parallel moves for the same instruction.";
6689 } else {
6690 DCHECK(false) << "Doing parallel moves for the same instruction.";
6691 }
6692 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006693 }
6694 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006695 for (const MoveOperands& move : moves_) {
6696 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006697 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006698 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006699 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006700 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006701 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006702 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006703 }
6704
Vladimir Marko225b6462015-09-28 12:17:40 +01006705 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006706 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006707 }
6708
Vladimir Marko225b6462015-09-28 12:17:40 +01006709 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006710
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006711 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006712
6713 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006714 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006715
6716 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6717};
6718
Mark Mendell0616ae02015-04-17 12:49:27 -04006719} // namespace art
6720
Aart Bikf8f5a162017-02-06 15:35:29 -08006721#include "nodes_vector.h"
6722
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006723#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6724#include "nodes_shared.h"
6725#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006726#ifdef ART_ENABLE_CODEGEN_arm
6727#include "nodes_arm.h"
6728#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006729#ifdef ART_ENABLE_CODEGEN_mips
6730#include "nodes_mips.h"
6731#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006732#ifdef ART_ENABLE_CODEGEN_x86
6733#include "nodes_x86.h"
6734#endif
6735
6736namespace art {
6737
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006738class HGraphVisitor : public ValueObject {
6739 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006740 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6741 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006742
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006743 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006744 virtual void VisitBasicBlock(HBasicBlock* block);
6745
Roland Levillain633021e2014-10-01 14:12:25 +01006746 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006747 void VisitInsertionOrder();
6748
Roland Levillain633021e2014-10-01 14:12:25 +01006749 // Visit the graph following dominator tree reverse post-order.
6750 void VisitReversePostOrder();
6751
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006752 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006753
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006754 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006755#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006756 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6757
6758 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6759
6760#undef DECLARE_VISIT_INSTRUCTION
6761
6762 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006763 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006764
6765 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6766};
6767
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006768class HGraphDelegateVisitor : public HGraphVisitor {
6769 public:
6770 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6771 virtual ~HGraphDelegateVisitor() {}
6772
6773 // Visit functions that delegate to to super class.
6774#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006775 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006776
6777 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6778
6779#undef DECLARE_VISIT_INSTRUCTION
6780
6781 private:
6782 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6783};
6784
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006785// Iterator over the blocks that art part of the loop. Includes blocks part
6786// of an inner loop. The order in which the blocks are iterated is on their
6787// block id.
6788class HBlocksInLoopIterator : public ValueObject {
6789 public:
6790 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6791 : blocks_in_loop_(info.GetBlocks()),
6792 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6793 index_(0) {
6794 if (!blocks_in_loop_.IsBitSet(index_)) {
6795 Advance();
6796 }
6797 }
6798
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006799 bool Done() const { return index_ == blocks_.size(); }
6800 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006801 void Advance() {
6802 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006803 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006804 if (blocks_in_loop_.IsBitSet(index_)) {
6805 break;
6806 }
6807 }
6808 }
6809
6810 private:
6811 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006812 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006813 size_t index_;
6814
6815 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6816};
6817
Mingyao Yang3584bce2015-05-19 16:01:59 -07006818// Iterator over the blocks that art part of the loop. Includes blocks part
6819// of an inner loop. The order in which the blocks are iterated is reverse
6820// post order.
6821class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6822 public:
6823 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6824 : blocks_in_loop_(info.GetBlocks()),
6825 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6826 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006827 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006828 Advance();
6829 }
6830 }
6831
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006832 bool Done() const { return index_ == blocks_.size(); }
6833 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006834 void Advance() {
6835 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006836 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6837 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006838 break;
6839 }
6840 }
6841 }
6842
6843 private:
6844 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006845 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006846 size_t index_;
6847
6848 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6849};
6850
Aart Bikf3e61ee2017-04-12 17:09:20 -07006851// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006852inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006853 if (constant->IsIntConstant()) {
6854 return constant->AsIntConstant()->GetValue();
6855 } else if (constant->IsLongConstant()) {
6856 return constant->AsLongConstant()->GetValue();
6857 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006858 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006859 return 0;
6860 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006861}
6862
Aart Bikf3e61ee2017-04-12 17:09:20 -07006863// Returns true iff instruction is an integral constant (and sets value on success).
6864inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
6865 if (instruction->IsIntConstant()) {
6866 *value = instruction->AsIntConstant()->GetValue();
6867 return true;
6868 } else if (instruction->IsLongConstant()) {
6869 *value = instruction->AsLongConstant()->GetValue();
6870 return true;
6871 } else if (instruction->IsNullConstant()) {
6872 *value = 0;
6873 return true;
6874 }
6875 return false;
6876}
6877
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006878#define INSTRUCTION_TYPE_CHECK(type, super) \
6879 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6880 inline const H##type* HInstruction::As##type() const { \
6881 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6882 } \
6883 inline H##type* HInstruction::As##type() { \
6884 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6885 }
6886
6887 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6888#undef INSTRUCTION_TYPE_CHECK
6889
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006890// Create space in `blocks` for adding `number_of_new_blocks` entries
6891// starting at location `at`. Blocks after `at` are moved accordingly.
6892inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6893 size_t number_of_new_blocks,
6894 size_t after) {
6895 DCHECK_LT(after, blocks->size());
6896 size_t old_size = blocks->size();
6897 size_t new_size = old_size + number_of_new_blocks;
6898 blocks->resize(new_size);
6899 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6900}
6901
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006902/*
6903 * Hunt "under the hood" of array lengths (leading to array references),
6904 * null checks (also leading to array references), and new arrays
6905 * (leading to the actual length). This makes it more likely related
6906 * instructions become actually comparable.
6907 */
6908inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6909 while (instruction->IsArrayLength() ||
6910 instruction->IsNullCheck() ||
6911 instruction->IsNewArray()) {
6912 instruction = instruction->IsNewArray()
6913 ? instruction->AsNewArray()->GetLength()
6914 : instruction->InputAt(0);
6915 }
6916 return instruction;
6917}
6918
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006919} // namespace art
6920
6921#endif // ART_COMPILER_OPTIMIZING_NODES_H_