blob: d92092c911dc9437f81c8f951572a14a128336dc [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,
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100308 bool should_generate_constructor_barrier,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 InstructionSet instruction_set,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100310 InvokeType invoke_type = kInvalidInvokeType,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100311 bool debuggable = false,
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000312 bool osr = false,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100313 int start_instruction_id = 0)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000314 : arena_(arena),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 blocks_(arena->Adapter(kArenaAllocBlockList)),
316 reverse_post_order_(arena->Adapter(kArenaAllocReversePostOrder)),
317 linear_order_(arena->Adapter(kArenaAllocLinearOrder)),
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700318 entry_block_(nullptr),
319 exit_block_(nullptr),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100320 maximum_number_of_out_vregs_(0),
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100321 number_of_vregs_(0),
322 number_of_in_vregs_(0),
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000323 temporaries_vreg_slots_(0),
Mark Mendell1152c922015-04-24 17:06:35 -0400324 has_bounds_checks_(false),
David Brazdil77a48ae2015-09-15 12:34:04 +0000325 has_try_catch_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000326 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000327 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000328 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100329 dex_file_(dex_file),
330 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100331 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100332 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100333 should_generate_constructor_barrier_(should_generate_constructor_barrier),
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 Geoffrayb331feb2016-02-05 16:51:53 +0000342 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700343 osr_(osr),
344 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100345 blocks_.reserve(kDefaultNumberOfBlocks);
346 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000347
David Brazdilbadd8262016-02-02 16:28:56 +0000348 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700349 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000350
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000351 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100352 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
353
David Brazdil69ba7b72015-06-23 18:27:30 +0100354 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000355 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100356
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000357 HBasicBlock* GetEntryBlock() const { return entry_block_; }
358 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100359 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
362 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000363
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000364 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100365
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100366 void ComputeDominanceInformation();
367 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000368 void ClearLoopInformation();
369 void FindBackEdges(ArenaBitVector* visited);
370 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100371 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100372 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000373
David Brazdil4833f5a2015-12-16 10:37:39 +0000374 // Analyze all natural loops in this graph. Returns a code specifying that it
375 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000376 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000377 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100378
David Brazdilffee3d32015-07-06 11:48:53 +0100379 // Iterate over blocks to compute try block membership. Needs reverse post
380 // order and loop information.
381 void ComputeTryBlockInformation();
382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000384 // Returns the instruction to replace the invoke expression or null if the
385 // invoke is for a void method. Note that the caller is responsible for replacing
386 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000387 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000388
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000389 // Update the loop and try membership of `block`, which was spawned from `reference`.
390 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
391 // should be the new back edge.
392 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
393 HBasicBlock* reference,
394 bool replace_if_back_edge);
395
Mingyao Yang3584bce2015-05-19 16:01:59 -0700396 // Need to add a couple of blocks to test if the loop body is entered and
397 // put deoptimization instructions, etc.
398 void TransformLoopHeaderForBCE(HBasicBlock* header);
399
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000400 // Removes `block` from the graph. Assumes `block` has been disconnected from
401 // other blocks and has no instructions or phis.
402 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000403
David Brazdilfc6a86a2015-06-26 10:33:45 +0000404 // Splits the edge between `block` and `successor` while preserving the
405 // indices in the predecessor/successor lists. If there are multiple edges
406 // between the blocks, the lowest indices are used.
407 // Returns the new block which is empty and has the same dex pc as `successor`.
408 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
409
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100410 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
411 void SimplifyLoop(HBasicBlock* header);
412
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000413 int32_t GetNextInstructionId() {
414 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000415 return current_instruction_id_++;
416 }
417
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000418 int32_t GetCurrentInstructionId() const {
419 return current_instruction_id_;
420 }
421
422 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000423 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000424 current_instruction_id_ = id;
425 }
426
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100427 uint16_t GetMaximumNumberOfOutVRegs() const {
428 return maximum_number_of_out_vregs_;
429 }
430
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000431 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
432 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100433 }
434
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100435 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
436 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
437 }
438
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000439 void UpdateTemporariesVRegSlots(size_t slots) {
440 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100441 }
442
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000443 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100444 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000445 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100446 }
447
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100448 void SetNumberOfVRegs(uint16_t number_of_vregs) {
449 number_of_vregs_ = number_of_vregs;
450 }
451
452 uint16_t GetNumberOfVRegs() const {
453 return number_of_vregs_;
454 }
455
456 void SetNumberOfInVRegs(uint16_t value) {
457 number_of_in_vregs_ = value;
458 }
459
David Brazdildee58d62016-04-07 09:54:26 +0000460 uint16_t GetNumberOfInVRegs() const {
461 return number_of_in_vregs_;
462 }
463
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100464 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100465 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100466 return number_of_vregs_ - number_of_in_vregs_;
467 }
468
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100469 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100470 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100471 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100472
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100473 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
474 DCHECK(GetReversePostOrder()[0] == entry_block_);
475 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
476 }
477
478 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
479 return ReverseRange(GetReversePostOrder());
480 }
481
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100482 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100483 return linear_order_;
484 }
485
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100486 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
487 return ReverseRange(GetLinearOrder());
488 }
489
Mark Mendell1152c922015-04-24 17:06:35 -0400490 bool HasBoundsChecks() const {
491 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800492 }
493
Mark Mendell1152c922015-04-24 17:06:35 -0400494 void SetHasBoundsChecks(bool value) {
495 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800496 }
497
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100498 bool ShouldGenerateConstructorBarrier() const {
499 return should_generate_constructor_barrier_;
500 }
501
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000502 bool IsDebuggable() const { return debuggable_; }
503
David Brazdil8d5b8b22015-03-24 10:51:52 +0000504 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000505 // already, it is created and inserted into the graph. This method is only for
506 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600507 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000508
509 // TODO: This is problematic for the consistency of reference type propagation
510 // because it can be created anytime after the pass and thus it will be left
511 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600512 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000513
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600514 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
515 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000516 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600517 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
518 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000519 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600520 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
521 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000522 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600523 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
524 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000525 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000526
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100527 HCurrentMethod* GetCurrentMethod();
528
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100529 const DexFile& GetDexFile() const {
530 return dex_file_;
531 }
532
533 uint32_t GetMethodIdx() const {
534 return method_idx_;
535 }
536
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100537 InvokeType GetInvokeType() const {
538 return invoke_type_;
539 }
540
Mark Mendellc4701932015-04-10 13:18:51 -0400541 InstructionSet GetInstructionSet() const {
542 return instruction_set_;
543 }
544
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000545 bool IsCompilingOsr() const { return osr_; }
546
Mingyao Yang063fc772016-08-02 11:02:54 -0700547 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
548 return cha_single_implementation_list_;
549 }
550
551 void AddCHASingleImplementationDependency(ArtMethod* method) {
552 cha_single_implementation_list_.insert(method);
553 }
554
555 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800556 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700557 }
558
David Brazdil77a48ae2015-09-15 12:34:04 +0000559 bool HasTryCatch() const { return has_try_catch_; }
560 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100561
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000562 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
563 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
564
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100565 ArtMethod* GetArtMethod() const { return art_method_; }
566 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
567
Mark Mendellf6529172015-11-17 11:16:56 -0500568 // Returns an instruction with the opposite boolean value from 'cond'.
569 // The instruction has been inserted into the graph, either as a constant, or
570 // before cursor.
571 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
572
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000573 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
574
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800575 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
576 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
577 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
578
David Brazdil2d7352b2015-04-20 14:52:42 +0100579 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000580 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100581 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000582
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000583 template <class InstructionType, typename ValueType>
584 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600585 ArenaSafeMap<ValueType, InstructionType*>* cache,
586 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 // Try to find an existing constant of the given value.
588 InstructionType* constant = nullptr;
589 auto cached_constant = cache->find(value);
590 if (cached_constant != cache->end()) {
591 constant = cached_constant->second;
592 }
593
594 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100595 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000596 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600597 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000598 cache->Overwrite(value, constant);
599 InsertConstant(constant);
600 }
601 return constant;
602 }
603
David Brazdil8d5b8b22015-03-24 10:51:52 +0000604 void InsertConstant(HConstant* instruction);
605
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000606 // Cache a float constant into the graph. This method should only be
607 // called by the SsaBuilder when creating "equivalent" instructions.
608 void CacheFloatConstant(HFloatConstant* constant);
609
610 // See CacheFloatConstant comment.
611 void CacheDoubleConstant(HDoubleConstant* constant);
612
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000613 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000614
615 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100616 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100618 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100619 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000620
Aart Bik281c6812016-08-26 11:31:48 -0700621 // List of blocks to perform a linear order tree traversal. Unlike the reverse
622 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100624
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 HBasicBlock* entry_block_;
626 HBasicBlock* exit_block_;
627
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100628 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100629 uint16_t maximum_number_of_out_vregs_;
630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100631 // The number of virtual registers in this method. Contains the parameters.
632 uint16_t number_of_vregs_;
633
634 // The number of virtual registers used by parameters of this method.
635 uint16_t number_of_in_vregs_;
636
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000637 // Number of vreg size slots that the temporaries use (used in baseline compiler).
638 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100639
Mark Mendell1152c922015-04-24 17:06:35 -0400640 // Has bounds checks. We can totally skip BCE if it's false.
641 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800642
David Brazdil77a48ae2015-09-15 12:34:04 +0000643 // Flag whether there are any try/catch blocks in the graph. We will skip
644 // try/catch-related passes if false.
645 bool has_try_catch_;
646
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000647 // Flag whether there are any irreducible loops in the graph.
648 bool has_irreducible_loops_;
649
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000650 // Indicates whether the graph should be compiled in a way that
651 // ensures full debuggability. If false, we can apply more
652 // aggressive optimizations that may limit the level of debugging.
653 const bool debuggable_;
654
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000655 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000656 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000657
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100658 // The dex file from which the method is from.
659 const DexFile& dex_file_;
660
661 // The method index in the dex file.
662 const uint32_t method_idx_;
663
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100664 // If inlined, this encodes how the callee is being invoked.
665 const InvokeType invoke_type_;
666
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100667 // Whether the graph has been transformed to SSA form. Only used
668 // in debug mode to ensure we are not using properties only valid
669 // for non-SSA form (like the number of temporaries).
670 bool in_ssa_form_;
671
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100672 const bool should_generate_constructor_barrier_;
673
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800674 // Number of CHA guards in the graph. Used to short-circuit the
675 // CHA guard optimization pass when there is no CHA guard left.
676 uint32_t number_of_cha_guards_;
677
Mathieu Chartiere401d142015-04-22 13:56:20 -0700678 const InstructionSet instruction_set_;
679
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000680 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000681 HNullConstant* cached_null_constant_;
682 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000683 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000684 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000685 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000686
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100687 HCurrentMethod* cached_current_method_;
688
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100689 // The ArtMethod this graph is for. Note that for AOT, it may be null,
690 // for example for methods whose declaring class could not be resolved
691 // (such as when the superclass could not be found).
692 ArtMethod* art_method_;
693
David Brazdil4833f5a2015-12-16 10:37:39 +0000694 // Keep the RTI of inexact Object to avoid having to pass stack handle
695 // collection pointer to passes which may create NullConstant.
696 ReferenceTypeInfo inexact_object_rti_;
697
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000698 // Whether we are compiling this graph for on stack replacement: this will
699 // make all loops seen as irreducible and emit special stack maps to mark
700 // compiled code entries which the interpreter can directly jump to.
701 const bool osr_;
702
Mingyao Yang063fc772016-08-02 11:02:54 -0700703 // List of methods that are assumed to have single implementation.
704 ArenaSet<ArtMethod*> cha_single_implementation_list_;
705
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000706 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100707 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000708 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000709 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000710 DISALLOW_COPY_AND_ASSIGN(HGraph);
711};
712
Vladimir Markof9f64412015-09-02 14:05:49 +0100713class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000714 public:
715 HLoopInformation(HBasicBlock* header, HGraph* graph)
716 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100717 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000718 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100719 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100720 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100721 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000722 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100723 back_edges_.reserve(kDefaultNumberOfBackEdges);
724 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100725
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000726 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100727 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000728
729 void Dump(std::ostream& os);
730
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100731 HBasicBlock* GetHeader() const {
732 return header_;
733 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000734
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000735 void SetHeader(HBasicBlock* block) {
736 header_ = block;
737 }
738
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100739 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
740 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
741 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
742
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000743 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100744 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000745 }
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100748 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100749 }
750
David Brazdil46e2a392015-03-16 17:31:52 +0000751 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100752 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100753 }
754
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000755 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100756 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000757 }
758
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100759 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100760
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100761 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100762 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100763 }
764
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100765 // Returns the lifetime position of the back edge that has the
766 // greatest lifetime position.
767 size_t GetLifetimeEnd() const;
768
769 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100770 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100771 }
772
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000773 // Finds blocks that are part of this loop.
774 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100775
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100776 // Returns whether this loop information contains `block`.
777 // Note that this loop information *must* be populated before entering this function.
778 bool Contains(const HBasicBlock& block) const;
779
780 // Returns whether this loop information is an inner loop of `other`.
781 // Note that `other` *must* be populated before entering this function.
782 bool IsIn(const HLoopInformation& other) const;
783
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800784 // Returns true if instruction is not defined within this loop.
785 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700786
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100787 const ArenaBitVector& GetBlocks() const { return blocks_; }
788
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000789 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000790 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000791
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000792 void ClearAllBlocks() {
793 blocks_.ClearAllBits();
794 }
795
David Brazdil3f4a5222016-05-06 12:46:21 +0100796 bool HasBackEdgeNotDominatedByHeader() const;
797
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100798 bool IsPopulated() const {
799 return blocks_.GetHighestBitSet() != -1;
800 }
801
Anton Shaminf89381f2016-05-16 16:44:13 +0600802 bool DominatesAllBackEdges(HBasicBlock* block);
803
David Sehrc757dec2016-11-04 15:48:34 -0700804 bool HasExitEdge() const;
805
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000806 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100807 // Internal recursive implementation of `Populate`.
808 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100809 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100810
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000811 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100812 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000813 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100815 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100816 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000817
818 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
819};
820
David Brazdilec16f792015-08-19 15:04:01 +0100821// Stores try/catch information for basic blocks.
822// Note that HGraph is constructed so that catch blocks cannot simultaneously
823// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100824class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100825 public:
826 // Try block information constructor.
827 explicit TryCatchInformation(const HTryBoundary& try_entry)
828 : try_entry_(&try_entry),
829 catch_dex_file_(nullptr),
830 catch_type_index_(DexFile::kDexNoIndex16) {
831 DCHECK(try_entry_ != nullptr);
832 }
833
834 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800835 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100836 : try_entry_(nullptr),
837 catch_dex_file_(&dex_file),
838 catch_type_index_(catch_type_index) {}
839
840 bool IsTryBlock() const { return try_entry_ != nullptr; }
841
842 const HTryBoundary& GetTryEntry() const {
843 DCHECK(IsTryBlock());
844 return *try_entry_;
845 }
846
847 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
848
849 bool IsCatchAllTypeIndex() const {
850 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100852 }
853
Andreas Gampea5b09a62016-11-17 15:21:22 -0800854 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100855 DCHECK(IsCatchBlock());
856 return catch_type_index_;
857 }
858
859 const DexFile& GetCatchDexFile() const {
860 DCHECK(IsCatchBlock());
861 return *catch_dex_file_;
862 }
863
864 private:
865 // One of possibly several TryBoundary instructions entering the block's try.
866 // Only set for try blocks.
867 const HTryBoundary* try_entry_;
868
869 // Exception type information. Only set for catch blocks.
870 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800871 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100872};
873
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100874static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100875static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100876
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000877// A block in a method. Contains the list of instructions represented
878// as a double linked list. Each block knows its predecessors and
879// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100880
Vladimir Markof9f64412015-09-02 14:05:49 +0100881class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000882 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700883 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000884 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000885 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
886 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000887 loop_information_(nullptr),
888 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000889 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100890 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100891 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000893 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000894 try_catch_information_(nullptr) {
895 predecessors_.reserve(kDefaultNumberOfPredecessors);
896 successors_.reserve(kDefaultNumberOfSuccessors);
897 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
898 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000899
Vladimir Marko60584552015-09-03 13:35:12 +0000900 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100901 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000902 }
903
Vladimir Marko60584552015-09-03 13:35:12 +0000904 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100905 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000906 }
907
David Brazdild26a4112015-11-10 11:07:31 +0000908 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
909 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
910
Vladimir Marko60584552015-09-03 13:35:12 +0000911 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
912 return ContainsElement(successors_, block, start_from);
913 }
914
915 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100916 return dominated_blocks_;
917 }
918
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100919 bool IsEntryBlock() const {
920 return graph_->GetEntryBlock() == this;
921 }
922
923 bool IsExitBlock() const {
924 return graph_->GetExitBlock() == this;
925 }
926
David Brazdil46e2a392015-03-16 17:31:52 +0000927 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000928 bool IsSingleTryBoundary() const;
929
930 // Returns true if this block emits nothing but a jump.
931 bool IsSingleJump() const {
932 HLoopInformation* loop_info = GetLoopInformation();
933 return (IsSingleGoto() || IsSingleTryBoundary())
934 // Back edges generate a suspend check.
935 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
936 }
David Brazdil46e2a392015-03-16 17:31:52 +0000937
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000938 void AddBackEdge(HBasicBlock* back_edge) {
939 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000940 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000941 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100942 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000943 loop_information_->AddBackEdge(back_edge);
944 }
945
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000946 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000947 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000948
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100949 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000950 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600951 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000952
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000953 HBasicBlock* GetDominator() const { return dominator_; }
954 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000955 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
956
957 void RemoveDominatedBlock(HBasicBlock* block) {
958 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100959 }
Vladimir Marko60584552015-09-03 13:35:12 +0000960
961 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
962 ReplaceElement(dominated_blocks_, existing, new_block);
963 }
964
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100965 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000966
967 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100968 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000969 }
970
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100971 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
972 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100973 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100975 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
976 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000977
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000978 HInstruction* GetFirstInstructionDisregardMoves() const;
979
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000980 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000981 successors_.push_back(block);
982 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000983 }
984
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100985 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
986 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100987 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000988 new_block->predecessors_.push_back(this);
989 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000990 }
991
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000992 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
993 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000994 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +0000995 new_block->successors_.push_back(this);
996 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000997 }
998
Nicolas Geoffray8b20f882015-06-19 16:17:05 +0100999 // Insert `this` between `predecessor` and `successor. This method
1000 // preserves the indicies, and will update the first edge found between
1001 // `predecessor` and `successor`.
1002 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1003 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001004 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001005 successor->predecessors_[predecessor_index] = this;
1006 predecessor->successors_[successor_index] = this;
1007 successors_.push_back(successor);
1008 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001009 }
1010
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001011 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001012 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001013 }
1014
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001015 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001016 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001017 }
1018
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001019 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001020 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001021 }
1022
1023 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001024 predecessors_.push_back(block);
1025 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001026 }
1027
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001028 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001029 DCHECK_EQ(predecessors_.size(), 2u);
1030 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001031 }
1032
David Brazdil769c9e52015-04-27 13:54:09 +01001033 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001034 DCHECK_EQ(successors_.size(), 2u);
1035 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001036 }
1037
David Brazdilfc6a86a2015-06-26 10:33:45 +00001038 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001039 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001040 }
1041
David Brazdilfc6a86a2015-06-26 10:33:45 +00001042 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001043 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001044 }
1045
David Brazdilfc6a86a2015-06-26 10:33:45 +00001046 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001047 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001048 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001049 }
1050
1051 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001052 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001053 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 }
1055
1056 // Returns whether the first occurrence of `predecessor` in the list of
1057 // predecessors is at index `idx`.
1058 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001059 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001060 return GetPredecessorIndexOf(predecessor) == idx;
1061 }
1062
David Brazdild7558da2015-09-22 13:04:14 +01001063 // Create a new block between this block and its predecessors. The new block
1064 // is added to the graph, all predecessor edges are relinked to it and an edge
1065 // is created to `this`. Returns the new empty block. Reverse post order or
1066 // loop and try/catch information are not updated.
1067 HBasicBlock* CreateImmediateDominator();
1068
David Brazdilfc6a86a2015-06-26 10:33:45 +00001069 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001070 // created, latter block. Note that this method will add the block to the
1071 // graph, create a Goto at the end of the former block and will create an edge
1072 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001073 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001074 HBasicBlock* SplitBefore(HInstruction* cursor);
1075
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001076 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001077 // created block. Note that this method just updates raw block information,
1078 // like predecessors, successors, dominators, and instruction list. It does not
1079 // update the graph, reverse post order, loop information, nor make sure the
1080 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001081 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1082
1083 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1084 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001085
1086 // Merge `other` at the end of `this`. Successors and dominated blocks of
1087 // `other` are changed to be successors and dominated blocks of `this`. Note
1088 // that this method does not update the graph, reverse post order, loop
1089 // information, nor make sure the blocks are consistent (for example ending
1090 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001091 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001092
1093 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1094 // of `this` are moved to `other`.
1095 // Note that this method does not update the graph, reverse post order, loop
1096 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001097 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001098 void ReplaceWith(HBasicBlock* other);
1099
Aart Bik6b69e0a2017-01-11 10:20:43 -08001100 // Merges the instructions of `other` at the end of `this`.
1101 void MergeInstructionsWith(HBasicBlock* other);
1102
David Brazdil2d7352b2015-04-20 14:52:42 +01001103 // Merge `other` at the end of `this`. This method updates loops, reverse post
1104 // order, links to predecessors, successors, dominators and deletes the block
1105 // from the graph. The two blocks must be successive, i.e. `this` the only
1106 // predecessor of `other` and vice versa.
1107 void MergeWith(HBasicBlock* other);
1108
1109 // Disconnects `this` from all its predecessors, successors and dominator,
1110 // removes it from all loops it is included in and eventually from the graph.
1111 // The block must not dominate any other block. Predecessors and successors
1112 // are safely updated.
1113 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001114
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001115 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001116 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001117 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001118 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001119 // Replace instruction `initial` with `replacement` within this block.
1120 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1121 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001122 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001123 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001124 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1125 // instruction list. With 'ensure_safety' set to true, it verifies that the
1126 // instruction is not in use and removes it from the use lists of its inputs.
1127 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1128 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001129 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001130
1131 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001132 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001133 }
1134
Roland Levillain6b879dd2014-09-22 17:13:44 +01001135 bool IsLoopPreHeaderFirstPredecessor() const {
1136 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001137 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001138 }
1139
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001140 bool IsFirstPredecessorBackEdge() const {
1141 DCHECK(IsLoopHeader());
1142 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1143 }
1144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001145 HLoopInformation* GetLoopInformation() const {
1146 return loop_information_;
1147 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001148
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001149 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001150 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001151 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001152 void SetInLoop(HLoopInformation* info) {
1153 if (IsLoopHeader()) {
1154 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001155 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001156 loop_information_ = info;
1157 } else if (loop_information_->Contains(*info->GetHeader())) {
1158 // Block is currently part of an outer loop. Make it part of this inner loop.
1159 // Note that a non loop header having a loop information means this loop information
1160 // has already been populated
1161 loop_information_ = info;
1162 } else {
1163 // Block is part of an inner loop. Do not update the loop information.
1164 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1165 // at this point, because this method is being called while populating `info`.
1166 }
1167 }
1168
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001169 // Raw update of the loop information.
1170 void SetLoopInformation(HLoopInformation* info) {
1171 loop_information_ = info;
1172 }
1173
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001174 bool IsInLoop() const { return loop_information_ != nullptr; }
1175
David Brazdilec16f792015-08-19 15:04:01 +01001176 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1177
1178 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1179 try_catch_information_ = try_catch_information;
1180 }
1181
1182 bool IsTryBlock() const {
1183 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1184 }
1185
1186 bool IsCatchBlock() const {
1187 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1188 }
David Brazdilffee3d32015-07-06 11:48:53 +01001189
1190 // Returns the try entry that this block's successors should have. They will
1191 // be in the same try, unless the block ends in a try boundary. In that case,
1192 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001193 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001194
David Brazdild7558da2015-09-22 13:04:14 +01001195 bool HasThrowingInstructions() const;
1196
David Brazdila4b8c212015-05-07 09:59:30 +01001197 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001198 bool Dominates(HBasicBlock* block) const;
1199
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001200 size_t GetLifetimeStart() const { return lifetime_start_; }
1201 size_t GetLifetimeEnd() const { return lifetime_end_; }
1202
1203 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1204 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1205
David Brazdil8d5b8b22015-03-24 10:51:52 +00001206 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001207 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001208 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001209 bool HasSinglePhi() const;
1210
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001211 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001212 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001213 ArenaVector<HBasicBlock*> predecessors_;
1214 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001215 HInstructionList instructions_;
1216 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001217 HLoopInformation* loop_information_;
1218 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001219 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001220 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001221 // The dex program counter of the first instruction of this block.
1222 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001223 size_t lifetime_start_;
1224 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001225 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001226
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001227 friend class HGraph;
1228 friend class HInstruction;
1229
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001230 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1231};
1232
David Brazdilb2bd1c52015-03-25 11:17:37 +00001233// Iterates over the LoopInformation of all loops which contain 'block'
1234// from the innermost to the outermost.
1235class HLoopInformationOutwardIterator : public ValueObject {
1236 public:
1237 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1238 : current_(block.GetLoopInformation()) {}
1239
1240 bool Done() const { return current_ == nullptr; }
1241
1242 void Advance() {
1243 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001244 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001245 }
1246
1247 HLoopInformation* Current() const {
1248 DCHECK(!Done());
1249 return current_;
1250 }
1251
1252 private:
1253 HLoopInformation* current_;
1254
1255 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1256};
1257
Alexandre Ramesef20f712015-06-09 10:29:30 +01001258#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001259 M(Above, Condition) \
1260 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001262 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001263 M(ArrayGet, Instruction) \
1264 M(ArrayLength, Instruction) \
1265 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001266 M(Below, Condition) \
1267 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001268 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001269 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001270 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001271 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001272 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001273 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001274 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001275 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001276 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001277 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001278 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001280 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001281 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001282 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001283 M(Exit, Instruction) \
1284 M(FloatConstant, Constant) \
1285 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001286 M(GreaterThan, Condition) \
1287 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001288 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001289 M(InstanceFieldGet, Instruction) \
1290 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001291 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001292 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001293 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001294 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001295 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001296 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001297 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001298 M(LessThan, Condition) \
1299 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001300 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001301 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001302 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001303 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001304 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001305 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001306 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001307 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001308 M(Neg, UnaryOperation) \
1309 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001311 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001312 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001313 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001315 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001316 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001317 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001318 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001320 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001321 M(Return, Instruction) \
1322 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001323 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001324 M(Shl, BinaryOperation) \
1325 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001326 M(StaticFieldGet, Instruction) \
1327 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001328 M(UnresolvedInstanceFieldGet, Instruction) \
1329 M(UnresolvedInstanceFieldSet, Instruction) \
1330 M(UnresolvedStaticFieldGet, Instruction) \
1331 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001332 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001334 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001335 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001336 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001337 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001338 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001339 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001340
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001341/*
1342 * Instructions, shared across several (not all) architectures.
1343 */
1344#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1345#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1346#else
1347#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001348 M(BitwiseNegatedRight, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001349 M(MultiplyAccumulate, Instruction) \
1350 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001351#endif
1352
Vladimir Markob4536b72015-11-24 13:45:23 +00001353#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001354#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001355#else
1356#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1357 M(ArmDexCacheArraysBase, Instruction)
1358#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001359
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001360#ifndef ART_ENABLE_CODEGEN_arm64
Alexandre Ramesef20f712015-06-09 10:29:30 +01001361#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001362#else
1363#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Artem Serov328429f2016-07-06 16:23:04 +01001364 M(Arm64DataProcWithShifterOp, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001365#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001366
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001367#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001369#else
1370#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1371 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001372 M(MipsDexCacheArraysBase, Instruction) \
1373 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001374#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001375
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001376#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1377
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001378#ifndef ART_ENABLE_CODEGEN_x86
1379#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1380#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001381#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1382 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001383 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001384 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001385 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001386#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001387
1388#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1389
1390#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1391 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001392 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001393 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1394 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001396 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001397 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1398 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1399
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001400#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1401 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001402 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001403 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001404 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001405 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001406
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001407#define FOR_EACH_INSTRUCTION(M) \
1408 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1409 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1410
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001412FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1413#undef FORWARD_DECLARATION
1414
Vladimir Marko372f10e2016-05-17 16:30:10 +01001415#define DECLARE_INSTRUCTION(type) \
1416 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1417 const char* DebugName() const OVERRIDE { return #type; } \
1418 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1419 return other->Is##type(); \
1420 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001421 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001422
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001423#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1424 bool Is##type() const { return As##type() != nullptr; } \
1425 const H##type* As##type() const { return this; } \
1426 H##type* As##type() { return this; }
1427
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001428template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001429class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001430 public:
David Brazdiled596192015-01-23 10:39:45 +00001431 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001432 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001433 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001434
Vladimir Marko46817b82016-03-29 12:21:58 +01001435 // Hook for the IntrusiveForwardList<>.
1436 // TODO: Hide this better.
1437 IntrusiveForwardListHook hook;
1438
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001439 private:
David Brazdiled596192015-01-23 10:39:45 +00001440 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001441 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001442
1443 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001444 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001445
Vladimir Marko46817b82016-03-29 12:21:58 +01001446 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001447
1448 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1449};
1450
David Brazdiled596192015-01-23 10:39:45 +00001451template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001452using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001453
David Brazdil1abb4192015-02-17 18:33:36 +00001454// This class is used by HEnvironment and HInstruction classes to record the
1455// instructions they use and pointers to the corresponding HUseListNodes kept
1456// by the used instructions.
1457template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001458class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001459 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001460 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1461 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001462
Vladimir Marko46817b82016-03-29 12:21:58 +01001463 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1464 : HUserRecord(old_record.instruction_, before_use_node) {}
1465 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1466 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001467 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001468 }
1469
1470 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001471 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1472 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001473
1474 private:
1475 // Instruction used by the user.
1476 HInstruction* instruction_;
1477
Vladimir Marko46817b82016-03-29 12:21:58 +01001478 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1479 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001480};
1481
Vladimir Markoe9004912016-06-16 16:50:52 +01001482// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1483// This is used for HInstruction::GetInputs() to return a container wrapper providing
1484// HInstruction* values even though the underlying container has HUserRecord<>s.
1485struct HInputExtractor {
1486 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1487 return record.GetInstruction();
1488 }
1489 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1490 return record.GetInstruction();
1491 }
1492};
1493
1494using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1495using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1496
Aart Bik854a02b2015-07-14 16:07:00 -07001497/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001498 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001499 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001500 * For write/read dependences on fields/arrays, the dependence analysis uses
1501 * type disambiguation (e.g. a float field write cannot modify the value of an
1502 * integer field read) and the access type (e.g. a reference array write cannot
1503 * modify the value of a reference field read [although it may modify the
1504 * reference fetch prior to reading the field, which is represented by its own
1505 * write/read dependence]). The analysis makes conservative points-to
1506 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1507 * the same, and any reference read depends on any reference read without
1508 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001509 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 * The internal representation uses 38-bit and is described in the table below.
1511 * The first line indicates the side effect, and for field/array accesses the
1512 * second line indicates the type of the access (in the order of the
1513 * Primitive::Type enum).
1514 * The two numbered lines below indicate the bit position in the bitfield (read
1515 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001516 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001517 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1518 * +-------------+---------+---------+--------------+---------+---------+
1519 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1520 * | 3 |333333322|222222221| 1 |111111110|000000000|
1521 * | 7 |654321098|765432109| 8 |765432109|876543210|
1522 *
1523 * Note that, to ease the implementation, 'changes' bits are least significant
1524 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001525 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001526class SideEffects : public ValueObject {
1527 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001528 SideEffects() : flags_(0) {}
1529
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001530 static SideEffects None() {
1531 return SideEffects(0);
1532 }
1533
1534 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001535 return SideEffects(kAllChangeBits | kAllDependOnBits);
1536 }
1537
1538 static SideEffects AllChanges() {
1539 return SideEffects(kAllChangeBits);
1540 }
1541
1542 static SideEffects AllDependencies() {
1543 return SideEffects(kAllDependOnBits);
1544 }
1545
1546 static SideEffects AllExceptGCDependency() {
1547 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1548 }
1549
1550 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001551 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001552 }
1553
Aart Bik34c3ba92015-07-20 14:08:59 -07001554 static SideEffects AllWrites() {
1555 return SideEffects(kAllWrites);
1556 }
1557
1558 static SideEffects AllReads() {
1559 return SideEffects(kAllReads);
1560 }
1561
1562 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1563 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001564 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001565 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001566 }
1567
Aart Bik854a02b2015-07-14 16:07:00 -07001568 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001569 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001570 }
1571
Aart Bik34c3ba92015-07-20 14:08:59 -07001572 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1573 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001574 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001575 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001576 }
1577
1578 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001579 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001580 }
1581
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001582 static SideEffects CanTriggerGC() {
1583 return SideEffects(1ULL << kCanTriggerGCBit);
1584 }
1585
1586 static SideEffects DependsOnGC() {
1587 return SideEffects(1ULL << kDependsOnGCBit);
1588 }
1589
Aart Bik854a02b2015-07-14 16:07:00 -07001590 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001591 SideEffects Union(SideEffects other) const {
1592 return SideEffects(flags_ | other.flags_);
1593 }
1594
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001595 SideEffects Exclusion(SideEffects other) const {
1596 return SideEffects(flags_ & ~other.flags_);
1597 }
1598
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001599 void Add(SideEffects other) {
1600 flags_ |= other.flags_;
1601 }
1602
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001603 bool Includes(SideEffects other) const {
1604 return (other.flags_ & flags_) == other.flags_;
1605 }
1606
1607 bool HasSideEffects() const {
1608 return (flags_ & kAllChangeBits);
1609 }
1610
1611 bool HasDependencies() const {
1612 return (flags_ & kAllDependOnBits);
1613 }
1614
1615 // Returns true if there are no side effects or dependencies.
1616 bool DoesNothing() const {
1617 return flags_ == 0;
1618 }
1619
Aart Bik854a02b2015-07-14 16:07:00 -07001620 // Returns true if something is written.
1621 bool DoesAnyWrite() const {
1622 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001623 }
1624
Aart Bik854a02b2015-07-14 16:07:00 -07001625 // Returns true if something is read.
1626 bool DoesAnyRead() const {
1627 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001628 }
1629
Aart Bik854a02b2015-07-14 16:07:00 -07001630 // Returns true if potentially everything is written and read
1631 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001632 bool DoesAllReadWrite() const {
1633 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1634 }
1635
Aart Bik854a02b2015-07-14 16:07:00 -07001636 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001637 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001638 }
1639
Roland Levillain0d5a2812015-11-13 10:07:31 +00001640 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001641 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001642 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1643 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001644 }
1645
1646 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001647 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001648 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001649 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001650 for (int s = kLastBit; s >= 0; s--) {
1651 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1652 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1653 // This is a bit for the GC side effect.
1654 if (current_bit_is_set) {
1655 flags += "GC";
1656 }
Aart Bik854a02b2015-07-14 16:07:00 -07001657 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001658 } else {
1659 // This is a bit for the array/field analysis.
1660 // The underscore character stands for the 'can trigger GC' bit.
1661 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1662 if (current_bit_is_set) {
1663 flags += kDebug[s];
1664 }
1665 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1666 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1667 flags += "|";
1668 }
1669 }
Aart Bik854a02b2015-07-14 16:07:00 -07001670 }
1671 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001672 }
1673
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001674 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001675
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001676 private:
1677 static constexpr int kFieldArrayAnalysisBits = 9;
1678
1679 static constexpr int kFieldWriteOffset = 0;
1680 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1681 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1682 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1683
1684 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1685
1686 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1687 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1688 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1689 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1690
1691 static constexpr int kLastBit = kDependsOnGCBit;
1692 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1693
1694 // Aliases.
1695
1696 static_assert(kChangeBits == kDependOnBits,
1697 "the 'change' bits should match the 'depend on' bits.");
1698
1699 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1700 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1701 static constexpr uint64_t kAllWrites =
1702 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1703 static constexpr uint64_t kAllReads =
1704 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001705
Aart Bik854a02b2015-07-14 16:07:00 -07001706 // Translates type to bit flag.
1707 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1708 CHECK_NE(type, Primitive::kPrimVoid);
1709 const uint64_t one = 1;
1710 const int shift = type; // 0-based consecutive enum
1711 DCHECK_LE(kFieldWriteOffset, shift);
1712 DCHECK_LT(shift, kArrayWriteOffset);
1713 return one << (type + offset);
1714 }
1715
1716 // Private constructor on direct flags value.
1717 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1718
1719 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001720};
1721
David Brazdiled596192015-01-23 10:39:45 +00001722// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001723class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001724 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001725 HEnvironment(ArenaAllocator* arena,
1726 size_t number_of_vregs,
1727 const DexFile& dex_file,
1728 uint32_t method_idx,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001729 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001730 InvokeType invoke_type,
1731 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001732 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1733 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001734 parent_(nullptr),
1735 dex_file_(dex_file),
1736 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001737 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001738 invoke_type_(invoke_type),
1739 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001740 }
1741
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001742 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001743 : HEnvironment(arena,
1744 to_copy.Size(),
1745 to_copy.GetDexFile(),
1746 to_copy.GetMethodIdx(),
1747 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001748 to_copy.GetInvokeType(),
1749 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001750
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001751 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001752 if (parent_ != nullptr) {
1753 parent_->SetAndCopyParentChain(allocator, parent);
1754 } else {
1755 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1756 parent_->CopyFrom(parent);
1757 if (parent->GetParent() != nullptr) {
1758 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1759 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001760 }
David Brazdiled596192015-01-23 10:39:45 +00001761 }
1762
Vladimir Marko71bf8092015-09-15 15:33:14 +01001763 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001764 void CopyFrom(HEnvironment* environment);
1765
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001766 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1767 // input to the loop phi instead. This is for inserting instructions that
1768 // require an environment (like HDeoptimization) in the loop pre-header.
1769 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001770
1771 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001772 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001773 }
1774
1775 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001776 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001777 }
1778
David Brazdil1abb4192015-02-17 18:33:36 +00001779 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001780
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001781 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001782
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001783 HEnvironment* GetParent() const { return parent_; }
1784
1785 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001786 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001787 }
1788
1789 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001790 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001791 }
1792
1793 uint32_t GetDexPc() const {
1794 return dex_pc_;
1795 }
1796
1797 uint32_t GetMethodIdx() const {
1798 return method_idx_;
1799 }
1800
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001801 InvokeType GetInvokeType() const {
1802 return invoke_type_;
1803 }
1804
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 const DexFile& GetDexFile() const {
1806 return dex_file_;
1807 }
1808
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001809 HInstruction* GetHolder() const {
1810 return holder_;
1811 }
1812
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001813
1814 bool IsFromInlinedInvoke() const {
1815 return GetParent() != nullptr;
1816 }
1817
David Brazdiled596192015-01-23 10:39:45 +00001818 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001819 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1820 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001821 HEnvironment* parent_;
1822 const DexFile& dex_file_;
1823 const uint32_t method_idx_;
1824 const uint32_t dex_pc_;
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001825 const InvokeType invoke_type_;
David Brazdiled596192015-01-23 10:39:45 +00001826
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001827 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001828 HInstruction* const holder_;
1829
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001830 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001831
1832 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1833};
1834
Vladimir Markof9f64412015-09-02 14:05:49 +01001835class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001836 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001837 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838 : previous_(nullptr),
1839 next_(nullptr),
1840 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001841 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001842 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001843 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001844 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001845 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001846 locations_(nullptr),
1847 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001848 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001849 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001850 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1851 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1852 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001853
Dave Allison20dfc792014-06-16 20:44:29 -07001854 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001855
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001856#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001857 enum InstructionKind {
1858 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1859 };
1860#undef DECLARE_KIND
1861
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001862 HInstruction* GetNext() const { return next_; }
1863 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001864
Calin Juravle77520bc2015-01-12 18:45:46 +00001865 HInstruction* GetNextDisregardingMoves() const;
1866 HInstruction* GetPreviousDisregardingMoves() const;
1867
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001868 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001869 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001871 bool IsInBlock() const { return block_ != nullptr; }
1872 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001873 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1874 bool IsIrreducibleLoopHeaderPhi() const {
1875 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1876 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001877
Vladimir Marko372f10e2016-05-17 16:30:10 +01001878 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1879
1880 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1881 // One virtual method is enough, just const_cast<> and then re-add the const.
1882 return ArrayRef<const HUserRecord<HInstruction*>>(
1883 const_cast<HInstruction*>(this)->GetInputRecords());
1884 }
1885
Vladimir Markoe9004912016-06-16 16:50:52 +01001886 HInputsRef GetInputs() {
1887 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001888 }
1889
Vladimir Markoe9004912016-06-16 16:50:52 +01001890 HConstInputsRef GetInputs() const {
1891 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001892 }
1893
1894 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001895 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001896
Aart Bik2767f4b2016-10-28 15:03:53 -07001897 bool HasInput(HInstruction* input) const {
1898 for (const HInstruction* i : GetInputs()) {
1899 if (i == input) {
1900 return true;
1901 }
1902 }
1903 return false;
1904 }
1905
Vladimir Marko372f10e2016-05-17 16:30:10 +01001906 void SetRawInputAt(size_t index, HInstruction* input) {
1907 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1908 }
1909
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001910 virtual void Accept(HGraphVisitor* visitor) = 0;
1911 virtual const char* DebugName() const = 0;
1912
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1914
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001915 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001916
1917 uint32_t GetDexPc() const { return dex_pc_; }
1918
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001919 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001920
Roland Levillaine161a2a2014-10-03 12:45:18 +01001921 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001922 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001923
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001924 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001925 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001926
Calin Juravle10e244f2015-01-26 18:54:32 +00001927 // Does not apply for all instructions, but having this at top level greatly
1928 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001929 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001930 virtual bool CanBeNull() const {
1931 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1932 return true;
1933 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001934
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001935 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001936 return false;
1937 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001938
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001939 virtual bool IsActualObject() const {
1940 return GetType() == Primitive::kPrimNot;
1941 }
1942
Calin Juravle2e768302015-07-28 14:41:11 +00001943 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001944
Calin Juravle61d544b2015-02-23 16:46:57 +00001945 ReferenceTypeInfo GetReferenceTypeInfo() const {
1946 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001947 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001948 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001949 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001950
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001951 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001952 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001953 // Note: fixup_end remains valid across push_front().
1954 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1955 HUseListNode<HInstruction*>* new_node =
1956 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1957 uses_.push_front(*new_node);
1958 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959 }
1960
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001961 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001962 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001963 // Note: env_fixup_end remains valid across push_front().
1964 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1965 HUseListNode<HEnvironment*>* new_node =
1966 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1967 env_uses_.push_front(*new_node);
1968 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001969 }
1970
David Brazdil1abb4192015-02-17 18:33:36 +00001971 void RemoveAsUserOfInput(size_t input) {
1972 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001973 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1974 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1975 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001976 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001977
Vladimir Marko372f10e2016-05-17 16:30:10 +01001978 void RemoveAsUserOfAllInputs() {
1979 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1980 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1981 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1982 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1983 }
1984 }
1985
David Brazdil1abb4192015-02-17 18:33:36 +00001986 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1987 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001988
Vladimir Marko46817b82016-03-29 12:21:58 +01001989 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1990 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1991 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001992 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001993 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001994 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001995
Aart Bikcc42be02016-10-20 16:14:16 -07001996 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001997 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001998 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001999 !CanThrow() &&
2000 !IsSuspendCheck() &&
2001 !IsControlFlow() &&
2002 !IsNativeDebugInfo() &&
2003 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002004 // If we added an explicit barrier then we should keep it.
2005 !IsMemoryBarrier();
2006 }
2007
Aart Bikcc42be02016-10-20 16:14:16 -07002008 bool IsDeadAndRemovable() const {
2009 return IsRemovable() && !HasUses();
2010 }
2011
Roland Levillain6c82d402014-10-13 16:10:27 +01002012 // Does this instruction strictly dominate `other_instruction`?
2013 // Returns false if this instruction and `other_instruction` are the same.
2014 // Aborts if this instruction and `other_instruction` are both phis.
2015 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002016
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002017 int GetId() const { return id_; }
2018 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002019
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002020 int GetSsaIndex() const { return ssa_index_; }
2021 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2022 bool HasSsaIndex() const { return ssa_index_ != -1; }
2023
2024 bool HasEnvironment() const { return environment_ != nullptr; }
2025 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002026 // Set the `environment_` field. Raw because this method does not
2027 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002028 void SetRawEnvironment(HEnvironment* environment) {
2029 DCHECK(environment_ == nullptr);
2030 DCHECK_EQ(environment->GetHolder(), this);
2031 environment_ = environment;
2032 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002033
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002034 void InsertRawEnvironment(HEnvironment* environment) {
2035 DCHECK(environment_ != nullptr);
2036 DCHECK_EQ(environment->GetHolder(), this);
2037 DCHECK(environment->GetParent() == nullptr);
2038 environment->parent_ = environment_;
2039 environment_ = environment;
2040 }
2041
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002042 void RemoveEnvironment();
2043
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002044 // Set the environment of this instruction, copying it from `environment`. While
2045 // copying, the uses lists are being updated.
2046 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002047 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002048 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002049 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002050 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002051 if (environment->GetParent() != nullptr) {
2052 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2053 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002054 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002055
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002056 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2057 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002058 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002059 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002060 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002061 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002062 if (environment->GetParent() != nullptr) {
2063 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2064 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002065 }
2066
Nicolas Geoffray39468442014-09-02 15:17:15 +01002067 // Returns the number of entries in the environment. Typically, that is the
2068 // number of dex registers in a method. It could be more in case of inlining.
2069 size_t EnvironmentSize() const;
2070
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002071 LocationSummary* GetLocations() const { return locations_; }
2072 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002073
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002074 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002075 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002076
Alexandre Rames188d4312015-04-09 18:30:21 +01002077 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2078 // uses of this instruction by `other` are *not* updated.
2079 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2080 ReplaceWith(other);
2081 other->ReplaceInput(this, use_index);
2082 }
2083
Nicolas Geoffray82091da2015-01-26 10:02:45 +00002084 // Move `this` instruction before `cursor`.
2085 void MoveBefore(HInstruction* cursor);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002086
Vladimir Markofb337ea2015-11-25 15:25:10 +00002087 // Move `this` before its first user and out of any loops. If there is no
2088 // out-of-loop user that dominates all other users, move the instruction
2089 // to the end of the out-of-loop common dominator of the user's blocks.
2090 //
2091 // This can be used only on non-throwing instructions with no side effects that
2092 // have at least one use but no environment uses.
2093 void MoveBeforeFirstUserAndOutOfLoops();
2094
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002095#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 bool Is##type() const; \
2097 const H##type* As##type() const; \
2098 H##type* As##type();
2099
2100 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2101#undef INSTRUCTION_TYPE_CHECK
2102
2103#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002104 bool Is##type() const { return (As##type() != nullptr); } \
2105 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002106 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002107 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002108#undef INSTRUCTION_TYPE_CHECK
2109
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002110 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002111 // TODO: this method is used by LICM and GVN with possibly different
2112 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002113 virtual bool CanBeMoved() const { return false; }
2114
2115 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002116 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002117 return false;
2118 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002119
2120 // Returns whether any data encoded in the two instructions is equal.
2121 // This method does not look at the inputs. Both instructions must be
2122 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002123 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002124 return false;
2125 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002126
2127 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002128 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002129 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002130 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002131
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2133 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2134 // the virtual function because the __attribute__((__pure__)) doesn't really
2135 // apply the strong requirement for virtual functions, preventing optimizations.
2136 InstructionKind GetKind() const PURE;
2137 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002138
2139 virtual size_t ComputeHashCode() const {
2140 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002141 for (const HInstruction* input : GetInputs()) {
2142 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002143 }
2144 return result;
2145 }
2146
2147 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002148 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002149 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002150
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002151 size_t GetLifetimePosition() const { return lifetime_position_; }
2152 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2153 LiveInterval* GetLiveInterval() const { return live_interval_; }
2154 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2155 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2156
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002157 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2158
2159 // Returns whether the code generation of the instruction will require to have access
2160 // to the current method. Such instructions are:
2161 // (1): Instructions that require an environment, as calling the runtime requires
2162 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002163 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2164 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002165 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002166 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002167 }
2168
Vladimir Markodc151b22015-10-15 18:02:30 +01002169 // Returns whether the code generation of the instruction will require to have access
2170 // to the dex cache of the current method's declaring class via the current method.
2171 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002172
Mark Mendellc4701932015-04-10 13:18:51 -04002173 // Does this instruction have any use in an environment before
2174 // control flow hits 'other'?
2175 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2176
2177 // Remove all references to environment uses of this instruction.
2178 // The caller must ensure that this is safe to do.
2179 void RemoveEnvironmentUsers();
2180
Vladimir Markoa1de9182016-02-25 11:37:38 +00002181 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2182 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002183
David Brazdil1abb4192015-02-17 18:33:36 +00002184 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002185 // If set, the machine code for this instruction is assumed to be generated by
2186 // its users. Used by liveness analysis to compute use positions accordingly.
2187 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2188 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2189 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2190 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2191
Vladimir Marko372f10e2016-05-17 16:30:10 +01002192 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2193 return GetInputRecords()[i];
2194 }
2195
2196 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2197 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2198 input_records[index] = input;
2199 }
David Brazdil1abb4192015-02-17 18:33:36 +00002200
Vladimir Markoa1de9182016-02-25 11:37:38 +00002201 uint32_t GetPackedFields() const {
2202 return packed_fields_;
2203 }
2204
2205 template <size_t flag>
2206 bool GetPackedFlag() const {
2207 return (packed_fields_ & (1u << flag)) != 0u;
2208 }
2209
2210 template <size_t flag>
2211 void SetPackedFlag(bool value = true) {
2212 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2213 }
2214
2215 template <typename BitFieldType>
2216 typename BitFieldType::value_type GetPackedField() const {
2217 return BitFieldType::Decode(packed_fields_);
2218 }
2219
2220 template <typename BitFieldType>
2221 void SetPackedField(typename BitFieldType::value_type value) {
2222 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2223 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2224 }
2225
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002226 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002227 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2228 auto before_use_node = uses_.before_begin();
2229 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2230 HInstruction* user = use_node->GetUser();
2231 size_t input_index = use_node->GetIndex();
2232 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2233 before_use_node = use_node;
2234 }
2235 }
2236
2237 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2238 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2239 if (next != uses_.end()) {
2240 HInstruction* next_user = next->GetUser();
2241 size_t next_index = next->GetIndex();
2242 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2243 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2244 }
2245 }
2246
2247 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2248 auto before_env_use_node = env_uses_.before_begin();
2249 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2250 HEnvironment* user = env_use_node->GetUser();
2251 size_t input_index = env_use_node->GetIndex();
2252 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2253 before_env_use_node = env_use_node;
2254 }
2255 }
2256
2257 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2258 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2259 if (next != env_uses_.end()) {
2260 HEnvironment* next_user = next->GetUser();
2261 size_t next_index = next->GetIndex();
2262 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2263 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2264 }
2265 }
David Brazdil1abb4192015-02-17 18:33:36 +00002266
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267 HInstruction* previous_;
2268 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002269 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002270 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002271
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002272 // An instruction gets an id when it is added to the graph.
2273 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002274 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002275 int id_;
2276
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002277 // When doing liveness analysis, instructions that have uses get an SSA index.
2278 int ssa_index_;
2279
Vladimir Markoa1de9182016-02-25 11:37:38 +00002280 // Packed fields.
2281 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002282
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002283 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002284 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002285
2286 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002287 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002288
Nicolas Geoffray39468442014-09-02 15:17:15 +01002289 // The environment associated with this instruction. Not null if the instruction
2290 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002291 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002292
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002293 // Set by the code generator.
2294 LocationSummary* locations_;
2295
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002296 // Set by the liveness analysis.
2297 LiveInterval* live_interval_;
2298
2299 // Set by the liveness analysis, this is the position in a linear
2300 // order of blocks where this instruction's live interval start.
2301 size_t lifetime_position_;
2302
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002303 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002304
Vladimir Markoa1de9182016-02-25 11:37:38 +00002305 // The reference handle part of the reference type info.
2306 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002307 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002308 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002309
David Brazdil1abb4192015-02-17 18:33:36 +00002310 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002312 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002313 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002314 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315
2316 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2317};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002318std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002320// Iterates over the instructions, while preserving the next instruction
2321// in case the current instruction gets removed from the list by the user
2322// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002323class HInstructionIterator : public ValueObject {
2324 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002325 explicit HInstructionIterator(const HInstructionList& instructions)
2326 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002327 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002328 }
2329
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330 bool Done() const { return instruction_ == nullptr; }
2331 HInstruction* Current() const { return instruction_; }
2332 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002333 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002334 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002335 }
2336
2337 private:
2338 HInstruction* instruction_;
2339 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002340
2341 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002342};
2343
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002344// Iterates over the instructions without saving the next instruction,
2345// therefore handling changes in the graph potentially made by the user
2346// of this iterator.
2347class HInstructionIteratorHandleChanges : public ValueObject {
2348 public:
2349 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2350 : instruction_(instructions.first_instruction_) {
2351 }
2352
2353 bool Done() const { return instruction_ == nullptr; }
2354 HInstruction* Current() const { return instruction_; }
2355 void Advance() {
2356 instruction_ = instruction_->GetNext();
2357 }
2358
2359 private:
2360 HInstruction* instruction_;
2361
2362 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2363};
2364
2365
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002366class HBackwardInstructionIterator : public ValueObject {
2367 public:
2368 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2369 : instruction_(instructions.last_instruction_) {
2370 next_ = Done() ? nullptr : instruction_->GetPrevious();
2371 }
2372
2373 bool Done() const { return instruction_ == nullptr; }
2374 HInstruction* Current() const { return instruction_; }
2375 void Advance() {
2376 instruction_ = next_;
2377 next_ = Done() ? nullptr : instruction_->GetPrevious();
2378 }
2379
2380 private:
2381 HInstruction* instruction_;
2382 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002383
2384 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002385};
2386
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002387class HVariableInputSizeInstruction : public HInstruction {
2388 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002389 using HInstruction::GetInputRecords; // Keep the const version visible.
2390 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2391 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2392 }
2393
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002394 void AddInput(HInstruction* input);
2395 void InsertInputAt(size_t index, HInstruction* input);
2396 void RemoveInputAt(size_t index);
2397
2398 protected:
2399 HVariableInputSizeInstruction(SideEffects side_effects,
2400 uint32_t dex_pc,
2401 ArenaAllocator* arena,
2402 size_t number_of_inputs,
2403 ArenaAllocKind kind)
2404 : HInstruction(side_effects, dex_pc),
2405 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2406
2407 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2408
2409 private:
2410 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2411};
2412
Vladimir Markof9f64412015-09-02 14:05:49 +01002413template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002414class HTemplateInstruction: public HInstruction {
2415 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002416 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002417 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002418 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002419
Vladimir Marko372f10e2016-05-17 16:30:10 +01002420 using HInstruction::GetInputRecords; // Keep the const version visible.
2421 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2422 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002423 }
2424
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002426 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002427
2428 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002429};
2430
Vladimir Markof9f64412015-09-02 14:05:49 +01002431// HTemplateInstruction specialization for N=0.
2432template<>
2433class HTemplateInstruction<0>: public HInstruction {
2434 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002435 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002436 : HInstruction(side_effects, dex_pc) {}
2437
Vladimir Markof9f64412015-09-02 14:05:49 +01002438 virtual ~HTemplateInstruction() {}
2439
Vladimir Marko372f10e2016-05-17 16:30:10 +01002440 using HInstruction::GetInputRecords; // Keep the const version visible.
2441 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2442 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002443 }
2444
2445 private:
2446 friend class SsaBuilder;
2447};
2448
Dave Allison20dfc792014-06-16 20:44:29 -07002449template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002450class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002451 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002452 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002453 : HTemplateInstruction<N>(side_effects, dex_pc) {
2454 this->template SetPackedField<TypeField>(type);
2455 }
Dave Allison20dfc792014-06-16 20:44:29 -07002456 virtual ~HExpression() {}
2457
Vladimir Markoa1de9182016-02-25 11:37:38 +00002458 Primitive::Type GetType() const OVERRIDE {
2459 return TypeField::Decode(this->GetPackedFields());
2460 }
Dave Allison20dfc792014-06-16 20:44:29 -07002461
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002462 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002463 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2464 static constexpr size_t kFieldTypeSize =
2465 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2466 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2467 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2468 "Too many packed fields.");
2469 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002470};
2471
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002472// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2473// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002474class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002475 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002476 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2477 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002479 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002480
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002481 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002482
2483 private:
2484 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2485};
2486
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002487// Represents dex's RETURN opcodes. A HReturn is a control flow
2488// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002489class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002491 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2492 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002493 SetRawInputAt(0, value);
2494 }
2495
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002496 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002497
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002498 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002499
2500 private:
2501 DISALLOW_COPY_AND_ASSIGN(HReturn);
2502};
2503
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002504class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002505 public:
2506 HPhi(ArenaAllocator* arena,
2507 uint32_t reg_number,
2508 size_t number_of_inputs,
2509 Primitive::Type type,
2510 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002511 : HVariableInputSizeInstruction(
2512 SideEffects::None(),
2513 dex_pc,
2514 arena,
2515 number_of_inputs,
2516 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002517 reg_number_(reg_number) {
2518 SetPackedField<TypeField>(ToPhiType(type));
2519 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2520 // Phis are constructed live and marked dead if conflicting or unused.
2521 // Individual steps of SsaBuilder should assume that if a phi has been
2522 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2523 SetPackedFlag<kFlagIsLive>(true);
2524 SetPackedFlag<kFlagCanBeNull>(true);
2525 }
2526
2527 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2528 static Primitive::Type ToPhiType(Primitive::Type type) {
2529 return Primitive::PrimitiveKind(type);
2530 }
2531
2532 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2533
David Brazdildee58d62016-04-07 09:54:26 +00002534 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2535 void SetType(Primitive::Type new_type) {
2536 // Make sure that only valid type changes occur. The following are allowed:
2537 // (1) int -> float/ref (primitive type propagation),
2538 // (2) long -> double (primitive type propagation).
2539 DCHECK(GetType() == new_type ||
2540 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2541 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2542 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2543 SetPackedField<TypeField>(new_type);
2544 }
2545
2546 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2547 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2548
2549 uint32_t GetRegNumber() const { return reg_number_; }
2550
2551 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2552 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2553 bool IsDead() const { return !IsLive(); }
2554 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2555
Vladimir Markoe9004912016-06-16 16:50:52 +01002556 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002557 return other != nullptr
2558 && other->IsPhi()
2559 && other->AsPhi()->GetBlock() == GetBlock()
2560 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2561 }
2562
2563 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2564 // An equivalent phi is a phi having the same dex register and type.
2565 // It assumes that phis with the same dex register are adjacent.
2566 HPhi* GetNextEquivalentPhiWithSameType() {
2567 HInstruction* next = GetNext();
2568 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2569 if (next->GetType() == GetType()) {
2570 return next->AsPhi();
2571 }
2572 next = next->GetNext();
2573 }
2574 return nullptr;
2575 }
2576
2577 DECLARE_INSTRUCTION(Phi);
2578
David Brazdildee58d62016-04-07 09:54:26 +00002579 private:
2580 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2581 static constexpr size_t kFieldTypeSize =
2582 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2583 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2584 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2585 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2586 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2587 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2588
David Brazdildee58d62016-04-07 09:54:26 +00002589 const uint32_t reg_number_;
2590
2591 DISALLOW_COPY_AND_ASSIGN(HPhi);
2592};
2593
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002595// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002596// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002597class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002599 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002600
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002601 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002602
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002603 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002604
2605 private:
2606 DISALLOW_COPY_AND_ASSIGN(HExit);
2607};
2608
2609// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002610class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002611 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002612 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002613
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002614 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002615
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002616 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002617 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002618 }
2619
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002620 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002621
2622 private:
2623 DISALLOW_COPY_AND_ASSIGN(HGoto);
2624};
2625
Roland Levillain9867bc72015-08-05 10:21:34 +01002626class HConstant : public HExpression<0> {
2627 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002628 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2629 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002630
2631 bool CanBeMoved() const OVERRIDE { return true; }
2632
Roland Levillain1a653882016-03-18 18:05:57 +00002633 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002634 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002635 // Is this constant 0 in the arithmetic sense?
2636 virtual bool IsArithmeticZero() const { return false; }
2637 // Is this constant a 0-bit pattern?
2638 virtual bool IsZeroBitPattern() const { return false; }
2639 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002640 virtual bool IsOne() const { return false; }
2641
David Brazdil77a48ae2015-09-15 12:34:04 +00002642 virtual uint64_t GetValueAsUint64() const = 0;
2643
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002644 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002645
2646 private:
2647 DISALLOW_COPY_AND_ASSIGN(HConstant);
2648};
2649
Vladimir Markofcb503c2016-05-18 12:48:17 +01002650class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002651 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002652 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002653 return true;
2654 }
2655
David Brazdil77a48ae2015-09-15 12:34:04 +00002656 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2657
Roland Levillain9867bc72015-08-05 10:21:34 +01002658 size_t ComputeHashCode() const OVERRIDE { return 0; }
2659
Roland Levillain1a653882016-03-18 18:05:57 +00002660 // The null constant representation is a 0-bit pattern.
2661 virtual bool IsZeroBitPattern() const { return true; }
2662
Roland Levillain9867bc72015-08-05 10:21:34 +01002663 DECLARE_INSTRUCTION(NullConstant);
2664
2665 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002666 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002667
2668 friend class HGraph;
2669 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2670};
2671
2672// Constants of the type int. Those can be from Dex instructions, or
2673// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002674class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002675 public:
2676 int32_t GetValue() const { return value_; }
2677
David Brazdil9f389d42015-10-01 14:32:56 +01002678 uint64_t GetValueAsUint64() const OVERRIDE {
2679 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2680 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002681
Vladimir Marko372f10e2016-05-17 16:30:10 +01002682 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002683 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002684 return other->AsIntConstant()->value_ == value_;
2685 }
2686
2687 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2688
2689 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002690 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2691 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002692 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2693
Roland Levillain1a653882016-03-18 18:05:57 +00002694 // Integer constants are used to encode Boolean values as well,
2695 // where 1 means true and 0 means false.
2696 bool IsTrue() const { return GetValue() == 1; }
2697 bool IsFalse() const { return GetValue() == 0; }
2698
Roland Levillain9867bc72015-08-05 10:21:34 +01002699 DECLARE_INSTRUCTION(IntConstant);
2700
2701 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002702 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2703 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2704 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2705 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002706
2707 const int32_t value_;
2708
2709 friend class HGraph;
2710 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2711 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2712 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2713};
2714
Vladimir Markofcb503c2016-05-18 12:48:17 +01002715class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002716 public:
2717 int64_t GetValue() const { return value_; }
2718
David Brazdil77a48ae2015-09-15 12:34:04 +00002719 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2720
Vladimir Marko372f10e2016-05-17 16:30:10 +01002721 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002722 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002723 return other->AsLongConstant()->value_ == value_;
2724 }
2725
2726 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2727
2728 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002729 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2730 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002731 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2732
2733 DECLARE_INSTRUCTION(LongConstant);
2734
2735 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002736 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2737 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002738
2739 const int64_t value_;
2740
2741 friend class HGraph;
2742 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2743};
Dave Allison20dfc792014-06-16 20:44:29 -07002744
Vladimir Markofcb503c2016-05-18 12:48:17 +01002745class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002746 public:
2747 float GetValue() const { return value_; }
2748
2749 uint64_t GetValueAsUint64() const OVERRIDE {
2750 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2751 }
2752
Vladimir Marko372f10e2016-05-17 16:30:10 +01002753 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002754 DCHECK(other->IsFloatConstant()) << other->DebugName();
2755 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2756 }
2757
2758 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2759
2760 bool IsMinusOne() const OVERRIDE {
2761 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2762 }
Roland Levillain1a653882016-03-18 18:05:57 +00002763 bool IsArithmeticZero() const OVERRIDE {
2764 return std::fpclassify(value_) == FP_ZERO;
2765 }
2766 bool IsArithmeticPositiveZero() const {
2767 return IsArithmeticZero() && !std::signbit(value_);
2768 }
2769 bool IsArithmeticNegativeZero() const {
2770 return IsArithmeticZero() && std::signbit(value_);
2771 }
2772 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002773 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002774 }
2775 bool IsOne() const OVERRIDE {
2776 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2777 }
2778 bool IsNaN() const {
2779 return std::isnan(value_);
2780 }
2781
2782 DECLARE_INSTRUCTION(FloatConstant);
2783
2784 private:
2785 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2786 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2787 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2788 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2789
2790 const float value_;
2791
2792 // Only the SsaBuilder and HGraph can create floating-point constants.
2793 friend class SsaBuilder;
2794 friend class HGraph;
2795 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2796};
2797
Vladimir Markofcb503c2016-05-18 12:48:17 +01002798class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002799 public:
2800 double GetValue() const { return value_; }
2801
2802 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2803
Vladimir Marko372f10e2016-05-17 16:30:10 +01002804 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002805 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2806 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2807 }
2808
2809 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2810
2811 bool IsMinusOne() const OVERRIDE {
2812 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2813 }
Roland Levillain1a653882016-03-18 18:05:57 +00002814 bool IsArithmeticZero() const OVERRIDE {
2815 return std::fpclassify(value_) == FP_ZERO;
2816 }
2817 bool IsArithmeticPositiveZero() const {
2818 return IsArithmeticZero() && !std::signbit(value_);
2819 }
2820 bool IsArithmeticNegativeZero() const {
2821 return IsArithmeticZero() && std::signbit(value_);
2822 }
2823 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002824 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002825 }
2826 bool IsOne() const OVERRIDE {
2827 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2828 }
2829 bool IsNaN() const {
2830 return std::isnan(value_);
2831 }
2832
2833 DECLARE_INSTRUCTION(DoubleConstant);
2834
2835 private:
2836 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2837 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2838 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2839 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2840
2841 const double value_;
2842
2843 // Only the SsaBuilder and HGraph can create floating-point constants.
2844 friend class SsaBuilder;
2845 friend class HGraph;
2846 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2847};
2848
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002849// Conditional branch. A block ending with an HIf instruction must have
2850// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002851class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002852 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002853 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2854 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002855 SetRawInputAt(0, input);
2856 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002857
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002858 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002859
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002860 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002861 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002862 }
2863
2864 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002865 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002866 }
2867
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002868 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002869
2870 private:
2871 DISALLOW_COPY_AND_ASSIGN(HIf);
2872};
2873
David Brazdilfc6a86a2015-06-26 10:33:45 +00002874
2875// Abstract instruction which marks the beginning and/or end of a try block and
2876// links it to the respective exception handlers. Behaves the same as a Goto in
2877// non-exceptional control flow.
2878// Normal-flow successor is stored at index zero, exception handlers under
2879// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002880class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002881 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002882 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002883 kEntry,
2884 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002885 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002886 };
2887
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002888 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002889 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2890 SetPackedField<BoundaryKindField>(kind);
2891 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002892
2893 bool IsControlFlow() const OVERRIDE { return true; }
2894
2895 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002896 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002897
David Brazdild26a4112015-11-10 11:07:31 +00002898 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2899 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2900 }
2901
David Brazdilfc6a86a2015-06-26 10:33:45 +00002902 // Returns whether `handler` is among its exception handlers (non-zero index
2903 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002904 bool HasExceptionHandler(const HBasicBlock& handler) const {
2905 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002906 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002907 }
2908
2909 // If not present already, adds `handler` to its block's list of exception
2910 // handlers.
2911 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002912 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002913 GetBlock()->AddSuccessor(handler);
2914 }
2915 }
2916
Vladimir Markoa1de9182016-02-25 11:37:38 +00002917 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2918 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002919
David Brazdilffee3d32015-07-06 11:48:53 +01002920 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2921
David Brazdilfc6a86a2015-06-26 10:33:45 +00002922 DECLARE_INSTRUCTION(TryBoundary);
2923
2924 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002925 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2926 static constexpr size_t kFieldBoundaryKindSize =
2927 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2928 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2929 kFieldBoundaryKind + kFieldBoundaryKindSize;
2930 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2931 "Too many packed fields.");
2932 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002933
2934 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2935};
2936
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002937// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002938class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002939 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002940 // We set CanTriggerGC to prevent any intermediate address to be live
2941 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002942 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002943 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002944 SetRawInputAt(0, cond);
2945 }
2946
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002947 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002948 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002949 return true;
2950 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002951 bool NeedsEnvironment() const OVERRIDE { return true; }
2952 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953
2954 DECLARE_INSTRUCTION(Deoptimize);
2955
2956 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002957 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2958};
2959
Mingyao Yang063fc772016-08-02 11:02:54 -07002960// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2961// The compiled code checks this flag value in a guard before devirtualized call and
2962// if it's true, starts to do deoptimization.
2963// It has a 4-byte slot on stack.
2964// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002965class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002966 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002967 // CHA guards are only optimized in a separate pass and it has no side effects
2968 // with regard to other passes.
2969 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2970 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002971 }
2972
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002973 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2974
2975 // We do all CHA guard elimination/motion in a single pass, after which there is no
2976 // further guard elimination/motion since a guard might have been used for justification
2977 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2978 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002979 bool CanBeMoved() const OVERRIDE { return false; }
2980
2981 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2982
2983 private:
2984 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2985};
2986
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002987// Represents the ArtMethod that was passed as a first argument to
2988// the method. It is used by instructions that depend on it, like
2989// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002990class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002991 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002992 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2993 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002994
2995 DECLARE_INSTRUCTION(CurrentMethod);
2996
2997 private:
2998 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2999};
3000
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003001// Fetches an ArtMethod from the virtual table or the interface method table
3002// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003003class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003004 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003005 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003006 kVTable,
3007 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003008 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003009 };
3010 HClassTableGet(HInstruction* cls,
3011 Primitive::Type type,
3012 TableKind kind,
3013 size_t index,
3014 uint32_t dex_pc)
3015 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003016 index_(index) {
3017 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003018 SetRawInputAt(0, cls);
3019 }
3020
3021 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003022 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003023 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003024 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003025 }
3026
Vladimir Markoa1de9182016-02-25 11:37:38 +00003027 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003028 size_t GetIndex() const { return index_; }
3029
3030 DECLARE_INSTRUCTION(ClassTableGet);
3031
3032 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003033 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3034 static constexpr size_t kFieldTableKindSize =
3035 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3036 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3037 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3038 "Too many packed fields.");
3039 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3040
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003041 // The index of the ArtMethod in the table.
3042 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003043
3044 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3045};
3046
Mark Mendellfe57faa2015-09-18 09:26:15 -04003047// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3048// have one successor for each entry in the switch table, and the final successor
3049// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003050class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003051 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003052 HPackedSwitch(int32_t start_value,
3053 uint32_t num_entries,
3054 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003055 uint32_t dex_pc = kNoDexPc)
3056 : HTemplateInstruction(SideEffects::None(), dex_pc),
3057 start_value_(start_value),
3058 num_entries_(num_entries) {
3059 SetRawInputAt(0, input);
3060 }
3061
3062 bool IsControlFlow() const OVERRIDE { return true; }
3063
3064 int32_t GetStartValue() const { return start_value_; }
3065
Vladimir Marko211c2112015-09-24 16:52:33 +01003066 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003067
3068 HBasicBlock* GetDefaultBlock() const {
3069 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003070 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003071 }
3072 DECLARE_INSTRUCTION(PackedSwitch);
3073
3074 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003075 const int32_t start_value_;
3076 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003077
3078 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3079};
3080
Roland Levillain88cb1752014-10-20 16:36:47 +01003081class HUnaryOperation : public HExpression<1> {
3082 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003083 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3084 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003085 SetRawInputAt(0, input);
3086 }
3087
3088 HInstruction* GetInput() const { return InputAt(0); }
3089 Primitive::Type GetResultType() const { return GetType(); }
3090
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003091 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003092 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003093 return true;
3094 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003095
Roland Levillain31dd3d62016-02-16 12:21:02 +00003096 // Try to statically evaluate `this` and return a HConstant
3097 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003098 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003099 HConstant* TryStaticEvaluation() const;
3100
3101 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003102 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3103 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003104 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3105 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003106
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003107 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003108
3109 private:
3110 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3111};
3112
Dave Allison20dfc792014-06-16 20:44:29 -07003113class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003114 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003115 HBinaryOperation(Primitive::Type result_type,
3116 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003117 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003118 SideEffects side_effects = SideEffects::None(),
3119 uint32_t dex_pc = kNoDexPc)
3120 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003121 SetRawInputAt(0, left);
3122 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123 }
3124
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003125 HInstruction* GetLeft() const { return InputAt(0); }
3126 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003127 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003128
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003129 virtual bool IsCommutative() const { return false; }
3130
3131 // Put constant on the right.
3132 // Returns whether order is changed.
3133 bool OrderInputsWithConstantOnTheRight() {
3134 HInstruction* left = InputAt(0);
3135 HInstruction* right = InputAt(1);
3136 if (left->IsConstant() && !right->IsConstant()) {
3137 ReplaceInput(right, 0);
3138 ReplaceInput(left, 1);
3139 return true;
3140 }
3141 return false;
3142 }
3143
3144 // Order inputs by instruction id, but favor constant on the right side.
3145 // This helps GVN for commutative ops.
3146 void OrderInputs() {
3147 DCHECK(IsCommutative());
3148 HInstruction* left = InputAt(0);
3149 HInstruction* right = InputAt(1);
3150 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3151 return;
3152 }
3153 if (OrderInputsWithConstantOnTheRight()) {
3154 return;
3155 }
3156 // Order according to instruction id.
3157 if (left->GetId() > right->GetId()) {
3158 ReplaceInput(right, 0);
3159 ReplaceInput(left, 1);
3160 }
3161 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003162
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003163 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003164 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003165 return true;
3166 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003167
Roland Levillain31dd3d62016-02-16 12:21:02 +00003168 // Try to statically evaluate `this` and return a HConstant
3169 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003170 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003171 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003172
3173 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3175 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003176 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3177 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003178 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003179 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3180 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3182 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003183 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3184 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003185 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003186 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3187 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003188
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003189 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003190 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003191 HConstant* GetConstantRight() const;
3192
3193 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003194 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003195 HInstruction* GetLeastConstantLeft() const;
3196
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003197 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003198
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003199 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003200 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3201};
3202
Mark Mendellc4701932015-04-10 13:18:51 -04003203// The comparison bias applies for floating point operations and indicates how NaN
3204// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003205enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003206 kNoBias, // bias is not applicable (i.e. for long operation)
3207 kGtBias, // return 1 for NaN comparisons
3208 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003209 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003210};
3211
Roland Levillain31dd3d62016-02-16 12:21:02 +00003212std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3213
Dave Allison20dfc792014-06-16 20:44:29 -07003214class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003215 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003216 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003217 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3218 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3219 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003220
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003221 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003222 // `instruction`, and disregard moves in between.
3223 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003224
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003225 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003226
3227 virtual IfCondition GetCondition() const = 0;
3228
Mark Mendellc4701932015-04-10 13:18:51 -04003229 virtual IfCondition GetOppositeCondition() const = 0;
3230
Vladimir Markoa1de9182016-02-25 11:37:38 +00003231 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003232 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3233
Vladimir Markoa1de9182016-02-25 11:37:38 +00003234 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3235 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003236
Vladimir Marko372f10e2016-05-17 16:30:10 +01003237 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003238 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003239 }
3240
Roland Levillain4fa13f62015-07-06 18:11:54 +01003241 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003242 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003243 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003244 if (if_cond == kCondNE) {
3245 return true;
3246 } else if (if_cond == kCondEQ) {
3247 return false;
3248 }
3249 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003250 }
3251
3252 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003253 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003254 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003255 if (if_cond == kCondEQ) {
3256 return true;
3257 } else if (if_cond == kCondNE) {
3258 return false;
3259 }
3260 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003261 }
3262
Roland Levillain31dd3d62016-02-16 12:21:02 +00003263 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003264 // Needed if we merge a HCompare into a HCondition.
3265 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3266 static constexpr size_t kFieldComparisonBiasSize =
3267 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3268 static constexpr size_t kNumberOfConditionPackedBits =
3269 kFieldComparisonBias + kFieldComparisonBiasSize;
3270 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3271 using ComparisonBiasField =
3272 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3273
Roland Levillain31dd3d62016-02-16 12:21:02 +00003274 template <typename T>
3275 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3276
3277 template <typename T>
3278 int32_t CompareFP(T x, T y) const {
3279 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3280 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3281 // Handle the bias.
3282 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3283 }
3284
3285 // Return an integer constant containing the result of a condition evaluated at compile time.
3286 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3287 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3288 }
3289
Dave Allison20dfc792014-06-16 20:44:29 -07003290 private:
3291 DISALLOW_COPY_AND_ASSIGN(HCondition);
3292};
3293
3294// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003295class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003296 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003297 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3298 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003299
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003300 bool IsCommutative() const OVERRIDE { return true; }
3301
Vladimir Marko9e23df52015-11-10 17:14:35 +00003302 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3303 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003304 return MakeConstantCondition(true, GetDexPc());
3305 }
3306 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3307 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3308 }
3309 // In the following Evaluate methods, a HCompare instruction has
3310 // been merged into this HEqual instruction; evaluate it as
3311 // `Compare(x, y) == 0`.
3312 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3314 GetDexPc());
3315 }
3316 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3317 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3318 }
3319 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3320 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003321 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003322
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003323 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003324
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003325 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003326 return kCondEQ;
3327 }
3328
Mark Mendellc4701932015-04-10 13:18:51 -04003329 IfCondition GetOppositeCondition() const OVERRIDE {
3330 return kCondNE;
3331 }
3332
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003333 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003334 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003335
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003336 DISALLOW_COPY_AND_ASSIGN(HEqual);
3337};
3338
Vladimir Markofcb503c2016-05-18 12:48:17 +01003339class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003340 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003341 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3342 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003343
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003344 bool IsCommutative() const OVERRIDE { return true; }
3345
Vladimir Marko9e23df52015-11-10 17:14:35 +00003346 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3347 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003348 return MakeConstantCondition(false, GetDexPc());
3349 }
3350 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3351 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3352 }
3353 // In the following Evaluate methods, a HCompare instruction has
3354 // been merged into this HNotEqual instruction; evaluate it as
3355 // `Compare(x, y) != 0`.
3356 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3357 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3358 }
3359 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3360 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3361 }
3362 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3363 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003364 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003365
Dave Allison20dfc792014-06-16 20:44:29 -07003366 DECLARE_INSTRUCTION(NotEqual);
3367
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003368 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003369 return kCondNE;
3370 }
3371
Mark Mendellc4701932015-04-10 13:18:51 -04003372 IfCondition GetOppositeCondition() const OVERRIDE {
3373 return kCondEQ;
3374 }
3375
Dave Allison20dfc792014-06-16 20:44:29 -07003376 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003377 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003378
Dave Allison20dfc792014-06-16 20:44:29 -07003379 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3380};
3381
Vladimir Markofcb503c2016-05-18 12:48:17 +01003382class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003383 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003384 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3385 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003386
Roland Levillain9867bc72015-08-05 10:21:34 +01003387 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003388 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003389 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 // In the following Evaluate methods, a HCompare instruction has
3391 // been merged into this HLessThan instruction; evaluate it as
3392 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003393 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003394 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3395 }
3396 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3397 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3398 }
3399 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3400 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003401 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003402
Dave Allison20dfc792014-06-16 20:44:29 -07003403 DECLARE_INSTRUCTION(LessThan);
3404
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003405 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003406 return kCondLT;
3407 }
3408
Mark Mendellc4701932015-04-10 13:18:51 -04003409 IfCondition GetOppositeCondition() const OVERRIDE {
3410 return kCondGE;
3411 }
3412
Dave Allison20dfc792014-06-16 20:44:29 -07003413 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003414 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003415
Dave Allison20dfc792014-06-16 20:44:29 -07003416 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3417};
3418
Vladimir Markofcb503c2016-05-18 12:48:17 +01003419class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003420 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003421 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3422 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003423
Roland Levillain9867bc72015-08-05 10:21:34 +01003424 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003425 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003426 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 // In the following Evaluate methods, a HCompare instruction has
3428 // been merged into this HLessThanOrEqual instruction; evaluate it as
3429 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003430 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003431 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3432 }
3433 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3434 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3435 }
3436 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3437 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003438 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003439
Dave Allison20dfc792014-06-16 20:44:29 -07003440 DECLARE_INSTRUCTION(LessThanOrEqual);
3441
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003442 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003443 return kCondLE;
3444 }
3445
Mark Mendellc4701932015-04-10 13:18:51 -04003446 IfCondition GetOppositeCondition() const OVERRIDE {
3447 return kCondGT;
3448 }
3449
Dave Allison20dfc792014-06-16 20:44:29 -07003450 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003451 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003452
Dave Allison20dfc792014-06-16 20:44:29 -07003453 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3454};
3455
Vladimir Markofcb503c2016-05-18 12:48:17 +01003456class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003457 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003458 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3459 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003460
Roland Levillain9867bc72015-08-05 10:21:34 +01003461 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003462 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003463 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 // In the following Evaluate methods, a HCompare instruction has
3465 // been merged into this HGreaterThan instruction; evaluate it as
3466 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003467 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003468 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3469 }
3470 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3471 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3472 }
3473 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3474 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003475 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003476
Dave Allison20dfc792014-06-16 20:44:29 -07003477 DECLARE_INSTRUCTION(GreaterThan);
3478
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003479 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003480 return kCondGT;
3481 }
3482
Mark Mendellc4701932015-04-10 13:18:51 -04003483 IfCondition GetOppositeCondition() const OVERRIDE {
3484 return kCondLE;
3485 }
3486
Dave Allison20dfc792014-06-16 20:44:29 -07003487 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003488 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003489
Dave Allison20dfc792014-06-16 20:44:29 -07003490 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3491};
3492
Vladimir Markofcb503c2016-05-18 12:48:17 +01003493class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003494 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003495 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3496 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003497
Roland Levillain9867bc72015-08-05 10:21:34 +01003498 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003499 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003500 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 // In the following Evaluate methods, a HCompare instruction has
3502 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3503 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003504 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003505 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3506 }
3507 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3508 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3509 }
3510 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3511 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003512 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003513
Dave Allison20dfc792014-06-16 20:44:29 -07003514 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3515
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003516 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003517 return kCondGE;
3518 }
3519
Mark Mendellc4701932015-04-10 13:18:51 -04003520 IfCondition GetOppositeCondition() const OVERRIDE {
3521 return kCondLT;
3522 }
3523
Dave Allison20dfc792014-06-16 20:44:29 -07003524 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003525 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003526
Dave Allison20dfc792014-06-16 20:44:29 -07003527 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3528};
3529
Vladimir Markofcb503c2016-05-18 12:48:17 +01003530class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003531 public:
3532 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3533 : HCondition(first, second, dex_pc) {}
3534
3535 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003536 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003537 }
3538 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003539 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3540 }
3541 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3542 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3543 LOG(FATAL) << DebugName() << " is not defined for float values";
3544 UNREACHABLE();
3545 }
3546 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3547 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3548 LOG(FATAL) << DebugName() << " is not defined for double values";
3549 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003550 }
3551
3552 DECLARE_INSTRUCTION(Below);
3553
3554 IfCondition GetCondition() const OVERRIDE {
3555 return kCondB;
3556 }
3557
3558 IfCondition GetOppositeCondition() const OVERRIDE {
3559 return kCondAE;
3560 }
3561
3562 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003563 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003564 return MakeUnsigned(x) < MakeUnsigned(y);
3565 }
Aart Bike9f37602015-10-09 11:15:55 -07003566
3567 DISALLOW_COPY_AND_ASSIGN(HBelow);
3568};
3569
Vladimir Markofcb503c2016-05-18 12:48:17 +01003570class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003571 public:
3572 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3573 : HCondition(first, second, dex_pc) {}
3574
3575 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003576 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003577 }
3578 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003579 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3580 }
3581 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3582 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3583 LOG(FATAL) << DebugName() << " is not defined for float values";
3584 UNREACHABLE();
3585 }
3586 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3587 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3588 LOG(FATAL) << DebugName() << " is not defined for double values";
3589 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003590 }
3591
3592 DECLARE_INSTRUCTION(BelowOrEqual);
3593
3594 IfCondition GetCondition() const OVERRIDE {
3595 return kCondBE;
3596 }
3597
3598 IfCondition GetOppositeCondition() const OVERRIDE {
3599 return kCondA;
3600 }
3601
3602 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003603 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003604 return MakeUnsigned(x) <= MakeUnsigned(y);
3605 }
Aart Bike9f37602015-10-09 11:15:55 -07003606
3607 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3608};
3609
Vladimir Markofcb503c2016-05-18 12:48:17 +01003610class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003611 public:
3612 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3613 : HCondition(first, second, dex_pc) {}
3614
3615 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003616 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003617 }
3618 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003619 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3620 }
3621 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3622 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3623 LOG(FATAL) << DebugName() << " is not defined for float values";
3624 UNREACHABLE();
3625 }
3626 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3627 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3628 LOG(FATAL) << DebugName() << " is not defined for double values";
3629 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003630 }
3631
3632 DECLARE_INSTRUCTION(Above);
3633
3634 IfCondition GetCondition() const OVERRIDE {
3635 return kCondA;
3636 }
3637
3638 IfCondition GetOppositeCondition() const OVERRIDE {
3639 return kCondBE;
3640 }
3641
3642 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003643 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003644 return MakeUnsigned(x) > MakeUnsigned(y);
3645 }
Aart Bike9f37602015-10-09 11:15:55 -07003646
3647 DISALLOW_COPY_AND_ASSIGN(HAbove);
3648};
3649
Vladimir Markofcb503c2016-05-18 12:48:17 +01003650class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003651 public:
3652 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3653 : HCondition(first, second, dex_pc) {}
3654
3655 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003656 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003657 }
3658 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003659 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3660 }
3661 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3662 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3663 LOG(FATAL) << DebugName() << " is not defined for float values";
3664 UNREACHABLE();
3665 }
3666 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3667 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3668 LOG(FATAL) << DebugName() << " is not defined for double values";
3669 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003670 }
3671
3672 DECLARE_INSTRUCTION(AboveOrEqual);
3673
3674 IfCondition GetCondition() const OVERRIDE {
3675 return kCondAE;
3676 }
3677
3678 IfCondition GetOppositeCondition() const OVERRIDE {
3679 return kCondB;
3680 }
3681
3682 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003683 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003684 return MakeUnsigned(x) >= MakeUnsigned(y);
3685 }
Aart Bike9f37602015-10-09 11:15:55 -07003686
3687 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3688};
Dave Allison20dfc792014-06-16 20:44:29 -07003689
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003690// Instruction to check how two inputs compare to each other.
3691// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003692class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003693 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003694 // Note that `comparison_type` is the type of comparison performed
3695 // between the comparison's inputs, not the type of the instantiated
3696 // HCompare instruction (which is always Primitive::kPrimInt).
3697 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003698 HInstruction* first,
3699 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003700 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003701 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003702 : HBinaryOperation(Primitive::kPrimInt,
3703 first,
3704 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003705 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003706 dex_pc) {
3707 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003708 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3709 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003710 }
3711
Roland Levillain9867bc72015-08-05 10:21:34 +01003712 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003713 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3714
3715 template <typename T>
3716 int32_t ComputeFP(T x, T y) const {
3717 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3718 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3719 // Handle the bias.
3720 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3721 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003722
Roland Levillain9867bc72015-08-05 10:21:34 +01003723 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003724 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3725 // reach this code path when processing a freshly built HIR
3726 // graph. However HCompare integer instructions can be synthesized
3727 // by the instruction simplifier to implement IntegerCompare and
3728 // IntegerSignum intrinsics, so we have to handle this case.
3729 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003730 }
3731 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003732 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3733 }
3734 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3735 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3736 }
3737 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3738 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003739 }
3740
Vladimir Marko372f10e2016-05-17 16:30:10 +01003741 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003742 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003743 }
3744
Vladimir Markoa1de9182016-02-25 11:37:38 +00003745 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003746
Roland Levillain31dd3d62016-02-16 12:21:02 +00003747 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003748 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003749 bool IsGtBias() const {
3750 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003751 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003752 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003753
Roland Levillain1693a1f2016-03-15 14:57:31 +00003754 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3755 // Comparisons do not require a runtime call in any back end.
3756 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003757 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003758
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003759 DECLARE_INSTRUCTION(Compare);
3760
Roland Levillain31dd3d62016-02-16 12:21:02 +00003761 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003762 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3763 static constexpr size_t kFieldComparisonBiasSize =
3764 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3765 static constexpr size_t kNumberOfComparePackedBits =
3766 kFieldComparisonBias + kFieldComparisonBiasSize;
3767 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3768 using ComparisonBiasField =
3769 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3770
Roland Levillain31dd3d62016-02-16 12:21:02 +00003771 // Return an integer constant containing the result of a comparison evaluated at compile time.
3772 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3773 DCHECK(value == -1 || value == 0 || value == 1) << value;
3774 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3775 }
3776
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003777 private:
3778 DISALLOW_COPY_AND_ASSIGN(HCompare);
3779};
3780
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003781class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003782 public:
3783 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003784 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003785 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003786 const DexFile& dex_file,
Mingyao Yang062157f2016-03-02 10:15:36 -08003787 bool needs_access_check,
David Brazdil6de19382016-01-08 17:37:10 +00003788 bool finalizable,
3789 QuickEntrypointEnum entrypoint)
3790 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3791 type_index_(type_index),
3792 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003793 entrypoint_(entrypoint) {
Mingyao Yang062157f2016-03-02 10:15:36 -08003794 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003795 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003796 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003797 }
3798
Andreas Gampea5b09a62016-11-17 15:21:22 -08003799 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003800 const DexFile& GetDexFile() const { return dex_file_; }
3801
3802 // Calls runtime so needs an environment.
3803 bool NeedsEnvironment() const OVERRIDE { return true; }
3804
Mingyao Yang062157f2016-03-02 10:15:36 -08003805 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3806 bool CanThrow() const OVERRIDE { return true; }
3807
3808 // Needs to call into runtime to make sure it's instantiable/accessible.
3809 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003810
Vladimir Markoa1de9182016-02-25 11:37:38 +00003811 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003812
3813 bool CanBeNull() const OVERRIDE { return false; }
3814
3815 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3816
3817 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3818 entrypoint_ = entrypoint;
3819 }
3820
3821 bool IsStringAlloc() const;
3822
3823 DECLARE_INSTRUCTION(NewInstance);
3824
3825 private:
Mingyao Yang062157f2016-03-02 10:15:36 -08003826 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfExpressionPackedBits;
3827 static constexpr size_t kFlagFinalizable = kFlagNeedsAccessCheck + 1;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003828 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3829 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3830 "Too many packed fields.");
3831
Andreas Gampea5b09a62016-11-17 15:21:22 -08003832 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003833 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003834 QuickEntrypointEnum entrypoint_;
3835
3836 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3837};
3838
Agi Csaki05f20562015-08-19 14:58:14 -07003839enum IntrinsicNeedsEnvironmentOrCache {
3840 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3841 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003842};
3843
Aart Bik5d75afe2015-12-14 11:57:01 -08003844enum IntrinsicSideEffects {
3845 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3846 kReadSideEffects, // Intrinsic may read heap memory.
3847 kWriteSideEffects, // Intrinsic may write heap memory.
3848 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3849};
3850
3851enum IntrinsicExceptions {
3852 kNoThrow, // Intrinsic does not throw any exceptions.
3853 kCanThrow // Intrinsic may throw exceptions.
3854};
3855
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003856class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003857 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003858 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003859
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003860 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003861 SetRawInputAt(index, argument);
3862 }
3863
Roland Levillain3e3d7332015-04-28 11:00:54 +01003864 // Return the number of arguments. This number can be lower than
3865 // the number of inputs returned by InputCount(), as some invoke
3866 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3867 // inputs at the end of their list of inputs.
3868 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3869
Vladimir Markoa1de9182016-02-25 11:37:38 +00003870 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003871
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003872 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01003873 const DexFile& GetDexFile() const { return GetEnvironment()->GetDexFile(); }
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003874
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003875 InvokeType GetInvokeType() const {
3876 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003877 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003878
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003879 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003880 return intrinsic_;
3881 }
3882
Aart Bik5d75afe2015-12-14 11:57:01 -08003883 void SetIntrinsic(Intrinsics intrinsic,
3884 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3885 IntrinsicSideEffects side_effects,
3886 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003887
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003888 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003889 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003890 }
3891
Aart Bikff7d89c2016-11-07 08:49:28 -08003892 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3893
Vladimir Markoa1de9182016-02-25 11:37:38 +00003894 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003895
Aart Bik71bf7b42016-11-16 10:17:46 -08003896 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003897
Vladimir Marko372f10e2016-05-17 16:30:10 +01003898 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003899 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3900 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003901
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003902 uint32_t* GetIntrinsicOptimizations() {
3903 return &intrinsic_optimizations_;
3904 }
3905
3906 const uint32_t* GetIntrinsicOptimizations() const {
3907 return &intrinsic_optimizations_;
3908 }
3909
3910 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3911
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003912 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3913
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003914 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003915
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003916 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003917 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3918 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003919 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3920 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003921 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003922 static constexpr size_t kFieldReturnTypeSize =
3923 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3924 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3925 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3926 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003927 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003928 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3929
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003930 HInvoke(ArenaAllocator* arena,
3931 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003932 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003933 Primitive::Type return_type,
3934 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003935 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003936 ArtMethod* resolved_method,
3937 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003938 : HVariableInputSizeInstruction(
3939 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3940 dex_pc,
3941 arena,
3942 number_of_arguments + number_of_other_inputs,
3943 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003944 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003945 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003946 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003947 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003948 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003949 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003950 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003951 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003952 }
3953
Roland Levillain3e3d7332015-04-28 11:00:54 +01003954 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003955 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003956 const uint32_t dex_method_index_;
3957 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003958
3959 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3960 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003961
3962 private:
3963 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3964};
3965
Vladimir Markofcb503c2016-05-18 12:48:17 +01003966class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003967 public:
3968 HInvokeUnresolved(ArenaAllocator* arena,
3969 uint32_t number_of_arguments,
3970 Primitive::Type return_type,
3971 uint32_t dex_pc,
3972 uint32_t dex_method_index,
3973 InvokeType invoke_type)
3974 : HInvoke(arena,
3975 number_of_arguments,
3976 0u /* number_of_other_inputs */,
3977 return_type,
3978 dex_pc,
3979 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003980 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003981 invoke_type) {
3982 }
3983
3984 DECLARE_INSTRUCTION(InvokeUnresolved);
3985
3986 private:
3987 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3988};
3989
Orion Hodsonac141392017-01-13 11:53:47 +00003990class HInvokePolymorphic FINAL : public HInvoke {
3991 public:
3992 HInvokePolymorphic(ArenaAllocator* arena,
3993 uint32_t number_of_arguments,
3994 Primitive::Type return_type,
3995 uint32_t dex_pc,
3996 uint32_t dex_method_index)
3997 : HInvoke(arena,
3998 number_of_arguments,
3999 0u /* number_of_other_inputs */,
4000 return_type,
4001 dex_pc,
4002 dex_method_index,
4003 nullptr,
4004 kVirtual) {}
4005
4006 DECLARE_INSTRUCTION(InvokePolymorphic);
4007
4008 private:
4009 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4010};
4011
Vladimir Markofcb503c2016-05-18 12:48:17 +01004012class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004013 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004014 // Requirements of this method call regarding the class
4015 // initialization (clinit) check of its declaring class.
4016 enum class ClinitCheckRequirement {
4017 kNone, // Class already initialized.
4018 kExplicit, // Static call having explicit clinit check as last input.
4019 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004020 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004021 };
4022
Vladimir Marko58155012015-08-19 12:49:41 +00004023 // Determines how to load the target ArtMethod*.
4024 enum class MethodLoadKind {
4025 // Use a String init ArtMethod* loaded from Thread entrypoints.
4026 kStringInit,
4027
4028 // Use the method's own ArtMethod* loaded by the register allocator.
4029 kRecursive,
4030
4031 // Use ArtMethod* at a known address, embed the direct address in the code.
4032 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4033 kDirectAddress,
4034
Vladimir Markoa1de9182016-02-25 11:37:38 +00004035 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004036 // Used when we need to use the dex cache, for example for invoke-static that
4037 // may cause class initialization (the entry may point to a resolution method),
4038 // and we know that we can access the dex cache arrays using a PC-relative load.
4039 kDexCachePcRelative,
4040
4041 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4042 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4043 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4044 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4045 kDexCacheViaMethod,
4046 };
4047
4048 // Determines the location of the code pointer.
4049 enum class CodePtrLocation {
4050 // Recursive call, use local PC-relative call instruction.
4051 kCallSelf,
4052
Vladimir Marko58155012015-08-19 12:49:41 +00004053 // Use code pointer from the ArtMethod*.
4054 // Used when we don't know the target code. This is also the last-resort-kind used when
4055 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4056 kCallArtMethod,
4057 };
4058
4059 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004060 MethodLoadKind method_load_kind;
4061 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004062 // The method load data holds
4063 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4064 // Note that there are multiple string init methods, each having its own offset.
4065 // - the method address for kDirectAddress
4066 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004067 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004068 };
4069
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004070 HInvokeStaticOrDirect(ArenaAllocator* arena,
4071 uint32_t number_of_arguments,
4072 Primitive::Type return_type,
4073 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004074 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004075 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004076 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004077 InvokeType invoke_type,
4078 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004079 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004080 : HInvoke(arena,
4081 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004082 // There is potentially one extra argument for the HCurrentMethod node, and
4083 // potentially one other if the clinit check is explicit, and potentially
4084 // one other if the method is a string factory.
4085 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004086 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004087 return_type,
4088 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004089 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004090 resolved_method,
4091 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004092 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004093 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004094 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4095 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004096
Vladimir Markodc151b22015-10-15 18:02:30 +01004097 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004098 bool had_current_method_input = HasCurrentMethodInput();
4099 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4100
4101 // Using the current method is the default and once we find a better
4102 // method load kind, we should not go back to using the current method.
4103 DCHECK(had_current_method_input || !needs_current_method_input);
4104
4105 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004106 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4107 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004108 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004109 dispatch_info_ = dispatch_info;
4110 }
4111
Vladimir Markoc53c0792015-11-19 15:48:33 +00004112 void AddSpecialInput(HInstruction* input) {
4113 // We allow only one special input.
4114 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4115 DCHECK(InputCount() == GetSpecialInputIndex() ||
4116 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4117 InsertInputAt(GetSpecialInputIndex(), input);
4118 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004119
Vladimir Marko372f10e2016-05-17 16:30:10 +01004120 using HInstruction::GetInputRecords; // Keep the const version visible.
4121 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4122 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4123 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4124 DCHECK(!input_records.empty());
4125 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4126 HInstruction* last_input = input_records.back().GetInstruction();
4127 // Note: `last_input` may be null during arguments setup.
4128 if (last_input != nullptr) {
4129 // `last_input` is the last input of a static invoke marked as having
4130 // an explicit clinit check. It must either be:
4131 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4132 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4133 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4134 }
4135 }
4136 return input_records;
4137 }
4138
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004139 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004140 // We access the method via the dex cache so we can't do an implicit null check.
4141 // TODO: for intrinsics we can generate implicit null checks.
4142 return false;
4143 }
4144
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004145 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004146 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004147 }
4148
Vladimir Markoc53c0792015-11-19 15:48:33 +00004149 // Get the index of the special input, if any.
4150 //
David Brazdil6de19382016-01-08 17:37:10 +00004151 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4152 // method pointer; otherwise there may be one platform-specific special input,
4153 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004154 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004155 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004156
Vladimir Marko58155012015-08-19 12:49:41 +00004157 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4158 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4159 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004160 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004161 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004162 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004163 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004164 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4165 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004166 bool HasCurrentMethodInput() const {
4167 // This function can be called only after the invoke has been fully initialized by the builder.
4168 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004169 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004170 return true;
4171 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004172 DCHECK(InputCount() == GetSpecialInputIndex() ||
4173 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004174 return false;
4175 }
4176 }
Vladimir Marko58155012015-08-19 12:49:41 +00004177
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004178 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004179 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004180 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004181 }
4182
4183 uint64_t GetMethodAddress() const {
4184 DCHECK(HasMethodAddress());
4185 return dispatch_info_.method_load_data;
4186 }
4187
4188 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004189 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004190 return dispatch_info_.method_load_data;
4191 }
4192
Vladimir Markoa1de9182016-02-25 11:37:38 +00004193 ClinitCheckRequirement GetClinitCheckRequirement() const {
4194 return GetPackedField<ClinitCheckRequirementField>();
4195 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004196
Roland Levillain4c0eb422015-04-24 16:43:49 +01004197 // Is this instruction a call to a static method?
4198 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004199 return GetInvokeType() == kStatic;
4200 }
4201
4202 MethodReference GetTargetMethod() const {
4203 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004204 }
4205
Vladimir Markofbb184a2015-11-13 14:47:00 +00004206 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4207 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4208 // instruction; only relevant for static calls with explicit clinit check.
4209 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004210 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004211 size_t last_input_index = inputs_.size() - 1u;
4212 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004213 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004214 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004215 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004216 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004217 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004218 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004219 }
4220
4221 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004222 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004223 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004224 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004225 }
4226
4227 // Is this a call to a static method whose declaring class has an
4228 // implicit intialization check requirement?
4229 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004230 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004231 }
4232
Vladimir Markob554b5a2015-11-06 12:57:55 +00004233 // Does this method load kind need the current method as an input?
4234 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4235 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4236 }
4237
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004238 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004239
4240 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004241 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004242 static constexpr size_t kFieldClinitCheckRequirementSize =
4243 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4244 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4245 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4246 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4247 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004248 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4249 kFieldClinitCheckRequirement,
4250 kFieldClinitCheckRequirementSize>;
4251
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004252 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004253 MethodReference target_method_;
4254 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004255
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004256 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004257};
Vladimir Markof64242a2015-12-01 14:58:23 +00004258std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004259std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004260
Vladimir Markofcb503c2016-05-18 12:48:17 +01004261class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004262 public:
4263 HInvokeVirtual(ArenaAllocator* arena,
4264 uint32_t number_of_arguments,
4265 Primitive::Type return_type,
4266 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004267 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004268 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004269 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004270 : HInvoke(arena,
4271 number_of_arguments,
4272 0u,
4273 return_type,
4274 dex_pc,
4275 dex_method_index,
4276 resolved_method,
4277 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004278 vtable_index_(vtable_index) {}
4279
Aart Bik71bf7b42016-11-16 10:17:46 -08004280 bool CanBeNull() const OVERRIDE {
4281 switch (GetIntrinsic()) {
4282 case Intrinsics::kThreadCurrentThread:
4283 case Intrinsics::kStringBufferAppend:
4284 case Intrinsics::kStringBufferToString:
4285 case Intrinsics::kStringBuilderAppend:
4286 case Intrinsics::kStringBuilderToString:
4287 return false;
4288 default:
4289 return HInvoke::CanBeNull();
4290 }
4291 }
4292
Calin Juravle641547a2015-04-21 22:08:51 +01004293 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004294 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004295 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004296 }
4297
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004298 uint32_t GetVTableIndex() const { return vtable_index_; }
4299
4300 DECLARE_INSTRUCTION(InvokeVirtual);
4301
4302 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004303 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004304 const uint32_t vtable_index_;
4305
4306 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4307};
4308
Vladimir Markofcb503c2016-05-18 12:48:17 +01004309class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004310 public:
4311 HInvokeInterface(ArenaAllocator* arena,
4312 uint32_t number_of_arguments,
4313 Primitive::Type return_type,
4314 uint32_t dex_pc,
4315 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004316 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004317 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004318 : HInvoke(arena,
4319 number_of_arguments,
4320 0u,
4321 return_type,
4322 dex_pc,
4323 dex_method_index,
4324 resolved_method,
4325 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004326 imt_index_(imt_index) {}
4327
Calin Juravle641547a2015-04-21 22:08:51 +01004328 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004330 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004331 }
4332
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004333 uint32_t GetImtIndex() const { return imt_index_; }
4334 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4335
4336 DECLARE_INSTRUCTION(InvokeInterface);
4337
4338 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004339 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004340 const uint32_t imt_index_;
4341
4342 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4343};
4344
Vladimir Markofcb503c2016-05-18 12:48:17 +01004345class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004346 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004347 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004348 : HUnaryOperation(result_type, input, dex_pc) {
4349 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4350 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004351
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004352 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004353
4354 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004355 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004356 }
4357 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004358 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004359 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004360 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4361 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4362 }
4363 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4364 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4365 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004366
Roland Levillain88cb1752014-10-20 16:36:47 +01004367 DECLARE_INSTRUCTION(Neg);
4368
4369 private:
4370 DISALLOW_COPY_AND_ASSIGN(HNeg);
4371};
4372
Vladimir Markofcb503c2016-05-18 12:48:17 +01004373class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004374 public:
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004375 HNewArray(HInstruction* length,
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004376 HCurrentMethod* current_method,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004377 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004378 dex::TypeIndex type_index,
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004379 const DexFile& dex_file,
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004380 QuickEntrypointEnum entrypoint)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004381 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004382 type_index_(type_index),
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004383 dex_file_(dex_file),
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004384 entrypoint_(entrypoint) {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004385 SetRawInputAt(0, length);
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004386 SetRawInputAt(1, current_method);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004387 }
4388
Andreas Gampea5b09a62016-11-17 15:21:22 -08004389 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004390 const DexFile& GetDexFile() const { return dex_file_; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004391
4392 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004393 bool NeedsEnvironment() const OVERRIDE { return true; }
4394
Mingyao Yang0c365e62015-03-31 15:09:29 -07004395 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4396 bool CanThrow() const OVERRIDE { return true; }
4397
Calin Juravle10e244f2015-01-26 18:54:32 +00004398 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004399
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004400 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
4401
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004402 DECLARE_INSTRUCTION(NewArray);
4403
4404 private:
Andreas Gampea5b09a62016-11-17 15:21:22 -08004405 const dex::TypeIndex type_index_;
Guillaume "Vermeille" Sanchez81d804a2015-05-20 12:42:25 +01004406 const DexFile& dex_file_;
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004407 const QuickEntrypointEnum entrypoint_;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004408
4409 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4410};
4411
Vladimir Markofcb503c2016-05-18 12:48:17 +01004412class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004413 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004414 HAdd(Primitive::Type result_type,
4415 HInstruction* left,
4416 HInstruction* right,
4417 uint32_t dex_pc = kNoDexPc)
4418 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004419
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004420 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004421
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004422 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004423
4424 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004425 return GetBlock()->GetGraph()->GetIntConstant(
4426 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004427 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004428 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004429 return GetBlock()->GetGraph()->GetLongConstant(
4430 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004431 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004432 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4433 return GetBlock()->GetGraph()->GetFloatConstant(
4434 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4435 }
4436 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4437 return GetBlock()->GetGraph()->GetDoubleConstant(
4438 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4439 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004440
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004441 DECLARE_INSTRUCTION(Add);
4442
4443 private:
4444 DISALLOW_COPY_AND_ASSIGN(HAdd);
4445};
4446
Vladimir Markofcb503c2016-05-18 12:48:17 +01004447class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004448 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004449 HSub(Primitive::Type result_type,
4450 HInstruction* left,
4451 HInstruction* right,
4452 uint32_t dex_pc = kNoDexPc)
4453 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004454
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004455 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004456
4457 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004458 return GetBlock()->GetGraph()->GetIntConstant(
4459 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004460 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004461 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004462 return GetBlock()->GetGraph()->GetLongConstant(
4463 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004464 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004465 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4466 return GetBlock()->GetGraph()->GetFloatConstant(
4467 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4468 }
4469 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4470 return GetBlock()->GetGraph()->GetDoubleConstant(
4471 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4472 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004473
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004474 DECLARE_INSTRUCTION(Sub);
4475
4476 private:
4477 DISALLOW_COPY_AND_ASSIGN(HSub);
4478};
4479
Vladimir Markofcb503c2016-05-18 12:48:17 +01004480class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004481 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004482 HMul(Primitive::Type result_type,
4483 HInstruction* left,
4484 HInstruction* right,
4485 uint32_t dex_pc = kNoDexPc)
4486 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004487
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004488 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004489
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004490 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004491
4492 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004493 return GetBlock()->GetGraph()->GetIntConstant(
4494 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004495 }
4496 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004497 return GetBlock()->GetGraph()->GetLongConstant(
4498 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004499 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004500 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4501 return GetBlock()->GetGraph()->GetFloatConstant(
4502 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4503 }
4504 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4505 return GetBlock()->GetGraph()->GetDoubleConstant(
4506 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4507 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004508
4509 DECLARE_INSTRUCTION(Mul);
4510
4511 private:
4512 DISALLOW_COPY_AND_ASSIGN(HMul);
4513};
4514
Vladimir Markofcb503c2016-05-18 12:48:17 +01004515class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004516 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004517 HDiv(Primitive::Type result_type,
4518 HInstruction* left,
4519 HInstruction* right,
4520 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004521 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004522
Roland Levillain9867bc72015-08-05 10:21:34 +01004523 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004524 T ComputeIntegral(T x, T y) const {
4525 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004526 // Our graph structure ensures we never have 0 for `y` during
4527 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004528 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004529 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004530 return (y == -1) ? -x : x / y;
4531 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004532
Roland Levillain31dd3d62016-02-16 12:21:02 +00004533 template <typename T>
4534 T ComputeFP(T x, T y) const {
4535 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4536 return x / y;
4537 }
4538
Roland Levillain9867bc72015-08-05 10:21:34 +01004539 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004540 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004541 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004542 }
4543 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004544 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004545 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4546 }
4547 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4548 return GetBlock()->GetGraph()->GetFloatConstant(
4549 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4550 }
4551 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4552 return GetBlock()->GetGraph()->GetDoubleConstant(
4553 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004554 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004555
4556 DECLARE_INSTRUCTION(Div);
4557
4558 private:
4559 DISALLOW_COPY_AND_ASSIGN(HDiv);
4560};
4561
Vladimir Markofcb503c2016-05-18 12:48:17 +01004562class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004563 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004564 HRem(Primitive::Type result_type,
4565 HInstruction* left,
4566 HInstruction* right,
4567 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004568 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004569
Roland Levillain9867bc72015-08-05 10:21:34 +01004570 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004571 T ComputeIntegral(T x, T y) const {
4572 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004573 // Our graph structure ensures we never have 0 for `y` during
4574 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004575 DCHECK_NE(y, 0);
4576 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4577 return (y == -1) ? 0 : x % y;
4578 }
4579
Roland Levillain31dd3d62016-02-16 12:21:02 +00004580 template <typename T>
4581 T ComputeFP(T x, T y) const {
4582 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4583 return std::fmod(x, y);
4584 }
4585
Roland Levillain9867bc72015-08-05 10:21:34 +01004586 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004587 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004588 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004589 }
4590 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004591 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004592 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004593 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004594 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4595 return GetBlock()->GetGraph()->GetFloatConstant(
4596 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4597 }
4598 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4599 return GetBlock()->GetGraph()->GetDoubleConstant(
4600 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4601 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004602
4603 DECLARE_INSTRUCTION(Rem);
4604
4605 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004606 DISALLOW_COPY_AND_ASSIGN(HRem);
4607};
4608
Vladimir Markofcb503c2016-05-18 12:48:17 +01004609class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004610 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004611 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4612 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004613 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004614 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004615 SetRawInputAt(0, value);
4616 }
4617
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004618 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4619
Calin Juravled0d48522014-11-04 16:40:20 +00004620 bool CanBeMoved() const OVERRIDE { return true; }
4621
Vladimir Marko372f10e2016-05-17 16:30:10 +01004622 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004623 return true;
4624 }
4625
4626 bool NeedsEnvironment() const OVERRIDE { return true; }
4627 bool CanThrow() const OVERRIDE { return true; }
4628
Calin Juravled0d48522014-11-04 16:40:20 +00004629 DECLARE_INSTRUCTION(DivZeroCheck);
4630
4631 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004632 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4633};
4634
Vladimir Markofcb503c2016-05-18 12:48:17 +01004635class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004636 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004637 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004638 HInstruction* value,
4639 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004640 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004641 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4642 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4643 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004644 }
4645
Roland Levillain5b5b9312016-03-22 14:57:31 +00004646 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004647 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004648 return value << (distance & max_shift_distance);
4649 }
4650
4651 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004652 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004653 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004654 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004655 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004656 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004657 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004658 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004659 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4660 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4661 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4662 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004663 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004664 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4665 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004666 LOG(FATAL) << DebugName() << " is not defined for float values";
4667 UNREACHABLE();
4668 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004669 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4670 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004671 LOG(FATAL) << DebugName() << " is not defined for double values";
4672 UNREACHABLE();
4673 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004674
4675 DECLARE_INSTRUCTION(Shl);
4676
4677 private:
4678 DISALLOW_COPY_AND_ASSIGN(HShl);
4679};
4680
Vladimir Markofcb503c2016-05-18 12:48:17 +01004681class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004682 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004683 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004684 HInstruction* value,
4685 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004686 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004687 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4688 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4689 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004690 }
4691
Roland Levillain5b5b9312016-03-22 14:57:31 +00004692 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004693 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004694 return value >> (distance & max_shift_distance);
4695 }
4696
4697 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004698 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004699 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004700 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004701 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004702 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004703 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004704 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004705 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4706 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4707 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4708 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004709 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004710 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4711 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004712 LOG(FATAL) << DebugName() << " is not defined for float values";
4713 UNREACHABLE();
4714 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004715 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4716 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004717 LOG(FATAL) << DebugName() << " is not defined for double values";
4718 UNREACHABLE();
4719 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004720
4721 DECLARE_INSTRUCTION(Shr);
4722
4723 private:
4724 DISALLOW_COPY_AND_ASSIGN(HShr);
4725};
4726
Vladimir Markofcb503c2016-05-18 12:48:17 +01004727class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004728 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004729 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 HInstruction* value,
4731 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004732 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004733 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4734 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4735 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004736 }
4737
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004739 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 typedef typename std::make_unsigned<T>::type V;
4741 V ux = static_cast<V>(value);
4742 return static_cast<T>(ux >> (distance & max_shift_distance));
4743 }
4744
4745 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004746 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004747 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004748 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004749 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004750 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004751 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004752 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004753 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4754 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4755 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4756 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004757 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4759 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004760 LOG(FATAL) << DebugName() << " is not defined for float values";
4761 UNREACHABLE();
4762 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4764 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004765 LOG(FATAL) << DebugName() << " is not defined for double values";
4766 UNREACHABLE();
4767 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004768
4769 DECLARE_INSTRUCTION(UShr);
4770
4771 private:
4772 DISALLOW_COPY_AND_ASSIGN(HUShr);
4773};
4774
Vladimir Markofcb503c2016-05-18 12:48:17 +01004775class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004776 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004777 HAnd(Primitive::Type result_type,
4778 HInstruction* left,
4779 HInstruction* right,
4780 uint32_t dex_pc = kNoDexPc)
4781 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004782
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004783 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004784
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004785 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004786
4787 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004788 return GetBlock()->GetGraph()->GetIntConstant(
4789 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004790 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004792 return GetBlock()->GetGraph()->GetLongConstant(
4793 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004794 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004795 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4796 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4797 LOG(FATAL) << DebugName() << " is not defined for float values";
4798 UNREACHABLE();
4799 }
4800 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4801 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4802 LOG(FATAL) << DebugName() << " is not defined for double values";
4803 UNREACHABLE();
4804 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004805
4806 DECLARE_INSTRUCTION(And);
4807
4808 private:
4809 DISALLOW_COPY_AND_ASSIGN(HAnd);
4810};
4811
Vladimir Markofcb503c2016-05-18 12:48:17 +01004812class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004813 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004814 HOr(Primitive::Type result_type,
4815 HInstruction* left,
4816 HInstruction* right,
4817 uint32_t dex_pc = kNoDexPc)
4818 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004819
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004820 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004821
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004822 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004823
4824 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004825 return GetBlock()->GetGraph()->GetIntConstant(
4826 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004827 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004829 return GetBlock()->GetGraph()->GetLongConstant(
4830 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004831 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004832 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4833 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4834 LOG(FATAL) << DebugName() << " is not defined for float values";
4835 UNREACHABLE();
4836 }
4837 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4838 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4839 LOG(FATAL) << DebugName() << " is not defined for double values";
4840 UNREACHABLE();
4841 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004842
4843 DECLARE_INSTRUCTION(Or);
4844
4845 private:
4846 DISALLOW_COPY_AND_ASSIGN(HOr);
4847};
4848
Vladimir Markofcb503c2016-05-18 12:48:17 +01004849class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004850 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004851 HXor(Primitive::Type result_type,
4852 HInstruction* left,
4853 HInstruction* right,
4854 uint32_t dex_pc = kNoDexPc)
4855 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004856
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004857 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004858
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004859 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004860
4861 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004862 return GetBlock()->GetGraph()->GetIntConstant(
4863 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004864 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004866 return GetBlock()->GetGraph()->GetLongConstant(
4867 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004868 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004869 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4870 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4871 LOG(FATAL) << DebugName() << " is not defined for float values";
4872 UNREACHABLE();
4873 }
4874 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4875 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4876 LOG(FATAL) << DebugName() << " is not defined for double values";
4877 UNREACHABLE();
4878 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004879
4880 DECLARE_INSTRUCTION(Xor);
4881
4882 private:
4883 DISALLOW_COPY_AND_ASSIGN(HXor);
4884};
4885
Vladimir Markofcb503c2016-05-18 12:48:17 +01004886class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004887 public:
4888 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004889 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004890 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4891 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004892 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004893
Roland Levillain5b5b9312016-03-22 14:57:31 +00004894 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004895 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004896 typedef typename std::make_unsigned<T>::type V;
4897 V ux = static_cast<V>(value);
4898 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004899 return static_cast<T>(ux);
4900 } else {
4901 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004902 return static_cast<T>(ux >> (distance & max_shift_value)) |
4903 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004904 }
4905 }
4906
Roland Levillain5b5b9312016-03-22 14:57:31 +00004907 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004908 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004909 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004910 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004911 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004912 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004913 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004914 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004915 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4916 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4917 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4918 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004919 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004920 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4921 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004922 LOG(FATAL) << DebugName() << " is not defined for float values";
4923 UNREACHABLE();
4924 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004925 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4926 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004927 LOG(FATAL) << DebugName() << " is not defined for double values";
4928 UNREACHABLE();
4929 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004930
4931 DECLARE_INSTRUCTION(Ror);
4932
4933 private:
4934 DISALLOW_COPY_AND_ASSIGN(HRor);
4935};
4936
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004937// The value of a parameter in this method. Its location depends on
4938// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004939class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004940 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004941 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004942 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004943 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004944 Primitive::Type parameter_type,
4945 bool is_this = false)
4946 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004947 dex_file_(dex_file),
4948 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004949 index_(index) {
4950 SetPackedFlag<kFlagIsThis>(is_this);
4951 SetPackedFlag<kFlagCanBeNull>(!is_this);
4952 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004953
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004954 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004955 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004956 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004957 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004958
Vladimir Markoa1de9182016-02-25 11:37:38 +00004959 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4960 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004961
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004962 DECLARE_INSTRUCTION(ParameterValue);
4963
4964 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004965 // Whether or not the parameter value corresponds to 'this' argument.
4966 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4967 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4968 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4969 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4970 "Too many packed fields.");
4971
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004972 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004973 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004974 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004975 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004976 const uint8_t index_;
4977
4978 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4979};
4980
Vladimir Markofcb503c2016-05-18 12:48:17 +01004981class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004982 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004983 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4984 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004985
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004986 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004987 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004988 return true;
4989 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004990
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004991 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004992
4993 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004994 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004995 }
4996 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004997 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004998 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004999 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5000 LOG(FATAL) << DebugName() << " is not defined for float values";
5001 UNREACHABLE();
5002 }
5003 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5004 LOG(FATAL) << DebugName() << " is not defined for double values";
5005 UNREACHABLE();
5006 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005007
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005008 DECLARE_INSTRUCTION(Not);
5009
5010 private:
5011 DISALLOW_COPY_AND_ASSIGN(HNot);
5012};
5013
Vladimir Markofcb503c2016-05-18 12:48:17 +01005014class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005015 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005016 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5017 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005018
5019 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005020 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005021 return true;
5022 }
5023
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005024 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005025 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005026 return !x;
5027 }
5028
Roland Levillain9867bc72015-08-05 10:21:34 +01005029 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005030 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005031 }
5032 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5033 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005034 UNREACHABLE();
5035 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005036 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5037 LOG(FATAL) << DebugName() << " is not defined for float values";
5038 UNREACHABLE();
5039 }
5040 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5041 LOG(FATAL) << DebugName() << " is not defined for double values";
5042 UNREACHABLE();
5043 }
David Brazdil66d126e2015-04-03 16:02:44 +01005044
5045 DECLARE_INSTRUCTION(BooleanNot);
5046
5047 private:
5048 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5049};
5050
Vladimir Markofcb503c2016-05-18 12:48:17 +01005051class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005052 public:
5053 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005054 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005055 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005056 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005057 // Invariant: We should never generate a conversion to a Boolean value.
5058 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005059 }
5060
5061 HInstruction* GetInput() const { return InputAt(0); }
5062 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5063 Primitive::Type GetResultType() const { return GetType(); }
5064
5065 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005066 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5067 return true;
5068 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005069
Mark Mendelle82549b2015-05-06 10:55:34 -04005070 // Try to statically evaluate the conversion and return a HConstant
5071 // containing the result. If the input cannot be converted, return nullptr.
5072 HConstant* TryStaticEvaluation() const;
5073
Roland Levillaindff1f282014-11-05 14:15:05 +00005074 DECLARE_INSTRUCTION(TypeConversion);
5075
5076 private:
5077 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5078};
5079
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005080static constexpr uint32_t kNoRegNumber = -1;
5081
Vladimir Markofcb503c2016-05-18 12:48:17 +01005082class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005084 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5085 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005086 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005087 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005088 SetRawInputAt(0, value);
5089 }
5090
Calin Juravle10e244f2015-01-26 18:54:32 +00005091 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005092 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005093 return true;
5094 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005095
Calin Juravle10e244f2015-01-26 18:54:32 +00005096 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097
Calin Juravle10e244f2015-01-26 18:54:32 +00005098 bool CanThrow() const OVERRIDE { return true; }
5099
5100 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005101
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005102
5103 DECLARE_INSTRUCTION(NullCheck);
5104
5105 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005106 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5107};
5108
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005109// Embeds an ArtField and all the information required by the compiler. We cache
5110// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111class FieldInfo : public ValueObject {
5112 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005113 FieldInfo(ArtField* field,
5114 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005115 Primitive::Type field_type,
5116 bool is_volatile,
5117 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005118 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005119 const DexFile& dex_file)
5120 : field_(field),
5121 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005122 field_type_(field_type),
5123 is_volatile_(is_volatile),
5124 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005125 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005126 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005127
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005128 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005129 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005130 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005131 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005132 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005133 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005134 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005135
5136 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005137 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005138 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005139 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005140 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005141 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005142 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005143 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005144};
5145
Vladimir Markofcb503c2016-05-18 12:48:17 +01005146class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005147 public:
5148 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005149 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005151 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005152 bool is_volatile,
5153 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005154 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005155 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005156 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005157 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005158 field_info_(field,
5159 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005160 field_type,
5161 is_volatile,
5162 field_idx,
5163 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005164 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005165 SetRawInputAt(0, value);
5166 }
5167
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005168 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005169
Vladimir Marko372f10e2016-05-17 16:30:10 +01005170 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5171 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005172 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005173 }
5174
Calin Juravle641547a2015-04-21 22:08:51 +01005175 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005176 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005177 }
5178
5179 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005180 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5181 }
5182
Calin Juravle52c48962014-12-16 17:02:57 +00005183 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005185 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005186 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187
5188 DECLARE_INSTRUCTION(InstanceFieldGet);
5189
5190 private:
5191 const FieldInfo field_info_;
5192
5193 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5194};
5195
Vladimir Markofcb503c2016-05-18 12:48:17 +01005196class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005197 public:
5198 HInstanceFieldSet(HInstruction* object,
5199 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005200 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005201 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005202 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005203 bool is_volatile,
5204 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005205 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005206 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005207 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005208 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005209 field_info_(field,
5210 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005211 field_type,
5212 is_volatile,
5213 field_idx,
5214 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005215 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005216 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005217 SetRawInputAt(0, object);
5218 SetRawInputAt(1, value);
5219 }
5220
Calin Juravle641547a2015-04-21 22:08:51 +01005221 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005222 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 }
5224
Calin Juravle52c48962014-12-16 17:02:57 +00005225 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005226 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005227 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005228 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005229 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005230 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5231 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005232
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005233 DECLARE_INSTRUCTION(InstanceFieldSet);
5234
5235 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005236 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5237 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5238 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5239 "Too many packed fields.");
5240
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005241 const FieldInfo field_info_;
5242
5243 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5244};
5245
Vladimir Markofcb503c2016-05-18 12:48:17 +01005246class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005248 HArrayGet(HInstruction* array,
5249 HInstruction* index,
5250 Primitive::Type type,
5251 uint32_t dex_pc,
5252 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005253 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005254 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255 SetRawInputAt(0, array);
5256 SetRawInputAt(1, index);
5257 }
5258
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005259 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005260 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005261 return true;
5262 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005263 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005264 // TODO: We can be smarter here.
5265 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5266 // which generates the implicit null check. There are cases when these can be removed
5267 // to produce better code. If we ever add optimizations to do so we should allow an
5268 // implicit check here (as long as the address falls in the first page).
5269 return false;
5270 }
5271
David Brazdil4833f5a2015-12-16 10:37:39 +00005272 bool IsEquivalentOf(HArrayGet* other) const {
5273 bool result = (GetDexPc() == other->GetDexPc());
5274 if (kIsDebugBuild && result) {
5275 DCHECK_EQ(GetBlock(), other->GetBlock());
5276 DCHECK_EQ(GetArray(), other->GetArray());
5277 DCHECK_EQ(GetIndex(), other->GetIndex());
5278 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005279 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005280 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005281 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5282 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005283 }
5284 }
5285 return result;
5286 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005287
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005288 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5289
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005290 HInstruction* GetArray() const { return InputAt(0); }
5291 HInstruction* GetIndex() const { return InputAt(1); }
5292
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 DECLARE_INSTRUCTION(ArrayGet);
5294
5295 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005296 // We treat a String as an array, creating the HArrayGet from String.charAt()
5297 // intrinsic in the instruction simplifier. We can always determine whether
5298 // a particular HArrayGet is actually a String.charAt() by looking at the type
5299 // of the input but that requires holding the mutator lock, so we prefer to use
5300 // a flag, so that code generators don't need to do the locking.
5301 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5302 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5303 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5304 "Too many packed fields.");
5305
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5307};
5308
Vladimir Markofcb503c2016-05-18 12:48:17 +01005309class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 public:
5311 HArraySet(HInstruction* array,
5312 HInstruction* index,
5313 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005314 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005315 uint32_t dex_pc)
5316 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005317 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5318 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5319 SetPackedFlag<kFlagValueCanBeNull>(true);
5320 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 SetRawInputAt(0, array);
5322 SetRawInputAt(1, index);
5323 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005324 // Make a best guess now, may be refined during SSA building.
5325 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 }
5327
Calin Juravle77520bc2015-01-12 18:45:46 +00005328 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005329 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005330 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 }
5332
Mingyao Yang81014cb2015-06-02 03:16:27 -07005333 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005334 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005336 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005337 // TODO: Same as for ArrayGet.
5338 return false;
5339 }
5340
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005341 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005342 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005343 }
5344
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005345 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005346 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005347 }
5348
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005349 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005350 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005351 }
5352
Vladimir Markoa1de9182016-02-25 11:37:38 +00005353 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5354 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5355 bool StaticTypeOfArrayIsObjectArray() const {
5356 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5357 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005358
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005359 HInstruction* GetArray() const { return InputAt(0); }
5360 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005361 HInstruction* GetValue() const { return InputAt(2); }
5362
5363 Primitive::Type GetComponentType() const {
5364 // The Dex format does not type floating point index operations. Since the
5365 // `expected_component_type_` is set during building and can therefore not
5366 // be correct, we also check what is the value type. If it is a floating
5367 // point type, we must use that type.
5368 Primitive::Type value_type = GetValue()->GetType();
5369 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5370 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005371 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005372 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005373
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005374 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005375 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005376 }
5377
Aart Bik18b36ab2016-04-13 16:41:35 -07005378 void ComputeSideEffects() {
5379 Primitive::Type type = GetComponentType();
5380 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5381 SideEffectsForArchRuntimeCalls(type)));
5382 }
5383
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005384 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5385 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5386 }
5387
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005388 DECLARE_INSTRUCTION(ArraySet);
5389
5390 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005391 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5392 static constexpr size_t kFieldExpectedComponentTypeSize =
5393 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5394 static constexpr size_t kFlagNeedsTypeCheck =
5395 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5396 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 // Cached information for the reference_type_info_ so that codegen
5398 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005399 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5400 static constexpr size_t kNumberOfArraySetPackedBits =
5401 kFlagStaticTypeOfArrayIsObjectArray + 1;
5402 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5403 using ExpectedComponentTypeField =
5404 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405
5406 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5407};
5408
Vladimir Markofcb503c2016-05-18 12:48:17 +01005409class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005411 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005412 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005413 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005414 // Note that arrays do not change length, so the instruction does not
5415 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 SetRawInputAt(0, array);
5417 }
5418
Calin Juravle77520bc2015-01-12 18:45:46 +00005419 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005420 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005421 return true;
5422 }
Calin Juravle641547a2015-04-21 22:08:51 +01005423 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5424 return obj == InputAt(0);
5425 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005426
Vladimir Markodce016e2016-04-28 13:10:02 +01005427 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5428
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 DECLARE_INSTRUCTION(ArrayLength);
5430
5431 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005432 // We treat a String as an array, creating the HArrayLength from String.length()
5433 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5434 // determine whether a particular HArrayLength is actually a String.length() by
5435 // looking at the type of the input but that requires holding the mutator lock, so
5436 // we prefer to use a flag, so that code generators don't need to do the locking.
5437 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5438 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5439 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5440 "Too many packed fields.");
5441
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005442 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5443};
5444
Vladimir Markofcb503c2016-05-18 12:48:17 +01005445class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005446 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005447 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5448 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005449 HBoundsCheck(HInstruction* index,
5450 HInstruction* length,
5451 uint32_t dex_pc,
5452 uint32_t string_char_at_method_index = DexFile::kDexNoIndex)
5453 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc),
5454 string_char_at_method_index_(string_char_at_method_index) {
David Brazdildee58d62016-04-07 09:54:26 +00005455 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005456 SetRawInputAt(0, index);
5457 SetRawInputAt(1, length);
5458 }
5459
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005460 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005461 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005462 return true;
5463 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005464
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005465 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005466
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005467 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005468
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005469 bool IsStringCharAt() const { return GetStringCharAtMethodIndex() != DexFile::kDexNoIndex; }
5470 uint32_t GetStringCharAtMethodIndex() const { return string_char_at_method_index_; }
5471
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005472 HInstruction* GetIndex() const { return InputAt(0); }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005473
5474 DECLARE_INSTRUCTION(BoundsCheck);
5475
5476 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005477 // We treat a String as an array, creating the HBoundsCheck from String.charAt()
5478 // intrinsic in the instruction simplifier. We want to include the String.charAt()
5479 // in the stack trace if we actually throw the StringIndexOutOfBoundsException,
5480 // so we need to create an HEnvironment which will be translated to an InlineInfo
5481 // indicating the extra stack frame. Since we add this HEnvironment quite late,
5482 // in the PrepareForRegisterAllocation pass, we need to remember the method index
5483 // from the invoke as we don't want to look again at the dex bytecode.
5484 uint32_t string_char_at_method_index_; // DexFile::kDexNoIndex if regular array.
5485
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005486 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5487};
5488
Vladimir Markofcb503c2016-05-18 12:48:17 +01005489class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005490 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005491 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005492 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005493
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005494 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005495 return true;
5496 }
5497
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005498 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5499 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005500
5501 DECLARE_INSTRUCTION(SuspendCheck);
5502
5503 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005504 // Only used for code generation, in order to share the same slow path between back edges
5505 // of a same loop.
5506 SlowPathCode* slow_path_;
5507
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005508 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5509};
5510
David Srbecky0cf44932015-12-09 14:09:59 +00005511// Pseudo-instruction which provides the native debugger with mapping information.
5512// It ensures that we can generate line number and local variables at this point.
5513class HNativeDebugInfo : public HTemplateInstruction<0> {
5514 public:
5515 explicit HNativeDebugInfo(uint32_t dex_pc)
5516 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5517
5518 bool NeedsEnvironment() const OVERRIDE {
5519 return true;
5520 }
5521
5522 DECLARE_INSTRUCTION(NativeDebugInfo);
5523
5524 private:
5525 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5526};
5527
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005528/**
5529 * Instruction to load a Class object.
5530 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005532 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005533 // Determines how to load the Class.
5534 enum class LoadKind {
5535 // Use the Class* from the method's own ArtMethod*.
5536 kReferrersClass,
5537
5538 // Use boot image Class* address that will be known at link time.
5539 // Used for boot image classes referenced by boot image code in non-PIC mode.
5540 kBootImageLinkTimeAddress,
5541
5542 // Use PC-relative boot image Class* address that will be known at link time.
5543 // Used for boot image classes referenced by boot image code in PIC mode.
5544 kBootImageLinkTimePcRelative,
5545
5546 // Use a known boot image Class* address, embedded in the code by the codegen.
5547 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5548 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5549 // GetIncludePatchInformation().
5550 kBootImageAddress,
5551
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005552 // Load from the root table associated with the JIT compiled method.
5553 kJitTableAddress,
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005554
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005555 // Load from resolved types array accessed through the class loaded from
5556 // the compiled method's own ArtMethod*. This is the default access type when
5557 // all other types are unavailable.
5558 kDexCacheViaMethod,
5559
5560 kLast = kDexCacheViaMethod
5561 };
5562
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005563 HLoadClass(HCurrentMethod* current_method,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005564 dex::TypeIndex type_index,
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005565 const DexFile& dex_file,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005566 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005567 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005568 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005569 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5570 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005571 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005572 dex_file_(dex_file),
Vladimir Marko48886c22017-01-06 11:45:47 +00005573 address_(0u),
Calin Juravle2e768302015-07-28 14:41:11 +00005574 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005575 // Referrers class should not need access check. We never inline unverified
5576 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005577 DCHECK(!is_referrers_class || !needs_access_check);
5578
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005579 SetPackedField<LoadKindField>(
5580 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005581 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005582 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005583 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005584 }
5585
5586 void SetLoadKindWithAddress(LoadKind load_kind, uint64_t address) {
5587 DCHECK(HasAddress(load_kind));
Vladimir Marko48886c22017-01-06 11:45:47 +00005588 DCHECK_NE(address, 0u);
5589 address_ = address;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005590 SetLoadKindInternal(load_kind);
5591 }
5592
5593 void SetLoadKindWithTypeReference(LoadKind load_kind,
5594 const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08005595 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005596 DCHECK(HasTypeReference(load_kind));
5597 DCHECK(IsSameDexFile(dex_file_, dex_file));
5598 DCHECK_EQ(type_index_, type_index);
5599 SetLoadKindInternal(load_kind);
5600 }
5601
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005602 LoadKind GetLoadKind() const {
5603 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005604 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005605
5606 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005608 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005609
Andreas Gampea5b09a62016-11-17 15:21:22 -08005610 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005611
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005612 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005613
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005614 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005615 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616 }
5617
Calin Juravle0ba218d2015-05-19 18:46:01 +01005618 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005619 // The entrypoint the code generator is going to call does not do
5620 // clinit of the class.
5621 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005622 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005623 }
5624
5625 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005626 return NeedsAccessCheck() ||
5627 MustGenerateClinitCheck() ||
5628 GetLoadKind() == LoadKind::kDexCacheViaMethod;
Calin Juravle98893e12015-10-02 21:05:03 +01005629 }
5630
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005631 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005632 return NeedsAccessCheck() ||
5633 MustGenerateClinitCheck() ||
5634 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5635 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5636 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
5637 (GetLoadKind() == LoadKind::kDexCacheViaMethod && !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005638 }
5639
Calin Juravleacf735c2015-02-12 15:25:22 +00005640 ReferenceTypeInfo GetLoadedClassRTI() {
5641 return loaded_class_rti_;
5642 }
5643
5644 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5645 // Make sure we only set exact types (the loaded class should never be merged).
5646 DCHECK(rti.IsExact());
5647 loaded_class_rti_ = rti;
5648 }
5649
Andreas Gampea5b09a62016-11-17 15:21:22 -08005650 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005651 const DexFile& GetDexFile() const { return dex_file_; }
5652
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005653 uint64_t GetAddress() const {
5654 DCHECK(HasAddress(GetLoadKind()));
Vladimir Marko48886c22017-01-06 11:45:47 +00005655 return address_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005656 }
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005657
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005658 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005659 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005660 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005661
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005662 static SideEffects SideEffectsForArchRuntimeCalls() {
5663 return SideEffects::CanTriggerGC();
5664 }
5665
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005666 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005667 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005668 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005669 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005670
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005671 void MarkInBootImage() {
5672 SetPackedFlag<kFlagIsInBootImage>(true);
5673 }
5674
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005675 void AddSpecialInput(HInstruction* special_input);
5676
5677 using HInstruction::GetInputRecords; // Keep the const version visible.
5678 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5679 return ArrayRef<HUserRecord<HInstruction*>>(
5680 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5681 }
5682
5683 Primitive::Type GetType() const OVERRIDE {
5684 return Primitive::kPrimNot;
5685 }
5686
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005687 DECLARE_INSTRUCTION(LoadClass);
5688
5689 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005690 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005691 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005692 // Whether this instruction must generate the initialization check.
5693 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005694 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005695 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5696 static constexpr size_t kFieldLoadKindSize =
5697 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5698 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005699 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005700 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5701
5702 static bool HasTypeReference(LoadKind load_kind) {
5703 return load_kind == LoadKind::kBootImageLinkTimeAddress ||
5704 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
5705 load_kind == LoadKind::kDexCacheViaMethod ||
5706 load_kind == LoadKind::kReferrersClass;
5707 }
5708
5709 static bool HasAddress(LoadKind load_kind) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005710 return load_kind == LoadKind::kBootImageAddress ||
5711 load_kind == LoadKind::kJitTableAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005712 }
5713
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005714 void SetLoadKindInternal(LoadKind load_kind);
5715
5716 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5717 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005718 // for PC-relative loads, i.e. kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005720
Andreas Gampea5b09a62016-11-17 15:21:22 -08005721 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005722 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723
Vladimir Marko48886c22017-01-06 11:45:47 +00005724 uint64_t address_; // Up to 64-bit, needed for kJitTableAddress on 64-bit targets.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005725
Calin Juravleacf735c2015-02-12 15:25:22 +00005726 ReferenceTypeInfo loaded_class_rti_;
5727
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005728 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5729};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005730std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5731
5732// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005733inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005734 // The special input is used for PC-relative loads on some architectures,
5735 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005736 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005737 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5738 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005739 DCHECK(special_input_.GetInstruction() == nullptr);
5740 special_input_ = HUserRecord<HInstruction*>(special_input);
5741 special_input->AddUseAt(this, 0);
5742}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005743
Vladimir Marko372f10e2016-05-17 16:30:10 +01005744class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005745 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005746 // Determines how to load the String.
5747 enum class LoadKind {
5748 // Use boot image String* address that will be known at link time.
5749 // Used for boot image strings referenced by boot image code in non-PIC mode.
5750 kBootImageLinkTimeAddress,
5751
5752 // Use PC-relative boot image String* address that will be known at link time.
5753 // Used for boot image strings referenced by boot image code in PIC mode.
5754 kBootImageLinkTimePcRelative,
5755
5756 // Use a known boot image String* address, embedded in the code by the codegen.
5757 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5758 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5759 // GetIncludePatchInformation().
5760 kBootImageAddress,
5761
Vladimir Markoaad75c62016-10-03 08:46:48 +00005762 // Load from an entry in the .bss section using a PC-relative load.
5763 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5764 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005765
5766 // Load from resolved strings array accessed through the class loaded from
5767 // the compiled method's own ArtMethod*. This is the default access type when
5768 // all other types are unavailable.
5769 kDexCacheViaMethod,
5770
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005771 // Load from the root table associated with the JIT compiled method.
5772 kJitTableAddress,
5773
5774 kLast = kJitTableAddress,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005775 };
5776
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005777 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005778 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005779 const DexFile& dex_file,
5780 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005781 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5782 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005783 string_index_(string_index),
5784 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005785 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005786 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005787
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005788 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005789
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 LoadKind GetLoadKind() const {
5791 return GetPackedField<LoadKindField>();
5792 }
5793
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005794 const DexFile& GetDexFile() const {
5795 return dex_file_;
5796 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005797
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005798 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005799 return string_index_;
5800 }
5801
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005802 Handle<mirror::String> GetString() const {
5803 return string_;
5804 }
5805
5806 void SetString(Handle<mirror::String> str) {
5807 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005808 }
5809
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005810 bool CanBeMoved() const OVERRIDE { return true; }
5811
Vladimir Marko372f10e2016-05-17 16:30:10 +01005812 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005813
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005814 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005815
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005816 // Will call the runtime if we need to load the string through
5817 // the dex cache and the string is not guaranteed to be there yet.
5818 bool NeedsEnvironment() const OVERRIDE {
5819 LoadKind load_kind = GetLoadKind();
5820 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5821 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005822 load_kind == LoadKind::kBootImageAddress ||
5823 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005824 return false;
5825 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005826 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005827 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005828
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005829 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5830 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5831 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005832
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005833 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005834 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005835
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005836 static SideEffects SideEffectsForArchRuntimeCalls() {
5837 return SideEffects::CanTriggerGC();
5838 }
5839
Vladimir Marko372f10e2016-05-17 16:30:10 +01005840 void AddSpecialInput(HInstruction* special_input);
5841
5842 using HInstruction::GetInputRecords; // Keep the const version visible.
5843 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5844 return ArrayRef<HUserRecord<HInstruction*>>(
5845 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005846 }
5847
Vladimir Marko372f10e2016-05-17 16:30:10 +01005848 Primitive::Type GetType() const OVERRIDE {
5849 return Primitive::kPrimNot;
5850 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005851
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005852 DECLARE_INSTRUCTION(LoadString);
5853
5854 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005855 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005856 static constexpr size_t kFieldLoadKindSize =
5857 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5858 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005859 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005860 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005861
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862 void SetLoadKindInternal(LoadKind load_kind);
5863
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005864 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5865 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005866 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005867 HUserRecord<HInstruction*> special_input_;
5868
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005869 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005870 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005871
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005872 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005873
5874 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5875};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5877
5878// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005880 // The special input is used for PC-relative loads on some architectures,
5881 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005882 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005883 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005884 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5885 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005886 // HLoadString::GetInputRecords() returns an empty array at this point,
5887 // so use the GetInputRecords() from the base class to set the input record.
5888 DCHECK(special_input_.GetInstruction() == nullptr);
5889 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 special_input->AddUseAt(this, 0);
5891}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005892
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005893/**
5894 * Performs an initialization check on its Class object input.
5895 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005896class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005897 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005898 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005899 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005900 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005901 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5902 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 SetRawInputAt(0, constant);
5904 }
5905
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005906 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005907 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908 return true;
5909 }
5910
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005911 bool NeedsEnvironment() const OVERRIDE {
5912 // May call runtime to initialize the class.
5913 return true;
5914 }
5915
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005916 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917
5918 HLoadClass* GetLoadClass() const { return InputAt(0)->AsLoadClass(); }
5919
5920 DECLARE_INSTRUCTION(ClinitCheck);
5921
5922 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005923 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5924};
5925
Vladimir Markofcb503c2016-05-18 12:48:17 +01005926class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005927 public:
5928 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005929 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005930 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005931 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005932 bool is_volatile,
5933 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005934 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005935 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005936 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005937 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005938 field_info_(field,
5939 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005940 field_type,
5941 is_volatile,
5942 field_idx,
5943 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005944 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 SetRawInputAt(0, cls);
5946 }
5947
Calin Juravle52c48962014-12-16 17:02:57 +00005948
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005949 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005950
Vladimir Marko372f10e2016-05-17 16:30:10 +01005951 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5952 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005953 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005954 }
5955
5956 size_t ComputeHashCode() const OVERRIDE {
5957 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5958 }
5959
Calin Juravle52c48962014-12-16 17:02:57 +00005960 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005961 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5962 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005963 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964
5965 DECLARE_INSTRUCTION(StaticFieldGet);
5966
5967 private:
5968 const FieldInfo field_info_;
5969
5970 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5971};
5972
Vladimir Markofcb503c2016-05-18 12:48:17 +01005973class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005974 public:
5975 HStaticFieldSet(HInstruction* cls,
5976 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005977 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005978 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005979 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005980 bool is_volatile,
5981 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005982 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005983 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005984 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005985 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005986 field_info_(field,
5987 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005988 field_type,
5989 is_volatile,
5990 field_idx,
5991 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005992 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005993 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005994 SetRawInputAt(0, cls);
5995 SetRawInputAt(1, value);
5996 }
5997
Calin Juravle52c48962014-12-16 17:02:57 +00005998 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005999 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
6000 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00006001 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006002
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006003 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
6005 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006006
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006007 DECLARE_INSTRUCTION(StaticFieldSet);
6008
6009 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006010 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6011 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6012 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6013 "Too many packed fields.");
6014
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006015 const FieldInfo field_info_;
6016
6017 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6018};
6019
Vladimir Markofcb503c2016-05-18 12:48:17 +01006020class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006021 public:
6022 HUnresolvedInstanceFieldGet(HInstruction* obj,
6023 Primitive::Type field_type,
6024 uint32_t field_index,
6025 uint32_t dex_pc)
6026 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6027 field_index_(field_index) {
6028 SetRawInputAt(0, obj);
6029 }
6030
6031 bool NeedsEnvironment() const OVERRIDE { return true; }
6032 bool CanThrow() const OVERRIDE { return true; }
6033
6034 Primitive::Type GetFieldType() const { return GetType(); }
6035 uint32_t GetFieldIndex() const { return field_index_; }
6036
6037 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6038
6039 private:
6040 const uint32_t field_index_;
6041
6042 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6043};
6044
Vladimir Markofcb503c2016-05-18 12:48:17 +01006045class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006046 public:
6047 HUnresolvedInstanceFieldSet(HInstruction* obj,
6048 HInstruction* value,
6049 Primitive::Type field_type,
6050 uint32_t field_index,
6051 uint32_t dex_pc)
6052 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006053 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006054 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006055 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006056 SetRawInputAt(0, obj);
6057 SetRawInputAt(1, value);
6058 }
6059
6060 bool NeedsEnvironment() const OVERRIDE { return true; }
6061 bool CanThrow() const OVERRIDE { return true; }
6062
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006064 uint32_t GetFieldIndex() const { return field_index_; }
6065
6066 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6067
6068 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006069 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6070 static constexpr size_t kFieldFieldTypeSize =
6071 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6072 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6073 kFieldFieldType + kFieldFieldTypeSize;
6074 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6075 "Too many packed fields.");
6076 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6077
Calin Juravlee460d1d2015-09-29 04:52:17 +01006078 const uint32_t field_index_;
6079
6080 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6081};
6082
Vladimir Markofcb503c2016-05-18 12:48:17 +01006083class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006084 public:
6085 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6086 uint32_t field_index,
6087 uint32_t dex_pc)
6088 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6089 field_index_(field_index) {
6090 }
6091
6092 bool NeedsEnvironment() const OVERRIDE { return true; }
6093 bool CanThrow() const OVERRIDE { return true; }
6094
6095 Primitive::Type GetFieldType() const { return GetType(); }
6096 uint32_t GetFieldIndex() const { return field_index_; }
6097
6098 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6099
6100 private:
6101 const uint32_t field_index_;
6102
6103 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6104};
6105
Vladimir Markofcb503c2016-05-18 12:48:17 +01006106class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006107 public:
6108 HUnresolvedStaticFieldSet(HInstruction* value,
6109 Primitive::Type field_type,
6110 uint32_t field_index,
6111 uint32_t dex_pc)
6112 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006113 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006114 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006115 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006116 SetRawInputAt(0, value);
6117 }
6118
6119 bool NeedsEnvironment() const OVERRIDE { return true; }
6120 bool CanThrow() const OVERRIDE { return true; }
6121
Vladimir Markoa1de9182016-02-25 11:37:38 +00006122 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006123 uint32_t GetFieldIndex() const { return field_index_; }
6124
6125 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6126
6127 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006128 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6129 static constexpr size_t kFieldFieldTypeSize =
6130 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6131 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6132 kFieldFieldType + kFieldFieldTypeSize;
6133 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6134 "Too many packed fields.");
6135 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6136
Calin Juravlee460d1d2015-09-29 04:52:17 +01006137 const uint32_t field_index_;
6138
6139 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6140};
6141
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006142// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006143class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006144 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006145 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6146 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006147
David Brazdilbbd733e2015-08-18 17:48:17 +01006148 bool CanBeNull() const OVERRIDE { return false; }
6149
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006150 DECLARE_INSTRUCTION(LoadException);
6151
6152 private:
6153 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6154};
6155
David Brazdilcb1c0552015-08-04 16:22:25 +01006156// Implicit part of move-exception which clears thread-local exception storage.
6157// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006158class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006159 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006160 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6161 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006162
6163 DECLARE_INSTRUCTION(ClearException);
6164
6165 private:
6166 DISALLOW_COPY_AND_ASSIGN(HClearException);
6167};
6168
Vladimir Markofcb503c2016-05-18 12:48:17 +01006169class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006170 public:
6171 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006172 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006173 SetRawInputAt(0, exception);
6174 }
6175
6176 bool IsControlFlow() const OVERRIDE { return true; }
6177
6178 bool NeedsEnvironment() const OVERRIDE { return true; }
6179
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006180 bool CanThrow() const OVERRIDE { return true; }
6181
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006182
6183 DECLARE_INSTRUCTION(Throw);
6184
6185 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006186 DISALLOW_COPY_AND_ASSIGN(HThrow);
6187};
6188
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189/**
6190 * Implementation strategies for the code generator of a HInstanceOf
6191 * or `HCheckCast`.
6192 */
6193enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006194 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 kExactCheck, // Can do a single class compare.
6196 kClassHierarchyCheck, // Can just walk the super class chain.
6197 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6198 kInterfaceCheck, // No optimization yet when checking against an interface.
6199 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006200 kArrayCheck, // No optimization yet when checking against a generic array.
6201 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202};
6203
Roland Levillain86503782016-02-11 19:07:30 +00006204std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6205
Vladimir Markofcb503c2016-05-18 12:48:17 +01006206class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006207 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006208 HInstanceOf(HInstruction* object,
6209 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006211 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006212 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006214 dex_pc) {
6215 SetPackedField<TypeCheckKindField>(check_kind);
6216 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006217 SetRawInputAt(0, object);
6218 SetRawInputAt(1, constant);
6219 }
6220
6221 bool CanBeMoved() const OVERRIDE { return true; }
6222
Vladimir Marko372f10e2016-05-17 16:30:10 +01006223 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006224 return true;
6225 }
6226
6227 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006228 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006229 }
6230
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006231 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006232 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6233 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6234 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6235 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006236
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006237 static bool CanCallRuntime(TypeCheckKind check_kind) {
6238 // Mips currently does runtime calls for any other checks.
6239 return check_kind != TypeCheckKind::kExactCheck;
6240 }
6241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006243 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006244 }
6245
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006246 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006247
6248 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006249 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6250 static constexpr size_t kFieldTypeCheckKindSize =
6251 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6252 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6253 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6254 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6255 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006256
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006257 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6258};
6259
Vladimir Markofcb503c2016-05-18 12:48:17 +01006260class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006261 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006262 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006263 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006264 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6265 SetPackedFlag<kFlagUpperCanBeNull>(true);
6266 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006267 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006268 SetRawInputAt(0, input);
6269 }
6270
David Brazdilf5552582015-12-27 13:36:12 +00006271 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006272 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006273 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006274 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006275
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006276 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006277 DCHECK(GetUpperCanBeNull() || !can_be_null);
6278 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006279 }
6280
Vladimir Markoa1de9182016-02-25 11:37:38 +00006281 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006282
Calin Juravleb1498f62015-02-16 13:13:29 +00006283 DECLARE_INSTRUCTION(BoundType);
6284
6285 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006286 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6287 // is false then CanBeNull() cannot be true).
6288 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6289 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6290 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6291 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6292
Calin Juravleb1498f62015-02-16 13:13:29 +00006293 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006294 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6295 // It is used to bound the type in cases like:
6296 // if (x instanceof ClassX) {
6297 // // uper_bound_ will be ClassX
6298 // }
David Brazdilf5552582015-12-27 13:36:12 +00006299 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006300
6301 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6302};
6303
Vladimir Markofcb503c2016-05-18 12:48:17 +01006304class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006305 public:
6306 HCheckCast(HInstruction* object,
6307 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006309 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006310 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6311 SetPackedField<TypeCheckKindField>(check_kind);
6312 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006313 SetRawInputAt(0, object);
6314 SetRawInputAt(1, constant);
6315 }
6316
6317 bool CanBeMoved() const OVERRIDE { return true; }
6318
Vladimir Marko372f10e2016-05-17 16:30:10 +01006319 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006320 return true;
6321 }
6322
6323 bool NeedsEnvironment() const OVERRIDE {
6324 // Instruction may throw a CheckCastError.
6325 return true;
6326 }
6327
6328 bool CanThrow() const OVERRIDE { return true; }
6329
Vladimir Markoa1de9182016-02-25 11:37:38 +00006330 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6331 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6332 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6333 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006334
6335 DECLARE_INSTRUCTION(CheckCast);
6336
6337 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006338 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6339 static constexpr size_t kFieldTypeCheckKindSize =
6340 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6341 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6342 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6343 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6344 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006345
6346 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006347};
6348
Andreas Gampe26de38b2016-07-27 17:53:11 -07006349/**
6350 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6351 * @details We define the combined barrier types that are actually required
6352 * by the Java Memory Model, rather than using exactly the terminology from
6353 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6354 * primitives. Note that the JSR-133 cookbook generally does not deal with
6355 * store atomicity issues, and the recipes there are not always entirely sufficient.
6356 * The current recipe is as follows:
6357 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6358 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6359 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6360 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6361 * class has final fields.
6362 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6363 * store-to-memory instructions. Only generated together with non-temporal stores.
6364 */
6365enum MemBarrierKind {
6366 kAnyStore,
6367 kLoadAny,
6368 kStoreStore,
6369 kAnyAny,
6370 kNTStoreStore,
6371 kLastBarrierKind = kNTStoreStore
6372};
6373std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6374
Vladimir Markofcb503c2016-05-18 12:48:17 +01006375class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006376 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006377 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006378 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006379 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6380 SetPackedField<BarrierKindField>(barrier_kind);
6381 }
Calin Juravle27df7582015-04-17 19:12:31 +01006382
Vladimir Markoa1de9182016-02-25 11:37:38 +00006383 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006384
6385 DECLARE_INSTRUCTION(MemoryBarrier);
6386
6387 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006388 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6389 static constexpr size_t kFieldBarrierKindSize =
6390 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6391 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6392 kFieldBarrierKind + kFieldBarrierKindSize;
6393 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6394 "Too many packed fields.");
6395 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006396
6397 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6398};
6399
Vladimir Markofcb503c2016-05-18 12:48:17 +01006400class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006401 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006402 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006403 kEnter,
6404 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006405 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006406 };
6407
6408 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006409 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006410 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6411 dex_pc) {
6412 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006413 SetRawInputAt(0, object);
6414 }
6415
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006416 // Instruction may go into runtime, so we need an environment.
6417 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006418
6419 bool CanThrow() const OVERRIDE {
6420 // Verifier guarantees that monitor-exit cannot throw.
6421 // This is important because it allows the HGraphBuilder to remove
6422 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6423 return IsEnter();
6424 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006425
Vladimir Markoa1de9182016-02-25 11:37:38 +00006426 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6427 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006428
6429 DECLARE_INSTRUCTION(MonitorOperation);
6430
Calin Juravle52c48962014-12-16 17:02:57 +00006431 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006432 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6433 static constexpr size_t kFieldOperationKindSize =
6434 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6435 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6436 kFieldOperationKind + kFieldOperationKindSize;
6437 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6438 "Too many packed fields.");
6439 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006440
6441 private:
6442 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6443};
6444
Vladimir Markofcb503c2016-05-18 12:48:17 +01006445class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006446 public:
6447 HSelect(HInstruction* condition,
6448 HInstruction* true_value,
6449 HInstruction* false_value,
6450 uint32_t dex_pc)
6451 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6452 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6453
6454 // First input must be `true_value` or `false_value` to allow codegens to
6455 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6456 // that architectures which implement HSelect as a conditional move also
6457 // will not need to invert the condition.
6458 SetRawInputAt(0, false_value);
6459 SetRawInputAt(1, true_value);
6460 SetRawInputAt(2, condition);
6461 }
6462
6463 HInstruction* GetFalseValue() const { return InputAt(0); }
6464 HInstruction* GetTrueValue() const { return InputAt(1); }
6465 HInstruction* GetCondition() const { return InputAt(2); }
6466
6467 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006468 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6469 return true;
6470 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006471
6472 bool CanBeNull() const OVERRIDE {
6473 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6474 }
6475
6476 DECLARE_INSTRUCTION(Select);
6477
6478 private:
6479 DISALLOW_COPY_AND_ASSIGN(HSelect);
6480};
6481
Vladimir Markof9f64412015-09-02 14:05:49 +01006482class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006483 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006484 MoveOperands(Location source,
6485 Location destination,
6486 Primitive::Type type,
6487 HInstruction* instruction)
6488 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006489
6490 Location GetSource() const { return source_; }
6491 Location GetDestination() const { return destination_; }
6492
6493 void SetSource(Location value) { source_ = value; }
6494 void SetDestination(Location value) { destination_ = value; }
6495
6496 // The parallel move resolver marks moves as "in-progress" by clearing the
6497 // destination (but not the source).
6498 Location MarkPending() {
6499 DCHECK(!IsPending());
6500 Location dest = destination_;
6501 destination_ = Location::NoLocation();
6502 return dest;
6503 }
6504
6505 void ClearPending(Location dest) {
6506 DCHECK(IsPending());
6507 destination_ = dest;
6508 }
6509
6510 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006511 DCHECK(source_.IsValid() || destination_.IsInvalid());
6512 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006513 }
6514
6515 // True if this blocks a move from the given location.
6516 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006517 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006518 }
6519
6520 // A move is redundant if it's been eliminated, if its source and
6521 // destination are the same, or if its destination is unneeded.
6522 bool IsRedundant() const {
6523 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6524 }
6525
6526 // We clear both operands to indicate move that's been eliminated.
6527 void Eliminate() {
6528 source_ = destination_ = Location::NoLocation();
6529 }
6530
6531 bool IsEliminated() const {
6532 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6533 return source_.IsInvalid();
6534 }
6535
Alexey Frunze4dda3372015-06-01 18:31:49 -07006536 Primitive::Type GetType() const { return type_; }
6537
Nicolas Geoffray90218252015-04-15 11:56:51 +01006538 bool Is64BitMove() const {
6539 return Primitive::Is64BitType(type_);
6540 }
6541
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006542 HInstruction* GetInstruction() const { return instruction_; }
6543
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006544 private:
6545 Location source_;
6546 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006547 // The type this move is for.
6548 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006549 // The instruction this move is assocatied with. Null when this move is
6550 // for moving an input in the expected locations of user (including a phi user).
6551 // This is only used in debug mode, to ensure we do not connect interval siblings
6552 // in the same parallel move.
6553 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006554};
6555
Roland Levillainc9285912015-12-18 10:38:42 +00006556std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6557
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006558static constexpr size_t kDefaultNumberOfMoves = 4;
6559
Vladimir Markofcb503c2016-05-18 12:48:17 +01006560class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006562 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006563 : HTemplateInstruction(SideEffects::None(), dex_pc),
6564 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6565 moves_.reserve(kDefaultNumberOfMoves);
6566 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006567
Nicolas Geoffray90218252015-04-15 11:56:51 +01006568 void AddMove(Location source,
6569 Location destination,
6570 Primitive::Type type,
6571 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006572 DCHECK(source.IsValid());
6573 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006574 if (kIsDebugBuild) {
6575 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006576 for (const MoveOperands& move : moves_) {
6577 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006578 // Special case the situation where the move is for the spill slot
6579 // of the instruction.
6580 if ((GetPrevious() == instruction)
6581 || ((GetPrevious() == nullptr)
6582 && instruction->IsPhi()
6583 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006584 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006585 << "Doing parallel moves for the same instruction.";
6586 } else {
6587 DCHECK(false) << "Doing parallel moves for the same instruction.";
6588 }
6589 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006590 }
6591 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006592 for (const MoveOperands& move : moves_) {
6593 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006594 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006595 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006596 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006597 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006598 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006599 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006600 }
6601
Vladimir Marko225b6462015-09-28 12:17:40 +01006602 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006603 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006604 }
6605
Vladimir Marko225b6462015-09-28 12:17:40 +01006606 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006607
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006608 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006609
6610 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006611 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006612
6613 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6614};
6615
Mark Mendell0616ae02015-04-17 12:49:27 -04006616} // namespace art
6617
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006618#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6619#include "nodes_shared.h"
6620#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006621#ifdef ART_ENABLE_CODEGEN_arm
6622#include "nodes_arm.h"
6623#endif
Alexandre Ramese6dbf482015-10-19 10:10:41 +01006624#ifdef ART_ENABLE_CODEGEN_arm64
6625#include "nodes_arm64.h"
6626#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006627#ifdef ART_ENABLE_CODEGEN_mips
6628#include "nodes_mips.h"
6629#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006630#ifdef ART_ENABLE_CODEGEN_x86
6631#include "nodes_x86.h"
6632#endif
6633
6634namespace art {
6635
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006636class HGraphVisitor : public ValueObject {
6637 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006638 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6639 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006640
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006641 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006642 virtual void VisitBasicBlock(HBasicBlock* block);
6643
Roland Levillain633021e2014-10-01 14:12:25 +01006644 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006645 void VisitInsertionOrder();
6646
Roland Levillain633021e2014-10-01 14:12:25 +01006647 // Visit the graph following dominator tree reverse post-order.
6648 void VisitReversePostOrder();
6649
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006650 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006651
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006652 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006653#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006654 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6655
6656 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6657
6658#undef DECLARE_VISIT_INSTRUCTION
6659
6660 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006661 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006662
6663 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6664};
6665
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006666class HGraphDelegateVisitor : public HGraphVisitor {
6667 public:
6668 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6669 virtual ~HGraphDelegateVisitor() {}
6670
6671 // Visit functions that delegate to to super class.
6672#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006673 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006674
6675 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6676
6677#undef DECLARE_VISIT_INSTRUCTION
6678
6679 private:
6680 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6681};
6682
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006683// Iterator over the blocks that art part of the loop. Includes blocks part
6684// of an inner loop. The order in which the blocks are iterated is on their
6685// block id.
6686class HBlocksInLoopIterator : public ValueObject {
6687 public:
6688 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6689 : blocks_in_loop_(info.GetBlocks()),
6690 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6691 index_(0) {
6692 if (!blocks_in_loop_.IsBitSet(index_)) {
6693 Advance();
6694 }
6695 }
6696
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006697 bool Done() const { return index_ == blocks_.size(); }
6698 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006699 void Advance() {
6700 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006701 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006702 if (blocks_in_loop_.IsBitSet(index_)) {
6703 break;
6704 }
6705 }
6706 }
6707
6708 private:
6709 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006710 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006711 size_t index_;
6712
6713 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6714};
6715
Mingyao Yang3584bce2015-05-19 16:01:59 -07006716// Iterator over the blocks that art part of the loop. Includes blocks part
6717// of an inner loop. The order in which the blocks are iterated is reverse
6718// post order.
6719class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6720 public:
6721 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6722 : blocks_in_loop_(info.GetBlocks()),
6723 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6724 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006725 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006726 Advance();
6727 }
6728 }
6729
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006730 bool Done() const { return index_ == blocks_.size(); }
6731 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006732 void Advance() {
6733 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006734 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6735 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006736 break;
6737 }
6738 }
6739 }
6740
6741 private:
6742 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006743 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006744 size_t index_;
6745
6746 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6747};
6748
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006749inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006750 if (constant->IsIntConstant()) {
6751 return constant->AsIntConstant()->GetValue();
6752 } else if (constant->IsLongConstant()) {
6753 return constant->AsLongConstant()->GetValue();
6754 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006755 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006756 return 0;
6757 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006758}
6759
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006760#define INSTRUCTION_TYPE_CHECK(type, super) \
6761 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6762 inline const H##type* HInstruction::As##type() const { \
6763 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6764 } \
6765 inline H##type* HInstruction::As##type() { \
6766 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6767 }
6768
6769 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6770#undef INSTRUCTION_TYPE_CHECK
6771
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006772// Create space in `blocks` for adding `number_of_new_blocks` entries
6773// starting at location `at`. Blocks after `at` are moved accordingly.
6774inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6775 size_t number_of_new_blocks,
6776 size_t after) {
6777 DCHECK_LT(after, blocks->size());
6778 size_t old_size = blocks->size();
6779 size_t new_size = old_size + number_of_new_blocks;
6780 blocks->resize(new_size);
6781 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6782}
6783
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006784} // namespace art
6785
6786#endif // ART_COMPILER_OPTIMIZING_NODES_H_