blob: 8368026e924b66471b7d053550b9c4d1c83f4b9a [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_H_
18#define ART_COMPILER_OPTIMIZING_NODES_H_
19
Vladimir Marko60584552015-09-03 13:35:12 +000020#include <algorithm>
Vladimir Markof9f64412015-09-02 14:05:49 +010021#include <array>
Roland Levillain9867bc72015-08-05 10:21:34 +010022#include <type_traits>
23
Mathieu Chartiere5d80f82015-10-15 17:47:48 -070024#include "base/arena_bit_vector.h"
David Brazdil8d5b8b22015-03-24 10:51:52 +000025#include "base/arena_containers.h"
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "base/arena_object.h"
David Brazdild9c90372016-09-14 16:53:55 +010027#include "base/array_ref.h"
Vladimir Marko2c45bc92016-10-25 16:54:12 +010028#include "base/iteration_range.h"
Vladimir Marko60584552015-09-03 13:35:12 +000029#include "base/stl_util.h"
David Brazdild9c90372016-09-14 16:53:55 +010030#include "base/transform_array_ref.h"
Vladimir Marko87f3fcb2016-04-28 15:52:11 +010031#include "dex_file.h"
Andreas Gampea5b09a62016-11-17 15:21:22 -080032#include "dex_file_types.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000033#include "entrypoints/quick/quick_entrypoints_enum.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000034#include "handle.h"
35#include "handle_scope.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000036#include "invoke_type.h"
Nicolas Geoffray762869d2016-07-15 15:28:35 +010037#include "intrinsics_enum.h"
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +010038#include "locations.h"
Vladimir Marko58155012015-08-19 12:49:41 +000039#include "method_reference.h"
Calin Juravleacf735c2015-02-12 15:25:22 +000040#include "mirror/class.h"
Nicolas Geoffraye5038322014-07-04 09:41:32 +010041#include "offsets.h"
Ian Rogerse63db272014-07-15 15:36:11 -070042#include "primitive.h"
Vladimir Marko46817b82016-03-29 12:21:58 +010043#include "utils/intrusive_forward_list.h"
Nicolas Geoffray818f2102014-02-18 16:43:35 +000044
45namespace art {
46
David Brazdil1abb4192015-02-17 18:33:36 +000047class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000048class HBasicBlock;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049class HCurrentMethod;
David Brazdil8d5b8b22015-03-24 10:51:52 +000050class HDoubleConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051class HEnvironment;
David Brazdil8d5b8b22015-03-24 10:51:52 +000052class HFloatConstant;
David Brazdilfc6a86a2015-06-26 10:33:45 +000053class HGraphBuilder;
David Brazdil8d5b8b22015-03-24 10:51:52 +000054class HGraphVisitor;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000055class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +000056class HIntConstant;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000057class HInvoke;
David Brazdil8d5b8b22015-03-24 10:51:52 +000058class HLongConstant;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000059class HNullConstant;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060class HPhi;
Nicolas Geoffray3c049742014-09-24 18:10:46 +010061class HSuspendCheck;
David Brazdilffee3d32015-07-06 11:48:53 +010062class HTryBoundary;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063class LiveInterval;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000064class LocationSummary;
Nicolas Geoffraydb216f42015-05-05 17:02:20 +010065class SlowPathCode;
David Brazdil8d5b8b22015-03-24 10:51:52 +000066class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000067
Mathieu Chartier736b5602015-09-02 14:54:11 -070068namespace mirror {
69class DexCache;
70} // namespace mirror
71
Nicolas Geoffray818f2102014-02-18 16:43:35 +000072static const int kDefaultNumberOfBlocks = 8;
73static const int kDefaultNumberOfSuccessors = 2;
74static const int kDefaultNumberOfPredecessors = 2;
David Brazdilb618ade2015-07-29 10:31:29 +010075static const int kDefaultNumberOfExceptionalPredecessors = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010076static const int kDefaultNumberOfDominatedBlocks = 1;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +000077static const int kDefaultNumberOfBackEdges = 1;
Nicolas Geoffray818f2102014-02-18 16:43:35 +000078
Roland Levillain5b5b9312016-03-22 14:57:31 +000079// The maximum (meaningful) distance (31) that can be used in an integer shift/rotate operation.
80static constexpr int32_t kMaxIntShiftDistance = 0x1f;
81// The maximum (meaningful) distance (63) that can be used in a long shift/rotate operation.
82static constexpr int32_t kMaxLongShiftDistance = 0x3f;
Calin Juravle9aec02f2014-11-18 23:06:35 +000083
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010084static constexpr uint32_t kUnknownFieldIndex = static_cast<uint32_t>(-1);
Mingyao Yang8df69d42015-10-22 15:40:58 -070085static constexpr uint16_t kUnknownClassDefIndex = static_cast<uint16_t>(-1);
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +010086
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +010087static constexpr InvokeType kInvalidInvokeType = static_cast<InvokeType>(-1);
88
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +060089static constexpr uint32_t kNoDexPc = -1;
90
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010091inline bool IsSameDexFile(const DexFile& lhs, const DexFile& rhs) {
92 // For the purposes of the compiler, the dex files must actually be the same object
93 // if we want to safely treat them as the same. This is especially important for JIT
94 // as custom class loaders can open the same underlying file (or memory) multiple
95 // times and provide different class resolution but no two class loaders should ever
96 // use the same DexFile object - doing so is an unsupported hack that can lead to
97 // all sorts of weird failures.
98 return &lhs == &rhs;
99}
100
Dave Allison20dfc792014-06-16 20:44:29 -0700101enum IfCondition {
Aart Bike9f37602015-10-09 11:15:55 -0700102 // All types.
103 kCondEQ, // ==
104 kCondNE, // !=
105 // Signed integers and floating-point numbers.
106 kCondLT, // <
107 kCondLE, // <=
108 kCondGT, // >
109 kCondGE, // >=
110 // Unsigned integers.
111 kCondB, // <
112 kCondBE, // <=
113 kCondA, // >
114 kCondAE, // >=
Scott Wakeling2c76e062016-08-31 09:48:54 +0100115 // First and last aliases.
116 kCondFirst = kCondEQ,
117 kCondLast = kCondAE,
Dave Allison20dfc792014-06-16 20:44:29 -0700118};
119
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000120enum GraphAnalysisResult {
David Brazdil86ea7ee2016-02-16 09:26:07 +0000121 kAnalysisSkipped,
David Brazdilbadd8262016-02-02 16:28:56 +0000122 kAnalysisInvalidBytecode,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000123 kAnalysisFailThrowCatchLoop,
124 kAnalysisFailAmbiguousArrayOp,
125 kAnalysisSuccess,
David Brazdil4833f5a2015-12-16 10:37:39 +0000126};
127
Andreas Gampe9186ced2016-12-12 14:28:21 -0800128template <typename T>
129static inline typename std::make_unsigned<T>::type MakeUnsigned(T x) {
130 return static_cast<typename std::make_unsigned<T>::type>(x);
131}
132
Vladimir Markof9f64412015-09-02 14:05:49 +0100133class HInstructionList : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100134 public:
135 HInstructionList() : first_instruction_(nullptr), last_instruction_(nullptr) {}
136
137 void AddInstruction(HInstruction* instruction);
138 void RemoveInstruction(HInstruction* instruction);
139
David Brazdilc3d743f2015-04-22 13:40:50 +0100140 // Insert `instruction` before/after an existing instruction `cursor`.
141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
143
Roland Levillain6b469232014-09-25 10:10:38 +0100144 // Return true if this list contains `instruction`.
145 bool Contains(HInstruction* instruction) const;
146
Roland Levillainccc07a92014-09-16 14:48:16 +0100147 // Return true if `instruction1` is found before `instruction2` in
148 // this instruction list and false otherwise. Abort if none
149 // of these instructions is found.
150 bool FoundBefore(const HInstruction* instruction1,
151 const HInstruction* instruction2) const;
152
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000153 bool IsEmpty() const { return first_instruction_ == nullptr; }
154 void Clear() { first_instruction_ = last_instruction_ = nullptr; }
155
156 // Update the block of all instructions to be `block`.
157 void SetBlockOfInstructions(HBasicBlock* block) const;
158
159 void AddAfter(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000160 void AddBefore(HInstruction* cursor, const HInstructionList& instruction_list);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000161 void Add(const HInstructionList& instruction_list);
162
David Brazdil2d7352b2015-04-20 14:52:42 +0100163 // Return the number of instructions in the list. This is an expensive operation.
164 size_t CountSize() const;
165
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100166 private:
167 HInstruction* first_instruction_;
168 HInstruction* last_instruction_;
169
170 friend class HBasicBlock;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000171 friend class HGraph;
172 friend class HInstruction;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100173 friend class HInstructionIterator;
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000174 friend class HInstructionIteratorHandleChanges;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100175 friend class HBackwardInstructionIterator;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100176
177 DISALLOW_COPY_AND_ASSIGN(HInstructionList);
178};
179
David Brazdil4833f5a2015-12-16 10:37:39 +0000180class ReferenceTypeInfo : ValueObject {
181 public:
182 typedef Handle<mirror::Class> TypeHandle;
183
Vladimir Markoa1de9182016-02-25 11:37:38 +0000184 static ReferenceTypeInfo Create(TypeHandle type_handle, bool is_exact);
185
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700186 static ReferenceTypeInfo Create(TypeHandle type_handle) REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil94ab38f2016-06-21 17:48:19 +0100187 return Create(type_handle, type_handle->CannotBeAssignedFromOtherTypes());
188 }
189
Vladimir Markoa1de9182016-02-25 11:37:38 +0000190 static ReferenceTypeInfo CreateUnchecked(TypeHandle type_handle, bool is_exact) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000191 return ReferenceTypeInfo(type_handle, is_exact);
192 }
193
194 static ReferenceTypeInfo CreateInvalid() { return ReferenceTypeInfo(); }
195
Vladimir Markof39745e2016-01-26 12:16:55 +0000196 static bool IsValidHandle(TypeHandle handle) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000197 return handle.GetReference() != nullptr;
198 }
199
Vladimir Marko456307a2016-04-19 14:12:13 +0000200 bool IsValid() const {
David Brazdil4833f5a2015-12-16 10:37:39 +0000201 return IsValidHandle(type_handle_);
202 }
203
204 bool IsExact() const { return is_exact_; }
205
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool IsObjectClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000207 DCHECK(IsValid());
208 return GetTypeHandle()->IsObjectClass();
209 }
210
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700211 bool IsStringClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000212 DCHECK(IsValid());
213 return GetTypeHandle()->IsStringClass();
214 }
215
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700216 bool IsObjectArray() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000217 DCHECK(IsValid());
218 return IsArrayClass() && GetTypeHandle()->GetComponentType()->IsObjectClass();
219 }
220
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700221 bool IsInterface() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000222 DCHECK(IsValid());
223 return GetTypeHandle()->IsInterface();
224 }
225
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700226 bool IsArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000227 DCHECK(IsValid());
228 return GetTypeHandle()->IsArrayClass();
229 }
230
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700231 bool IsPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000232 DCHECK(IsValid());
233 return GetTypeHandle()->IsPrimitiveArray();
234 }
235
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool IsNonPrimitiveArrayClass() const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000237 DCHECK(IsValid());
238 return GetTypeHandle()->IsArrayClass() && !GetTypeHandle()->IsPrimitiveArray();
239 }
240
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700241 bool CanArrayHold(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000242 DCHECK(IsValid());
243 if (!IsExact()) return false;
244 if (!IsArrayClass()) return false;
245 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(rti.GetTypeHandle().Get());
246 }
247
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700248 bool CanArrayHoldValuesOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000249 DCHECK(IsValid());
250 if (!IsExact()) return false;
251 if (!IsArrayClass()) return false;
252 if (!rti.IsArrayClass()) return false;
253 return GetTypeHandle()->GetComponentType()->IsAssignableFrom(
254 rti.GetTypeHandle()->GetComponentType());
255 }
256
257 Handle<mirror::Class> GetTypeHandle() const { return type_handle_; }
258
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700259 bool IsSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000260 DCHECK(IsValid());
261 DCHECK(rti.IsValid());
262 return GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
263 }
264
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700265 bool IsStrictSupertypeOf(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000266 DCHECK(IsValid());
267 DCHECK(rti.IsValid());
268 return GetTypeHandle().Get() != rti.GetTypeHandle().Get() &&
269 GetTypeHandle()->IsAssignableFrom(rti.GetTypeHandle().Get());
270 }
271
272 // Returns true if the type information provide the same amount of details.
273 // Note that it does not mean that the instructions have the same actual type
274 // (because the type can be the result of a merge).
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700275 bool IsEqual(ReferenceTypeInfo rti) const REQUIRES_SHARED(Locks::mutator_lock_) {
David Brazdil4833f5a2015-12-16 10:37:39 +0000276 if (!IsValid() && !rti.IsValid()) {
277 // Invalid types are equal.
278 return true;
279 }
280 if (!IsValid() || !rti.IsValid()) {
281 // One is valid, the other not.
282 return false;
283 }
284 return IsExact() == rti.IsExact()
285 && GetTypeHandle().Get() == rti.GetTypeHandle().Get();
286 }
287
288 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +0000289 ReferenceTypeInfo() : type_handle_(TypeHandle()), is_exact_(false) {}
290 ReferenceTypeInfo(TypeHandle type_handle, bool is_exact)
291 : type_handle_(type_handle), is_exact_(is_exact) { }
David Brazdil4833f5a2015-12-16 10:37:39 +0000292
293 // The class of the object.
294 TypeHandle type_handle_;
295 // Whether or not the type is exact or a superclass of the actual type.
296 // Whether or not we have any information about this type.
297 bool is_exact_;
298};
299
300std::ostream& operator<<(std::ostream& os, const ReferenceTypeInfo& rhs);
301
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000302// Control-flow graph of a method. Contains a list of basic blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100303class HGraph : public ArenaObject<kArenaAllocGraph> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000304 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100305 HGraph(ArenaAllocator* arena,
306 const DexFile& dex_file,
307 uint32_t method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100309 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100310 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000311 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100312 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000313 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 blocks_(arena->Adapter(kArenaAllocBlockList)),
315 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
316 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700317 entry_block_(nullptr),
318 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100319 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100320 number_of_vregs_(0),
321 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000322 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400323 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000324 has_try_catch_(false),
Aart Bikb13c65b2017-03-21 20:14:07 -0700325 has_simd_(false),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800326 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000327 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100330 dex_file_(dex_file),
331 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100332 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 in_ssa_form_(false),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800334 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700335 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000336 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100337 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
338 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000341 cached_current_method_(nullptr),
Nicolas Geoffray53fec082017-03-27 12:56:16 +0100342 art_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000343 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700344 osr_(osr),
345 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100346 blocks_.reserve(kDefaultNumberOfBlocks);
347 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
David Brazdilbadd8262016-02-02 16:28:56 +0000349 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700350 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
354
David Brazdil69ba7b72015-06-23 18:27:30 +0100355 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000356 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100357
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 HBasicBlock* GetEntryBlock() const { return entry_block_; }
359 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100360 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000362 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
363 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100366
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100367 void ComputeDominanceInformation();
368 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000369 void ClearLoopInformation();
370 void FindBackEdges(ArenaBitVector* visited);
371 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100372 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100373 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374
David Brazdil4833f5a2015-12-16 10:37:39 +0000375 // Analyze all natural loops in this graph. Returns a code specifying that it
376 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000377 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379
David Brazdilffee3d32015-07-06 11:48:53 +0100380 // Iterate over blocks to compute try block membership. Needs reverse post
381 // order and loop information.
382 void ComputeTryBlockInformation();
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000385 // Returns the instruction to replace the invoke expression or null if the
386 // invoke is for a void method. Note that the caller is responsible for replacing
387 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000388 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000390 // Update the loop and try membership of `block`, which was spawned from `reference`.
391 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
392 // should be the new back edge.
393 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
394 HBasicBlock* reference,
395 bool replace_if_back_edge);
396
Mingyao Yang3584bce2015-05-19 16:01:59 -0700397 // Need to add a couple of blocks to test if the loop body is entered and
398 // put deoptimization instructions, etc.
399 void TransformLoopHeaderForBCE(HBasicBlock* header);
400
Aart Bikf8f5a162017-02-06 15:35:29 -0800401 // Adds a new loop directly after the loop with the given header and exit.
402 // Returns the new preheader.
403 HBasicBlock* TransformLoopForVectorization(HBasicBlock* header,
404 HBasicBlock* body,
405 HBasicBlock* exit);
406
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000407 // Removes `block` from the graph. Assumes `block` has been disconnected from
408 // other blocks and has no instructions or phis.
409 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000410
David Brazdilfc6a86a2015-06-26 10:33:45 +0000411 // Splits the edge between `block` and `successor` while preserving the
412 // indices in the predecessor/successor lists. If there are multiple edges
413 // between the blocks, the lowest indices are used.
414 // Returns the new block which is empty and has the same dex pc as `successor`.
415 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
416
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100417 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
418 void SimplifyLoop(HBasicBlock* header);
419
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000420 int32_t GetNextInstructionId() {
421 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000422 return current_instruction_id_++;
423 }
424
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000425 int32_t GetCurrentInstructionId() const {
426 return current_instruction_id_;
427 }
428
429 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000430 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 current_instruction_id_ = id;
432 }
433
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100434 uint16_t GetMaximumNumberOfOutVRegs() const {
435 return maximum_number_of_out_vregs_;
436 }
437
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000438 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
439 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100440 }
441
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100442 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
443 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
444 }
445
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000446 void UpdateTemporariesVRegSlots(size_t slots) {
447 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100448 }
449
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000450 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100451 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000452 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100453 }
454
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100455 void SetNumberOfVRegs(uint16_t number_of_vregs) {
456 number_of_vregs_ = number_of_vregs;
457 }
458
459 uint16_t GetNumberOfVRegs() const {
460 return number_of_vregs_;
461 }
462
463 void SetNumberOfInVRegs(uint16_t value) {
464 number_of_in_vregs_ = value;
465 }
466
David Brazdildee58d62016-04-07 09:54:26 +0000467 uint16_t GetNumberOfInVRegs() const {
468 return number_of_in_vregs_;
469 }
470
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100471 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100472 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100473 return number_of_vregs_ - number_of_in_vregs_;
474 }
475
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100476 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100477 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100478 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100479
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100480 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
481 DCHECK(GetReversePostOrder()[0] == entry_block_);
482 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
483 }
484
485 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
486 return ReverseRange(GetReversePostOrder());
487 }
488
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100489 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100490 return linear_order_;
491 }
492
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100493 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
494 return ReverseRange(GetLinearOrder());
495 }
496
Mark Mendell1152c922015-04-24 17:06:35 -0400497 bool HasBoundsChecks() const {
498 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800499 }
500
Mark Mendell1152c922015-04-24 17:06:35 -0400501 void SetHasBoundsChecks(bool value) {
502 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800503 }
504
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000505 bool IsDebuggable() const { return debuggable_; }
506
David Brazdil8d5b8b22015-03-24 10:51:52 +0000507 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000508 // already, it is created and inserted into the graph. This method is only for
509 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600510 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000511
512 // TODO: This is problematic for the consistency of reference type propagation
513 // because it can be created anytime after the pass and thus it will be left
514 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000516
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600526 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
527 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000528 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000529
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100530 HCurrentMethod* GetCurrentMethod();
531
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100532 const DexFile& GetDexFile() const {
533 return dex_file_;
534 }
535
536 uint32_t GetMethodIdx() const {
537 return method_idx_;
538 }
539
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100540 InvokeType GetInvokeType() const {
541 return invoke_type_;
542 }
543
Mark Mendellc4701932015-04-10 13:18:51 -0400544 InstructionSet GetInstructionSet() const {
545 return instruction_set_;
546 }
547
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000548 bool IsCompilingOsr() const { return osr_; }
549
Mingyao Yang063fc772016-08-02 11:02:54 -0700550 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
551 return cha_single_implementation_list_;
552 }
553
554 void AddCHASingleImplementationDependency(ArtMethod* method) {
555 cha_single_implementation_list_.insert(method);
556 }
557
558 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800559 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700560 }
561
David Brazdil77a48ae2015-09-15 12:34:04 +0000562 bool HasTryCatch() const { return has_try_catch_; }
563 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100564
Aart Bikb13c65b2017-03-21 20:14:07 -0700565 bool HasSIMD() const { return has_simd_; }
566 void SetHasSIMD(bool value) { has_simd_ = value; }
567
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800568 bool HasLoops() const { return has_loops_; }
569 void SetHasLoops(bool value) { has_loops_ = value; }
570
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000571 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
572 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
573
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100574 ArtMethod* GetArtMethod() const { return art_method_; }
575 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
576
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100577 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500578 // The instruction has been inserted into the graph, either as a constant, or
579 // before cursor.
580 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
581
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000582 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
583
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800584 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
585 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
586 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
587
David Brazdil2d7352b2015-04-20 14:52:42 +0100588 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000589 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100590 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000591
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000592 template <class InstructionType, typename ValueType>
593 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600594 ArenaSafeMap<ValueType, InstructionType*>* cache,
595 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 // Try to find an existing constant of the given value.
597 InstructionType* constant = nullptr;
598 auto cached_constant = cache->find(value);
599 if (cached_constant != cache->end()) {
600 constant = cached_constant->second;
601 }
602
603 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100604 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000605 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600606 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000607 cache->Overwrite(value, constant);
608 InsertConstant(constant);
609 }
610 return constant;
611 }
612
David Brazdil8d5b8b22015-03-24 10:51:52 +0000613 void InsertConstant(HConstant* instruction);
614
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000615 // Cache a float constant into the graph. This method should only be
616 // called by the SsaBuilder when creating "equivalent" instructions.
617 void CacheFloatConstant(HFloatConstant* constant);
618
619 // See CacheFloatConstant comment.
620 void CacheDoubleConstant(HDoubleConstant* constant);
621
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000622 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000623
624 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100625 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000626
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100627 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100628 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000629
Aart Bik281c6812016-08-26 11:31:48 -0700630 // List of blocks to perform a linear order tree traversal. Unlike the reverse
631 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100632 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100633
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000634 HBasicBlock* entry_block_;
635 HBasicBlock* exit_block_;
636
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100637 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100638 uint16_t maximum_number_of_out_vregs_;
639
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100640 // The number of virtual registers in this method. Contains the parameters.
641 uint16_t number_of_vregs_;
642
643 // The number of virtual registers used by parameters of this method.
644 uint16_t number_of_in_vregs_;
645
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000646 // Number of vreg size slots that the temporaries use (used in baseline compiler).
647 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100648
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800649 // Flag whether there are bounds checks in the graph. We can skip
650 // BCE if it's false. It's only best effort to keep it up to date in
651 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400652 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800653
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800654 // Flag whether there are try/catch blocks in the graph. We will skip
655 // try/catch-related passes if it's false. It's only best effort to keep
656 // it up to date in the presence of code elimination so there might be
657 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000658 bool has_try_catch_;
659
Aart Bikb13c65b2017-03-21 20:14:07 -0700660 // Flag whether SIMD instructions appear in the graph. If true, the
661 // code generators may have to be more careful spilling the wider
662 // contents of SIMD registers.
663 bool has_simd_;
664
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800665 // Flag whether there are any loops in the graph. We can skip loop
666 // optimization if it's false. It's only best effort to keep it up
667 // to date in the presence of code elimination so there might be false
668 // positives.
669 bool has_loops_;
670
671 // Flag whether there are any irreducible loops in the graph. It's only
672 // best effort to keep it up to date in the presence of code elimination
673 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000674 bool has_irreducible_loops_;
675
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000676 // Indicates whether the graph should be compiled in a way that
677 // ensures full debuggability. If false, we can apply more
678 // aggressive optimizations that may limit the level of debugging.
679 const bool debuggable_;
680
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000681 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000682 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000683
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100684 // The dex file from which the method is from.
685 const DexFile& dex_file_;
686
687 // The method index in the dex file.
688 const uint32_t method_idx_;
689
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100690 // If inlined, this encodes how the callee is being invoked.
691 const InvokeType invoke_type_;
692
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100693 // Whether the graph has been transformed to SSA form. Only used
694 // in debug mode to ensure we are not using properties only valid
695 // for non-SSA form (like the number of temporaries).
696 bool in_ssa_form_;
697
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800698 // Number of CHA guards in the graph. Used to short-circuit the
699 // CHA guard optimization pass when there is no CHA guard left.
700 uint32_t number_of_cha_guards_;
701
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 const InstructionSet instruction_set_;
703
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000704 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000705 HNullConstant* cached_null_constant_;
706 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000707 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000708 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000709 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000710
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100711 HCurrentMethod* cached_current_method_;
712
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100713 // The ArtMethod this graph is for. Note that for AOT, it may be null,
714 // for example for methods whose declaring class could not be resolved
715 // (such as when the superclass could not be found).
716 ArtMethod* art_method_;
717
David Brazdil4833f5a2015-12-16 10:37:39 +0000718 // Keep the RTI of inexact Object to avoid having to pass stack handle
719 // collection pointer to passes which may create NullConstant.
720 ReferenceTypeInfo inexact_object_rti_;
721
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000722 // Whether we are compiling this graph for on stack replacement: this will
723 // make all loops seen as irreducible and emit special stack maps to mark
724 // compiled code entries which the interpreter can directly jump to.
725 const bool osr_;
726
Mingyao Yang063fc772016-08-02 11:02:54 -0700727 // List of methods that are assumed to have single implementation.
728 ArenaSet<ArtMethod*> cha_single_implementation_list_;
729
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000730 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100731 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000732 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000733 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000734 DISALLOW_COPY_AND_ASSIGN(HGraph);
735};
736
Vladimir Markof9f64412015-09-02 14:05:49 +0100737class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000738 public:
739 HLoopInformation(HBasicBlock* header, HGraph* graph)
740 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100741 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000742 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100743 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100745 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000746 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100747 back_edges_.reserve(kDefaultNumberOfBackEdges);
748 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000750 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100751 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000752
753 void Dump(std::ostream& os);
754
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100755 HBasicBlock* GetHeader() const {
756 return header_;
757 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000758
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000759 void SetHeader(HBasicBlock* block) {
760 header_ = block;
761 }
762
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100763 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
764 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
765 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
766
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000767 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100768 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000769 }
770
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100771 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100772 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100773 }
774
David Brazdil46e2a392015-03-16 17:31:52 +0000775 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100776 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100777 }
778
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000779 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100780 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000781 }
782
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100783 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100784
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100785 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100786 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100787 }
788
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100789 // Returns the lifetime position of the back edge that has the
790 // greatest lifetime position.
791 size_t GetLifetimeEnd() const;
792
793 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100794 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100795 }
796
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000797 // Finds blocks that are part of this loop.
798 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100799
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100800 // Returns whether this loop information contains `block`.
801 // Note that this loop information *must* be populated before entering this function.
802 bool Contains(const HBasicBlock& block) const;
803
804 // Returns whether this loop information is an inner loop of `other`.
805 // Note that `other` *must* be populated before entering this function.
806 bool IsIn(const HLoopInformation& other) const;
807
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800808 // Returns true if instruction is not defined within this loop.
809 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700810
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100811 const ArenaBitVector& GetBlocks() const { return blocks_; }
812
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000813 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000814 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000815
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000816 void ClearAllBlocks() {
817 blocks_.ClearAllBits();
818 }
819
David Brazdil3f4a5222016-05-06 12:46:21 +0100820 bool HasBackEdgeNotDominatedByHeader() const;
821
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100822 bool IsPopulated() const {
823 return blocks_.GetHighestBitSet() != -1;
824 }
825
Anton Shaminf89381f2016-05-16 16:44:13 +0600826 bool DominatesAllBackEdges(HBasicBlock* block);
827
David Sehrc757dec2016-11-04 15:48:34 -0700828 bool HasExitEdge() const;
829
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000830 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100831 // Internal recursive implementation of `Populate`.
832 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100833 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100834
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000835 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100836 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000837 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100838 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100839 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100840 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000841
842 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
843};
844
David Brazdilec16f792015-08-19 15:04:01 +0100845// Stores try/catch information for basic blocks.
846// Note that HGraph is constructed so that catch blocks cannot simultaneously
847// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100848class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100849 public:
850 // Try block information constructor.
851 explicit TryCatchInformation(const HTryBoundary& try_entry)
852 : try_entry_(&try_entry),
853 catch_dex_file_(nullptr),
854 catch_type_index_(DexFile::kDexNoIndex16) {
855 DCHECK(try_entry_ != nullptr);
856 }
857
858 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800859 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100860 : try_entry_(nullptr),
861 catch_dex_file_(&dex_file),
862 catch_type_index_(catch_type_index) {}
863
864 bool IsTryBlock() const { return try_entry_ != nullptr; }
865
866 const HTryBoundary& GetTryEntry() const {
867 DCHECK(IsTryBlock());
868 return *try_entry_;
869 }
870
871 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
872
873 bool IsCatchAllTypeIndex() const {
874 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800875 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100876 }
877
Andreas Gampea5b09a62016-11-17 15:21:22 -0800878 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100879 DCHECK(IsCatchBlock());
880 return catch_type_index_;
881 }
882
883 const DexFile& GetCatchDexFile() const {
884 DCHECK(IsCatchBlock());
885 return *catch_dex_file_;
886 }
887
888 private:
889 // One of possibly several TryBoundary instructions entering the block's try.
890 // Only set for try blocks.
891 const HTryBoundary* try_entry_;
892
893 // Exception type information. Only set for catch blocks.
894 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800895 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100896};
897
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100898static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100899static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000901// A block in a method. Contains the list of instructions represented
902// as a double linked list. Each block knows its predecessors and
903// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100904
Vladimir Markof9f64412015-09-02 14:05:49 +0100905class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700907 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000908 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000909 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
910 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000911 loop_information_(nullptr),
912 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000913 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100914 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100915 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000917 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000918 try_catch_information_(nullptr) {
919 predecessors_.reserve(kDefaultNumberOfPredecessors);
920 successors_.reserve(kDefaultNumberOfSuccessors);
921 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
922 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000923
Vladimir Marko60584552015-09-03 13:35:12 +0000924 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100925 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000926 }
927
Vladimir Marko60584552015-09-03 13:35:12 +0000928 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100929 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000930 }
931
David Brazdild26a4112015-11-10 11:07:31 +0000932 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
933 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
934
Vladimir Marko60584552015-09-03 13:35:12 +0000935 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
936 return ContainsElement(successors_, block, start_from);
937 }
938
939 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100940 return dominated_blocks_;
941 }
942
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100943 bool IsEntryBlock() const {
944 return graph_->GetEntryBlock() == this;
945 }
946
947 bool IsExitBlock() const {
948 return graph_->GetExitBlock() == this;
949 }
950
David Brazdil46e2a392015-03-16 17:31:52 +0000951 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000952 bool IsSingleTryBoundary() const;
953
954 // Returns true if this block emits nothing but a jump.
955 bool IsSingleJump() const {
956 HLoopInformation* loop_info = GetLoopInformation();
957 return (IsSingleGoto() || IsSingleTryBoundary())
958 // Back edges generate a suspend check.
959 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
960 }
David Brazdil46e2a392015-03-16 17:31:52 +0000961
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000962 void AddBackEdge(HBasicBlock* back_edge) {
963 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000964 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000965 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100966 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000967 loop_information_->AddBackEdge(back_edge);
968 }
969
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000970 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000971 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000972
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100973 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000974 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600975 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000976
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000977 HBasicBlock* GetDominator() const { return dominator_; }
978 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000979 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
980
981 void RemoveDominatedBlock(HBasicBlock* block) {
982 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100983 }
Vladimir Marko60584552015-09-03 13:35:12 +0000984
985 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
986 ReplaceElement(dominated_blocks_, existing, new_block);
987 }
988
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100989 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990
991 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100992 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000993 }
994
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100995 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
996 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100997 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100998 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100999 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
1000 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001001
Nicolas Geoffray09aa1472016-01-19 10:52:54 +00001002 HInstruction* GetFirstInstructionDisregardMoves() const;
1003
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001004 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successors_.push_back(block);
1006 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001007 }
1008
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001009 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1010 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001012 new_block->predecessors_.push_back(this);
1013 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001014 }
1015
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001016 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1017 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001018 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001019 new_block->successors_.push_back(this);
1020 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001021 }
1022
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001023 // Insert `this` between `predecessor` and `successor. This method
1024 // preserves the indicies, and will update the first edge found between
1025 // `predecessor` and `successor`.
1026 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1027 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001028 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001029 successor->predecessors_[predecessor_index] = this;
1030 predecessor->successors_[successor_index] = this;
1031 successors_.push_back(successor);
1032 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001033 }
1034
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001035 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001036 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001037 }
1038
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001039 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001040 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001041 }
1042
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001043 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001044 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001045 }
1046
1047 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001048 predecessors_.push_back(block);
1049 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001050 }
1051
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001052 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001053 DCHECK_EQ(predecessors_.size(), 2u);
1054 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001055 }
1056
David Brazdil769c9e52015-04-27 13:54:09 +01001057 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001058 DCHECK_EQ(successors_.size(), 2u);
1059 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001060 }
1061
David Brazdilfc6a86a2015-06-26 10:33:45 +00001062 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001063 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001064 }
1065
David Brazdilfc6a86a2015-06-26 10:33:45 +00001066 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001067 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001068 }
1069
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001071 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001072 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001073 }
1074
1075 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001076 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001077 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001078 }
1079
1080 // Returns whether the first occurrence of `predecessor` in the list of
1081 // predecessors is at index `idx`.
1082 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001083 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001084 return GetPredecessorIndexOf(predecessor) == idx;
1085 }
1086
David Brazdild7558da2015-09-22 13:04:14 +01001087 // Create a new block between this block and its predecessors. The new block
1088 // is added to the graph, all predecessor edges are relinked to it and an edge
1089 // is created to `this`. Returns the new empty block. Reverse post order or
1090 // loop and try/catch information are not updated.
1091 HBasicBlock* CreateImmediateDominator();
1092
David Brazdilfc6a86a2015-06-26 10:33:45 +00001093 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001094 // created, latter block. Note that this method will add the block to the
1095 // graph, create a Goto at the end of the former block and will create an edge
1096 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001097 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001098 HBasicBlock* SplitBefore(HInstruction* cursor);
1099
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001100 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001101 // created block. Note that this method just updates raw block information,
1102 // like predecessors, successors, dominators, and instruction list. It does not
1103 // update the graph, reverse post order, loop information, nor make sure the
1104 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001105 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1106
1107 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1108 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001109
1110 // Merge `other` at the end of `this`. Successors and dominated blocks of
1111 // `other` are changed to be successors and dominated blocks of `this`. Note
1112 // that this method does not update the graph, reverse post order, loop
1113 // information, nor make sure the blocks are consistent (for example ending
1114 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001115 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001116
1117 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1118 // of `this` are moved to `other`.
1119 // Note that this method does not update the graph, reverse post order, loop
1120 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001121 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001122 void ReplaceWith(HBasicBlock* other);
1123
Aart Bik6b69e0a2017-01-11 10:20:43 -08001124 // Merges the instructions of `other` at the end of `this`.
1125 void MergeInstructionsWith(HBasicBlock* other);
1126
David Brazdil2d7352b2015-04-20 14:52:42 +01001127 // Merge `other` at the end of `this`. This method updates loops, reverse post
1128 // order, links to predecessors, successors, dominators and deletes the block
1129 // from the graph. The two blocks must be successive, i.e. `this` the only
1130 // predecessor of `other` and vice versa.
1131 void MergeWith(HBasicBlock* other);
1132
1133 // Disconnects `this` from all its predecessors, successors and dominator,
1134 // removes it from all loops it is included in and eventually from the graph.
1135 // The block must not dominate any other block. Predecessors and successors
1136 // are safely updated.
1137 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001138
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001139 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001140 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001141 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001142 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001143 // Replace instruction `initial` with `replacement` within this block.
1144 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1145 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001147 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001148 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1149 // instruction list. With 'ensure_safety' set to true, it verifies that the
1150 // instruction is not in use and removes it from the use lists of its inputs.
1151 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1152 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001153 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001154
1155 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001156 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001157 }
1158
Roland Levillain6b879dd2014-09-22 17:13:44 +01001159 bool IsLoopPreHeaderFirstPredecessor() const {
1160 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001161 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001162 }
1163
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001164 bool IsFirstPredecessorBackEdge() const {
1165 DCHECK(IsLoopHeader());
1166 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1167 }
1168
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001169 HLoopInformation* GetLoopInformation() const {
1170 return loop_information_;
1171 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001172
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001173 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001174 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001175 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001176 void SetInLoop(HLoopInformation* info) {
1177 if (IsLoopHeader()) {
1178 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001179 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001180 loop_information_ = info;
1181 } else if (loop_information_->Contains(*info->GetHeader())) {
1182 // Block is currently part of an outer loop. Make it part of this inner loop.
1183 // Note that a non loop header having a loop information means this loop information
1184 // has already been populated
1185 loop_information_ = info;
1186 } else {
1187 // Block is part of an inner loop. Do not update the loop information.
1188 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1189 // at this point, because this method is being called while populating `info`.
1190 }
1191 }
1192
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001193 // Raw update of the loop information.
1194 void SetLoopInformation(HLoopInformation* info) {
1195 loop_information_ = info;
1196 }
1197
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001198 bool IsInLoop() const { return loop_information_ != nullptr; }
1199
David Brazdilec16f792015-08-19 15:04:01 +01001200 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1201
1202 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1203 try_catch_information_ = try_catch_information;
1204 }
1205
1206 bool IsTryBlock() const {
1207 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1208 }
1209
1210 bool IsCatchBlock() const {
1211 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1212 }
David Brazdilffee3d32015-07-06 11:48:53 +01001213
1214 // Returns the try entry that this block's successors should have. They will
1215 // be in the same try, unless the block ends in a try boundary. In that case,
1216 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001217 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001218
David Brazdild7558da2015-09-22 13:04:14 +01001219 bool HasThrowingInstructions() const;
1220
David Brazdila4b8c212015-05-07 09:59:30 +01001221 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001222 bool Dominates(HBasicBlock* block) const;
1223
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001224 size_t GetLifetimeStart() const { return lifetime_start_; }
1225 size_t GetLifetimeEnd() const { return lifetime_end_; }
1226
1227 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1228 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1229
David Brazdil8d5b8b22015-03-24 10:51:52 +00001230 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001231 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001232 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233 bool HasSinglePhi() const;
1234
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001235 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001236 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001237 ArenaVector<HBasicBlock*> predecessors_;
1238 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001239 HInstructionList instructions_;
1240 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001241 HLoopInformation* loop_information_;
1242 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001243 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001244 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001245 // The dex program counter of the first instruction of this block.
1246 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001247 size_t lifetime_start_;
1248 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001249 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001250
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001251 friend class HGraph;
1252 friend class HInstruction;
1253
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001254 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1255};
1256
David Brazdilb2bd1c52015-03-25 11:17:37 +00001257// Iterates over the LoopInformation of all loops which contain 'block'
1258// from the innermost to the outermost.
1259class HLoopInformationOutwardIterator : public ValueObject {
1260 public:
1261 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1262 : current_(block.GetLoopInformation()) {}
1263
1264 bool Done() const { return current_ == nullptr; }
1265
1266 void Advance() {
1267 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001268 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001269 }
1270
1271 HLoopInformation* Current() const {
1272 DCHECK(!Done());
1273 return current_;
1274 }
1275
1276 private:
1277 HLoopInformation* current_;
1278
1279 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1280};
1281
Alexandre Ramesef20f712015-06-09 10:29:30 +01001282#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001283 M(Above, Condition) \
1284 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001285 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001286 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001287 M(ArrayGet, Instruction) \
1288 M(ArrayLength, Instruction) \
1289 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001290 M(Below, Condition) \
1291 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001292 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001293 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001294 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001295 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001296 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001297 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001298 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001299 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001300 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001301 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001302 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001303 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001304 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001306 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001307 M(Exit, Instruction) \
1308 M(FloatConstant, Constant) \
1309 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 M(GreaterThan, Condition) \
1311 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001313 M(InstanceFieldGet, Instruction) \
1314 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001315 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001316 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001317 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001318 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001319 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001320 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001321 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001322 M(LessThan, Condition) \
1323 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001324 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001325 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001326 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001327 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001328 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001329 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001330 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001331 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001332 M(Neg, UnaryOperation) \
1333 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001335 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001336 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001337 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001338 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001339 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001340 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001341 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001342 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001343 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001344 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001345 M(Return, Instruction) \
1346 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001347 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001348 M(Shl, BinaryOperation) \
1349 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001350 M(StaticFieldGet, Instruction) \
1351 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001352 M(UnresolvedInstanceFieldGet, Instruction) \
1353 M(UnresolvedInstanceFieldSet, Instruction) \
1354 M(UnresolvedStaticFieldGet, Instruction) \
1355 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001356 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001357 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001358 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001359 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001360 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001361 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001362 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001363 M(Xor, BinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001364 M(VecReplicateScalar, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001365 M(VecSumReduce, VecUnaryOperation) \
1366 M(VecCnv, VecUnaryOperation) \
1367 M(VecNeg, VecUnaryOperation) \
Aart Bik6daebeb2017-04-03 14:35:41 -07001368 M(VecAbs, VecUnaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001369 M(VecNot, VecUnaryOperation) \
1370 M(VecAdd, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001371 M(VecHalvingAdd, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001372 M(VecSub, VecBinaryOperation) \
1373 M(VecMul, VecBinaryOperation) \
1374 M(VecDiv, VecBinaryOperation) \
Aart Bikf3e61ee2017-04-12 17:09:20 -07001375 M(VecMin, VecBinaryOperation) \
1376 M(VecMax, VecBinaryOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001377 M(VecAnd, VecBinaryOperation) \
1378 M(VecAndNot, VecBinaryOperation) \
1379 M(VecOr, VecBinaryOperation) \
1380 M(VecXor, VecBinaryOperation) \
1381 M(VecShl, VecBinaryOperation) \
1382 M(VecShr, VecBinaryOperation) \
1383 M(VecUShr, VecBinaryOperation) \
Aart Bik8de59162017-04-21 09:42:01 -07001384 M(VecSetScalars, VecOperation) \
Artem Serovf34dd202017-04-10 17:41:46 +01001385 M(VecMultiplyAccumulate, VecOperation) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001386 M(VecLoad, VecMemoryOperation) \
1387 M(VecStore, VecMemoryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001388
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001389/*
1390 * Instructions, shared across several (not all) architectures.
1391 */
1392#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1393#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1394#else
1395#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001396 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001397 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001398 M(MultiplyAccumulate, Instruction) \
1399 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001400#endif
1401
Vladimir Markob4536b72015-11-24 13:45:23 +00001402#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001403#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001404#else
1405#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1406 M(ArmDexCacheArraysBase, Instruction)
1407#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001408
1409#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1410
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001411#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001412#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001413#else
1414#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1415 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001416 M(MipsDexCacheArraysBase, Instruction) \
1417 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001418#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001420#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1421
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001422#ifndef ART_ENABLE_CODEGEN_x86
1423#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1424#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001425#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1426 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001427 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001428 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001429 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001430#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001431
1432#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1433
1434#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1435 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001436 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001437 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1438 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001439 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001440 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001441 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1442 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1443
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001444#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1445 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001446 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001447 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001448 M(BinaryOperation, Instruction) \
Aart Bikf8f5a162017-02-06 15:35:29 -08001449 M(Invoke, Instruction) \
1450 M(VecOperation, Instruction) \
1451 M(VecUnaryOperation, VecOperation) \
1452 M(VecBinaryOperation, VecOperation) \
1453 M(VecMemoryOperation, VecOperation)
Dave Allison20dfc792014-06-16 20:44:29 -07001454
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001455#define FOR_EACH_INSTRUCTION(M) \
1456 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1457 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1458
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001459#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001460FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1461#undef FORWARD_DECLARATION
1462
Vladimir Marko372f10e2016-05-17 16:30:10 +01001463#define DECLARE_INSTRUCTION(type) \
1464 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1465 const char* DebugName() const OVERRIDE { return #type; } \
1466 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1467 return other->Is##type(); \
1468 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001469 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001470
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001471#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1472 bool Is##type() const { return As##type() != nullptr; } \
1473 const H##type* As##type() const { return this; } \
1474 H##type* As##type() { return this; }
1475
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001476template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001477class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001478 public:
David Brazdiled596192015-01-23 10:39:45 +00001479 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001480 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001481 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001482
Vladimir Marko46817b82016-03-29 12:21:58 +01001483 // Hook for the IntrusiveForwardList<>.
1484 // TODO: Hide this better.
1485 IntrusiveForwardListHook hook;
1486
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001487 private:
David Brazdiled596192015-01-23 10:39:45 +00001488 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001489 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001490
1491 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001492 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001493
Vladimir Marko46817b82016-03-29 12:21:58 +01001494 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001495
1496 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1497};
1498
David Brazdiled596192015-01-23 10:39:45 +00001499template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001500using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001501
David Brazdil1abb4192015-02-17 18:33:36 +00001502// This class is used by HEnvironment and HInstruction classes to record the
1503// instructions they use and pointers to the corresponding HUseListNodes kept
1504// by the used instructions.
1505template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001506class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001507 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001508 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1509 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001510
Vladimir Marko46817b82016-03-29 12:21:58 +01001511 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1512 : HUserRecord(old_record.instruction_, before_use_node) {}
1513 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1514 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001515 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001516 }
1517
1518 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001519 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1520 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001521
1522 private:
1523 // Instruction used by the user.
1524 HInstruction* instruction_;
1525
Vladimir Marko46817b82016-03-29 12:21:58 +01001526 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1527 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001528};
1529
Vladimir Markoe9004912016-06-16 16:50:52 +01001530// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1531// This is used for HInstruction::GetInputs() to return a container wrapper providing
1532// HInstruction* values even though the underlying container has HUserRecord<>s.
1533struct HInputExtractor {
1534 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1535 return record.GetInstruction();
1536 }
1537 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1538 return record.GetInstruction();
1539 }
1540};
1541
1542using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1543using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1544
Aart Bik854a02b2015-07-14 16:07:00 -07001545/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001546 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001547 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001548 * For write/read dependences on fields/arrays, the dependence analysis uses
1549 * type disambiguation (e.g. a float field write cannot modify the value of an
1550 * integer field read) and the access type (e.g. a reference array write cannot
1551 * modify the value of a reference field read [although it may modify the
1552 * reference fetch prior to reading the field, which is represented by its own
1553 * write/read dependence]). The analysis makes conservative points-to
1554 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1555 * the same, and any reference read depends on any reference read without
1556 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001557 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001558 * The internal representation uses 38-bit and is described in the table below.
1559 * The first line indicates the side effect, and for field/array accesses the
1560 * second line indicates the type of the access (in the order of the
1561 * Primitive::Type enum).
1562 * The two numbered lines below indicate the bit position in the bitfield (read
1563 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001564 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001565 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1566 * +-------------+---------+---------+--------------+---------+---------+
1567 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1568 * | 3 |333333322|222222221| 1 |111111110|000000000|
1569 * | 7 |654321098|765432109| 8 |765432109|876543210|
1570 *
1571 * Note that, to ease the implementation, 'changes' bits are least significant
1572 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001573 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001574class SideEffects : public ValueObject {
1575 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001576 SideEffects() : flags_(0) {}
1577
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 static SideEffects None() {
1579 return SideEffects(0);
1580 }
1581
1582 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001583 return SideEffects(kAllChangeBits | kAllDependOnBits);
1584 }
1585
1586 static SideEffects AllChanges() {
1587 return SideEffects(kAllChangeBits);
1588 }
1589
1590 static SideEffects AllDependencies() {
1591 return SideEffects(kAllDependOnBits);
1592 }
1593
1594 static SideEffects AllExceptGCDependency() {
1595 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1596 }
1597
1598 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001599 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001600 }
1601
Aart Bik34c3ba92015-07-20 14:08:59 -07001602 static SideEffects AllWrites() {
1603 return SideEffects(kAllWrites);
1604 }
1605
1606 static SideEffects AllReads() {
1607 return SideEffects(kAllReads);
1608 }
1609
1610 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1611 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001612 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001613 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001614 }
1615
Aart Bik854a02b2015-07-14 16:07:00 -07001616 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001617 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001618 }
1619
Aart Bik34c3ba92015-07-20 14:08:59 -07001620 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1621 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001622 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001623 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001624 }
1625
1626 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001627 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001628 }
1629
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001630 static SideEffects CanTriggerGC() {
1631 return SideEffects(1ULL << kCanTriggerGCBit);
1632 }
1633
1634 static SideEffects DependsOnGC() {
1635 return SideEffects(1ULL << kDependsOnGCBit);
1636 }
1637
Aart Bik854a02b2015-07-14 16:07:00 -07001638 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001639 SideEffects Union(SideEffects other) const {
1640 return SideEffects(flags_ | other.flags_);
1641 }
1642
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001643 SideEffects Exclusion(SideEffects other) const {
1644 return SideEffects(flags_ & ~other.flags_);
1645 }
1646
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001647 void Add(SideEffects other) {
1648 flags_ |= other.flags_;
1649 }
1650
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001651 bool Includes(SideEffects other) const {
1652 return (other.flags_ & flags_) == other.flags_;
1653 }
1654
1655 bool HasSideEffects() const {
1656 return (flags_ & kAllChangeBits);
1657 }
1658
1659 bool HasDependencies() const {
1660 return (flags_ & kAllDependOnBits);
1661 }
1662
1663 // Returns true if there are no side effects or dependencies.
1664 bool DoesNothing() const {
1665 return flags_ == 0;
1666 }
1667
Aart Bik854a02b2015-07-14 16:07:00 -07001668 // Returns true if something is written.
1669 bool DoesAnyWrite() const {
1670 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001671 }
1672
Aart Bik854a02b2015-07-14 16:07:00 -07001673 // Returns true if something is read.
1674 bool DoesAnyRead() const {
1675 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001676 }
1677
Aart Bik854a02b2015-07-14 16:07:00 -07001678 // Returns true if potentially everything is written and read
1679 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001680 bool DoesAllReadWrite() const {
1681 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1682 }
1683
Aart Bik854a02b2015-07-14 16:07:00 -07001684 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001685 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001686 }
1687
Roland Levillain0d5a2812015-11-13 10:07:31 +00001688 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001689 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001690 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1691 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001692 }
1693
1694 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001695 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001696 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001697 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001698 for (int s = kLastBit; s >= 0; s--) {
1699 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1700 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1701 // This is a bit for the GC side effect.
1702 if (current_bit_is_set) {
1703 flags += "GC";
1704 }
Aart Bik854a02b2015-07-14 16:07:00 -07001705 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001706 } else {
1707 // This is a bit for the array/field analysis.
1708 // The underscore character stands for the 'can trigger GC' bit.
1709 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1710 if (current_bit_is_set) {
1711 flags += kDebug[s];
1712 }
1713 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1714 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1715 flags += "|";
1716 }
1717 }
Aart Bik854a02b2015-07-14 16:07:00 -07001718 }
1719 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001720 }
1721
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001722 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001723
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001724 private:
1725 static constexpr int kFieldArrayAnalysisBits = 9;
1726
1727 static constexpr int kFieldWriteOffset = 0;
1728 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1729 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1730 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1731
1732 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1733
1734 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1735 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1736 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1737 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1738
1739 static constexpr int kLastBit = kDependsOnGCBit;
1740 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1741
1742 // Aliases.
1743
1744 static_assert(kChangeBits == kDependOnBits,
1745 "the 'change' bits should match the 'depend on' bits.");
1746
1747 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1748 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1749 static constexpr uint64_t kAllWrites =
1750 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1751 static constexpr uint64_t kAllReads =
1752 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001753
Aart Bik854a02b2015-07-14 16:07:00 -07001754 // Translates type to bit flag.
1755 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1756 CHECK_NE(type, Primitive::kPrimVoid);
1757 const uint64_t one = 1;
1758 const int shift = type; // 0-based consecutive enum
1759 DCHECK_LE(kFieldWriteOffset, shift);
1760 DCHECK_LT(shift, kArrayWriteOffset);
1761 return one << (type + offset);
1762 }
1763
1764 // Private constructor on direct flags value.
1765 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1766
1767 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001768};
1769
David Brazdiled596192015-01-23 10:39:45 +00001770// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001771class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001772 public:
Mingyao Yang01b47b02017-02-03 12:09:57 -08001773 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena,
1774 size_t number_of_vregs,
1775 ArtMethod* method,
1776 uint32_t dex_pc,
1777 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1779 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001780 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001781 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001782 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001783 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001784 }
1785
Mingyao Yang01b47b02017-02-03 12:09:57 -08001786 ALWAYS_INLINE HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001787 : HEnvironment(arena,
1788 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001789 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001790 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001791 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001792
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001794 if (parent_ != nullptr) {
1795 parent_->SetAndCopyParentChain(allocator, parent);
1796 } else {
1797 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1798 parent_->CopyFrom(parent);
1799 if (parent->GetParent() != nullptr) {
1800 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1801 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001802 }
David Brazdiled596192015-01-23 10:39:45 +00001803 }
1804
Vladimir Marko71bf8092015-09-15 15:33:14 +01001805 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001806 void CopyFrom(HEnvironment* environment);
1807
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001808 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1809 // input to the loop phi instead. This is for inserting instructions that
1810 // require an environment (like HDeoptimization) in the loop pre-header.
1811 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001812
1813 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001814 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001815 }
1816
1817 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001818 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001819 }
1820
David Brazdil1abb4192015-02-17 18:33:36 +00001821 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001822
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001823 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001824
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001825 HEnvironment* GetParent() const { return parent_; }
1826
1827 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001828 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001829 }
1830
1831 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001832 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001833 }
1834
1835 uint32_t GetDexPc() const {
1836 return dex_pc_;
1837 }
1838
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001839 ArtMethod* GetMethod() const {
1840 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001841 }
1842
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001843 HInstruction* GetHolder() const {
1844 return holder_;
1845 }
1846
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001847
1848 bool IsFromInlinedInvoke() const {
1849 return GetParent() != nullptr;
1850 }
1851
David Brazdiled596192015-01-23 10:39:45 +00001852 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001853 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1854 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001855 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001856 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001857 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001858
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001859 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001860 HInstruction* const holder_;
1861
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001862 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001863
1864 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1865};
1866
Vladimir Markof9f64412015-09-02 14:05:49 +01001867class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001868 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001869 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001870 : previous_(nullptr),
1871 next_(nullptr),
1872 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001873 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001874 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001875 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001876 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001877 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001878 locations_(nullptr),
1879 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001880 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001881 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001882 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1883 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1884 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001885
Dave Allison20dfc792014-06-16 20:44:29 -07001886 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001887
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001888#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001889 enum InstructionKind {
1890 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1891 };
1892#undef DECLARE_KIND
1893
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 HInstruction* GetNext() const { return next_; }
1895 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001896
Calin Juravle77520bc2015-01-12 18:45:46 +00001897 HInstruction* GetNextDisregardingMoves() const;
1898 HInstruction* GetPreviousDisregardingMoves() const;
1899
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001901 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001903 bool IsInBlock() const { return block_ != nullptr; }
1904 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001905 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1906 bool IsIrreducibleLoopHeaderPhi() const {
1907 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1908 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001909
Vladimir Marko372f10e2016-05-17 16:30:10 +01001910 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1911
1912 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1913 // One virtual method is enough, just const_cast<> and then re-add the const.
1914 return ArrayRef<const HUserRecord<HInstruction*>>(
1915 const_cast<HInstruction*>(this)->GetInputRecords());
1916 }
1917
Vladimir Markoe9004912016-06-16 16:50:52 +01001918 HInputsRef GetInputs() {
1919 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001920 }
1921
Vladimir Markoe9004912016-06-16 16:50:52 +01001922 HConstInputsRef GetInputs() const {
1923 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001924 }
1925
1926 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001927 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001928
Aart Bik2767f4b2016-10-28 15:03:53 -07001929 bool HasInput(HInstruction* input) const {
1930 for (const HInstruction* i : GetInputs()) {
1931 if (i == input) {
1932 return true;
1933 }
1934 }
1935 return false;
1936 }
1937
Vladimir Marko372f10e2016-05-17 16:30:10 +01001938 void SetRawInputAt(size_t index, HInstruction* input) {
1939 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1940 }
1941
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001942 virtual void Accept(HGraphVisitor* visitor) = 0;
1943 virtual const char* DebugName() const = 0;
1944
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001945 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001948
1949 uint32_t GetDexPc() const { return dex_pc_; }
1950
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001951 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001952
Nicolas Geoffray331605a2017-03-01 11:01:41 +00001953 // Can the instruction throw?
1954 // TODO: We should rename to CanVisiblyThrow, as some instructions (like HNewInstance),
1955 // could throw OOME, but it is still OK to remove them if they are unused.
Roland Levillaine161a2a2014-10-03 12:45:18 +01001956 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001957 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001958
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001959 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001960 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961
Calin Juravle10e244f2015-01-26 18:54:32 +00001962 // Does not apply for all instructions, but having this at top level greatly
1963 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001964 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001965 virtual bool CanBeNull() const {
1966 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1967 return true;
1968 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001969
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001970 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001971 return false;
1972 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001973
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001974 virtual bool IsActualObject() const {
1975 return GetType() == Primitive::kPrimNot;
1976 }
1977
Calin Juravle2e768302015-07-28 14:41:11 +00001978 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001979
Calin Juravle61d544b2015-02-23 16:46:57 +00001980 ReferenceTypeInfo GetReferenceTypeInfo() const {
1981 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001982 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001983 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001984 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001985
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001986 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001987 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001988 // Note: fixup_end remains valid across push_front().
1989 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1990 HUseListNode<HInstruction*>* new_node =
1991 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1992 uses_.push_front(*new_node);
1993 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001994 }
1995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001996 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001997 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001998 // Note: env_fixup_end remains valid across push_front().
1999 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
2000 HUseListNode<HEnvironment*>* new_node =
2001 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
2002 env_uses_.push_front(*new_node);
2003 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002004 }
2005
David Brazdil1abb4192015-02-17 18:33:36 +00002006 void RemoveAsUserOfInput(size_t input) {
2007 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01002008 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2009 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2010 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00002011 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002012
Vladimir Marko372f10e2016-05-17 16:30:10 +01002013 void RemoveAsUserOfAllInputs() {
2014 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
2015 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
2016 input_use.GetInstruction()->uses_.erase_after(before_use_node);
2017 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
2018 }
2019 }
2020
David Brazdil1abb4192015-02-17 18:33:36 +00002021 const HUseList<HInstruction*>& GetUses() const { return uses_; }
2022 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002023
Vladimir Marko46817b82016-03-29 12:21:58 +01002024 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
2025 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
2026 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01002027 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01002028 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01002029 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002030
Aart Bikcc42be02016-10-20 16:14:16 -07002031 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07002032 return
Aart Bikff7d89c2016-11-07 08:49:28 -08002033 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07002034 !CanThrow() &&
2035 !IsSuspendCheck() &&
2036 !IsControlFlow() &&
2037 !IsNativeDebugInfo() &&
2038 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002039 // If we added an explicit barrier then we should keep it.
2040 !IsMemoryBarrier();
2041 }
2042
Aart Bikcc42be02016-10-20 16:14:16 -07002043 bool IsDeadAndRemovable() const {
2044 return IsRemovable() && !HasUses();
2045 }
2046
Roland Levillain6c82d402014-10-13 16:10:27 +01002047 // Does this instruction strictly dominate `other_instruction`?
2048 // Returns false if this instruction and `other_instruction` are the same.
2049 // Aborts if this instruction and `other_instruction` are both phis.
2050 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002051
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002052 int GetId() const { return id_; }
2053 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002054
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002055 int GetSsaIndex() const { return ssa_index_; }
2056 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2057 bool HasSsaIndex() const { return ssa_index_ != -1; }
2058
2059 bool HasEnvironment() const { return environment_ != nullptr; }
2060 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002061 // Set the `environment_` field. Raw because this method does not
2062 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002063 void SetRawEnvironment(HEnvironment* environment) {
2064 DCHECK(environment_ == nullptr);
2065 DCHECK_EQ(environment->GetHolder(), this);
2066 environment_ = environment;
2067 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002068
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002069 void InsertRawEnvironment(HEnvironment* environment) {
2070 DCHECK(environment_ != nullptr);
2071 DCHECK_EQ(environment->GetHolder(), this);
2072 DCHECK(environment->GetParent() == nullptr);
2073 environment->parent_ = environment_;
2074 environment_ = environment;
2075 }
2076
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002077 void RemoveEnvironment();
2078
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002079 // Set the environment of this instruction, copying it from `environment`. While
2080 // copying, the uses lists are being updated.
2081 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002082 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002083 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002084 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002085 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002086 if (environment->GetParent() != nullptr) {
2087 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2088 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002089 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002090
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002091 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2092 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002093 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002094 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002095 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002096 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002097 if (environment->GetParent() != nullptr) {
2098 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2099 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002100 }
2101
Nicolas Geoffray39468442014-09-02 15:17:15 +01002102 // Returns the number of entries in the environment. Typically, that is the
2103 // number of dex registers in a method. It could be more in case of inlining.
2104 size_t EnvironmentSize() const;
2105
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002106 LocationSummary* GetLocations() const { return locations_; }
2107 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002109 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002110 void ReplaceUsesDominatedBy(HInstruction* dominator, HInstruction* replacement);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002111 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002112
Alexandre Rames188d4312015-04-09 18:30:21 +01002113 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2114 // uses of this instruction by `other` are *not* updated.
2115 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2116 ReplaceWith(other);
2117 other->ReplaceInput(this, use_index);
2118 }
2119
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002120 // Move `this` instruction before `cursor`
2121 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002122
Vladimir Markofb337ea2015-11-25 15:25:10 +00002123 // Move `this` before its first user and out of any loops. If there is no
2124 // out-of-loop user that dominates all other users, move the instruction
2125 // to the end of the out-of-loop common dominator of the user's blocks.
2126 //
2127 // This can be used only on non-throwing instructions with no side effects that
2128 // have at least one use but no environment uses.
2129 void MoveBeforeFirstUserAndOutOfLoops();
2130
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002131#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 bool Is##type() const; \
2133 const H##type* As##type() const; \
2134 H##type* As##type();
2135
2136 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2137#undef INSTRUCTION_TYPE_CHECK
2138
2139#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002140 bool Is##type() const { return (As##type() != nullptr); } \
2141 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002143 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144#undef INSTRUCTION_TYPE_CHECK
2145
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002146 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002147 // TODO: this method is used by LICM and GVN with possibly different
2148 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002149 virtual bool CanBeMoved() const { return false; }
2150
2151 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002152 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002153 return false;
2154 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002155
2156 // Returns whether any data encoded in the two instructions is equal.
2157 // This method does not look at the inputs. Both instructions must be
2158 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002159 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002160 return false;
2161 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002162
2163 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002164 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002165 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002166 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002167
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002168 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2169 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2170 // the virtual function because the __attribute__((__pure__)) doesn't really
2171 // apply the strong requirement for virtual functions, preventing optimizations.
2172 InstructionKind GetKind() const PURE;
2173 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002174
2175 virtual size_t ComputeHashCode() const {
2176 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002177 for (const HInstruction* input : GetInputs()) {
2178 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002179 }
2180 return result;
2181 }
2182
2183 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002184 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002185 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002186
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002187 size_t GetLifetimePosition() const { return lifetime_position_; }
2188 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2189 LiveInterval* GetLiveInterval() const { return live_interval_; }
2190 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2191 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2192
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002193 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2194
2195 // Returns whether the code generation of the instruction will require to have access
2196 // to the current method. Such instructions are:
2197 // (1): Instructions that require an environment, as calling the runtime requires
2198 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002199 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2200 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002201 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002202 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002203 }
2204
Vladimir Markodc151b22015-10-15 18:02:30 +01002205 // Returns whether the code generation of the instruction will require to have access
2206 // to the dex cache of the current method's declaring class via the current method.
2207 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002208
Mark Mendellc4701932015-04-10 13:18:51 -04002209 // Does this instruction have any use in an environment before
2210 // control flow hits 'other'?
2211 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2212
2213 // Remove all references to environment uses of this instruction.
2214 // The caller must ensure that this is safe to do.
2215 void RemoveEnvironmentUsers();
2216
Vladimir Markoa1de9182016-02-25 11:37:38 +00002217 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2218 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002219
David Brazdil1abb4192015-02-17 18:33:36 +00002220 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002221 // If set, the machine code for this instruction is assumed to be generated by
2222 // its users. Used by liveness analysis to compute use positions accordingly.
2223 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2224 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2225 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2226 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2227
Vladimir Marko372f10e2016-05-17 16:30:10 +01002228 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2229 return GetInputRecords()[i];
2230 }
2231
2232 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2233 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2234 input_records[index] = input;
2235 }
David Brazdil1abb4192015-02-17 18:33:36 +00002236
Vladimir Markoa1de9182016-02-25 11:37:38 +00002237 uint32_t GetPackedFields() const {
2238 return packed_fields_;
2239 }
2240
2241 template <size_t flag>
2242 bool GetPackedFlag() const {
2243 return (packed_fields_ & (1u << flag)) != 0u;
2244 }
2245
2246 template <size_t flag>
2247 void SetPackedFlag(bool value = true) {
2248 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2249 }
2250
2251 template <typename BitFieldType>
2252 typename BitFieldType::value_type GetPackedField() const {
2253 return BitFieldType::Decode(packed_fields_);
2254 }
2255
2256 template <typename BitFieldType>
2257 void SetPackedField(typename BitFieldType::value_type value) {
2258 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2259 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2260 }
2261
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002262 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002263 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2264 auto before_use_node = uses_.before_begin();
2265 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2266 HInstruction* user = use_node->GetUser();
2267 size_t input_index = use_node->GetIndex();
2268 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2269 before_use_node = use_node;
2270 }
2271 }
2272
2273 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2274 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2275 if (next != uses_.end()) {
2276 HInstruction* next_user = next->GetUser();
2277 size_t next_index = next->GetIndex();
2278 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2279 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2280 }
2281 }
2282
2283 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2284 auto before_env_use_node = env_uses_.before_begin();
2285 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2286 HEnvironment* user = env_use_node->GetUser();
2287 size_t input_index = env_use_node->GetIndex();
2288 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2289 before_env_use_node = env_use_node;
2290 }
2291 }
2292
2293 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2294 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2295 if (next != env_uses_.end()) {
2296 HEnvironment* next_user = next->GetUser();
2297 size_t next_index = next->GetIndex();
2298 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2299 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2300 }
2301 }
David Brazdil1abb4192015-02-17 18:33:36 +00002302
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002303 HInstruction* previous_;
2304 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002305 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002306 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002308 // An instruction gets an id when it is added to the graph.
2309 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002310 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002311 int id_;
2312
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002313 // When doing liveness analysis, instructions that have uses get an SSA index.
2314 int ssa_index_;
2315
Vladimir Markoa1de9182016-02-25 11:37:38 +00002316 // Packed fields.
2317 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002318
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002319 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002320 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002321
2322 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002323 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002324
Nicolas Geoffray39468442014-09-02 15:17:15 +01002325 // The environment associated with this instruction. Not null if the instruction
2326 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002327 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002328
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002329 // Set by the code generator.
2330 LocationSummary* locations_;
2331
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002332 // Set by the liveness analysis.
2333 LiveInterval* live_interval_;
2334
2335 // Set by the liveness analysis, this is the position in a linear
2336 // order of blocks where this instruction's live interval start.
2337 size_t lifetime_position_;
2338
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002339 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002340
Vladimir Markoa1de9182016-02-25 11:37:38 +00002341 // The reference handle part of the reference type info.
2342 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002343 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002344 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002345
David Brazdil1abb4192015-02-17 18:33:36 +00002346 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002347 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002348 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002349 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002350 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002351
2352 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2353};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002354std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002355
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002356// Iterates over the instructions, while preserving the next instruction
2357// in case the current instruction gets removed from the list by the user
2358// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002359class HInstructionIterator : public ValueObject {
2360 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002361 explicit HInstructionIterator(const HInstructionList& instructions)
2362 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002363 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002364 }
2365
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002366 bool Done() const { return instruction_ == nullptr; }
2367 HInstruction* Current() const { return instruction_; }
2368 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002369 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002370 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002371 }
2372
2373 private:
2374 HInstruction* instruction_;
2375 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002376
2377 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002378};
2379
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002380// Iterates over the instructions without saving the next instruction,
2381// therefore handling changes in the graph potentially made by the user
2382// of this iterator.
2383class HInstructionIteratorHandleChanges : public ValueObject {
2384 public:
2385 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2386 : instruction_(instructions.first_instruction_) {
2387 }
2388
2389 bool Done() const { return instruction_ == nullptr; }
2390 HInstruction* Current() const { return instruction_; }
2391 void Advance() {
2392 instruction_ = instruction_->GetNext();
2393 }
2394
2395 private:
2396 HInstruction* instruction_;
2397
2398 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2399};
2400
2401
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002402class HBackwardInstructionIterator : public ValueObject {
2403 public:
2404 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2405 : instruction_(instructions.last_instruction_) {
2406 next_ = Done() ? nullptr : instruction_->GetPrevious();
2407 }
2408
2409 bool Done() const { return instruction_ == nullptr; }
2410 HInstruction* Current() const { return instruction_; }
2411 void Advance() {
2412 instruction_ = next_;
2413 next_ = Done() ? nullptr : instruction_->GetPrevious();
2414 }
2415
2416 private:
2417 HInstruction* instruction_;
2418 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002419
2420 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002421};
2422
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002423class HVariableInputSizeInstruction : public HInstruction {
2424 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002425 using HInstruction::GetInputRecords; // Keep the const version visible.
2426 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2427 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2428 }
2429
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002430 void AddInput(HInstruction* input);
2431 void InsertInputAt(size_t index, HInstruction* input);
2432 void RemoveInputAt(size_t index);
2433
2434 protected:
2435 HVariableInputSizeInstruction(SideEffects side_effects,
2436 uint32_t dex_pc,
2437 ArenaAllocator* arena,
2438 size_t number_of_inputs,
2439 ArenaAllocKind kind)
2440 : HInstruction(side_effects, dex_pc),
2441 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2442
2443 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2444
2445 private:
2446 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2447};
2448
Vladimir Markof9f64412015-09-02 14:05:49 +01002449template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002450class HTemplateInstruction: public HInstruction {
2451 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002452 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002453 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002454 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002455
Vladimir Marko372f10e2016-05-17 16:30:10 +01002456 using HInstruction::GetInputRecords; // Keep the const version visible.
2457 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2458 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002459 }
2460
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002461 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002462 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002463
2464 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002465};
2466
Vladimir Markof9f64412015-09-02 14:05:49 +01002467// HTemplateInstruction specialization for N=0.
2468template<>
2469class HTemplateInstruction<0>: public HInstruction {
2470 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002471 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 : HInstruction(side_effects, dex_pc) {}
2473
Vladimir Markof9f64412015-09-02 14:05:49 +01002474 virtual ~HTemplateInstruction() {}
2475
Vladimir Marko372f10e2016-05-17 16:30:10 +01002476 using HInstruction::GetInputRecords; // Keep the const version visible.
2477 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2478 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002479 }
2480
2481 private:
2482 friend class SsaBuilder;
2483};
2484
Dave Allison20dfc792014-06-16 20:44:29 -07002485template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002486class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002487 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002488 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002489 : HTemplateInstruction<N>(side_effects, dex_pc) {
2490 this->template SetPackedField<TypeField>(type);
2491 }
Dave Allison20dfc792014-06-16 20:44:29 -07002492 virtual ~HExpression() {}
2493
Vladimir Markoa1de9182016-02-25 11:37:38 +00002494 Primitive::Type GetType() const OVERRIDE {
2495 return TypeField::Decode(this->GetPackedFields());
2496 }
Dave Allison20dfc792014-06-16 20:44:29 -07002497
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002498 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002499 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2500 static constexpr size_t kFieldTypeSize =
2501 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2502 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2503 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2504 "Too many packed fields.");
2505 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002506};
2507
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002508// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2509// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002510class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002511 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002512 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2513 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002514
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002515 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002516
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002517 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002518
2519 private:
2520 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2521};
2522
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002523// Represents dex's RETURN opcodes. A HReturn is a control flow
2524// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002525class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002526 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002527 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2528 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002529 SetRawInputAt(0, value);
2530 }
2531
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002532 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002533
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002534 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002535
2536 private:
2537 DISALLOW_COPY_AND_ASSIGN(HReturn);
2538};
2539
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002540class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002541 public:
2542 HPhi(ArenaAllocator* arena,
2543 uint32_t reg_number,
2544 size_t number_of_inputs,
2545 Primitive::Type type,
2546 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002547 : HVariableInputSizeInstruction(
2548 SideEffects::None(),
2549 dex_pc,
2550 arena,
2551 number_of_inputs,
2552 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002553 reg_number_(reg_number) {
2554 SetPackedField<TypeField>(ToPhiType(type));
2555 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2556 // Phis are constructed live and marked dead if conflicting or unused.
2557 // Individual steps of SsaBuilder should assume that if a phi has been
2558 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2559 SetPackedFlag<kFlagIsLive>(true);
2560 SetPackedFlag<kFlagCanBeNull>(true);
2561 }
2562
2563 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2564 static Primitive::Type ToPhiType(Primitive::Type type) {
2565 return Primitive::PrimitiveKind(type);
2566 }
2567
2568 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2569
David Brazdildee58d62016-04-07 09:54:26 +00002570 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2571 void SetType(Primitive::Type new_type) {
2572 // Make sure that only valid type changes occur. The following are allowed:
2573 // (1) int -> float/ref (primitive type propagation),
2574 // (2) long -> double (primitive type propagation).
2575 DCHECK(GetType() == new_type ||
2576 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2577 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2578 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2579 SetPackedField<TypeField>(new_type);
2580 }
2581
2582 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2583 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2584
2585 uint32_t GetRegNumber() const { return reg_number_; }
2586
2587 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2588 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2589 bool IsDead() const { return !IsLive(); }
2590 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2591
Vladimir Markoe9004912016-06-16 16:50:52 +01002592 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002593 return other != nullptr
2594 && other->IsPhi()
2595 && other->AsPhi()->GetBlock() == GetBlock()
2596 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2597 }
2598
2599 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2600 // An equivalent phi is a phi having the same dex register and type.
2601 // It assumes that phis with the same dex register are adjacent.
2602 HPhi* GetNextEquivalentPhiWithSameType() {
2603 HInstruction* next = GetNext();
2604 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2605 if (next->GetType() == GetType()) {
2606 return next->AsPhi();
2607 }
2608 next = next->GetNext();
2609 }
2610 return nullptr;
2611 }
2612
2613 DECLARE_INSTRUCTION(Phi);
2614
David Brazdildee58d62016-04-07 09:54:26 +00002615 private:
2616 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2617 static constexpr size_t kFieldTypeSize =
2618 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2619 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2620 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2621 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2622 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2623 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2624
David Brazdildee58d62016-04-07 09:54:26 +00002625 const uint32_t reg_number_;
2626
2627 DISALLOW_COPY_AND_ASSIGN(HPhi);
2628};
2629
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002630// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002631// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002632// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002633class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002634 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002635 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002636
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002637 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002638
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002639 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002640
2641 private:
2642 DISALLOW_COPY_AND_ASSIGN(HExit);
2643};
2644
2645// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002646class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002647 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002648 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002649
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002650 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002651
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002652 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002653 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002654 }
2655
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002656 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002657
2658 private:
2659 DISALLOW_COPY_AND_ASSIGN(HGoto);
2660};
2661
Roland Levillain9867bc72015-08-05 10:21:34 +01002662class HConstant : public HExpression<0> {
2663 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002664 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2665 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002666
2667 bool CanBeMoved() const OVERRIDE { return true; }
2668
Roland Levillain1a653882016-03-18 18:05:57 +00002669 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002670 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002671 // Is this constant 0 in the arithmetic sense?
2672 virtual bool IsArithmeticZero() const { return false; }
2673 // Is this constant a 0-bit pattern?
2674 virtual bool IsZeroBitPattern() const { return false; }
2675 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002676 virtual bool IsOne() const { return false; }
2677
David Brazdil77a48ae2015-09-15 12:34:04 +00002678 virtual uint64_t GetValueAsUint64() const = 0;
2679
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002680 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002681
2682 private:
2683 DISALLOW_COPY_AND_ASSIGN(HConstant);
2684};
2685
Vladimir Markofcb503c2016-05-18 12:48:17 +01002686class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002687 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002688 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002689 return true;
2690 }
2691
David Brazdil77a48ae2015-09-15 12:34:04 +00002692 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2693
Roland Levillain9867bc72015-08-05 10:21:34 +01002694 size_t ComputeHashCode() const OVERRIDE { return 0; }
2695
Roland Levillain1a653882016-03-18 18:05:57 +00002696 // The null constant representation is a 0-bit pattern.
2697 virtual bool IsZeroBitPattern() const { return true; }
2698
Roland Levillain9867bc72015-08-05 10:21:34 +01002699 DECLARE_INSTRUCTION(NullConstant);
2700
2701 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002703
2704 friend class HGraph;
2705 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2706};
2707
2708// Constants of the type int. Those can be from Dex instructions, or
2709// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002710class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002711 public:
2712 int32_t GetValue() const { return value_; }
2713
David Brazdil9f389d42015-10-01 14:32:56 +01002714 uint64_t GetValueAsUint64() const OVERRIDE {
2715 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2716 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002717
Vladimir Marko372f10e2016-05-17 16:30:10 +01002718 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002719 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002720 return other->AsIntConstant()->value_ == value_;
2721 }
2722
2723 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2724
2725 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002726 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2727 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002728 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2729
Roland Levillain1a653882016-03-18 18:05:57 +00002730 // Integer constants are used to encode Boolean values as well,
2731 // where 1 means true and 0 means false.
2732 bool IsTrue() const { return GetValue() == 1; }
2733 bool IsFalse() const { return GetValue() == 0; }
2734
Roland Levillain9867bc72015-08-05 10:21:34 +01002735 DECLARE_INSTRUCTION(IntConstant);
2736
2737 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002738 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2739 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2740 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2741 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002742
2743 const int32_t value_;
2744
2745 friend class HGraph;
2746 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2747 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2748 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2749};
2750
Vladimir Markofcb503c2016-05-18 12:48:17 +01002751class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002752 public:
2753 int64_t GetValue() const { return value_; }
2754
David Brazdil77a48ae2015-09-15 12:34:04 +00002755 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2756
Vladimir Marko372f10e2016-05-17 16:30:10 +01002757 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002758 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002759 return other->AsLongConstant()->value_ == value_;
2760 }
2761
2762 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2763
2764 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002765 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2766 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002767 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2768
2769 DECLARE_INSTRUCTION(LongConstant);
2770
2771 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002772 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2773 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002774
2775 const int64_t value_;
2776
2777 friend class HGraph;
2778 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2779};
Dave Allison20dfc792014-06-16 20:44:29 -07002780
Vladimir Markofcb503c2016-05-18 12:48:17 +01002781class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002782 public:
2783 float GetValue() const { return value_; }
2784
2785 uint64_t GetValueAsUint64() const OVERRIDE {
2786 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2787 }
2788
Vladimir Marko372f10e2016-05-17 16:30:10 +01002789 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002790 DCHECK(other->IsFloatConstant()) << other->DebugName();
2791 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2792 }
2793
2794 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2795
2796 bool IsMinusOne() const OVERRIDE {
2797 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2798 }
Roland Levillain1a653882016-03-18 18:05:57 +00002799 bool IsArithmeticZero() const OVERRIDE {
2800 return std::fpclassify(value_) == FP_ZERO;
2801 }
2802 bool IsArithmeticPositiveZero() const {
2803 return IsArithmeticZero() && !std::signbit(value_);
2804 }
2805 bool IsArithmeticNegativeZero() const {
2806 return IsArithmeticZero() && std::signbit(value_);
2807 }
2808 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002809 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002810 }
2811 bool IsOne() const OVERRIDE {
2812 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2813 }
2814 bool IsNaN() const {
2815 return std::isnan(value_);
2816 }
2817
2818 DECLARE_INSTRUCTION(FloatConstant);
2819
2820 private:
2821 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2822 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2823 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2824 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2825
2826 const float value_;
2827
2828 // Only the SsaBuilder and HGraph can create floating-point constants.
2829 friend class SsaBuilder;
2830 friend class HGraph;
2831 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2832};
2833
Vladimir Markofcb503c2016-05-18 12:48:17 +01002834class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002835 public:
2836 double GetValue() const { return value_; }
2837
2838 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2839
Vladimir Marko372f10e2016-05-17 16:30:10 +01002840 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002841 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2842 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2843 }
2844
2845 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2846
2847 bool IsMinusOne() const OVERRIDE {
2848 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2849 }
Roland Levillain1a653882016-03-18 18:05:57 +00002850 bool IsArithmeticZero() const OVERRIDE {
2851 return std::fpclassify(value_) == FP_ZERO;
2852 }
2853 bool IsArithmeticPositiveZero() const {
2854 return IsArithmeticZero() && !std::signbit(value_);
2855 }
2856 bool IsArithmeticNegativeZero() const {
2857 return IsArithmeticZero() && std::signbit(value_);
2858 }
2859 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002860 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002861 }
2862 bool IsOne() const OVERRIDE {
2863 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2864 }
2865 bool IsNaN() const {
2866 return std::isnan(value_);
2867 }
2868
2869 DECLARE_INSTRUCTION(DoubleConstant);
2870
2871 private:
2872 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2873 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2874 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2875 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2876
2877 const double value_;
2878
2879 // Only the SsaBuilder and HGraph can create floating-point constants.
2880 friend class SsaBuilder;
2881 friend class HGraph;
2882 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2883};
2884
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002885// Conditional branch. A block ending with an HIf instruction must have
2886// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002887class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002888 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002889 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2890 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002891 SetRawInputAt(0, input);
2892 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002893
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002894 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002895
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002896 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002897 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002898 }
2899
2900 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002901 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002902 }
2903
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002904 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002905
2906 private:
2907 DISALLOW_COPY_AND_ASSIGN(HIf);
2908};
2909
David Brazdilfc6a86a2015-06-26 10:33:45 +00002910
2911// Abstract instruction which marks the beginning and/or end of a try block and
2912// links it to the respective exception handlers. Behaves the same as a Goto in
2913// non-exceptional control flow.
2914// Normal-flow successor is stored at index zero, exception handlers under
2915// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002916class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002917 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002918 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002919 kEntry,
2920 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002922 };
2923
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002924 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002925 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2926 SetPackedField<BoundaryKindField>(kind);
2927 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002928
2929 bool IsControlFlow() const OVERRIDE { return true; }
2930
2931 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002932 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002933
David Brazdild26a4112015-11-10 11:07:31 +00002934 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2935 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2936 }
2937
David Brazdilfc6a86a2015-06-26 10:33:45 +00002938 // Returns whether `handler` is among its exception handlers (non-zero index
2939 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002940 bool HasExceptionHandler(const HBasicBlock& handler) const {
2941 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002942 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002943 }
2944
2945 // If not present already, adds `handler` to its block's list of exception
2946 // handlers.
2947 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002948 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002949 GetBlock()->AddSuccessor(handler);
2950 }
2951 }
2952
Vladimir Markoa1de9182016-02-25 11:37:38 +00002953 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2954 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002955
David Brazdilffee3d32015-07-06 11:48:53 +01002956 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2957
David Brazdilfc6a86a2015-06-26 10:33:45 +00002958 DECLARE_INSTRUCTION(TryBoundary);
2959
2960 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002961 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2962 static constexpr size_t kFieldBoundaryKindSize =
2963 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2964 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2965 kFieldBoundaryKind + kFieldBoundaryKindSize;
2966 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2967 "Too many packed fields.");
2968 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002969
2970 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2971};
2972
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002973// Deoptimize to interpreter, upon checking a condition.
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002974class HDeoptimize FINAL : public HVariableInputSizeInstruction {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002975 public:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002976 enum class Kind {
2977 kBCE,
2978 kInline,
2979 kLast = kInline
2980 };
2981
2982 // Use this constructor when the `HDeoptimize` acts as a barrier, where no code can move
2983 // across.
2984 HDeoptimize(ArenaAllocator* arena, HInstruction* cond, Kind kind, uint32_t dex_pc)
2985 : HVariableInputSizeInstruction(
2986 SideEffects::All(),
2987 dex_pc,
2988 arena,
2989 /* number_of_inputs */ 1,
2990 kArenaAllocMisc) {
2991 SetPackedFlag<kFieldCanBeMoved>(false);
2992 SetPackedField<DeoptimizeKindField>(kind);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002993 SetRawInputAt(0, cond);
2994 }
2995
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00002996 // Use this constructor when the `HDeoptimize` guards an instruction, and any user
2997 // that relies on the deoptimization to pass should have its input be the `HDeoptimize`
2998 // instead of `guard`.
2999 // We set CanTriggerGC to prevent any intermediate address to be live
3000 // at the point of the `HDeoptimize`.
3001 HDeoptimize(ArenaAllocator* arena,
3002 HInstruction* cond,
3003 HInstruction* guard,
3004 Kind kind,
3005 uint32_t dex_pc)
3006 : HVariableInputSizeInstruction(
3007 SideEffects::CanTriggerGC(),
3008 dex_pc,
3009 arena,
3010 /* number_of_inputs */ 2,
3011 kArenaAllocMisc) {
3012 SetPackedFlag<kFieldCanBeMoved>(true);
3013 SetPackedField<DeoptimizeKindField>(kind);
3014 SetRawInputAt(0, cond);
3015 SetRawInputAt(1, guard);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01003016 }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003017
3018 bool CanBeMoved() const OVERRIDE { return GetPackedFlag<kFieldCanBeMoved>(); }
3019
3020 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
3021 return (other->CanBeMoved() == CanBeMoved()) && (other->AsDeoptimize()->GetKind() == GetKind());
3022 }
3023
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003024 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003025
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003026 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003028 Kind GetKind() const { return GetPackedField<DeoptimizeKindField>(); }
3029
3030 Primitive::Type GetType() const OVERRIDE {
3031 return GuardsAnInput() ? GuardedInput()->GetType() : Primitive::kPrimVoid;
3032 }
3033
3034 bool GuardsAnInput() const {
3035 return InputCount() == 2;
3036 }
3037
3038 HInstruction* GuardedInput() const {
3039 DCHECK(GuardsAnInput());
3040 return InputAt(1);
3041 }
3042
3043 void RemoveGuard() {
3044 RemoveInputAt(1);
3045 }
3046
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003047 DECLARE_INSTRUCTION(Deoptimize);
3048
3049 private:
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003050 static constexpr size_t kFieldCanBeMoved = kNumberOfGenericPackedBits;
3051 static constexpr size_t kFieldDeoptimizeKind = kNumberOfGenericPackedBits + 1;
3052 static constexpr size_t kFieldDeoptimizeKindSize =
3053 MinimumBitsToStore(static_cast<size_t>(Kind::kLast));
3054 static constexpr size_t kNumberOfDeoptimizePackedBits =
3055 kFieldDeoptimizeKind + kFieldDeoptimizeKindSize;
3056 static_assert(kNumberOfDeoptimizePackedBits <= kMaxNumberOfPackedBits,
3057 "Too many packed fields.");
3058 using DeoptimizeKindField = BitField<Kind, kFieldDeoptimizeKind, kFieldDeoptimizeKindSize>;
3059
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003060 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
3061};
3062
Nicolas Geoffray6f8e2c92017-03-23 14:37:26 +00003063std::ostream& operator<<(std::ostream& os, const HDeoptimize::Kind& rhs);
3064
Mingyao Yang063fc772016-08-02 11:02:54 -07003065// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
3066// The compiled code checks this flag value in a guard before devirtualized call and
3067// if it's true, starts to do deoptimization.
3068// It has a 4-byte slot on stack.
3069// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003070class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07003071 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003072 // CHA guards are only optimized in a separate pass and it has no side effects
3073 // with regard to other passes.
3074 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
3075 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07003076 }
3077
Mingyao Yangb0b051a2016-11-17 09:04:53 -08003078 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
3079
3080 // We do all CHA guard elimination/motion in a single pass, after which there is no
3081 // further guard elimination/motion since a guard might have been used for justification
3082 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
3083 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07003084 bool CanBeMoved() const OVERRIDE { return false; }
3085
3086 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
3087
3088 private:
3089 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
3090};
3091
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003092// Represents the ArtMethod that was passed as a first argument to
3093// the method. It is used by instructions that depend on it, like
3094// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003095class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003096 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003097 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
3098 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003099
3100 DECLARE_INSTRUCTION(CurrentMethod);
3101
3102 private:
3103 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
3104};
3105
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003106// Fetches an ArtMethod from the virtual table or the interface method table
3107// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003108class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003109 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003110 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003111 kVTable,
3112 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003113 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003114 };
3115 HClassTableGet(HInstruction* cls,
3116 Primitive::Type type,
3117 TableKind kind,
3118 size_t index,
3119 uint32_t dex_pc)
3120 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003121 index_(index) {
3122 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003123 SetRawInputAt(0, cls);
3124 }
3125
3126 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003127 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003128 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003129 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003130 }
3131
Vladimir Markoa1de9182016-02-25 11:37:38 +00003132 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003133 size_t GetIndex() const { return index_; }
3134
3135 DECLARE_INSTRUCTION(ClassTableGet);
3136
3137 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003138 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3139 static constexpr size_t kFieldTableKindSize =
3140 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3141 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3142 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3143 "Too many packed fields.");
3144 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3145
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003146 // The index of the ArtMethod in the table.
3147 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003148
3149 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3150};
3151
Mark Mendellfe57faa2015-09-18 09:26:15 -04003152// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3153// have one successor for each entry in the switch table, and the final successor
3154// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003155class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003156 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003157 HPackedSwitch(int32_t start_value,
3158 uint32_t num_entries,
3159 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003160 uint32_t dex_pc = kNoDexPc)
3161 : HTemplateInstruction(SideEffects::None(), dex_pc),
3162 start_value_(start_value),
3163 num_entries_(num_entries) {
3164 SetRawInputAt(0, input);
3165 }
3166
3167 bool IsControlFlow() const OVERRIDE { return true; }
3168
3169 int32_t GetStartValue() const { return start_value_; }
3170
Vladimir Marko211c2112015-09-24 16:52:33 +01003171 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003172
3173 HBasicBlock* GetDefaultBlock() const {
3174 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003175 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003176 }
3177 DECLARE_INSTRUCTION(PackedSwitch);
3178
3179 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003180 const int32_t start_value_;
3181 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003182
3183 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3184};
3185
Roland Levillain88cb1752014-10-20 16:36:47 +01003186class HUnaryOperation : public HExpression<1> {
3187 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003188 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3189 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003190 SetRawInputAt(0, input);
3191 }
3192
3193 HInstruction* GetInput() const { return InputAt(0); }
3194 Primitive::Type GetResultType() const { return GetType(); }
3195
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003196 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003197 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003198 return true;
3199 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003200
Roland Levillain31dd3d62016-02-16 12:21:02 +00003201 // Try to statically evaluate `this` and return a HConstant
3202 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003203 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003204 HConstant* TryStaticEvaluation() const;
3205
3206 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003207 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3208 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003209 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3210 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003211
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003212 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003213
3214 private:
3215 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3216};
3217
Dave Allison20dfc792014-06-16 20:44:29 -07003218class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003219 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003220 HBinaryOperation(Primitive::Type result_type,
3221 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003222 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003223 SideEffects side_effects = SideEffects::None(),
3224 uint32_t dex_pc = kNoDexPc)
3225 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003226 SetRawInputAt(0, left);
3227 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003228 }
3229
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003230 HInstruction* GetLeft() const { return InputAt(0); }
3231 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003232 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003233
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003234 virtual bool IsCommutative() const { return false; }
3235
3236 // Put constant on the right.
3237 // Returns whether order is changed.
3238 bool OrderInputsWithConstantOnTheRight() {
3239 HInstruction* left = InputAt(0);
3240 HInstruction* right = InputAt(1);
3241 if (left->IsConstant() && !right->IsConstant()) {
3242 ReplaceInput(right, 0);
3243 ReplaceInput(left, 1);
3244 return true;
3245 }
3246 return false;
3247 }
3248
3249 // Order inputs by instruction id, but favor constant on the right side.
3250 // This helps GVN for commutative ops.
3251 void OrderInputs() {
3252 DCHECK(IsCommutative());
3253 HInstruction* left = InputAt(0);
3254 HInstruction* right = InputAt(1);
3255 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3256 return;
3257 }
3258 if (OrderInputsWithConstantOnTheRight()) {
3259 return;
3260 }
3261 // Order according to instruction id.
3262 if (left->GetId() > right->GetId()) {
3263 ReplaceInput(right, 0);
3264 ReplaceInput(left, 1);
3265 }
3266 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003267
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003268 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003269 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003270 return true;
3271 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003272
Roland Levillain31dd3d62016-02-16 12:21:02 +00003273 // Try to statically evaluate `this` and return a HConstant
3274 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003275 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003276 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003277
3278 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003279 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3280 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003281 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3282 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003283 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003284 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3285 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003286 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3287 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003288 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3289 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003290 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003291 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3292 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003293
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003294 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003295 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003296 HConstant* GetConstantRight() const;
3297
3298 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003299 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003300 HInstruction* GetLeastConstantLeft() const;
3301
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003302 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003303
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003304 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003305 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3306};
3307
Mark Mendellc4701932015-04-10 13:18:51 -04003308// The comparison bias applies for floating point operations and indicates how NaN
3309// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003310enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003311 kNoBias, // bias is not applicable (i.e. for long operation)
3312 kGtBias, // return 1 for NaN comparisons
3313 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003314 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003315};
3316
Roland Levillain31dd3d62016-02-16 12:21:02 +00003317std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3318
Dave Allison20dfc792014-06-16 20:44:29 -07003319class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003320 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003321 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003322 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3323 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3324 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003325
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003326 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003327 // `instruction`, and disregard moves in between.
3328 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003329
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003330 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003331
3332 virtual IfCondition GetCondition() const = 0;
3333
Mark Mendellc4701932015-04-10 13:18:51 -04003334 virtual IfCondition GetOppositeCondition() const = 0;
3335
Vladimir Markoa1de9182016-02-25 11:37:38 +00003336 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003337 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3338
Vladimir Markoa1de9182016-02-25 11:37:38 +00003339 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3340 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003341
Vladimir Marko372f10e2016-05-17 16:30:10 +01003342 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003343 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003344 }
3345
Roland Levillain4fa13f62015-07-06 18:11:54 +01003346 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003347 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003348 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003349 if (if_cond == kCondNE) {
3350 return true;
3351 } else if (if_cond == kCondEQ) {
3352 return false;
3353 }
3354 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003355 }
3356
3357 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003358 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003359 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003360 if (if_cond == kCondEQ) {
3361 return true;
3362 } else if (if_cond == kCondNE) {
3363 return false;
3364 }
3365 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003366 }
3367
Roland Levillain31dd3d62016-02-16 12:21:02 +00003368 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003369 // Needed if we merge a HCompare into a HCondition.
3370 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3371 static constexpr size_t kFieldComparisonBiasSize =
3372 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3373 static constexpr size_t kNumberOfConditionPackedBits =
3374 kFieldComparisonBias + kFieldComparisonBiasSize;
3375 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3376 using ComparisonBiasField =
3377 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3378
Roland Levillain31dd3d62016-02-16 12:21:02 +00003379 template <typename T>
3380 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3381
3382 template <typename T>
3383 int32_t CompareFP(T x, T y) const {
3384 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3385 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3386 // Handle the bias.
3387 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3388 }
3389
3390 // Return an integer constant containing the result of a condition evaluated at compile time.
3391 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3392 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3393 }
3394
Dave Allison20dfc792014-06-16 20:44:29 -07003395 private:
3396 DISALLOW_COPY_AND_ASSIGN(HCondition);
3397};
3398
3399// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003400class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003401 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003402 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3403 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003404
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003405 bool IsCommutative() const OVERRIDE { return true; }
3406
Vladimir Marko9e23df52015-11-10 17:14:35 +00003407 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3408 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003409 return MakeConstantCondition(true, GetDexPc());
3410 }
3411 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3412 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3413 }
3414 // In the following Evaluate methods, a HCompare instruction has
3415 // been merged into this HEqual instruction; evaluate it as
3416 // `Compare(x, y) == 0`.
3417 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3418 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3419 GetDexPc());
3420 }
3421 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3422 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3423 }
3424 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3425 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003426 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003427
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003428 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003429
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003430 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003431 return kCondEQ;
3432 }
3433
Mark Mendellc4701932015-04-10 13:18:51 -04003434 IfCondition GetOppositeCondition() const OVERRIDE {
3435 return kCondNE;
3436 }
3437
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003438 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003439 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003440
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003441 DISALLOW_COPY_AND_ASSIGN(HEqual);
3442};
3443
Vladimir Markofcb503c2016-05-18 12:48:17 +01003444class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003445 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003446 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3447 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003448
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003449 bool IsCommutative() const OVERRIDE { return true; }
3450
Vladimir Marko9e23df52015-11-10 17:14:35 +00003451 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3452 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003453 return MakeConstantCondition(false, GetDexPc());
3454 }
3455 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3456 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3457 }
3458 // In the following Evaluate methods, a HCompare instruction has
3459 // been merged into this HNotEqual instruction; evaluate it as
3460 // `Compare(x, y) != 0`.
3461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3462 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3463 }
3464 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3465 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3466 }
3467 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3468 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003469 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003470
Dave Allison20dfc792014-06-16 20:44:29 -07003471 DECLARE_INSTRUCTION(NotEqual);
3472
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003473 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003474 return kCondNE;
3475 }
3476
Mark Mendellc4701932015-04-10 13:18:51 -04003477 IfCondition GetOppositeCondition() const OVERRIDE {
3478 return kCondEQ;
3479 }
3480
Dave Allison20dfc792014-06-16 20:44:29 -07003481 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003482 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003483
Dave Allison20dfc792014-06-16 20:44:29 -07003484 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3485};
3486
Vladimir Markofcb503c2016-05-18 12:48:17 +01003487class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003488 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003489 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3490 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003491
Roland Levillain9867bc72015-08-05 10:21:34 +01003492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003493 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003494 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 // In the following Evaluate methods, a HCompare instruction has
3496 // been merged into this HLessThan instruction; evaluate it as
3497 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003499 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3500 }
3501 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3502 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3503 }
3504 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3505 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003506 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003507
Dave Allison20dfc792014-06-16 20:44:29 -07003508 DECLARE_INSTRUCTION(LessThan);
3509
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003510 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003511 return kCondLT;
3512 }
3513
Mark Mendellc4701932015-04-10 13:18:51 -04003514 IfCondition GetOppositeCondition() const OVERRIDE {
3515 return kCondGE;
3516 }
3517
Dave Allison20dfc792014-06-16 20:44:29 -07003518 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003519 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003520
Dave Allison20dfc792014-06-16 20:44:29 -07003521 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3522};
3523
Vladimir Markofcb503c2016-05-18 12:48:17 +01003524class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003525 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003526 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3527 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003528
Roland Levillain9867bc72015-08-05 10:21:34 +01003529 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003530 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003531 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 // In the following Evaluate methods, a HCompare instruction has
3533 // been merged into this HLessThanOrEqual instruction; evaluate it as
3534 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003535 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3537 }
3538 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3539 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3540 }
3541 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3542 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003543 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003544
Dave Allison20dfc792014-06-16 20:44:29 -07003545 DECLARE_INSTRUCTION(LessThanOrEqual);
3546
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003547 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003548 return kCondLE;
3549 }
3550
Mark Mendellc4701932015-04-10 13:18:51 -04003551 IfCondition GetOppositeCondition() const OVERRIDE {
3552 return kCondGT;
3553 }
3554
Dave Allison20dfc792014-06-16 20:44:29 -07003555 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003556 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003557
Dave Allison20dfc792014-06-16 20:44:29 -07003558 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3559};
3560
Vladimir Markofcb503c2016-05-18 12:48:17 +01003561class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003562 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003563 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3564 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003565
Roland Levillain9867bc72015-08-05 10:21:34 +01003566 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003567 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003568 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003569 // In the following Evaluate methods, a HCompare instruction has
3570 // been merged into this HGreaterThan instruction; evaluate it as
3571 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003572 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003573 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3574 }
3575 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3576 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3577 }
3578 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3579 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003580 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003581
Dave Allison20dfc792014-06-16 20:44:29 -07003582 DECLARE_INSTRUCTION(GreaterThan);
3583
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003584 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003585 return kCondGT;
3586 }
3587
Mark Mendellc4701932015-04-10 13:18:51 -04003588 IfCondition GetOppositeCondition() const OVERRIDE {
3589 return kCondLE;
3590 }
3591
Dave Allison20dfc792014-06-16 20:44:29 -07003592 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003593 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003594
Dave Allison20dfc792014-06-16 20:44:29 -07003595 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3596};
3597
Vladimir Markofcb503c2016-05-18 12:48:17 +01003598class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003599 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003600 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3601 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003602
Roland Levillain9867bc72015-08-05 10:21:34 +01003603 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003605 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003606 // In the following Evaluate methods, a HCompare instruction has
3607 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3608 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003609 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003610 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3611 }
3612 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3613 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3614 }
3615 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3616 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003617 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003618
Dave Allison20dfc792014-06-16 20:44:29 -07003619 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3620
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003621 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003622 return kCondGE;
3623 }
3624
Mark Mendellc4701932015-04-10 13:18:51 -04003625 IfCondition GetOppositeCondition() const OVERRIDE {
3626 return kCondLT;
3627 }
3628
Dave Allison20dfc792014-06-16 20:44:29 -07003629 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003630 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003631
Dave Allison20dfc792014-06-16 20:44:29 -07003632 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3633};
3634
Vladimir Markofcb503c2016-05-18 12:48:17 +01003635class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003636 public:
3637 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3638 : HCondition(first, second, dex_pc) {}
3639
3640 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003641 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003642 }
3643 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003644 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3645 }
3646 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3647 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3648 LOG(FATAL) << DebugName() << " is not defined for float values";
3649 UNREACHABLE();
3650 }
3651 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3652 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3653 LOG(FATAL) << DebugName() << " is not defined for double values";
3654 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003655 }
3656
3657 DECLARE_INSTRUCTION(Below);
3658
3659 IfCondition GetCondition() const OVERRIDE {
3660 return kCondB;
3661 }
3662
3663 IfCondition GetOppositeCondition() const OVERRIDE {
3664 return kCondAE;
3665 }
3666
3667 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003668 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003669 return MakeUnsigned(x) < MakeUnsigned(y);
3670 }
Aart Bike9f37602015-10-09 11:15:55 -07003671
3672 DISALLOW_COPY_AND_ASSIGN(HBelow);
3673};
3674
Vladimir Markofcb503c2016-05-18 12:48:17 +01003675class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003676 public:
3677 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3678 : HCondition(first, second, dex_pc) {}
3679
3680 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003681 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003682 }
3683 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3685 }
3686 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3687 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3688 LOG(FATAL) << DebugName() << " is not defined for float values";
3689 UNREACHABLE();
3690 }
3691 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3692 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3693 LOG(FATAL) << DebugName() << " is not defined for double values";
3694 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003695 }
3696
3697 DECLARE_INSTRUCTION(BelowOrEqual);
3698
3699 IfCondition GetCondition() const OVERRIDE {
3700 return kCondBE;
3701 }
3702
3703 IfCondition GetOppositeCondition() const OVERRIDE {
3704 return kCondA;
3705 }
3706
3707 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003708 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 return MakeUnsigned(x) <= MakeUnsigned(y);
3710 }
Aart Bike9f37602015-10-09 11:15:55 -07003711
3712 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3713};
3714
Vladimir Markofcb503c2016-05-18 12:48:17 +01003715class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003716 public:
3717 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3718 : HCondition(first, second, dex_pc) {}
3719
3720 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003721 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003722 }
3723 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3725 }
3726 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3727 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3728 LOG(FATAL) << DebugName() << " is not defined for float values";
3729 UNREACHABLE();
3730 }
3731 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3732 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3733 LOG(FATAL) << DebugName() << " is not defined for double values";
3734 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003735 }
3736
3737 DECLARE_INSTRUCTION(Above);
3738
3739 IfCondition GetCondition() const OVERRIDE {
3740 return kCondA;
3741 }
3742
3743 IfCondition GetOppositeCondition() const OVERRIDE {
3744 return kCondBE;
3745 }
3746
3747 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003748 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 return MakeUnsigned(x) > MakeUnsigned(y);
3750 }
Aart Bike9f37602015-10-09 11:15:55 -07003751
3752 DISALLOW_COPY_AND_ASSIGN(HAbove);
3753};
3754
Vladimir Markofcb503c2016-05-18 12:48:17 +01003755class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003756 public:
3757 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3758 : HCondition(first, second, dex_pc) {}
3759
3760 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003762 }
3763 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003764 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3765 }
3766 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3767 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3768 LOG(FATAL) << DebugName() << " is not defined for float values";
3769 UNREACHABLE();
3770 }
3771 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3772 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3773 LOG(FATAL) << DebugName() << " is not defined for double values";
3774 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003775 }
3776
3777 DECLARE_INSTRUCTION(AboveOrEqual);
3778
3779 IfCondition GetCondition() const OVERRIDE {
3780 return kCondAE;
3781 }
3782
3783 IfCondition GetOppositeCondition() const OVERRIDE {
3784 return kCondB;
3785 }
3786
3787 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003788 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003789 return MakeUnsigned(x) >= MakeUnsigned(y);
3790 }
Aart Bike9f37602015-10-09 11:15:55 -07003791
3792 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3793};
Dave Allison20dfc792014-06-16 20:44:29 -07003794
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003795// Instruction to check how two inputs compare to each other.
3796// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003797class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003798 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003799 // Note that `comparison_type` is the type of comparison performed
3800 // between the comparison's inputs, not the type of the instantiated
3801 // HCompare instruction (which is always Primitive::kPrimInt).
3802 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003803 HInstruction* first,
3804 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003805 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003806 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003807 : HBinaryOperation(Primitive::kPrimInt,
3808 first,
3809 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003810 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003811 dex_pc) {
3812 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003813 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3814 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003815 }
3816
Roland Levillain9867bc72015-08-05 10:21:34 +01003817 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003818 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3819
3820 template <typename T>
3821 int32_t ComputeFP(T x, T y) const {
3822 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3823 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3824 // Handle the bias.
3825 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3826 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003827
Roland Levillain9867bc72015-08-05 10:21:34 +01003828 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003829 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3830 // reach this code path when processing a freshly built HIR
3831 // graph. However HCompare integer instructions can be synthesized
3832 // by the instruction simplifier to implement IntegerCompare and
3833 // IntegerSignum intrinsics, so we have to handle this case.
3834 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003835 }
3836 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003837 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3838 }
3839 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3840 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3841 }
3842 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3843 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003844 }
3845
Vladimir Marko372f10e2016-05-17 16:30:10 +01003846 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003847 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003848 }
3849
Vladimir Markoa1de9182016-02-25 11:37:38 +00003850 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003851
Roland Levillain31dd3d62016-02-16 12:21:02 +00003852 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003853 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003854 bool IsGtBias() const {
3855 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003856 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003857 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003858
Roland Levillain1693a1f2016-03-15 14:57:31 +00003859 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3860 // Comparisons do not require a runtime call in any back end.
3861 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003862 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003863
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003864 DECLARE_INSTRUCTION(Compare);
3865
Roland Levillain31dd3d62016-02-16 12:21:02 +00003866 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003867 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3868 static constexpr size_t kFieldComparisonBiasSize =
3869 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3870 static constexpr size_t kNumberOfComparePackedBits =
3871 kFieldComparisonBias + kFieldComparisonBiasSize;
3872 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3873 using ComparisonBiasField =
3874 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3875
Roland Levillain31dd3d62016-02-16 12:21:02 +00003876 // Return an integer constant containing the result of a comparison evaluated at compile time.
3877 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3878 DCHECK(value == -1 || value == 0 || value == 1) << value;
3879 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3880 }
3881
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003882 private:
3883 DISALLOW_COPY_AND_ASSIGN(HCompare);
3884};
3885
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003886class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003887 public:
3888 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003889 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003890 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003891 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003892 bool finalizable,
3893 QuickEntrypointEnum entrypoint)
3894 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3895 type_index_(type_index),
3896 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003897 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003898 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003899 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003900 }
3901
Andreas Gampea5b09a62016-11-17 15:21:22 -08003902 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003903 const DexFile& GetDexFile() const { return dex_file_; }
3904
3905 // Calls runtime so needs an environment.
3906 bool NeedsEnvironment() const OVERRIDE { return true; }
3907
Mingyao Yang062157f2016-03-02 10:15:36 -08003908 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3909 bool CanThrow() const OVERRIDE { return true; }
3910
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003911 bool NeedsChecks() const {
3912 return entrypoint_ == kQuickAllocObjectWithChecks;
3913 }
David Brazdil6de19382016-01-08 17:37:10 +00003914
Vladimir Markoa1de9182016-02-25 11:37:38 +00003915 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003916
3917 bool CanBeNull() const OVERRIDE { return false; }
3918
3919 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3920
3921 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3922 entrypoint_ = entrypoint;
3923 }
3924
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003925 HLoadClass* GetLoadClass() const {
3926 HInstruction* input = InputAt(0);
3927 if (input->IsClinitCheck()) {
3928 input = input->InputAt(0);
3929 }
3930 DCHECK(input->IsLoadClass());
3931 return input->AsLoadClass();
3932 }
3933
David Brazdil6de19382016-01-08 17:37:10 +00003934 bool IsStringAlloc() const;
3935
3936 DECLARE_INSTRUCTION(NewInstance);
3937
3938 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003939 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003940 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3941 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3942 "Too many packed fields.");
3943
Andreas Gampea5b09a62016-11-17 15:21:22 -08003944 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003945 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003946 QuickEntrypointEnum entrypoint_;
3947
3948 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3949};
3950
Agi Csaki05f20562015-08-19 14:58:14 -07003951enum IntrinsicNeedsEnvironmentOrCache {
3952 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3953 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003954};
3955
Aart Bik5d75afe2015-12-14 11:57:01 -08003956enum IntrinsicSideEffects {
3957 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3958 kReadSideEffects, // Intrinsic may read heap memory.
3959 kWriteSideEffects, // Intrinsic may write heap memory.
3960 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3961};
3962
3963enum IntrinsicExceptions {
3964 kNoThrow, // Intrinsic does not throw any exceptions.
3965 kCanThrow // Intrinsic may throw exceptions.
3966};
3967
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003968class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003969 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003970 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003971
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003972 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003973 SetRawInputAt(index, argument);
3974 }
3975
Roland Levillain3e3d7332015-04-28 11:00:54 +01003976 // Return the number of arguments. This number can be lower than
3977 // the number of inputs returned by InputCount(), as some invoke
3978 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3979 // inputs at the end of their list of inputs.
3980 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3981
Vladimir Markoa1de9182016-02-25 11:37:38 +00003982 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003983
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003984 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3985
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003986 InvokeType GetInvokeType() const {
3987 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003988 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003989
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003990 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003991 return intrinsic_;
3992 }
3993
Aart Bik5d75afe2015-12-14 11:57:01 -08003994 void SetIntrinsic(Intrinsics intrinsic,
3995 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3996 IntrinsicSideEffects side_effects,
3997 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003998
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003999 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00004000 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004001 }
4002
Aart Bikff7d89c2016-11-07 08:49:28 -08004003 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
4004
Vladimir Markoa1de9182016-02-25 11:37:38 +00004005 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004006
Aart Bik71bf7b42016-11-16 10:17:46 -08004007 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08004008
Vladimir Marko372f10e2016-05-17 16:30:10 +01004009 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08004010 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
4011 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01004012
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004013 uint32_t* GetIntrinsicOptimizations() {
4014 return &intrinsic_optimizations_;
4015 }
4016
4017 const uint32_t* GetIntrinsicOptimizations() const {
4018 return &intrinsic_optimizations_;
4019 }
4020
4021 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
4022
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004023 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004024 void SetResolvedMethod(ArtMethod* method) { resolved_method_ = method; }
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004025
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00004026 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01004027
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004028 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004029 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
4030 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00004031 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
4032 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004033 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004034 static constexpr size_t kFieldReturnTypeSize =
4035 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
4036 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
4037 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
4038 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004039 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004040 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
4041
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004042 HInvoke(ArenaAllocator* arena,
4043 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01004044 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004045 Primitive::Type return_type,
4046 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01004047 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004048 ArtMethod* resolved_method,
4049 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08004050 : HVariableInputSizeInstruction(
4051 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
4052 dex_pc,
4053 arena,
4054 number_of_arguments + number_of_other_inputs,
4055 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004056 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004057 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004058 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07004059 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004060 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004061 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004062 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00004063 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004064 }
4065
Roland Levillain3e3d7332015-04-28 11:00:54 +01004066 uint32_t number_of_arguments_;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +00004067 ArtMethod* resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004068 const uint32_t dex_method_index_;
4069 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01004070
4071 // A magic word holding optimizations for intrinsics. See intrinsics.h.
4072 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004073
4074 private:
4075 DISALLOW_COPY_AND_ASSIGN(HInvoke);
4076};
4077
Vladimir Markofcb503c2016-05-18 12:48:17 +01004078class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01004079 public:
4080 HInvokeUnresolved(ArenaAllocator* arena,
4081 uint32_t number_of_arguments,
4082 Primitive::Type return_type,
4083 uint32_t dex_pc,
4084 uint32_t dex_method_index,
4085 InvokeType invoke_type)
4086 : HInvoke(arena,
4087 number_of_arguments,
4088 0u /* number_of_other_inputs */,
4089 return_type,
4090 dex_pc,
4091 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004092 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01004093 invoke_type) {
4094 }
4095
4096 DECLARE_INSTRUCTION(InvokeUnresolved);
4097
4098 private:
4099 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
4100};
4101
Orion Hodsonac141392017-01-13 11:53:47 +00004102class HInvokePolymorphic FINAL : public HInvoke {
4103 public:
4104 HInvokePolymorphic(ArenaAllocator* arena,
4105 uint32_t number_of_arguments,
4106 Primitive::Type return_type,
4107 uint32_t dex_pc,
4108 uint32_t dex_method_index)
4109 : HInvoke(arena,
4110 number_of_arguments,
4111 0u /* number_of_other_inputs */,
4112 return_type,
4113 dex_pc,
4114 dex_method_index,
4115 nullptr,
4116 kVirtual) {}
4117
4118 DECLARE_INSTRUCTION(InvokePolymorphic);
4119
4120 private:
4121 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4122};
4123
Vladimir Markofcb503c2016-05-18 12:48:17 +01004124class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004125 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004126 // Requirements of this method call regarding the class
4127 // initialization (clinit) check of its declaring class.
4128 enum class ClinitCheckRequirement {
4129 kNone, // Class already initialized.
4130 kExplicit, // Static call having explicit clinit check as last input.
4131 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004132 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004133 };
4134
Vladimir Marko58155012015-08-19 12:49:41 +00004135 // Determines how to load the target ArtMethod*.
4136 enum class MethodLoadKind {
4137 // Use a String init ArtMethod* loaded from Thread entrypoints.
4138 kStringInit,
4139
4140 // Use the method's own ArtMethod* loaded by the register allocator.
4141 kRecursive,
4142
4143 // Use ArtMethod* at a known address, embed the direct address in the code.
4144 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4145 kDirectAddress,
4146
Vladimir Markoa1de9182016-02-25 11:37:38 +00004147 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004148 // Used when we need to use the dex cache, for example for invoke-static that
4149 // may cause class initialization (the entry may point to a resolution method),
4150 // and we know that we can access the dex cache arrays using a PC-relative load.
4151 kDexCachePcRelative,
4152
4153 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4154 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4155 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4156 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4157 kDexCacheViaMethod,
4158 };
4159
4160 // Determines the location of the code pointer.
4161 enum class CodePtrLocation {
4162 // Recursive call, use local PC-relative call instruction.
4163 kCallSelf,
4164
Vladimir Marko58155012015-08-19 12:49:41 +00004165 // Use code pointer from the ArtMethod*.
4166 // Used when we don't know the target code. This is also the last-resort-kind used when
4167 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4168 kCallArtMethod,
4169 };
4170
4171 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004172 MethodLoadKind method_load_kind;
4173 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004174 // The method load data holds
4175 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4176 // Note that there are multiple string init methods, each having its own offset.
4177 // - the method address for kDirectAddress
4178 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004179 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004180 };
4181
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004182 HInvokeStaticOrDirect(ArenaAllocator* arena,
4183 uint32_t number_of_arguments,
4184 Primitive::Type return_type,
4185 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004186 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004187 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004188 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004189 InvokeType invoke_type,
4190 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004191 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004192 : HInvoke(arena,
4193 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004194 // There is potentially one extra argument for the HCurrentMethod node, and
4195 // potentially one other if the clinit check is explicit, and potentially
4196 // one other if the method is a string factory.
4197 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004198 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004199 return_type,
4200 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004201 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004202 resolved_method,
4203 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004204 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004205 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004206 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4207 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004208
Vladimir Markodc151b22015-10-15 18:02:30 +01004209 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004210 bool had_current_method_input = HasCurrentMethodInput();
4211 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4212
4213 // Using the current method is the default and once we find a better
4214 // method load kind, we should not go back to using the current method.
4215 DCHECK(had_current_method_input || !needs_current_method_input);
4216
4217 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004218 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4219 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004220 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004221 dispatch_info_ = dispatch_info;
4222 }
4223
Aart Bik6daebeb2017-04-03 14:35:41 -07004224 DispatchInfo GetDispatchInfo() const {
4225 return dispatch_info_;
4226 }
4227
Vladimir Markoc53c0792015-11-19 15:48:33 +00004228 void AddSpecialInput(HInstruction* input) {
4229 // We allow only one special input.
4230 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4231 DCHECK(InputCount() == GetSpecialInputIndex() ||
4232 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4233 InsertInputAt(GetSpecialInputIndex(), input);
4234 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004235
Vladimir Marko372f10e2016-05-17 16:30:10 +01004236 using HInstruction::GetInputRecords; // Keep the const version visible.
4237 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4238 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4239 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4240 DCHECK(!input_records.empty());
4241 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4242 HInstruction* last_input = input_records.back().GetInstruction();
4243 // Note: `last_input` may be null during arguments setup.
4244 if (last_input != nullptr) {
4245 // `last_input` is the last input of a static invoke marked as having
4246 // an explicit clinit check. It must either be:
4247 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4248 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4249 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4250 }
4251 }
4252 return input_records;
4253 }
4254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004255 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004256 // We access the method via the dex cache so we can't do an implicit null check.
4257 // TODO: for intrinsics we can generate implicit null checks.
4258 return false;
4259 }
4260
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004261 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004262 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004263 }
4264
Vladimir Markoc53c0792015-11-19 15:48:33 +00004265 // Get the index of the special input, if any.
4266 //
David Brazdil6de19382016-01-08 17:37:10 +00004267 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4268 // method pointer; otherwise there may be one platform-specific special input,
4269 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004270 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004271 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004272
Vladimir Marko58155012015-08-19 12:49:41 +00004273 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4274 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4275 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004276 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004277 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004278 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004279 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004280 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4281 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004282 bool HasCurrentMethodInput() const {
4283 // This function can be called only after the invoke has been fully initialized by the builder.
4284 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004285 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004286 return true;
4287 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004288 DCHECK(InputCount() == GetSpecialInputIndex() ||
4289 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004290 return false;
4291 }
4292 }
Vladimir Marko58155012015-08-19 12:49:41 +00004293
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004294 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004295 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004296 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004297 }
4298
4299 uint64_t GetMethodAddress() const {
4300 DCHECK(HasMethodAddress());
4301 return dispatch_info_.method_load_data;
4302 }
4303
4304 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004305 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004306 return dispatch_info_.method_load_data;
4307 }
4308
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004309 const DexFile& GetDexFileForPcRelativeDexCache() const;
4310
Vladimir Markoa1de9182016-02-25 11:37:38 +00004311 ClinitCheckRequirement GetClinitCheckRequirement() const {
4312 return GetPackedField<ClinitCheckRequirementField>();
4313 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004314
Roland Levillain4c0eb422015-04-24 16:43:49 +01004315 // Is this instruction a call to a static method?
4316 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004317 return GetInvokeType() == kStatic;
4318 }
4319
4320 MethodReference GetTargetMethod() const {
4321 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004322 }
4323
Vladimir Markofbb184a2015-11-13 14:47:00 +00004324 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4325 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4326 // instruction; only relevant for static calls with explicit clinit check.
4327 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004328 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004329 size_t last_input_index = inputs_.size() - 1u;
4330 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004331 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004332 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004333 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004334 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004335 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004336 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004337 }
4338
4339 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004340 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004341 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004342 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004343 }
4344
4345 // Is this a call to a static method whose declaring class has an
4346 // implicit intialization check requirement?
4347 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004348 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004349 }
4350
Vladimir Markob554b5a2015-11-06 12:57:55 +00004351 // Does this method load kind need the current method as an input?
4352 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4353 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4354 }
4355
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004356 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004357
4358 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004359 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004360 static constexpr size_t kFieldClinitCheckRequirementSize =
4361 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4362 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4363 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4364 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4365 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004366 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4367 kFieldClinitCheckRequirement,
4368 kFieldClinitCheckRequirementSize>;
4369
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004370 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004371 MethodReference target_method_;
4372 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004373
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004374 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004375};
Vladimir Markof64242a2015-12-01 14:58:23 +00004376std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004377std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004378
Vladimir Markofcb503c2016-05-18 12:48:17 +01004379class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004380 public:
4381 HInvokeVirtual(ArenaAllocator* arena,
4382 uint32_t number_of_arguments,
4383 Primitive::Type return_type,
4384 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004385 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004386 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004387 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004388 : HInvoke(arena,
4389 number_of_arguments,
4390 0u,
4391 return_type,
4392 dex_pc,
4393 dex_method_index,
4394 resolved_method,
4395 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004396 vtable_index_(vtable_index) {}
4397
Aart Bik71bf7b42016-11-16 10:17:46 -08004398 bool CanBeNull() const OVERRIDE {
4399 switch (GetIntrinsic()) {
4400 case Intrinsics::kThreadCurrentThread:
4401 case Intrinsics::kStringBufferAppend:
4402 case Intrinsics::kStringBufferToString:
4403 case Intrinsics::kStringBuilderAppend:
4404 case Intrinsics::kStringBuilderToString:
4405 return false;
4406 default:
4407 return HInvoke::CanBeNull();
4408 }
4409 }
4410
Calin Juravle641547a2015-04-21 22:08:51 +01004411 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004412 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004413 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004414 }
4415
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004416 uint32_t GetVTableIndex() const { return vtable_index_; }
4417
4418 DECLARE_INSTRUCTION(InvokeVirtual);
4419
4420 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004421 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004422 const uint32_t vtable_index_;
4423
4424 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4425};
4426
Vladimir Markofcb503c2016-05-18 12:48:17 +01004427class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004428 public:
4429 HInvokeInterface(ArenaAllocator* arena,
4430 uint32_t number_of_arguments,
4431 Primitive::Type return_type,
4432 uint32_t dex_pc,
4433 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004434 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004435 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004436 : HInvoke(arena,
4437 number_of_arguments,
4438 0u,
4439 return_type,
4440 dex_pc,
4441 dex_method_index,
4442 resolved_method,
4443 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004444 imt_index_(imt_index) {}
4445
Calin Juravle641547a2015-04-21 22:08:51 +01004446 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004447 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004448 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004449 }
4450
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004451 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4452 // The assembly stub currently needs it.
4453 return true;
4454 }
4455
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004456 uint32_t GetImtIndex() const { return imt_index_; }
4457 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4458
4459 DECLARE_INSTRUCTION(InvokeInterface);
4460
4461 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004462 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004463 const uint32_t imt_index_;
4464
4465 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4466};
4467
Vladimir Markofcb503c2016-05-18 12:48:17 +01004468class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004469 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004470 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004471 : HUnaryOperation(result_type, input, dex_pc) {
4472 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4473 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004474
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004475 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004476
4477 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004478 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004479 }
4480 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004481 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004482 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004483 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4484 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4485 }
4486 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4487 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4488 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004489
Roland Levillain88cb1752014-10-20 16:36:47 +01004490 DECLARE_INSTRUCTION(Neg);
4491
4492 private:
4493 DISALLOW_COPY_AND_ASSIGN(HNeg);
4494};
4495
Vladimir Markofcb503c2016-05-18 12:48:17 +01004496class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004497 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004498 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4499 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4500 SetRawInputAt(0, cls);
4501 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004502 }
4503
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004504 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004505 bool NeedsEnvironment() const OVERRIDE { return true; }
4506
Mingyao Yang0c365e62015-03-31 15:09:29 -07004507 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4508 bool CanThrow() const OVERRIDE { return true; }
4509
Calin Juravle10e244f2015-01-26 18:54:32 +00004510 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004511
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004512 HLoadClass* GetLoadClass() const {
4513 DCHECK(InputAt(0)->IsLoadClass());
4514 return InputAt(0)->AsLoadClass();
4515 }
4516
4517 HInstruction* GetLength() const {
4518 return InputAt(1);
4519 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004520
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004521 DECLARE_INSTRUCTION(NewArray);
4522
4523 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004524 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4525};
4526
Vladimir Markofcb503c2016-05-18 12:48:17 +01004527class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004528 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004529 HAdd(Primitive::Type result_type,
4530 HInstruction* left,
4531 HInstruction* right,
4532 uint32_t dex_pc = kNoDexPc)
4533 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004534
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004535 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004536
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004537 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004538
4539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 return GetBlock()->GetGraph()->GetIntConstant(
4541 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004542 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 return GetBlock()->GetGraph()->GetLongConstant(
4545 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004546 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004547 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4548 return GetBlock()->GetGraph()->GetFloatConstant(
4549 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4550 }
4551 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4552 return GetBlock()->GetGraph()->GetDoubleConstant(
4553 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4554 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004555
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004556 DECLARE_INSTRUCTION(Add);
4557
4558 private:
4559 DISALLOW_COPY_AND_ASSIGN(HAdd);
4560};
4561
Vladimir Markofcb503c2016-05-18 12:48:17 +01004562class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004563 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004564 HSub(Primitive::Type result_type,
4565 HInstruction* left,
4566 HInstruction* right,
4567 uint32_t dex_pc = kNoDexPc)
4568 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004569
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004570 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004571
4572 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004573 return GetBlock()->GetGraph()->GetIntConstant(
4574 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004575 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004576 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004577 return GetBlock()->GetGraph()->GetLongConstant(
4578 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004579 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004580 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4581 return GetBlock()->GetGraph()->GetFloatConstant(
4582 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4583 }
4584 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4585 return GetBlock()->GetGraph()->GetDoubleConstant(
4586 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4587 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004588
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004589 DECLARE_INSTRUCTION(Sub);
4590
4591 private:
4592 DISALLOW_COPY_AND_ASSIGN(HSub);
4593};
4594
Vladimir Markofcb503c2016-05-18 12:48:17 +01004595class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004596 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004597 HMul(Primitive::Type result_type,
4598 HInstruction* left,
4599 HInstruction* right,
4600 uint32_t dex_pc = kNoDexPc)
4601 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004602
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004603 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004604
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004605 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004606
4607 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004608 return GetBlock()->GetGraph()->GetIntConstant(
4609 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004610 }
4611 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004612 return GetBlock()->GetGraph()->GetLongConstant(
4613 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004614 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004615 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4616 return GetBlock()->GetGraph()->GetFloatConstant(
4617 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4618 }
4619 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4620 return GetBlock()->GetGraph()->GetDoubleConstant(
4621 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4622 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004623
4624 DECLARE_INSTRUCTION(Mul);
4625
4626 private:
4627 DISALLOW_COPY_AND_ASSIGN(HMul);
4628};
4629
Vladimir Markofcb503c2016-05-18 12:48:17 +01004630class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004631 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004632 HDiv(Primitive::Type result_type,
4633 HInstruction* left,
4634 HInstruction* right,
4635 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004636 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004637
Roland Levillain9867bc72015-08-05 10:21:34 +01004638 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004639 T ComputeIntegral(T x, T y) const {
4640 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004641 // Our graph structure ensures we never have 0 for `y` during
4642 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004643 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004644 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004645 return (y == -1) ? -x : x / y;
4646 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004647
Roland Levillain31dd3d62016-02-16 12:21:02 +00004648 template <typename T>
4649 T ComputeFP(T x, T y) const {
4650 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4651 return x / y;
4652 }
4653
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004655 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004656 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004657 }
4658 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004659 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004660 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4661 }
4662 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4663 return GetBlock()->GetGraph()->GetFloatConstant(
4664 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4665 }
4666 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4667 return GetBlock()->GetGraph()->GetDoubleConstant(
4668 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004669 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004670
4671 DECLARE_INSTRUCTION(Div);
4672
4673 private:
4674 DISALLOW_COPY_AND_ASSIGN(HDiv);
4675};
4676
Vladimir Markofcb503c2016-05-18 12:48:17 +01004677class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004678 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004679 HRem(Primitive::Type result_type,
4680 HInstruction* left,
4681 HInstruction* right,
4682 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004683 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004684
Roland Levillain9867bc72015-08-05 10:21:34 +01004685 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004686 T ComputeIntegral(T x, T y) const {
4687 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004688 // Our graph structure ensures we never have 0 for `y` during
4689 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004690 DCHECK_NE(y, 0);
4691 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4692 return (y == -1) ? 0 : x % y;
4693 }
4694
Roland Levillain31dd3d62016-02-16 12:21:02 +00004695 template <typename T>
4696 T ComputeFP(T x, T y) const {
4697 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4698 return std::fmod(x, y);
4699 }
4700
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004702 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004703 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004704 }
4705 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004706 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004707 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004708 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004709 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4710 return GetBlock()->GetGraph()->GetFloatConstant(
4711 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4712 }
4713 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4714 return GetBlock()->GetGraph()->GetDoubleConstant(
4715 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4716 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004717
4718 DECLARE_INSTRUCTION(Rem);
4719
4720 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004721 DISALLOW_COPY_AND_ASSIGN(HRem);
4722};
4723
Vladimir Markofcb503c2016-05-18 12:48:17 +01004724class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004725 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004726 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4727 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004728 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004729 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004730 SetRawInputAt(0, value);
4731 }
4732
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004733 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4734
Calin Juravled0d48522014-11-04 16:40:20 +00004735 bool CanBeMoved() const OVERRIDE { return true; }
4736
Vladimir Marko372f10e2016-05-17 16:30:10 +01004737 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004738 return true;
4739 }
4740
4741 bool NeedsEnvironment() const OVERRIDE { return true; }
4742 bool CanThrow() const OVERRIDE { return true; }
4743
Calin Juravled0d48522014-11-04 16:40:20 +00004744 DECLARE_INSTRUCTION(DivZeroCheck);
4745
4746 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004747 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4748};
4749
Vladimir Markofcb503c2016-05-18 12:48:17 +01004750class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004751 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004752 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HInstruction* value,
4754 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004755 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004756 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4757 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4758 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004759 }
4760
Roland Levillain5b5b9312016-03-22 14:57:31 +00004761 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004762 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 return value << (distance & max_shift_distance);
4764 }
4765
4766 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004767 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004769 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004770 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004771 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004772 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004773 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4775 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4776 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4777 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004778 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004779 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4780 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004781 LOG(FATAL) << DebugName() << " is not defined for float values";
4782 UNREACHABLE();
4783 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4785 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004786 LOG(FATAL) << DebugName() << " is not defined for double values";
4787 UNREACHABLE();
4788 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004789
4790 DECLARE_INSTRUCTION(Shl);
4791
4792 private:
4793 DISALLOW_COPY_AND_ASSIGN(HShl);
4794};
4795
Vladimir Markofcb503c2016-05-18 12:48:17 +01004796class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004797 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004798 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004799 HInstruction* value,
4800 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004801 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004802 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4803 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4804 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004805 }
4806
Roland Levillain5b5b9312016-03-22 14:57:31 +00004807 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004808 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004809 return value >> (distance & max_shift_distance);
4810 }
4811
4812 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004813 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004814 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004815 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004816 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004817 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004818 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004819 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004820 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4821 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4822 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4823 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004824 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4826 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004827 LOG(FATAL) << DebugName() << " is not defined for float values";
4828 UNREACHABLE();
4829 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4831 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004832 LOG(FATAL) << DebugName() << " is not defined for double values";
4833 UNREACHABLE();
4834 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004835
4836 DECLARE_INSTRUCTION(Shr);
4837
4838 private:
4839 DISALLOW_COPY_AND_ASSIGN(HShr);
4840};
4841
Vladimir Markofcb503c2016-05-18 12:48:17 +01004842class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004843 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004844 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004845 HInstruction* value,
4846 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004847 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004848 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4849 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4850 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004851 }
4852
Roland Levillain5b5b9312016-03-22 14:57:31 +00004853 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004854 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004855 typedef typename std::make_unsigned<T>::type V;
4856 V ux = static_cast<V>(value);
4857 return static_cast<T>(ux >> (distance & max_shift_distance));
4858 }
4859
4860 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004861 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004862 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004863 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004864 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004866 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004867 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004868 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4869 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4870 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4871 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004872 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004873 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4874 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004875 LOG(FATAL) << DebugName() << " is not defined for float values";
4876 UNREACHABLE();
4877 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004878 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4879 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004880 LOG(FATAL) << DebugName() << " is not defined for double values";
4881 UNREACHABLE();
4882 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004883
4884 DECLARE_INSTRUCTION(UShr);
4885
4886 private:
4887 DISALLOW_COPY_AND_ASSIGN(HUShr);
4888};
4889
Vladimir Markofcb503c2016-05-18 12:48:17 +01004890class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004891 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004892 HAnd(Primitive::Type result_type,
4893 HInstruction* left,
4894 HInstruction* right,
4895 uint32_t dex_pc = kNoDexPc)
4896 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004897
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004898 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004899
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004900 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004901
4902 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004903 return GetBlock()->GetGraph()->GetIntConstant(
4904 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004905 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004906 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004907 return GetBlock()->GetGraph()->GetLongConstant(
4908 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004909 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004910 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4911 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4912 LOG(FATAL) << DebugName() << " is not defined for float values";
4913 UNREACHABLE();
4914 }
4915 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4916 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4917 LOG(FATAL) << DebugName() << " is not defined for double values";
4918 UNREACHABLE();
4919 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004920
4921 DECLARE_INSTRUCTION(And);
4922
4923 private:
4924 DISALLOW_COPY_AND_ASSIGN(HAnd);
4925};
4926
Vladimir Markofcb503c2016-05-18 12:48:17 +01004927class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004928 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004929 HOr(Primitive::Type result_type,
4930 HInstruction* left,
4931 HInstruction* right,
4932 uint32_t dex_pc = kNoDexPc)
4933 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004934
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004935 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004936
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004937 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004938
4939 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004940 return GetBlock()->GetGraph()->GetIntConstant(
4941 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004942 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004943 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 return GetBlock()->GetGraph()->GetLongConstant(
4945 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004946 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004947 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4948 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4949 LOG(FATAL) << DebugName() << " is not defined for float values";
4950 UNREACHABLE();
4951 }
4952 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4953 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4954 LOG(FATAL) << DebugName() << " is not defined for double values";
4955 UNREACHABLE();
4956 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004957
4958 DECLARE_INSTRUCTION(Or);
4959
4960 private:
4961 DISALLOW_COPY_AND_ASSIGN(HOr);
4962};
4963
Vladimir Markofcb503c2016-05-18 12:48:17 +01004964class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004965 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004966 HXor(Primitive::Type result_type,
4967 HInstruction* left,
4968 HInstruction* right,
4969 uint32_t dex_pc = kNoDexPc)
4970 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004971
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004972 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004973
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004974 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004975
4976 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004977 return GetBlock()->GetGraph()->GetIntConstant(
4978 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004979 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004980 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004981 return GetBlock()->GetGraph()->GetLongConstant(
4982 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004983 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004984 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4985 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4986 LOG(FATAL) << DebugName() << " is not defined for float values";
4987 UNREACHABLE();
4988 }
4989 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4990 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4991 LOG(FATAL) << DebugName() << " is not defined for double values";
4992 UNREACHABLE();
4993 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004994
4995 DECLARE_INSTRUCTION(Xor);
4996
4997 private:
4998 DISALLOW_COPY_AND_ASSIGN(HXor);
4999};
5000
Vladimir Markofcb503c2016-05-18 12:48:17 +01005001class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005002 public:
5003 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00005004 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005005 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
5006 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00005007 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005008
Roland Levillain5b5b9312016-03-22 14:57:31 +00005009 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005010 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005011 typedef typename std::make_unsigned<T>::type V;
5012 V ux = static_cast<V>(value);
5013 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005014 return static_cast<T>(ux);
5015 } else {
5016 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00005017 return static_cast<T>(ux >> (distance & max_shift_value)) |
5018 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005019 }
5020 }
5021
Roland Levillain5b5b9312016-03-22 14:57:31 +00005022 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005023 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005024 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005025 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005026 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005027 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00005028 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005029 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005030 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
5031 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
5032 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
5033 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005034 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005035 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
5036 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 LOG(FATAL) << DebugName() << " is not defined for float values";
5038 UNREACHABLE();
5039 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00005040 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
5041 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005042 LOG(FATAL) << DebugName() << " is not defined for double values";
5043 UNREACHABLE();
5044 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005045
5046 DECLARE_INSTRUCTION(Ror);
5047
5048 private:
5049 DISALLOW_COPY_AND_ASSIGN(HRor);
5050};
5051
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005052// The value of a parameter in this method. Its location depends on
5053// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01005054class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005055 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005056 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005057 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005058 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005059 Primitive::Type parameter_type,
5060 bool is_this = false)
5061 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005062 dex_file_(dex_file),
5063 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00005064 index_(index) {
5065 SetPackedFlag<kFlagIsThis>(is_this);
5066 SetPackedFlag<kFlagCanBeNull>(!is_this);
5067 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005068
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005069 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08005070 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005071 uint8_t GetIndex() const { return index_; }
Igor Murashkin032cacd2017-04-06 14:40:08 -07005072 bool IsThis() const ATTRIBUTE_UNUSED { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005073
Vladimir Markoa1de9182016-02-25 11:37:38 +00005074 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
5075 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00005076
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005077 DECLARE_INSTRUCTION(ParameterValue);
5078
5079 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005080 // Whether or not the parameter value corresponds to 'this' argument.
5081 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
5082 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
5083 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
5084 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
5085 "Too many packed fields.");
5086
Calin Juravlee6e3bea2015-10-14 13:53:10 +00005087 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08005088 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005089 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00005090 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005091 const uint8_t index_;
5092
5093 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
5094};
5095
Vladimir Markofcb503c2016-05-18 12:48:17 +01005096class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005097 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005098 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
5099 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005100
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005101 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005102 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005103 return true;
5104 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005105
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005106 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01005107
5108 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005109 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005110 }
5111 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005112 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005113 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005114 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5115 LOG(FATAL) << DebugName() << " is not defined for float values";
5116 UNREACHABLE();
5117 }
5118 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5119 LOG(FATAL) << DebugName() << " is not defined for double values";
5120 UNREACHABLE();
5121 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005122
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005123 DECLARE_INSTRUCTION(Not);
5124
5125 private:
5126 DISALLOW_COPY_AND_ASSIGN(HNot);
5127};
5128
Vladimir Markofcb503c2016-05-18 12:48:17 +01005129class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005130 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005131 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5132 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005133
5134 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005135 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005136 return true;
5137 }
5138
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005139 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005140 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005141 return !x;
5142 }
5143
Roland Levillain9867bc72015-08-05 10:21:34 +01005144 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005145 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005146 }
5147 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5148 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005149 UNREACHABLE();
5150 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005151 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5152 LOG(FATAL) << DebugName() << " is not defined for float values";
5153 UNREACHABLE();
5154 }
5155 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5156 LOG(FATAL) << DebugName() << " is not defined for double values";
5157 UNREACHABLE();
5158 }
David Brazdil66d126e2015-04-03 16:02:44 +01005159
5160 DECLARE_INSTRUCTION(BooleanNot);
5161
5162 private:
5163 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5164};
5165
Vladimir Markofcb503c2016-05-18 12:48:17 +01005166class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005167 public:
5168 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005169 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005170 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005171 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005172 // Invariant: We should never generate a conversion to a Boolean value.
5173 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005174 }
5175
5176 HInstruction* GetInput() const { return InputAt(0); }
5177 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5178 Primitive::Type GetResultType() const { return GetType(); }
5179
5180 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005181 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5182 return true;
5183 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005184
Mark Mendelle82549b2015-05-06 10:55:34 -04005185 // Try to statically evaluate the conversion and return a HConstant
5186 // containing the result. If the input cannot be converted, return nullptr.
5187 HConstant* TryStaticEvaluation() const;
5188
Roland Levillaindff1f282014-11-05 14:15:05 +00005189 DECLARE_INSTRUCTION(TypeConversion);
5190
5191 private:
5192 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5193};
5194
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005195static constexpr uint32_t kNoRegNumber = -1;
5196
Vladimir Markofcb503c2016-05-18 12:48:17 +01005197class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005198 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005199 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5200 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005202 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005203 SetRawInputAt(0, value);
5204 }
5205
Calin Juravle10e244f2015-01-26 18:54:32 +00005206 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005207 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005208 return true;
5209 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005210
Calin Juravle10e244f2015-01-26 18:54:32 +00005211 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212
Calin Juravle10e244f2015-01-26 18:54:32 +00005213 bool CanThrow() const OVERRIDE { return true; }
5214
5215 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217
5218 DECLARE_INSTRUCTION(NullCheck);
5219
5220 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005221 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5222};
5223
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005224// Embeds an ArtField and all the information required by the compiler. We cache
5225// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005226class FieldInfo : public ValueObject {
5227 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005228 FieldInfo(ArtField* field,
5229 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005230 Primitive::Type field_type,
5231 bool is_volatile,
5232 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005233 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005234 const DexFile& dex_file)
5235 : field_(field),
5236 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005237 field_type_(field_type),
5238 is_volatile_(is_volatile),
5239 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005240 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005241 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005242
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005243 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005244 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005245 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005246 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005247 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005248 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005249 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005250
5251 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005252 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005253 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005254 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005255 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005256 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005257 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005258 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005259};
5260
Vladimir Markofcb503c2016-05-18 12:48:17 +01005261class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005262 public:
5263 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005264 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005265 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005266 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005267 bool is_volatile,
5268 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005269 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005270 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005271 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005272 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005273 field_info_(field,
5274 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005275 field_type,
5276 is_volatile,
5277 field_idx,
5278 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005279 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005280 SetRawInputAt(0, value);
5281 }
5282
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005283 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005284
Vladimir Marko372f10e2016-05-17 16:30:10 +01005285 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5286 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005287 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005288 }
5289
Calin Juravle641547a2015-04-21 22:08:51 +01005290 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005291 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005292 }
5293
5294 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005295 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5296 }
5297
Calin Juravle52c48962014-12-16 17:02:57 +00005298 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005299 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005300 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005301 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005302
5303 DECLARE_INSTRUCTION(InstanceFieldGet);
5304
5305 private:
5306 const FieldInfo field_info_;
5307
5308 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5309};
5310
Vladimir Markofcb503c2016-05-18 12:48:17 +01005311class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005312 public:
5313 HInstanceFieldSet(HInstruction* object,
5314 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005315 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005316 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005317 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005318 bool is_volatile,
5319 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005320 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005321 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005322 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005323 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005324 field_info_(field,
5325 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005326 field_type,
5327 is_volatile,
5328 field_idx,
5329 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005330 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005332 SetRawInputAt(0, object);
5333 SetRawInputAt(1, value);
5334 }
5335
Calin Juravle641547a2015-04-21 22:08:51 +01005336 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005337 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 }
5339
Calin Juravle52c48962014-12-16 17:02:57 +00005340 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005341 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005342 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005343 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005344 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005345 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5346 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005348 DECLARE_INSTRUCTION(InstanceFieldSet);
5349
5350 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005351 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5352 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5353 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5354 "Too many packed fields.");
5355
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005356 const FieldInfo field_info_;
5357
5358 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5359};
5360
Vladimir Markofcb503c2016-05-18 12:48:17 +01005361class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005363 HArrayGet(HInstruction* array,
5364 HInstruction* index,
5365 Primitive::Type type,
5366 uint32_t dex_pc,
5367 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005368 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005369 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 SetRawInputAt(0, array);
5371 SetRawInputAt(1, index);
5372 }
5373
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005374 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005375 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005376 return true;
5377 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005378 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005379 // TODO: We can be smarter here.
5380 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5381 // which generates the implicit null check. There are cases when these can be removed
5382 // to produce better code. If we ever add optimizations to do so we should allow an
5383 // implicit check here (as long as the address falls in the first page).
5384 return false;
5385 }
5386
David Brazdil4833f5a2015-12-16 10:37:39 +00005387 bool IsEquivalentOf(HArrayGet* other) const {
5388 bool result = (GetDexPc() == other->GetDexPc());
5389 if (kIsDebugBuild && result) {
5390 DCHECK_EQ(GetBlock(), other->GetBlock());
5391 DCHECK_EQ(GetArray(), other->GetArray());
5392 DCHECK_EQ(GetIndex(), other->GetIndex());
5393 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005394 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005395 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005396 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5397 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005398 }
5399 }
5400 return result;
5401 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005402
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005403 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5404
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005405 HInstruction* GetArray() const { return InputAt(0); }
5406 HInstruction* GetIndex() const { return InputAt(1); }
5407
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 DECLARE_INSTRUCTION(ArrayGet);
5409
5410 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005411 // We treat a String as an array, creating the HArrayGet from String.charAt()
5412 // intrinsic in the instruction simplifier. We can always determine whether
5413 // a particular HArrayGet is actually a String.charAt() by looking at the type
5414 // of the input but that requires holding the mutator lock, so we prefer to use
5415 // a flag, so that code generators don't need to do the locking.
5416 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5417 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5418 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5419 "Too many packed fields.");
5420
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5422};
5423
Vladimir Markofcb503c2016-05-18 12:48:17 +01005424class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 public:
5426 HArraySet(HInstruction* array,
5427 HInstruction* index,
5428 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005429 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005430 uint32_t dex_pc)
5431 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005432 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5433 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5434 SetPackedFlag<kFlagValueCanBeNull>(true);
5435 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005436 SetRawInputAt(0, array);
5437 SetRawInputAt(1, index);
5438 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005439 // Make a best guess now, may be refined during SSA building.
5440 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441 }
5442
Calin Juravle77520bc2015-01-12 18:45:46 +00005443 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005444 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005445 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 }
5447
Mingyao Yang81014cb2015-06-02 03:16:27 -07005448 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005449 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005450
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005451 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005452 // TODO: Same as for ArrayGet.
5453 return false;
5454 }
5455
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005456 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005457 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005458 }
5459
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005460 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005461 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005462 }
5463
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005464 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005465 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005466 }
5467
Vladimir Markoa1de9182016-02-25 11:37:38 +00005468 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5469 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5470 bool StaticTypeOfArrayIsObjectArray() const {
5471 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5472 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005473
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005474 HInstruction* GetArray() const { return InputAt(0); }
5475 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005476 HInstruction* GetValue() const { return InputAt(2); }
5477
5478 Primitive::Type GetComponentType() const {
5479 // The Dex format does not type floating point index operations. Since the
5480 // `expected_component_type_` is set during building and can therefore not
5481 // be correct, we also check what is the value type. If it is a floating
5482 // point type, we must use that type.
5483 Primitive::Type value_type = GetValue()->GetType();
5484 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5485 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005486 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005487 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005488
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005489 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005490 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005491 }
5492
Aart Bik18b36ab2016-04-13 16:41:35 -07005493 void ComputeSideEffects() {
5494 Primitive::Type type = GetComponentType();
5495 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5496 SideEffectsForArchRuntimeCalls(type)));
5497 }
5498
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005499 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5500 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5501 }
5502
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005503 DECLARE_INSTRUCTION(ArraySet);
5504
5505 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005506 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5507 static constexpr size_t kFieldExpectedComponentTypeSize =
5508 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5509 static constexpr size_t kFlagNeedsTypeCheck =
5510 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5511 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005512 // Cached information for the reference_type_info_ so that codegen
5513 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005514 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5515 static constexpr size_t kNumberOfArraySetPackedBits =
5516 kFlagStaticTypeOfArrayIsObjectArray + 1;
5517 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5518 using ExpectedComponentTypeField =
5519 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520
5521 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5522};
5523
Vladimir Markofcb503c2016-05-18 12:48:17 +01005524class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005526 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005527 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005528 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005529 // Note that arrays do not change length, so the instruction does not
5530 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 SetRawInputAt(0, array);
5532 }
5533
Calin Juravle77520bc2015-01-12 18:45:46 +00005534 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005535 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005536 return true;
5537 }
Calin Juravle641547a2015-04-21 22:08:51 +01005538 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5539 return obj == InputAt(0);
5540 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005541
Vladimir Markodce016e2016-04-28 13:10:02 +01005542 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5543
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544 DECLARE_INSTRUCTION(ArrayLength);
5545
5546 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005547 // We treat a String as an array, creating the HArrayLength from String.length()
5548 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5549 // determine whether a particular HArrayLength is actually a String.length() by
5550 // looking at the type of the input but that requires holding the mutator lock, so
5551 // we prefer to use a flag, so that code generators don't need to do the locking.
5552 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5553 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5554 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5555 "Too many packed fields.");
5556
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005557 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5558};
5559
Vladimir Markofcb503c2016-05-18 12:48:17 +01005560class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005561 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005562 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5563 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005564 HBoundsCheck(HInstruction* index,
5565 HInstruction* length,
5566 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005567 bool string_char_at = false)
5568 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005569 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005570 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571 SetRawInputAt(0, index);
5572 SetRawInputAt(1, length);
5573 }
5574
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005575 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005576 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005577 return true;
5578 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005579
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005580 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005581
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005582 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005583
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005584 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005585
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005586 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005587
5588 DECLARE_INSTRUCTION(BoundsCheck);
5589
5590 private:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005591 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005592
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005593 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5594};
5595
Vladimir Markofcb503c2016-05-18 12:48:17 +01005596class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005597 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005598 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005599 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005601 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005602 return true;
5603 }
5604
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005605 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5606 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005607
5608 DECLARE_INSTRUCTION(SuspendCheck);
5609
5610 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005611 // Only used for code generation, in order to share the same slow path between back edges
5612 // of a same loop.
5613 SlowPathCode* slow_path_;
5614
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005615 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5616};
5617
David Srbecky0cf44932015-12-09 14:09:59 +00005618// Pseudo-instruction which provides the native debugger with mapping information.
5619// It ensures that we can generate line number and local variables at this point.
5620class HNativeDebugInfo : public HTemplateInstruction<0> {
5621 public:
5622 explicit HNativeDebugInfo(uint32_t dex_pc)
5623 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5624
5625 bool NeedsEnvironment() const OVERRIDE {
5626 return true;
5627 }
5628
5629 DECLARE_INSTRUCTION(NativeDebugInfo);
5630
5631 private:
5632 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5633};
5634
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005635/**
5636 * Instruction to load a Class object.
5637 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005638class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005639 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005640 // Determines how to load the Class.
5641 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005642 // We cannot load this class. See HSharpening::SharpenLoadClass.
5643 kInvalid = -1,
5644
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005645 // Use the Class* from the method's own ArtMethod*.
5646 kReferrersClass,
5647
5648 // Use boot image Class* address that will be known at link time.
5649 // Used for boot image classes referenced by boot image code in non-PIC mode.
5650 kBootImageLinkTimeAddress,
5651
5652 // Use PC-relative boot image Class* address that will be known at link time.
5653 // Used for boot image classes referenced by boot image code in PIC mode.
5654 kBootImageLinkTimePcRelative,
5655
5656 // Use a known boot image Class* address, embedded in the code by the codegen.
5657 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005658 kBootImageAddress,
5659
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005660 // Load from an entry in the .bss section using a PC-relative load.
5661 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5662 kBssEntry,
5663
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005664 // Load from the root table associated with the JIT compiled method.
5665 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005667 // Load from resolved types array accessed through the class loaded from
5668 // the compiled method's own ArtMethod*. This is the default access type when
5669 // all other types are unavailable.
5670 kDexCacheViaMethod,
5671
5672 kLast = kDexCacheViaMethod
5673 };
5674
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005675 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005676 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005677 const DexFile& dex_file,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005678 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005679 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005680 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005681 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005682 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5683 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005684 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005685 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005686 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005687 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005688 // Referrers class should not need access check. We never inline unverified
5689 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005690 DCHECK(!is_referrers_class || !needs_access_check);
5691
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005692 SetPackedField<LoadKindField>(
5693 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005694 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005695 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005696 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 }
5698
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005699 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005701 LoadKind GetLoadKind() const {
5702 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005703 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005704
5705 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005706
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005707 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005708
Andreas Gampea5b09a62016-11-17 15:21:22 -08005709 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005710
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005711 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005713 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005714 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005715 }
5716
Calin Juravle0ba218d2015-05-19 18:46:01 +01005717 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005718 // The entrypoint the code generator is going to call does not do
5719 // clinit of the class.
5720 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005721 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005722 }
5723
5724 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005725 return NeedsAccessCheck() ||
5726 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005727 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5728 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005729 }
5730
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005731 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005732 return NeedsAccessCheck() ||
5733 MustGenerateClinitCheck() ||
5734 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5735 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5736 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005737 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5738 GetLoadKind() == LoadKind::kBssEntry) &&
5739 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005740 }
5741
Calin Juravleacf735c2015-02-12 15:25:22 +00005742 ReferenceTypeInfo GetLoadedClassRTI() {
5743 return loaded_class_rti_;
5744 }
5745
5746 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5747 // Make sure we only set exact types (the loaded class should never be merged).
5748 DCHECK(rti.IsExact());
5749 loaded_class_rti_ = rti;
5750 }
5751
Andreas Gampea5b09a62016-11-17 15:21:22 -08005752 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005753 const DexFile& GetDexFile() const { return dex_file_; }
5754
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005755 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005756 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005757 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005758
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005759 static SideEffects SideEffectsForArchRuntimeCalls() {
5760 return SideEffects::CanTriggerGC();
5761 }
5762
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005763 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005764 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005765 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005766 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005767
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005768 void MarkInBootImage() {
5769 SetPackedFlag<kFlagIsInBootImage>(true);
5770 }
5771
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005772 void AddSpecialInput(HInstruction* special_input);
5773
5774 using HInstruction::GetInputRecords; // Keep the const version visible.
5775 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5776 return ArrayRef<HUserRecord<HInstruction*>>(
5777 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5778 }
5779
5780 Primitive::Type GetType() const OVERRIDE {
5781 return Primitive::kPrimNot;
5782 }
5783
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005784 Handle<mirror::Class> GetClass() const {
5785 return klass_;
5786 }
5787
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005788 DECLARE_INSTRUCTION(LoadClass);
5789
5790 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005791 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005792 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005793 // Whether this instruction must generate the initialization check.
5794 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005795 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005796 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5797 static constexpr size_t kFieldLoadKindSize =
5798 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5799 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005800 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005801 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5802
5803 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005804 return load_kind == LoadKind::kReferrersClass ||
5805 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005806 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005807 load_kind == LoadKind::kBssEntry ||
5808 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005809 }
5810
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005811 void SetLoadKindInternal(LoadKind load_kind);
5812
5813 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5814 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005815 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005816 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005817
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005818 // A type index and dex file where the class can be accessed. The dex file can be:
5819 // - The compiling method's dex file if the class is defined there too.
5820 // - The compiling method's dex file if the class is referenced there.
5821 // - The dex file where the class is defined. When the load kind can only be
5822 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005823 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005824 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005825
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005826 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827
Calin Juravleacf735c2015-02-12 15:25:22 +00005828 ReferenceTypeInfo loaded_class_rti_;
5829
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005830 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5831};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5833
5834// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005836 // The special input is used for PC-relative loads on some architectures,
5837 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005838 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005839 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005840 GetLoadKind() == LoadKind::kBootImageAddress ||
5841 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005842 DCHECK(special_input_.GetInstruction() == nullptr);
5843 special_input_ = HUserRecord<HInstruction*>(special_input);
5844 special_input->AddUseAt(this, 0);
5845}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005846
Vladimir Marko372f10e2016-05-17 16:30:10 +01005847class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005848 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005849 // Determines how to load the String.
5850 enum class LoadKind {
5851 // Use boot image String* address that will be known at link time.
5852 // Used for boot image strings referenced by boot image code in non-PIC mode.
5853 kBootImageLinkTimeAddress,
5854
5855 // Use PC-relative boot image String* address that will be known at link time.
5856 // Used for boot image strings referenced by boot image code in PIC mode.
5857 kBootImageLinkTimePcRelative,
5858
5859 // Use a known boot image String* address, embedded in the code by the codegen.
5860 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005861 kBootImageAddress,
5862
Vladimir Markoaad75c62016-10-03 08:46:48 +00005863 // Load from an entry in the .bss section using a PC-relative load.
5864 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5865 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005866
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005867 // Load from the root table associated with the JIT compiled method.
5868 kJitTableAddress,
5869
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870 // Load from resolved strings array accessed through the class loaded from
5871 // the compiled method's own ArtMethod*. This is the default access type when
5872 // all other types are unavailable.
5873 kDexCacheViaMethod,
5874
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005875 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 };
5877
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005878 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005879 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005880 const DexFile& dex_file,
5881 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005882 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5883 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005884 string_index_(string_index),
5885 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005886 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005887 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005888
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005889 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005891 LoadKind GetLoadKind() const {
5892 return GetPackedField<LoadKindField>();
5893 }
5894
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005895 const DexFile& GetDexFile() const {
5896 return dex_file_;
5897 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005898
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005899 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900 return string_index_;
5901 }
5902
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005903 Handle<mirror::String> GetString() const {
5904 return string_;
5905 }
5906
5907 void SetString(Handle<mirror::String> str) {
5908 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 }
5910
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005911 bool CanBeMoved() const OVERRIDE { return true; }
5912
Vladimir Marko372f10e2016-05-17 16:30:10 +01005913 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005914
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005915 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005916
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 // Will call the runtime if we need to load the string through
5918 // the dex cache and the string is not guaranteed to be there yet.
5919 bool NeedsEnvironment() const OVERRIDE {
5920 LoadKind load_kind = GetLoadKind();
5921 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5922 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005923 load_kind == LoadKind::kBootImageAddress ||
5924 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 return false;
5926 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005927 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005928 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005929
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005930 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5931 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5932 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005933
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005934 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005936
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005937 static SideEffects SideEffectsForArchRuntimeCalls() {
5938 return SideEffects::CanTriggerGC();
5939 }
5940
Vladimir Marko372f10e2016-05-17 16:30:10 +01005941 void AddSpecialInput(HInstruction* special_input);
5942
5943 using HInstruction::GetInputRecords; // Keep the const version visible.
5944 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5945 return ArrayRef<HUserRecord<HInstruction*>>(
5946 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947 }
5948
Vladimir Marko372f10e2016-05-17 16:30:10 +01005949 Primitive::Type GetType() const OVERRIDE {
5950 return Primitive::kPrimNot;
5951 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005953 DECLARE_INSTRUCTION(LoadString);
5954
5955 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005956 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005957 static constexpr size_t kFieldLoadKindSize =
5958 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5959 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005960 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005962
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963 void SetLoadKindInternal(LoadKind load_kind);
5964
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005965 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5966 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005967 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005968 HUserRecord<HInstruction*> special_input_;
5969
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005970 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005971 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005972
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005973 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005974
5975 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5976};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005977std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5978
5979// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005981 // The special input is used for PC-relative loads on some architectures,
5982 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005983 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005984 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005985 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5986 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005987 // HLoadString::GetInputRecords() returns an empty array at this point,
5988 // so use the GetInputRecords() from the base class to set the input record.
5989 DCHECK(special_input_.GetInstruction() == nullptr);
5990 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005991 special_input->AddUseAt(this, 0);
5992}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005993
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994/**
5995 * Performs an initialization check on its Class object input.
5996 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005997class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005998 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005999 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07006000 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006001 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006002 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
6003 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004 SetRawInputAt(0, constant);
6005 }
6006
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006007 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006008 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006009 return true;
6010 }
6011
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006012 bool NeedsEnvironment() const OVERRIDE {
6013 // May call runtime to initialize the class.
6014 return true;
6015 }
6016
Nicolas Geoffray729645a2015-11-19 13:29:02 +00006017 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006018
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006019 HLoadClass* GetLoadClass() const {
6020 DCHECK(InputAt(0)->IsLoadClass());
6021 return InputAt(0)->AsLoadClass();
6022 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006023
6024 DECLARE_INSTRUCTION(ClinitCheck);
6025
6026 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006027 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
6028};
6029
Vladimir Markofcb503c2016-05-18 12:48:17 +01006030class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006031 public:
6032 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006033 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006034 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006035 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006036 bool is_volatile,
6037 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006038 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006039 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006040 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006041 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006042 field_info_(field,
6043 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006044 field_type,
6045 is_volatile,
6046 field_idx,
6047 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006048 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006049 SetRawInputAt(0, cls);
6050 }
6051
Calin Juravle52c48962014-12-16 17:02:57 +00006052
Calin Juravle10c9cbe2014-12-19 10:50:19 +00006053 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006054
Vladimir Marko372f10e2016-05-17 16:30:10 +01006055 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
6056 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00006057 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006058 }
6059
6060 size_t ComputeHashCode() const OVERRIDE {
6061 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
6062 }
6063
Calin Juravle52c48962014-12-16 17:02:57 +00006064 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006065 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6066 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006067 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006068
6069 DECLARE_INSTRUCTION(StaticFieldGet);
6070
6071 private:
6072 const FieldInfo field_info_;
6073
6074 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
6075};
6076
Vladimir Markofcb503c2016-05-18 12:48:17 +01006077class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006078 public:
6079 HStaticFieldSet(HInstruction* cls,
6080 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006081 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006082 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00006083 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01006084 bool is_volatile,
6085 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006086 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07006087 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01006088 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01006089 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006090 field_info_(field,
6091 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07006092 field_type,
6093 is_volatile,
6094 field_idx,
6095 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00006096 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006097 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006098 SetRawInputAt(0, cls);
6099 SetRawInputAt(1, value);
6100 }
6101
Calin Juravle52c48962014-12-16 17:02:57 +00006102 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006103 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6104 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006105 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006106
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006107 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6109 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006110
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006111 DECLARE_INSTRUCTION(StaticFieldSet);
6112
6113 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6115 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6116 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6117 "Too many packed fields.");
6118
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006119 const FieldInfo field_info_;
6120
6121 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6122};
6123
Vladimir Markofcb503c2016-05-18 12:48:17 +01006124class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006125 public:
6126 HUnresolvedInstanceFieldGet(HInstruction* obj,
6127 Primitive::Type field_type,
6128 uint32_t field_index,
6129 uint32_t dex_pc)
6130 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6131 field_index_(field_index) {
6132 SetRawInputAt(0, obj);
6133 }
6134
6135 bool NeedsEnvironment() const OVERRIDE { return true; }
6136 bool CanThrow() const OVERRIDE { return true; }
6137
6138 Primitive::Type GetFieldType() const { return GetType(); }
6139 uint32_t GetFieldIndex() const { return field_index_; }
6140
6141 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6142
6143 private:
6144 const uint32_t field_index_;
6145
6146 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6147};
6148
Vladimir Markofcb503c2016-05-18 12:48:17 +01006149class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006150 public:
6151 HUnresolvedInstanceFieldSet(HInstruction* obj,
6152 HInstruction* value,
6153 Primitive::Type field_type,
6154 uint32_t field_index,
6155 uint32_t dex_pc)
6156 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006157 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006158 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006159 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006160 SetRawInputAt(0, obj);
6161 SetRawInputAt(1, value);
6162 }
6163
6164 bool NeedsEnvironment() const OVERRIDE { return true; }
6165 bool CanThrow() const OVERRIDE { return true; }
6166
Vladimir Markoa1de9182016-02-25 11:37:38 +00006167 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006168 uint32_t GetFieldIndex() const { return field_index_; }
6169
6170 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6171
6172 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006173 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6174 static constexpr size_t kFieldFieldTypeSize =
6175 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6176 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6177 kFieldFieldType + kFieldFieldTypeSize;
6178 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6179 "Too many packed fields.");
6180 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6181
Calin Juravlee460d1d2015-09-29 04:52:17 +01006182 const uint32_t field_index_;
6183
6184 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6185};
6186
Vladimir Markofcb503c2016-05-18 12:48:17 +01006187class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006188 public:
6189 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6190 uint32_t field_index,
6191 uint32_t dex_pc)
6192 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6193 field_index_(field_index) {
6194 }
6195
6196 bool NeedsEnvironment() const OVERRIDE { return true; }
6197 bool CanThrow() const OVERRIDE { return true; }
6198
6199 Primitive::Type GetFieldType() const { return GetType(); }
6200 uint32_t GetFieldIndex() const { return field_index_; }
6201
6202 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6203
6204 private:
6205 const uint32_t field_index_;
6206
6207 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6208};
6209
Vladimir Markofcb503c2016-05-18 12:48:17 +01006210class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006211 public:
6212 HUnresolvedStaticFieldSet(HInstruction* value,
6213 Primitive::Type field_type,
6214 uint32_t field_index,
6215 uint32_t dex_pc)
6216 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006217 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006218 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006219 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006220 SetRawInputAt(0, value);
6221 }
6222
6223 bool NeedsEnvironment() const OVERRIDE { return true; }
6224 bool CanThrow() const OVERRIDE { return true; }
6225
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006227 uint32_t GetFieldIndex() const { return field_index_; }
6228
6229 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6230
6231 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6233 static constexpr size_t kFieldFieldTypeSize =
6234 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6235 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6236 kFieldFieldType + kFieldFieldTypeSize;
6237 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6238 "Too many packed fields.");
6239 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6240
Calin Juravlee460d1d2015-09-29 04:52:17 +01006241 const uint32_t field_index_;
6242
6243 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6244};
6245
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006246// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006247class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006248 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006249 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6250 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006251
David Brazdilbbd733e2015-08-18 17:48:17 +01006252 bool CanBeNull() const OVERRIDE { return false; }
6253
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006254 DECLARE_INSTRUCTION(LoadException);
6255
6256 private:
6257 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6258};
6259
David Brazdilcb1c0552015-08-04 16:22:25 +01006260// Implicit part of move-exception which clears thread-local exception storage.
6261// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006262class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006263 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006264 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6265 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006266
6267 DECLARE_INSTRUCTION(ClearException);
6268
6269 private:
6270 DISALLOW_COPY_AND_ASSIGN(HClearException);
6271};
6272
Vladimir Markofcb503c2016-05-18 12:48:17 +01006273class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006274 public:
6275 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006276 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006277 SetRawInputAt(0, exception);
6278 }
6279
6280 bool IsControlFlow() const OVERRIDE { return true; }
6281
6282 bool NeedsEnvironment() const OVERRIDE { return true; }
6283
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006284 bool CanThrow() const OVERRIDE { return true; }
6285
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006286
6287 DECLARE_INSTRUCTION(Throw);
6288
6289 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006290 DISALLOW_COPY_AND_ASSIGN(HThrow);
6291};
6292
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006293/**
6294 * Implementation strategies for the code generator of a HInstanceOf
6295 * or `HCheckCast`.
6296 */
6297enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006298 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 kExactCheck, // Can do a single class compare.
6300 kClassHierarchyCheck, // Can just walk the super class chain.
6301 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6302 kInterfaceCheck, // No optimization yet when checking against an interface.
6303 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006304 kArrayCheck, // No optimization yet when checking against a generic array.
6305 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306};
6307
Roland Levillain86503782016-02-11 19:07:30 +00006308std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6309
Vladimir Markofcb503c2016-05-18 12:48:17 +01006310class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006311 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312 HInstanceOf(HInstruction* object,
6313 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006315 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006316 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006318 dex_pc) {
6319 SetPackedField<TypeCheckKindField>(check_kind);
6320 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006321 SetRawInputAt(0, object);
6322 SetRawInputAt(1, constant);
6323 }
6324
6325 bool CanBeMoved() const OVERRIDE { return true; }
6326
Vladimir Marko372f10e2016-05-17 16:30:10 +01006327 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006328 return true;
6329 }
6330
6331 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006333 }
6334
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006335 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006336 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6337 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6338 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6339 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006340
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006341 static bool CanCallRuntime(TypeCheckKind check_kind) {
6342 // Mips currently does runtime calls for any other checks.
6343 return check_kind != TypeCheckKind::kExactCheck;
6344 }
6345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006347 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006348 }
6349
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006350 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006351
6352 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006353 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6354 static constexpr size_t kFieldTypeCheckKindSize =
6355 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6356 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6357 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6358 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6359 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006360
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006361 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6362};
6363
Vladimir Markofcb503c2016-05-18 12:48:17 +01006364class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006365 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006366 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006367 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006368 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6369 SetPackedFlag<kFlagUpperCanBeNull>(true);
6370 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006371 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006372 SetRawInputAt(0, input);
6373 }
6374
David Brazdilf5552582015-12-27 13:36:12 +00006375 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006376 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006378 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006379
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006380 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006381 DCHECK(GetUpperCanBeNull() || !can_be_null);
6382 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006383 }
6384
Vladimir Markoa1de9182016-02-25 11:37:38 +00006385 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006386
Calin Juravleb1498f62015-02-16 13:13:29 +00006387 DECLARE_INSTRUCTION(BoundType);
6388
6389 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006390 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6391 // is false then CanBeNull() cannot be true).
6392 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6393 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6394 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6395 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6396
Calin Juravleb1498f62015-02-16 13:13:29 +00006397 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006398 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6399 // It is used to bound the type in cases like:
6400 // if (x instanceof ClassX) {
6401 // // uper_bound_ will be ClassX
6402 // }
David Brazdilf5552582015-12-27 13:36:12 +00006403 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006404
6405 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6406};
6407
Vladimir Markofcb503c2016-05-18 12:48:17 +01006408class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006409 public:
6410 HCheckCast(HInstruction* object,
6411 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006413 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006414 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6415 SetPackedField<TypeCheckKindField>(check_kind);
6416 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006417 SetRawInputAt(0, object);
6418 SetRawInputAt(1, constant);
6419 }
6420
6421 bool CanBeMoved() const OVERRIDE { return true; }
6422
Vladimir Marko372f10e2016-05-17 16:30:10 +01006423 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006424 return true;
6425 }
6426
6427 bool NeedsEnvironment() const OVERRIDE {
6428 // Instruction may throw a CheckCastError.
6429 return true;
6430 }
6431
6432 bool CanThrow() const OVERRIDE { return true; }
6433
Vladimir Markoa1de9182016-02-25 11:37:38 +00006434 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6435 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6436 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6437 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006438
6439 DECLARE_INSTRUCTION(CheckCast);
6440
6441 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006442 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6443 static constexpr size_t kFieldTypeCheckKindSize =
6444 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6445 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6446 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6447 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6448 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006449
6450 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006451};
6452
Andreas Gampe26de38b2016-07-27 17:53:11 -07006453/**
6454 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6455 * @details We define the combined barrier types that are actually required
6456 * by the Java Memory Model, rather than using exactly the terminology from
6457 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6458 * primitives. Note that the JSR-133 cookbook generally does not deal with
6459 * store atomicity issues, and the recipes there are not always entirely sufficient.
6460 * The current recipe is as follows:
6461 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6462 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6463 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6464 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6465 * class has final fields.
6466 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6467 * store-to-memory instructions. Only generated together with non-temporal stores.
6468 */
6469enum MemBarrierKind {
6470 kAnyStore,
6471 kLoadAny,
6472 kStoreStore,
6473 kAnyAny,
6474 kNTStoreStore,
6475 kLastBarrierKind = kNTStoreStore
6476};
6477std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6478
Vladimir Markofcb503c2016-05-18 12:48:17 +01006479class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006480 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006481 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006482 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006483 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6484 SetPackedField<BarrierKindField>(barrier_kind);
6485 }
Calin Juravle27df7582015-04-17 19:12:31 +01006486
Vladimir Markoa1de9182016-02-25 11:37:38 +00006487 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006488
6489 DECLARE_INSTRUCTION(MemoryBarrier);
6490
6491 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006492 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6493 static constexpr size_t kFieldBarrierKindSize =
6494 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6495 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6496 kFieldBarrierKind + kFieldBarrierKindSize;
6497 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6498 "Too many packed fields.");
6499 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006500
6501 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6502};
6503
Vladimir Markofcb503c2016-05-18 12:48:17 +01006504class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006505 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006506 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006507 kEnter,
6508 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006509 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006510 };
6511
6512 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006513 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006514 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6515 dex_pc) {
6516 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006517 SetRawInputAt(0, object);
6518 }
6519
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006520 // Instruction may go into runtime, so we need an environment.
6521 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006522
6523 bool CanThrow() const OVERRIDE {
6524 // Verifier guarantees that monitor-exit cannot throw.
6525 // This is important because it allows the HGraphBuilder to remove
6526 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6527 return IsEnter();
6528 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006529
Vladimir Markoa1de9182016-02-25 11:37:38 +00006530 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6531 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006532
6533 DECLARE_INSTRUCTION(MonitorOperation);
6534
Calin Juravle52c48962014-12-16 17:02:57 +00006535 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006536 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6537 static constexpr size_t kFieldOperationKindSize =
6538 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6539 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6540 kFieldOperationKind + kFieldOperationKindSize;
6541 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6542 "Too many packed fields.");
6543 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006544
6545 private:
6546 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6547};
6548
Vladimir Markofcb503c2016-05-18 12:48:17 +01006549class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006550 public:
6551 HSelect(HInstruction* condition,
6552 HInstruction* true_value,
6553 HInstruction* false_value,
6554 uint32_t dex_pc)
6555 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6556 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6557
6558 // First input must be `true_value` or `false_value` to allow codegens to
6559 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6560 // that architectures which implement HSelect as a conditional move also
6561 // will not need to invert the condition.
6562 SetRawInputAt(0, false_value);
6563 SetRawInputAt(1, true_value);
6564 SetRawInputAt(2, condition);
6565 }
6566
6567 HInstruction* GetFalseValue() const { return InputAt(0); }
6568 HInstruction* GetTrueValue() const { return InputAt(1); }
6569 HInstruction* GetCondition() const { return InputAt(2); }
6570
6571 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006572 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6573 return true;
6574 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006575
6576 bool CanBeNull() const OVERRIDE {
6577 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6578 }
6579
6580 DECLARE_INSTRUCTION(Select);
6581
6582 private:
6583 DISALLOW_COPY_AND_ASSIGN(HSelect);
6584};
6585
Vladimir Markof9f64412015-09-02 14:05:49 +01006586class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006587 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006588 MoveOperands(Location source,
6589 Location destination,
6590 Primitive::Type type,
6591 HInstruction* instruction)
6592 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006593
6594 Location GetSource() const { return source_; }
6595 Location GetDestination() const { return destination_; }
6596
6597 void SetSource(Location value) { source_ = value; }
6598 void SetDestination(Location value) { destination_ = value; }
6599
6600 // The parallel move resolver marks moves as "in-progress" by clearing the
6601 // destination (but not the source).
6602 Location MarkPending() {
6603 DCHECK(!IsPending());
6604 Location dest = destination_;
6605 destination_ = Location::NoLocation();
6606 return dest;
6607 }
6608
6609 void ClearPending(Location dest) {
6610 DCHECK(IsPending());
6611 destination_ = dest;
6612 }
6613
6614 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006615 DCHECK(source_.IsValid() || destination_.IsInvalid());
6616 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006617 }
6618
6619 // True if this blocks a move from the given location.
6620 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006621 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006622 }
6623
6624 // A move is redundant if it's been eliminated, if its source and
6625 // destination are the same, or if its destination is unneeded.
6626 bool IsRedundant() const {
6627 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6628 }
6629
6630 // We clear both operands to indicate move that's been eliminated.
6631 void Eliminate() {
6632 source_ = destination_ = Location::NoLocation();
6633 }
6634
6635 bool IsEliminated() const {
6636 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6637 return source_.IsInvalid();
6638 }
6639
Alexey Frunze4dda3372015-06-01 18:31:49 -07006640 Primitive::Type GetType() const { return type_; }
6641
Nicolas Geoffray90218252015-04-15 11:56:51 +01006642 bool Is64BitMove() const {
6643 return Primitive::Is64BitType(type_);
6644 }
6645
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006646 HInstruction* GetInstruction() const { return instruction_; }
6647
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006648 private:
6649 Location source_;
6650 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006651 // The type this move is for.
6652 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006653 // The instruction this move is assocatied with. Null when this move is
6654 // for moving an input in the expected locations of user (including a phi user).
6655 // This is only used in debug mode, to ensure we do not connect interval siblings
6656 // in the same parallel move.
6657 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006658};
6659
Roland Levillainc9285912015-12-18 10:38:42 +00006660std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6661
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006662static constexpr size_t kDefaultNumberOfMoves = 4;
6663
Vladimir Markofcb503c2016-05-18 12:48:17 +01006664class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006665 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006666 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006667 : HTemplateInstruction(SideEffects::None(), dex_pc),
6668 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6669 moves_.reserve(kDefaultNumberOfMoves);
6670 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006671
Nicolas Geoffray90218252015-04-15 11:56:51 +01006672 void AddMove(Location source,
6673 Location destination,
6674 Primitive::Type type,
6675 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006676 DCHECK(source.IsValid());
6677 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006678 if (kIsDebugBuild) {
6679 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006680 for (const MoveOperands& move : moves_) {
6681 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006682 // Special case the situation where the move is for the spill slot
6683 // of the instruction.
6684 if ((GetPrevious() == instruction)
6685 || ((GetPrevious() == nullptr)
6686 && instruction->IsPhi()
6687 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006688 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006689 << "Doing parallel moves for the same instruction.";
6690 } else {
6691 DCHECK(false) << "Doing parallel moves for the same instruction.";
6692 }
6693 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006694 }
6695 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006696 for (const MoveOperands& move : moves_) {
6697 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006698 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006699 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006700 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006701 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006702 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006703 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006704 }
6705
Vladimir Marko225b6462015-09-28 12:17:40 +01006706 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006707 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006708 }
6709
Vladimir Marko225b6462015-09-28 12:17:40 +01006710 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006711
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006712 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006713
6714 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006715 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006716
6717 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6718};
6719
Mark Mendell0616ae02015-04-17 12:49:27 -04006720} // namespace art
6721
Aart Bikf8f5a162017-02-06 15:35:29 -08006722#include "nodes_vector.h"
6723
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006724#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6725#include "nodes_shared.h"
6726#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006727#ifdef ART_ENABLE_CODEGEN_arm
6728#include "nodes_arm.h"
6729#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006730#ifdef ART_ENABLE_CODEGEN_mips
6731#include "nodes_mips.h"
6732#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006733#ifdef ART_ENABLE_CODEGEN_x86
6734#include "nodes_x86.h"
6735#endif
6736
6737namespace art {
6738
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006739class HGraphVisitor : public ValueObject {
6740 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006741 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6742 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006743
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006744 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006745 virtual void VisitBasicBlock(HBasicBlock* block);
6746
Roland Levillain633021e2014-10-01 14:12:25 +01006747 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006748 void VisitInsertionOrder();
6749
Roland Levillain633021e2014-10-01 14:12:25 +01006750 // Visit the graph following dominator tree reverse post-order.
6751 void VisitReversePostOrder();
6752
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006753 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006754
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006755 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006756#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006757 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6758
6759 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6760
6761#undef DECLARE_VISIT_INSTRUCTION
6762
6763 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006764 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006765
6766 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6767};
6768
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006769class HGraphDelegateVisitor : public HGraphVisitor {
6770 public:
6771 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6772 virtual ~HGraphDelegateVisitor() {}
6773
6774 // Visit functions that delegate to to super class.
6775#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006776 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006777
6778 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6779
6780#undef DECLARE_VISIT_INSTRUCTION
6781
6782 private:
6783 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6784};
6785
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006786// Iterator over the blocks that art part of the loop. Includes blocks part
6787// of an inner loop. The order in which the blocks are iterated is on their
6788// block id.
6789class HBlocksInLoopIterator : public ValueObject {
6790 public:
6791 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6792 : blocks_in_loop_(info.GetBlocks()),
6793 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6794 index_(0) {
6795 if (!blocks_in_loop_.IsBitSet(index_)) {
6796 Advance();
6797 }
6798 }
6799
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006800 bool Done() const { return index_ == blocks_.size(); }
6801 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006802 void Advance() {
6803 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006804 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006805 if (blocks_in_loop_.IsBitSet(index_)) {
6806 break;
6807 }
6808 }
6809 }
6810
6811 private:
6812 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006813 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006814 size_t index_;
6815
6816 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6817};
6818
Mingyao Yang3584bce2015-05-19 16:01:59 -07006819// Iterator over the blocks that art part of the loop. Includes blocks part
6820// of an inner loop. The order in which the blocks are iterated is reverse
6821// post order.
6822class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6823 public:
6824 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6825 : blocks_in_loop_(info.GetBlocks()),
6826 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6827 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006828 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006829 Advance();
6830 }
6831 }
6832
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006833 bool Done() const { return index_ == blocks_.size(); }
6834 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006835 void Advance() {
6836 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006837 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6838 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006839 break;
6840 }
6841 }
6842 }
6843
6844 private:
6845 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006846 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006847 size_t index_;
6848
6849 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6850};
6851
Aart Bikf3e61ee2017-04-12 17:09:20 -07006852// Returns int64_t value of a properly typed constant.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006853inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006854 if (constant->IsIntConstant()) {
6855 return constant->AsIntConstant()->GetValue();
6856 } else if (constant->IsLongConstant()) {
6857 return constant->AsLongConstant()->GetValue();
6858 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006859 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006860 return 0;
6861 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006862}
6863
Aart Bikf3e61ee2017-04-12 17:09:20 -07006864// Returns true iff instruction is an integral constant (and sets value on success).
6865inline bool IsInt64AndGet(HInstruction* instruction, /*out*/ int64_t* value) {
6866 if (instruction->IsIntConstant()) {
6867 *value = instruction->AsIntConstant()->GetValue();
6868 return true;
6869 } else if (instruction->IsLongConstant()) {
6870 *value = instruction->AsLongConstant()->GetValue();
6871 return true;
6872 } else if (instruction->IsNullConstant()) {
6873 *value = 0;
6874 return true;
6875 }
6876 return false;
6877}
6878
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006879#define INSTRUCTION_TYPE_CHECK(type, super) \
6880 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6881 inline const H##type* HInstruction::As##type() const { \
6882 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6883 } \
6884 inline H##type* HInstruction::As##type() { \
6885 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6886 }
6887
6888 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6889#undef INSTRUCTION_TYPE_CHECK
6890
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006891// Create space in `blocks` for adding `number_of_new_blocks` entries
6892// starting at location `at`. Blocks after `at` are moved accordingly.
6893inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6894 size_t number_of_new_blocks,
6895 size_t after) {
6896 DCHECK_LT(after, blocks->size());
6897 size_t old_size = blocks->size();
6898 size_t new_size = old_size + number_of_new_blocks;
6899 blocks->resize(new_size);
6900 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6901}
6902
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006903/*
6904 * Hunt "under the hood" of array lengths (leading to array references),
6905 * null checks (also leading to array references), and new arrays
6906 * (leading to the actual length). This makes it more likely related
6907 * instructions become actually comparable.
6908 */
6909inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6910 while (instruction->IsArrayLength() ||
6911 instruction->IsNullCheck() ||
6912 instruction->IsNewArray()) {
6913 instruction = instruction->IsNewArray()
6914 ? instruction->AsNewArray()->GetLength()
6915 : instruction->InputAt(0);
6916 }
6917 return instruction;
6918}
6919
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006920} // namespace art
6921
6922#endif // ART_COMPILER_OPTIMIZING_NODES_H_