blob: 8a9e61875a4688bdb9190724fe376ae945d93be0 [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),
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800326 has_loops_(false),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000327 has_irreducible_loops_(false),
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000328 debuggable_(debuggable),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000329 current_instruction_id_(start_instruction_id),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100330 dex_file_(dex_file),
331 method_idx_(method_idx),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100332 invoke_type_(invoke_type),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100333 in_ssa_form_(false),
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100334 should_generate_constructor_barrier_(should_generate_constructor_barrier),
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800335 number_of_cha_guards_(0),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 instruction_set_(instruction_set),
David Brazdil8d5b8b22015-03-24 10:51:52 +0000337 cached_null_constant_(nullptr),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100338 cached_int_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
339 cached_float_constants_(std::less<int32_t>(), arena->Adapter(kArenaAllocConstantsMap)),
340 cached_long_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
341 cached_double_constants_(std::less<int64_t>(), arena->Adapter(kArenaAllocConstantsMap)),
David Brazdil4833f5a2015-12-16 10:37:39 +0000342 cached_current_method_(nullptr),
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000343 inexact_object_rti_(ReferenceTypeInfo::CreateInvalid()),
Mingyao Yang063fc772016-08-02 11:02:54 -0700344 osr_(osr),
345 cha_single_implementation_list_(arena->Adapter(kArenaAllocCHA)) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100346 blocks_.reserve(kDefaultNumberOfBlocks);
347 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000348
David Brazdilbadd8262016-02-02 16:28:56 +0000349 // Acquires and stores RTI of inexact Object to be used when creating HNullConstant.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700350 void InitializeInexactObjectRTI(VariableSizedHandleScope* handles);
David Brazdilbadd8262016-02-02 16:28:56 +0000351
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000352 ArenaAllocator* GetArena() const { return arena_; }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100353 const ArenaVector<HBasicBlock*>& GetBlocks() const { return blocks_; }
354
David Brazdil69ba7b72015-06-23 18:27:30 +0100355 bool IsInSsaForm() const { return in_ssa_form_; }
David Brazdilbadd8262016-02-02 16:28:56 +0000356 void SetInSsaForm() { in_ssa_form_ = true; }
David Brazdil69ba7b72015-06-23 18:27:30 +0100357
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000358 HBasicBlock* GetEntryBlock() const { return entry_block_; }
359 HBasicBlock* GetExitBlock() const { return exit_block_; }
David Brazdilc7af85d2015-05-26 12:05:55 +0100360 bool HasExitBlock() const { return exit_block_ != nullptr; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000361
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000362 void SetEntryBlock(HBasicBlock* block) { entry_block_ = block; }
363 void SetExitBlock(HBasicBlock* block) { exit_block_ = block; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000365 void AddBlock(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100366
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100367 void ComputeDominanceInformation();
368 void ClearDominanceInformation();
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000369 void ClearLoopInformation();
370 void FindBackEdges(ArenaBitVector* visited);
371 GraphAnalysisResult BuildDominatorTree();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100372 void SimplifyCFG();
David Brazdilffee3d32015-07-06 11:48:53 +0100373 void SimplifyCatchBlocks();
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000374
David Brazdil4833f5a2015-12-16 10:37:39 +0000375 // Analyze all natural loops in this graph. Returns a code specifying that it
376 // was successful or the reason for failure. The method will fail if a loop
David Brazdil4833f5a2015-12-16 10:37:39 +0000377 // is a throw-catch loop, i.e. the header is a catch block.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000378 GraphAnalysisResult AnalyzeLoops() const;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100379
David Brazdilffee3d32015-07-06 11:48:53 +0100380 // Iterate over blocks to compute try block membership. Needs reverse post
381 // order and loop information.
382 void ComputeTryBlockInformation();
383
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000384 // Inline this graph in `outer_graph`, replacing the given `invoke` instruction.
Nicolas Geoffray55bd7492016-02-16 15:37:12 +0000385 // Returns the instruction to replace the invoke expression or null if the
386 // invoke is for a void method. Note that the caller is responsible for replacing
387 // and removing the invoke instruction.
Calin Juravle2e768302015-07-28 14:41:11 +0000388 HInstruction* InlineInto(HGraph* outer_graph, HInvoke* invoke);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389
Nicolas Geoffraya1d8ddf2016-02-29 11:46:58 +0000390 // Update the loop and try membership of `block`, which was spawned from `reference`.
391 // In case `reference` is a back edge, `replace_if_back_edge` notifies whether `block`
392 // should be the new back edge.
393 void UpdateLoopAndTryInformationOfNewBlock(HBasicBlock* block,
394 HBasicBlock* reference,
395 bool replace_if_back_edge);
396
Mingyao Yang3584bce2015-05-19 16:01:59 -0700397 // Need to add a couple of blocks to test if the loop body is entered and
398 // put deoptimization instructions, etc.
399 void TransformLoopHeaderForBCE(HBasicBlock* header);
400
David Brazdil8a7c0fe2015-11-02 20:24:55 +0000401 // Removes `block` from the graph. Assumes `block` has been disconnected from
402 // other blocks and has no instructions or phis.
403 void DeleteDeadEmptyBlock(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000404
David Brazdilfc6a86a2015-06-26 10:33:45 +0000405 // Splits the edge between `block` and `successor` while preserving the
406 // indices in the predecessor/successor lists. If there are multiple edges
407 // between the blocks, the lowest indices are used.
408 // Returns the new block which is empty and has the same dex pc as `successor`.
409 HBasicBlock* SplitEdge(HBasicBlock* block, HBasicBlock* successor);
410
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100411 void SplitCriticalEdge(HBasicBlock* block, HBasicBlock* successor);
412 void SimplifyLoop(HBasicBlock* header);
413
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000414 int32_t GetNextInstructionId() {
415 DCHECK_NE(current_instruction_id_, INT32_MAX);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000416 return current_instruction_id_++;
417 }
418
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000419 int32_t GetCurrentInstructionId() const {
420 return current_instruction_id_;
421 }
422
423 void SetCurrentInstructionId(int32_t id) {
David Brazdil3f523062016-02-29 16:53:33 +0000424 DCHECK_GE(id, current_instruction_id_);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000425 current_instruction_id_ = id;
426 }
427
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100428 uint16_t GetMaximumNumberOfOutVRegs() const {
429 return maximum_number_of_out_vregs_;
430 }
431
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000432 void SetMaximumNumberOfOutVRegs(uint16_t new_value) {
433 maximum_number_of_out_vregs_ = new_value;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100434 }
435
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100436 void UpdateMaximumNumberOfOutVRegs(uint16_t other_value) {
437 maximum_number_of_out_vregs_ = std::max(maximum_number_of_out_vregs_, other_value);
438 }
439
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000440 void UpdateTemporariesVRegSlots(size_t slots) {
441 temporaries_vreg_slots_ = std::max(slots, temporaries_vreg_slots_);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100442 }
443
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000444 size_t GetTemporariesVRegSlots() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100445 DCHECK(!in_ssa_form_);
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000446 return temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100447 }
448
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100449 void SetNumberOfVRegs(uint16_t number_of_vregs) {
450 number_of_vregs_ = number_of_vregs;
451 }
452
453 uint16_t GetNumberOfVRegs() const {
454 return number_of_vregs_;
455 }
456
457 void SetNumberOfInVRegs(uint16_t value) {
458 number_of_in_vregs_ = value;
459 }
460
David Brazdildee58d62016-04-07 09:54:26 +0000461 uint16_t GetNumberOfInVRegs() const {
462 return number_of_in_vregs_;
463 }
464
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100465 uint16_t GetNumberOfLocalVRegs() const {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100466 DCHECK(!in_ssa_form_);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100467 return number_of_vregs_ - number_of_in_vregs_;
468 }
469
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100470 const ArenaVector<HBasicBlock*>& GetReversePostOrder() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100471 return reverse_post_order_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100472 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100473
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100474 ArrayRef<HBasicBlock* const> GetReversePostOrderSkipEntryBlock() {
475 DCHECK(GetReversePostOrder()[0] == entry_block_);
476 return ArrayRef<HBasicBlock* const>(GetReversePostOrder()).SubArray(1);
477 }
478
479 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetPostOrder() const {
480 return ReverseRange(GetReversePostOrder());
481 }
482
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100483 const ArenaVector<HBasicBlock*>& GetLinearOrder() const {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100484 return linear_order_;
485 }
486
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100487 IterationRange<ArenaVector<HBasicBlock*>::const_reverse_iterator> GetLinearPostOrder() const {
488 return ReverseRange(GetLinearOrder());
489 }
490
Mark Mendell1152c922015-04-24 17:06:35 -0400491 bool HasBoundsChecks() const {
492 return has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800493 }
494
Mark Mendell1152c922015-04-24 17:06:35 -0400495 void SetHasBoundsChecks(bool value) {
496 has_bounds_checks_ = value;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800497 }
498
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100499 bool ShouldGenerateConstructorBarrier() const {
500 return should_generate_constructor_barrier_;
501 }
502
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000503 bool IsDebuggable() const { return debuggable_; }
504
David Brazdil8d5b8b22015-03-24 10:51:52 +0000505 // Returns a constant of the given type and value. If it does not exist
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000506 // already, it is created and inserted into the graph. This method is only for
507 // integral types.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600508 HConstant* GetConstant(Primitive::Type type, int64_t value, uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000509
510 // TODO: This is problematic for the consistency of reference type propagation
511 // because it can be created anytime after the pass and thus it will be left
512 // with an invalid type.
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600513 HNullConstant* GetNullConstant(uint32_t dex_pc = kNoDexPc);
Calin Juravle2e768302015-07-28 14:41:11 +0000514
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600515 HIntConstant* GetIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc) {
516 return CreateConstant(value, &cached_int_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000517 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600518 HLongConstant* GetLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc) {
519 return CreateConstant(value, &cached_long_constants_, dex_pc);
David Brazdil8d5b8b22015-03-24 10:51:52 +0000520 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600521 HFloatConstant* GetFloatConstant(float value, uint32_t dex_pc = kNoDexPc) {
522 return CreateConstant(bit_cast<int32_t, float>(value), &cached_float_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000523 }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600524 HDoubleConstant* GetDoubleConstant(double value, uint32_t dex_pc = kNoDexPc) {
525 return CreateConstant(bit_cast<int64_t, double>(value), &cached_double_constants_, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000526 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000527
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100528 HCurrentMethod* GetCurrentMethod();
529
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100530 const DexFile& GetDexFile() const {
531 return dex_file_;
532 }
533
534 uint32_t GetMethodIdx() const {
535 return method_idx_;
536 }
537
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100538 InvokeType GetInvokeType() const {
539 return invoke_type_;
540 }
541
Mark Mendellc4701932015-04-10 13:18:51 -0400542 InstructionSet GetInstructionSet() const {
543 return instruction_set_;
544 }
545
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000546 bool IsCompilingOsr() const { return osr_; }
547
Mingyao Yang063fc772016-08-02 11:02:54 -0700548 ArenaSet<ArtMethod*>& GetCHASingleImplementationList() {
549 return cha_single_implementation_list_;
550 }
551
552 void AddCHASingleImplementationDependency(ArtMethod* method) {
553 cha_single_implementation_list_.insert(method);
554 }
555
556 bool HasShouldDeoptimizeFlag() const {
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800557 return number_of_cha_guards_ != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700558 }
559
David Brazdil77a48ae2015-09-15 12:34:04 +0000560 bool HasTryCatch() const { return has_try_catch_; }
561 void SetHasTryCatch(bool value) { has_try_catch_ = value; }
David Brazdilbbd733e2015-08-18 17:48:17 +0100562
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800563 bool HasLoops() const { return has_loops_; }
564 void SetHasLoops(bool value) { has_loops_ = value; }
565
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000566 bool HasIrreducibleLoops() const { return has_irreducible_loops_; }
567 void SetHasIrreducibleLoops(bool value) { has_irreducible_loops_ = value; }
568
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100569 ArtMethod* GetArtMethod() const { return art_method_; }
570 void SetArtMethod(ArtMethod* method) { art_method_ = method; }
571
Roland Levillain5e8d5f02016-10-18 18:03:43 +0100572 // Returns an instruction with the opposite Boolean value from 'cond'.
Mark Mendellf6529172015-11-17 11:16:56 -0500573 // The instruction has been inserted into the graph, either as a constant, or
574 // before cursor.
575 HInstruction* InsertOppositeCondition(HInstruction* cond, HInstruction* cursor);
576
Nicolas Geoffray18401b72016-03-11 13:35:51 +0000577 ReferenceTypeInfo GetInexactObjectRti() const { return inexact_object_rti_; }
578
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800579 uint32_t GetNumberOfCHAGuards() { return number_of_cha_guards_; }
580 void SetNumberOfCHAGuards(uint32_t num) { number_of_cha_guards_ = num; }
581 void IncrementNumberOfCHAGuards() { number_of_cha_guards_++; }
582
David Brazdil2d7352b2015-04-20 14:52:42 +0100583 private:
Roland Levillainfc600dc2014-12-02 17:16:31 +0000584 void RemoveInstructionsAsUsersFromDeadBlocks(const ArenaBitVector& visited) const;
Nicolas Geoffrayf776b922015-04-15 18:22:45 +0100585 void RemoveDeadBlocks(const ArenaBitVector& visited);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000586
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000587 template <class InstructionType, typename ValueType>
588 InstructionType* CreateConstant(ValueType value,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600589 ArenaSafeMap<ValueType, InstructionType*>* cache,
590 uint32_t dex_pc = kNoDexPc) {
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000591 // Try to find an existing constant of the given value.
592 InstructionType* constant = nullptr;
593 auto cached_constant = cache->find(value);
594 if (cached_constant != cache->end()) {
595 constant = cached_constant->second;
596 }
597
598 // If not found or previously deleted, create and cache a new instruction.
Nicolas Geoffrayf78848f2015-06-17 11:57:56 +0100599 // Don't bother reviving a previously deleted instruction, for simplicity.
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000600 if (constant == nullptr || constant->GetBlock() == nullptr) {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600601 constant = new (arena_) InstructionType(value, dex_pc);
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000602 cache->Overwrite(value, constant);
603 InsertConstant(constant);
604 }
605 return constant;
606 }
607
David Brazdil8d5b8b22015-03-24 10:51:52 +0000608 void InsertConstant(HConstant* instruction);
609
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000610 // Cache a float constant into the graph. This method should only be
611 // called by the SsaBuilder when creating "equivalent" instructions.
612 void CacheFloatConstant(HFloatConstant* constant);
613
614 // See CacheFloatConstant comment.
615 void CacheDoubleConstant(HDoubleConstant* constant);
616
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000617 ArenaAllocator* const arena_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000618
619 // List of blocks in insertion order.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100620 ArenaVector<HBasicBlock*> blocks_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000621
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100622 // List of blocks to perform a reverse post order tree traversal.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100623 ArenaVector<HBasicBlock*> reverse_post_order_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000624
Aart Bik281c6812016-08-26 11:31:48 -0700625 // List of blocks to perform a linear order tree traversal. Unlike the reverse
626 // post order, this order is not incrementally kept up-to-date.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100627 ArenaVector<HBasicBlock*> linear_order_;
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100628
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000629 HBasicBlock* entry_block_;
630 HBasicBlock* exit_block_;
631
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100632 // The maximum number of virtual registers arguments passed to a HInvoke in this graph.
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100633 uint16_t maximum_number_of_out_vregs_;
634
Nicolas Geoffrayf583e592014-04-07 13:20:42 +0100635 // The number of virtual registers in this method. Contains the parameters.
636 uint16_t number_of_vregs_;
637
638 // The number of virtual registers used by parameters of this method.
639 uint16_t number_of_in_vregs_;
640
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000641 // Number of vreg size slots that the temporaries use (used in baseline compiler).
642 size_t temporaries_vreg_slots_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100643
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800644 // Flag whether there are bounds checks in the graph. We can skip
645 // BCE if it's false. It's only best effort to keep it up to date in
646 // the presence of code elimination so there might be false positives.
Mark Mendell1152c922015-04-24 17:06:35 -0400647 bool has_bounds_checks_;
Mingyao Yange4335eb2015-03-02 15:14:13 -0800648
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800649 // Flag whether there are try/catch blocks in the graph. We will skip
650 // try/catch-related passes if it's false. It's only best effort to keep
651 // it up to date in the presence of code elimination so there might be
652 // false positives.
David Brazdil77a48ae2015-09-15 12:34:04 +0000653 bool has_try_catch_;
654
Mingyao Yang69d75ff2017-02-07 13:06:06 -0800655 // Flag whether there are any loops in the graph. We can skip loop
656 // optimization if it's false. It's only best effort to keep it up
657 // to date in the presence of code elimination so there might be false
658 // positives.
659 bool has_loops_;
660
661 // Flag whether there are any irreducible loops in the graph. It's only
662 // best effort to keep it up to date in the presence of code elimination
663 // so there might be false positives.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000664 bool has_irreducible_loops_;
665
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +0000666 // Indicates whether the graph should be compiled in a way that
667 // ensures full debuggability. If false, we can apply more
668 // aggressive optimizations that may limit the level of debugging.
669 const bool debuggable_;
670
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000671 // The current id to assign to a newly added instruction. See HInstruction.id_.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000672 int32_t current_instruction_id_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +0000673
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100674 // The dex file from which the method is from.
675 const DexFile& dex_file_;
676
677 // The method index in the dex file.
678 const uint32_t method_idx_;
679
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100680 // If inlined, this encodes how the callee is being invoked.
681 const InvokeType invoke_type_;
682
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100683 // Whether the graph has been transformed to SSA form. Only used
684 // in debug mode to ensure we are not using properties only valid
685 // for non-SSA form (like the number of temporaries).
686 bool in_ssa_form_;
687
Calin Juravle3cd4fc82015-05-14 15:15:42 +0100688 const bool should_generate_constructor_barrier_;
689
Mingyao Yangb0b051a2016-11-17 09:04:53 -0800690 // Number of CHA guards in the graph. Used to short-circuit the
691 // CHA guard optimization pass when there is no CHA guard left.
692 uint32_t number_of_cha_guards_;
693
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 const InstructionSet instruction_set_;
695
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000696 // Cached constants.
David Brazdil8d5b8b22015-03-24 10:51:52 +0000697 HNullConstant* cached_null_constant_;
698 ArenaSafeMap<int32_t, HIntConstant*> cached_int_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000699 ArenaSafeMap<int32_t, HFloatConstant*> cached_float_constants_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000700 ArenaSafeMap<int64_t, HLongConstant*> cached_long_constants_;
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000701 ArenaSafeMap<int64_t, HDoubleConstant*> cached_double_constants_;
David Brazdil46e2a392015-03-16 17:31:52 +0000702
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100703 HCurrentMethod* cached_current_method_;
704
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100705 // The ArtMethod this graph is for. Note that for AOT, it may be null,
706 // for example for methods whose declaring class could not be resolved
707 // (such as when the superclass could not be found).
708 ArtMethod* art_method_;
709
David Brazdil4833f5a2015-12-16 10:37:39 +0000710 // Keep the RTI of inexact Object to avoid having to pass stack handle
711 // collection pointer to passes which may create NullConstant.
712 ReferenceTypeInfo inexact_object_rti_;
713
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000714 // Whether we are compiling this graph for on stack replacement: this will
715 // make all loops seen as irreducible and emit special stack maps to mark
716 // compiled code entries which the interpreter can directly jump to.
717 const bool osr_;
718
Mingyao Yang063fc772016-08-02 11:02:54 -0700719 // List of methods that are assumed to have single implementation.
720 ArenaSet<ArtMethod*> cha_single_implementation_list_;
721
Nicolas Geoffrayf213e052015-04-27 08:53:46 +0000722 friend class SsaBuilder; // For caching constants.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100723 friend class SsaLivenessAnalysis; // For the linear order.
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +0000724 friend class HInliner; // For the reverse post order.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000725 ART_FRIEND_TEST(GraphTest, IfSuccessorSimpleJoinBlock1);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000726 DISALLOW_COPY_AND_ASSIGN(HGraph);
727};
728
Vladimir Markof9f64412015-09-02 14:05:49 +0100729class HLoopInformation : public ArenaObject<kArenaAllocLoopInfo> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000730 public:
731 HLoopInformation(HBasicBlock* header, HGraph* graph)
732 : header_(header),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100733 suspend_check_(nullptr),
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000734 irreducible_(false),
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100735 contains_irreducible_loop_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100736 back_edges_(graph->GetArena()->Adapter(kArenaAllocLoopInfoBackEdges)),
Nicolas Geoffrayb09aacb2014-09-17 18:21:53 +0100737 // Make bit vector growable, as the number of blocks may change.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000738 blocks_(graph->GetArena(), graph->GetBlocks().size(), true, kArenaAllocLoopInfoBackEdges) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100739 back_edges_.reserve(kDefaultNumberOfBackEdges);
740 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100741
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000742 bool IsIrreducible() const { return irreducible_; }
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100743 bool ContainsIrreducibleLoop() const { return contains_irreducible_loop_; }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000744
745 void Dump(std::ostream& os);
746
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100747 HBasicBlock* GetHeader() const {
748 return header_;
749 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000750
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000751 void SetHeader(HBasicBlock* block) {
752 header_ = block;
753 }
754
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100755 HSuspendCheck* GetSuspendCheck() const { return suspend_check_; }
756 void SetSuspendCheck(HSuspendCheck* check) { suspend_check_ = check; }
757 bool HasSuspendCheck() const { return suspend_check_ != nullptr; }
758
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000759 void AddBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100760 back_edges_.push_back(back_edge);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000761 }
762
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100763 void RemoveBackEdge(HBasicBlock* back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100764 RemoveElement(back_edges_, back_edge);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100765 }
766
David Brazdil46e2a392015-03-16 17:31:52 +0000767 bool IsBackEdge(const HBasicBlock& block) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100768 return ContainsElement(back_edges_, &block);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100769 }
770
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000771 size_t NumberOfBackEdges() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100772 return back_edges_.size();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000773 }
774
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100775 HBasicBlock* GetPreHeader() const;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100776
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100777 const ArenaVector<HBasicBlock*>& GetBackEdges() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100778 return back_edges_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100779 }
780
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100781 // Returns the lifetime position of the back edge that has the
782 // greatest lifetime position.
783 size_t GetLifetimeEnd() const;
784
785 void ReplaceBackEdge(HBasicBlock* existing, HBasicBlock* new_back_edge) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100786 ReplaceElement(back_edges_, existing, new_back_edge);
Nicolas Geoffray57902602015-04-21 14:28:41 +0100787 }
788
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000789 // Finds blocks that are part of this loop.
790 void Populate();
David Brazdila4b8c212015-05-07 09:59:30 +0100791
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100792 // Returns whether this loop information contains `block`.
793 // Note that this loop information *must* be populated before entering this function.
794 bool Contains(const HBasicBlock& block) const;
795
796 // Returns whether this loop information is an inner loop of `other`.
797 // Note that `other` *must* be populated before entering this function.
798 bool IsIn(const HLoopInformation& other) const;
799
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800800 // Returns true if instruction is not defined within this loop.
801 bool IsDefinedOutOfTheLoop(HInstruction* instruction) const;
Aart Bik73f1f3b2015-10-28 15:28:08 -0700802
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100803 const ArenaBitVector& GetBlocks() const { return blocks_; }
804
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000805 void Add(HBasicBlock* block);
David Brazdil46e2a392015-03-16 17:31:52 +0000806 void Remove(HBasicBlock* block);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000807
Nicolas Geoffray788f2f02016-01-22 12:41:38 +0000808 void ClearAllBlocks() {
809 blocks_.ClearAllBits();
810 }
811
David Brazdil3f4a5222016-05-06 12:46:21 +0100812 bool HasBackEdgeNotDominatedByHeader() const;
813
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100814 bool IsPopulated() const {
815 return blocks_.GetHighestBitSet() != -1;
816 }
817
Anton Shaminf89381f2016-05-16 16:44:13 +0600818 bool DominatesAllBackEdges(HBasicBlock* block);
819
David Sehrc757dec2016-11-04 15:48:34 -0700820 bool HasExitEdge() const;
821
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000822 private:
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100823 // Internal recursive implementation of `Populate`.
824 void PopulateRecursive(HBasicBlock* block);
David Brazdilc2e8af92016-04-05 17:15:19 +0100825 void PopulateIrreducibleRecursive(HBasicBlock* block, ArenaBitVector* finalized);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100826
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000827 HBasicBlock* header_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100828 HSuspendCheck* suspend_check_;
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000829 bool irreducible_;
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100830 bool contains_irreducible_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100831 ArenaVector<HBasicBlock*> back_edges_;
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100832 ArenaBitVector blocks_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000833
834 DISALLOW_COPY_AND_ASSIGN(HLoopInformation);
835};
836
David Brazdilec16f792015-08-19 15:04:01 +0100837// Stores try/catch information for basic blocks.
838// Note that HGraph is constructed so that catch blocks cannot simultaneously
839// be try blocks.
Vladimir Markof9f64412015-09-02 14:05:49 +0100840class TryCatchInformation : public ArenaObject<kArenaAllocTryCatchInfo> {
David Brazdilec16f792015-08-19 15:04:01 +0100841 public:
842 // Try block information constructor.
843 explicit TryCatchInformation(const HTryBoundary& try_entry)
844 : try_entry_(&try_entry),
845 catch_dex_file_(nullptr),
846 catch_type_index_(DexFile::kDexNoIndex16) {
847 DCHECK(try_entry_ != nullptr);
848 }
849
850 // Catch block information constructor.
Andreas Gampea5b09a62016-11-17 15:21:22 -0800851 TryCatchInformation(dex::TypeIndex catch_type_index, const DexFile& dex_file)
David Brazdilec16f792015-08-19 15:04:01 +0100852 : try_entry_(nullptr),
853 catch_dex_file_(&dex_file),
854 catch_type_index_(catch_type_index) {}
855
856 bool IsTryBlock() const { return try_entry_ != nullptr; }
857
858 const HTryBoundary& GetTryEntry() const {
859 DCHECK(IsTryBlock());
860 return *try_entry_;
861 }
862
863 bool IsCatchBlock() const { return catch_dex_file_ != nullptr; }
864
865 bool IsCatchAllTypeIndex() const {
866 DCHECK(IsCatchBlock());
Andreas Gampea5b09a62016-11-17 15:21:22 -0800867 return !catch_type_index_.IsValid();
David Brazdilec16f792015-08-19 15:04:01 +0100868 }
869
Andreas Gampea5b09a62016-11-17 15:21:22 -0800870 dex::TypeIndex GetCatchTypeIndex() const {
David Brazdilec16f792015-08-19 15:04:01 +0100871 DCHECK(IsCatchBlock());
872 return catch_type_index_;
873 }
874
875 const DexFile& GetCatchDexFile() const {
876 DCHECK(IsCatchBlock());
877 return *catch_dex_file_;
878 }
879
880 private:
881 // One of possibly several TryBoundary instructions entering the block's try.
882 // Only set for try blocks.
883 const HTryBoundary* try_entry_;
884
885 // Exception type information. Only set for catch blocks.
886 const DexFile* catch_dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800887 const dex::TypeIndex catch_type_index_;
David Brazdilec16f792015-08-19 15:04:01 +0100888};
889
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100890static constexpr size_t kNoLifetime = -1;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100891static constexpr uint32_t kInvalidBlockId = static_cast<uint32_t>(-1);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100892
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000893// A block in a method. Contains the list of instructions represented
894// as a double linked list. Each block knows its predecessors and
895// successors.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100896
Vladimir Markof9f64412015-09-02 14:05:49 +0100897class HBasicBlock : public ArenaObject<kArenaAllocBasicBlock> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000898 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -0700899 explicit HBasicBlock(HGraph* graph, uint32_t dex_pc = kNoDexPc)
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000900 : graph_(graph),
Vladimir Marko60584552015-09-03 13:35:12 +0000901 predecessors_(graph->GetArena()->Adapter(kArenaAllocPredecessors)),
902 successors_(graph->GetArena()->Adapter(kArenaAllocSuccessors)),
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000903 loop_information_(nullptr),
904 dominator_(nullptr),
Vladimir Marko60584552015-09-03 13:35:12 +0000905 dominated_blocks_(graph->GetArena()->Adapter(kArenaAllocDominated)),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100906 block_id_(kInvalidBlockId),
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100907 dex_pc_(dex_pc),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100908 lifetime_start_(kNoLifetime),
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000909 lifetime_end_(kNoLifetime),
Vladimir Marko60584552015-09-03 13:35:12 +0000910 try_catch_information_(nullptr) {
911 predecessors_.reserve(kDefaultNumberOfPredecessors);
912 successors_.reserve(kDefaultNumberOfSuccessors);
913 dominated_blocks_.reserve(kDefaultNumberOfDominatedBlocks);
914 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000915
Vladimir Marko60584552015-09-03 13:35:12 +0000916 const ArenaVector<HBasicBlock*>& GetPredecessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100917 return predecessors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000918 }
919
Vladimir Marko60584552015-09-03 13:35:12 +0000920 const ArenaVector<HBasicBlock*>& GetSuccessors() const {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100921 return successors_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000922 }
923
David Brazdild26a4112015-11-10 11:07:31 +0000924 ArrayRef<HBasicBlock* const> GetNormalSuccessors() const;
925 ArrayRef<HBasicBlock* const> GetExceptionalSuccessors() const;
926
Vladimir Marko60584552015-09-03 13:35:12 +0000927 bool HasSuccessor(const HBasicBlock* block, size_t start_from = 0u) {
928 return ContainsElement(successors_, block, start_from);
929 }
930
931 const ArenaVector<HBasicBlock*>& GetDominatedBlocks() const {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100932 return dominated_blocks_;
933 }
934
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100935 bool IsEntryBlock() const {
936 return graph_->GetEntryBlock() == this;
937 }
938
939 bool IsExitBlock() const {
940 return graph_->GetExitBlock() == this;
941 }
942
David Brazdil46e2a392015-03-16 17:31:52 +0000943 bool IsSingleGoto() const;
David Brazdilfc6a86a2015-06-26 10:33:45 +0000944 bool IsSingleTryBoundary() const;
945
946 // Returns true if this block emits nothing but a jump.
947 bool IsSingleJump() const {
948 HLoopInformation* loop_info = GetLoopInformation();
949 return (IsSingleGoto() || IsSingleTryBoundary())
950 // Back edges generate a suspend check.
951 && (loop_info == nullptr || !loop_info->IsBackEdge(*this));
952 }
David Brazdil46e2a392015-03-16 17:31:52 +0000953
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000954 void AddBackEdge(HBasicBlock* back_edge) {
955 if (loop_information_ == nullptr) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000956 loop_information_ = new (graph_->GetArena()) HLoopInformation(this, graph_);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000957 }
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100958 DCHECK_EQ(loop_information_->GetHeader(), this);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000959 loop_information_->AddBackEdge(back_edge);
960 }
961
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000962 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffray276d9da2015-02-02 18:24:11 +0000963 void SetGraph(HGraph* graph) { graph_ = graph; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000964
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100965 uint32_t GetBlockId() const { return block_id_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000966 void SetBlockId(int id) { block_id_ = id; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +0600967 uint32_t GetDexPc() const { return dex_pc_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000968
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000969 HBasicBlock* GetDominator() const { return dominator_; }
970 void SetDominator(HBasicBlock* dominator) { dominator_ = dominator; }
Vladimir Marko60584552015-09-03 13:35:12 +0000971 void AddDominatedBlock(HBasicBlock* block) { dominated_blocks_.push_back(block); }
972
973 void RemoveDominatedBlock(HBasicBlock* block) {
974 RemoveElement(dominated_blocks_, block);
Vladimir Marko91e11c02015-09-02 17:03:22 +0100975 }
Vladimir Marko60584552015-09-03 13:35:12 +0000976
977 void ReplaceDominatedBlock(HBasicBlock* existing, HBasicBlock* new_block) {
978 ReplaceElement(dominated_blocks_, existing, new_block);
979 }
980
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100981 void ClearDominanceInformation();
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000982
983 int NumberOfBackEdges() const {
Nicolas Geoffray1f82ecc2015-06-24 12:20:24 +0100984 return IsLoopHeader() ? loop_information_->NumberOfBackEdges() : 0;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +0000985 }
986
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100987 HInstruction* GetFirstInstruction() const { return instructions_.first_instruction_; }
988 HInstruction* GetLastInstruction() const { return instructions_.last_instruction_; }
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100989 const HInstructionList& GetInstructions() const { return instructions_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100990 HInstruction* GetFirstPhi() const { return phis_.first_instruction_; }
David Brazdilc3d743f2015-04-22 13:40:50 +0100991 HInstruction* GetLastPhi() const { return phis_.last_instruction_; }
992 const HInstructionList& GetPhis() const { return phis_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000993
Nicolas Geoffray09aa1472016-01-19 10:52:54 +0000994 HInstruction* GetFirstInstructionDisregardMoves() const;
995
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000996 void AddSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +0000997 successors_.push_back(block);
998 block->predecessors_.push_back(this);
Nicolas Geoffray818f2102014-02-18 16:43:35 +0000999 }
1000
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001001 void ReplaceSuccessor(HBasicBlock* existing, HBasicBlock* new_block) {
1002 size_t successor_index = GetSuccessorIndexOf(existing);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001003 existing->RemovePredecessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001004 new_block->predecessors_.push_back(this);
1005 successors_[successor_index] = new_block;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001006 }
1007
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001008 void ReplacePredecessor(HBasicBlock* existing, HBasicBlock* new_block) {
1009 size_t predecessor_index = GetPredecessorIndexOf(existing);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001010 existing->RemoveSuccessor(this);
Vladimir Marko60584552015-09-03 13:35:12 +00001011 new_block->successors_.push_back(this);
1012 predecessors_[predecessor_index] = new_block;
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001013 }
1014
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001015 // Insert `this` between `predecessor` and `successor. This method
1016 // preserves the indicies, and will update the first edge found between
1017 // `predecessor` and `successor`.
1018 void InsertBetween(HBasicBlock* predecessor, HBasicBlock* successor) {
1019 size_t predecessor_index = successor->GetPredecessorIndexOf(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001020 size_t successor_index = predecessor->GetSuccessorIndexOf(successor);
Vladimir Marko60584552015-09-03 13:35:12 +00001021 successor->predecessors_[predecessor_index] = this;
1022 predecessor->successors_[successor_index] = this;
1023 successors_.push_back(successor);
1024 predecessors_.push_back(predecessor);
Nicolas Geoffray8b20f882015-06-19 16:17:05 +01001025 }
1026
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001027 void RemovePredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001028 predecessors_.erase(predecessors_.begin() + GetPredecessorIndexOf(block));
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001029 }
1030
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001031 void RemoveSuccessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001032 successors_.erase(successors_.begin() + GetSuccessorIndexOf(block));
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001033 }
1034
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001035 void ClearAllPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001036 predecessors_.clear();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001037 }
1038
1039 void AddPredecessor(HBasicBlock* block) {
Vladimir Marko60584552015-09-03 13:35:12 +00001040 predecessors_.push_back(block);
1041 block->successors_.push_back(this);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001042 }
1043
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001044 void SwapPredecessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001045 DCHECK_EQ(predecessors_.size(), 2u);
1046 std::swap(predecessors_[0], predecessors_[1]);
Nicolas Geoffray604c6e42014-09-17 12:08:44 +01001047 }
1048
David Brazdil769c9e52015-04-27 13:54:09 +01001049 void SwapSuccessors() {
Vladimir Marko60584552015-09-03 13:35:12 +00001050 DCHECK_EQ(successors_.size(), 2u);
1051 std::swap(successors_[0], successors_[1]);
David Brazdil769c9e52015-04-27 13:54:09 +01001052 }
1053
David Brazdilfc6a86a2015-06-26 10:33:45 +00001054 size_t GetPredecessorIndexOf(HBasicBlock* predecessor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001055 return IndexOfElement(predecessors_, predecessor);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001056 }
1057
David Brazdilfc6a86a2015-06-26 10:33:45 +00001058 size_t GetSuccessorIndexOf(HBasicBlock* successor) const {
Vladimir Marko60584552015-09-03 13:35:12 +00001059 return IndexOfElement(successors_, successor);
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001060 }
1061
David Brazdilfc6a86a2015-06-26 10:33:45 +00001062 HBasicBlock* GetSinglePredecessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001063 DCHECK_EQ(GetPredecessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001064 return GetPredecessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001065 }
1066
1067 HBasicBlock* GetSingleSuccessor() const {
Vladimir Marko60584552015-09-03 13:35:12 +00001068 DCHECK_EQ(GetSuccessors().size(), 1u);
Vladimir Markoec7802a2015-10-01 20:57:57 +01001069 return GetSuccessors()[0];
David Brazdilfc6a86a2015-06-26 10:33:45 +00001070 }
1071
1072 // Returns whether the first occurrence of `predecessor` in the list of
1073 // predecessors is at index `idx`.
1074 bool IsFirstIndexOfPredecessor(HBasicBlock* predecessor, size_t idx) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01001075 DCHECK_EQ(GetPredecessors()[idx], predecessor);
David Brazdilfc6a86a2015-06-26 10:33:45 +00001076 return GetPredecessorIndexOf(predecessor) == idx;
1077 }
1078
David Brazdild7558da2015-09-22 13:04:14 +01001079 // Create a new block between this block and its predecessors. The new block
1080 // is added to the graph, all predecessor edges are relinked to it and an edge
1081 // is created to `this`. Returns the new empty block. Reverse post order or
1082 // loop and try/catch information are not updated.
1083 HBasicBlock* CreateImmediateDominator();
1084
David Brazdilfc6a86a2015-06-26 10:33:45 +00001085 // Split the block into two blocks just before `cursor`. Returns the newly
David Brazdil56e1acc2015-06-30 15:41:36 +01001086 // created, latter block. Note that this method will add the block to the
1087 // graph, create a Goto at the end of the former block and will create an edge
1088 // between the blocks. It will not, however, update the reverse post order or
David Brazdild7558da2015-09-22 13:04:14 +01001089 // loop and try/catch information.
David Brazdilfc6a86a2015-06-26 10:33:45 +00001090 HBasicBlock* SplitBefore(HInstruction* cursor);
1091
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001092 // Split the block into two blocks just before `cursor`. Returns the newly
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001093 // created block. Note that this method just updates raw block information,
1094 // like predecessors, successors, dominators, and instruction list. It does not
1095 // update the graph, reverse post order, loop information, nor make sure the
1096 // blocks are consistent (for example ending with a control flow instruction).
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00001097 HBasicBlock* SplitBeforeForInlining(HInstruction* cursor);
1098
1099 // Similar to `SplitBeforeForInlining` but does it after `cursor`.
1100 HBasicBlock* SplitAfterForInlining(HInstruction* cursor);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001101
1102 // Merge `other` at the end of `this`. Successors and dominated blocks of
1103 // `other` are changed to be successors and dominated blocks of `this`. Note
1104 // that this method does not update the graph, reverse post order, loop
1105 // information, nor make sure the blocks are consistent (for example ending
1106 // with a control flow instruction).
David Brazdil2d7352b2015-04-20 14:52:42 +01001107 void MergeWithInlined(HBasicBlock* other);
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001108
1109 // Replace `this` with `other`. Predecessors, successors, and dominated blocks
1110 // of `this` are moved to `other`.
1111 // Note that this method does not update the graph, reverse post order, loop
1112 // information, nor make sure the blocks are consistent (for example ending
David Brazdil46e2a392015-03-16 17:31:52 +00001113 // with a control flow instruction).
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001114 void ReplaceWith(HBasicBlock* other);
1115
Aart Bik6b69e0a2017-01-11 10:20:43 -08001116 // Merges the instructions of `other` at the end of `this`.
1117 void MergeInstructionsWith(HBasicBlock* other);
1118
David Brazdil2d7352b2015-04-20 14:52:42 +01001119 // Merge `other` at the end of `this`. This method updates loops, reverse post
1120 // order, links to predecessors, successors, dominators and deletes the block
1121 // from the graph. The two blocks must be successive, i.e. `this` the only
1122 // predecessor of `other` and vice versa.
1123 void MergeWith(HBasicBlock* other);
1124
1125 // Disconnects `this` from all its predecessors, successors and dominator,
1126 // removes it from all loops it is included in and eventually from the graph.
1127 // The block must not dominate any other block. Predecessors and successors
1128 // are safely updated.
1129 void DisconnectAndDelete();
David Brazdil46e2a392015-03-16 17:31:52 +00001130
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001131 void AddInstruction(HInstruction* instruction);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001132 // Insert `instruction` before/after an existing instruction `cursor`.
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001133 void InsertInstructionBefore(HInstruction* instruction, HInstruction* cursor);
Guillaume "Vermeille" Sanchez2967ec62015-04-24 16:36:52 +01001134 void InsertInstructionAfter(HInstruction* instruction, HInstruction* cursor);
Roland Levillainccc07a92014-09-16 14:48:16 +01001135 // Replace instruction `initial` with `replacement` within this block.
1136 void ReplaceAndRemoveInstructionWith(HInstruction* initial,
1137 HInstruction* replacement);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001138 void AddPhi(HPhi* phi);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001139 void InsertPhiAfter(HPhi* instruction, HPhi* cursor);
David Brazdil1abb4192015-02-17 18:33:36 +00001140 // RemoveInstruction and RemovePhi delete a given instruction from the respective
1141 // instruction list. With 'ensure_safety' set to true, it verifies that the
1142 // instruction is not in use and removes it from the use lists of its inputs.
1143 void RemoveInstruction(HInstruction* instruction, bool ensure_safety = true);
1144 void RemovePhi(HPhi* phi, bool ensure_safety = true);
David Brazdilc7508e92015-04-27 13:28:57 +01001145 void RemoveInstructionOrPhi(HInstruction* instruction, bool ensure_safety = true);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001146
1147 bool IsLoopHeader() const {
David Brazdil69a28042015-04-29 17:16:07 +01001148 return IsInLoop() && (loop_information_->GetHeader() == this);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001149 }
1150
Roland Levillain6b879dd2014-09-22 17:13:44 +01001151 bool IsLoopPreHeaderFirstPredecessor() const {
1152 DCHECK(IsLoopHeader());
Vladimir Markoec7802a2015-10-01 20:57:57 +01001153 return GetPredecessors()[0] == GetLoopInformation()->GetPreHeader();
Roland Levillain6b879dd2014-09-22 17:13:44 +01001154 }
1155
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001156 bool IsFirstPredecessorBackEdge() const {
1157 DCHECK(IsLoopHeader());
1158 return GetLoopInformation()->IsBackEdge(*GetPredecessors()[0]);
1159 }
1160
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001161 HLoopInformation* GetLoopInformation() const {
1162 return loop_information_;
1163 }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001164
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001165 // Set the loop_information_ on this block. Overrides the current
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001166 // loop_information if it is an outer loop of the passed loop information.
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001167 // Note that this method is called while creating the loop information.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001168 void SetInLoop(HLoopInformation* info) {
1169 if (IsLoopHeader()) {
1170 // Nothing to do. This just means `info` is an outer loop.
David Brazdil69a28042015-04-29 17:16:07 +01001171 } else if (!IsInLoop()) {
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001172 loop_information_ = info;
1173 } else if (loop_information_->Contains(*info->GetHeader())) {
1174 // Block is currently part of an outer loop. Make it part of this inner loop.
1175 // Note that a non loop header having a loop information means this loop information
1176 // has already been populated
1177 loop_information_ = info;
1178 } else {
1179 // Block is part of an inner loop. Do not update the loop information.
1180 // Note that we cannot do the check `info->Contains(loop_information_)->GetHeader()`
1181 // at this point, because this method is being called while populating `info`.
1182 }
1183 }
1184
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001185 // Raw update of the loop information.
1186 void SetLoopInformation(HLoopInformation* info) {
1187 loop_information_ = info;
1188 }
1189
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001190 bool IsInLoop() const { return loop_information_ != nullptr; }
1191
David Brazdilec16f792015-08-19 15:04:01 +01001192 TryCatchInformation* GetTryCatchInformation() const { return try_catch_information_; }
1193
1194 void SetTryCatchInformation(TryCatchInformation* try_catch_information) {
1195 try_catch_information_ = try_catch_information;
1196 }
1197
1198 bool IsTryBlock() const {
1199 return try_catch_information_ != nullptr && try_catch_information_->IsTryBlock();
1200 }
1201
1202 bool IsCatchBlock() const {
1203 return try_catch_information_ != nullptr && try_catch_information_->IsCatchBlock();
1204 }
David Brazdilffee3d32015-07-06 11:48:53 +01001205
1206 // Returns the try entry that this block's successors should have. They will
1207 // be in the same try, unless the block ends in a try boundary. In that case,
1208 // the appropriate try entry will be returned.
David Brazdilec16f792015-08-19 15:04:01 +01001209 const HTryBoundary* ComputeTryEntryOfSuccessors() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001210
David Brazdild7558da2015-09-22 13:04:14 +01001211 bool HasThrowingInstructions() const;
1212
David Brazdila4b8c212015-05-07 09:59:30 +01001213 // Returns whether this block dominates the blocked passed as parameter.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001214 bool Dominates(HBasicBlock* block) const;
1215
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001216 size_t GetLifetimeStart() const { return lifetime_start_; }
1217 size_t GetLifetimeEnd() const { return lifetime_end_; }
1218
1219 void SetLifetimeStart(size_t start) { lifetime_start_ = start; }
1220 void SetLifetimeEnd(size_t end) { lifetime_end_ = end; }
1221
David Brazdil8d5b8b22015-03-24 10:51:52 +00001222 bool EndsWithControlFlowInstruction() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001223 bool EndsWithIf() const;
David Brazdilffee3d32015-07-06 11:48:53 +01001224 bool EndsWithTryBoundary() const;
David Brazdilb2bd1c52015-03-25 11:17:37 +00001225 bool HasSinglePhi() const;
1226
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001227 private:
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00001228 HGraph* graph_;
Vladimir Marko60584552015-09-03 13:35:12 +00001229 ArenaVector<HBasicBlock*> predecessors_;
1230 ArenaVector<HBasicBlock*> successors_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001231 HInstructionList instructions_;
1232 HInstructionList phis_;
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00001233 HLoopInformation* loop_information_;
1234 HBasicBlock* dominator_;
Vladimir Marko60584552015-09-03 13:35:12 +00001235 ArenaVector<HBasicBlock*> dominated_blocks_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001236 uint32_t block_id_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001237 // The dex program counter of the first instruction of this block.
1238 const uint32_t dex_pc_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001239 size_t lifetime_start_;
1240 size_t lifetime_end_;
David Brazdilec16f792015-08-19 15:04:01 +01001241 TryCatchInformation* try_catch_information_;
David Brazdilffee3d32015-07-06 11:48:53 +01001242
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001243 friend class HGraph;
1244 friend class HInstruction;
1245
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001246 DISALLOW_COPY_AND_ASSIGN(HBasicBlock);
1247};
1248
David Brazdilb2bd1c52015-03-25 11:17:37 +00001249// Iterates over the LoopInformation of all loops which contain 'block'
1250// from the innermost to the outermost.
1251class HLoopInformationOutwardIterator : public ValueObject {
1252 public:
1253 explicit HLoopInformationOutwardIterator(const HBasicBlock& block)
1254 : current_(block.GetLoopInformation()) {}
1255
1256 bool Done() const { return current_ == nullptr; }
1257
1258 void Advance() {
1259 DCHECK(!Done());
David Brazdil69a28042015-04-29 17:16:07 +01001260 current_ = current_->GetPreHeader()->GetLoopInformation();
David Brazdilb2bd1c52015-03-25 11:17:37 +00001261 }
1262
1263 HLoopInformation* Current() const {
1264 DCHECK(!Done());
1265 return current_;
1266 }
1267
1268 private:
1269 HLoopInformation* current_;
1270
1271 DISALLOW_COPY_AND_ASSIGN(HLoopInformationOutwardIterator);
1272};
1273
Alexandre Ramesef20f712015-06-09 10:29:30 +01001274#define FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Aart Bike9f37602015-10-09 11:15:55 -07001275 M(Above, Condition) \
1276 M(AboveOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001277 M(Add, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001278 M(And, BinaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001279 M(ArrayGet, Instruction) \
1280 M(ArrayLength, Instruction) \
1281 M(ArraySet, Instruction) \
Aart Bike9f37602015-10-09 11:15:55 -07001282 M(Below, Condition) \
1283 M(BelowOrEqual, Condition) \
David Brazdil66d126e2015-04-03 16:02:44 +01001284 M(BooleanNot, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001285 M(BoundsCheck, Instruction) \
Calin Juravleb1498f62015-02-16 13:13:29 +00001286 M(BoundType, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001287 M(CheckCast, Instruction) \
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00001288 M(ClassTableGet, Instruction) \
David Brazdilcb1c0552015-08-04 16:22:25 +01001289 M(ClearException, Instruction) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001290 M(ClinitCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001291 M(Compare, BinaryOperation) \
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001292 M(CurrentMethod, Instruction) \
Mingyao Yang063fc772016-08-02 11:02:54 -07001293 M(ShouldDeoptimizeFlag, Instruction) \
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001294 M(Deoptimize, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001295 M(Div, BinaryOperation) \
Calin Juravled0d48522014-11-04 16:40:20 +00001296 M(DivZeroCheck, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001297 M(DoubleConstant, Constant) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001298 M(Equal, Condition) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001299 M(Exit, Instruction) \
1300 M(FloatConstant, Constant) \
1301 M(Goto, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001302 M(GreaterThan, Condition) \
1303 M(GreaterThanOrEqual, Condition) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001304 M(If, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001305 M(InstanceFieldGet, Instruction) \
1306 M(InstanceFieldSet, Instruction) \
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00001307 M(InstanceOf, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001308 M(IntConstant, Constant) \
Calin Juravle175dc732015-08-25 15:42:32 +01001309 M(InvokeUnresolved, Invoke) \
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001310 M(InvokeInterface, Invoke) \
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001311 M(InvokeStaticOrDirect, Invoke) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001312 M(InvokeVirtual, Invoke) \
Orion Hodsonac141392017-01-13 11:53:47 +00001313 M(InvokePolymorphic, Invoke) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001314 M(LessThan, Condition) \
1315 M(LessThanOrEqual, Condition) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001316 M(LoadClass, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001317 M(LoadException, Instruction) \
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001318 M(LoadString, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001319 M(LongConstant, Constant) \
Calin Juravle27df7582015-04-17 19:12:31 +01001320 M(MemoryBarrier, Instruction) \
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00001321 M(MonitorOperation, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001322 M(Mul, BinaryOperation) \
David Srbecky0cf44932015-12-09 14:09:59 +00001323 M(NativeDebugInfo, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001324 M(Neg, UnaryOperation) \
1325 M(NewArray, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001326 M(NewInstance, Instruction) \
Roland Levillain1cc5f2512014-10-22 18:06:21 +01001327 M(Not, UnaryOperation) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001328 M(NotEqual, Condition) \
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001329 M(NullConstant, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001330 M(NullCheck, Instruction) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001331 M(Or, BinaryOperation) \
Mark Mendellfe57faa2015-09-18 09:26:15 -04001332 M(PackedSwitch, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001333 M(ParallelMove, Instruction) \
Calin Juravle7c4954d2014-10-28 16:57:40 +00001334 M(ParameterValue, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001335 M(Phi, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001336 M(Rem, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001337 M(Return, Instruction) \
1338 M(ReturnVoid, Instruction) \
Scott Wakeling40a04bf2015-12-11 09:50:36 +00001339 M(Ror, BinaryOperation) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001340 M(Shl, BinaryOperation) \
1341 M(Shr, BinaryOperation) \
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001342 M(StaticFieldGet, Instruction) \
1343 M(StaticFieldSet, Instruction) \
Calin Juravlee460d1d2015-09-29 04:52:17 +01001344 M(UnresolvedInstanceFieldGet, Instruction) \
1345 M(UnresolvedInstanceFieldSet, Instruction) \
1346 M(UnresolvedStaticFieldGet, Instruction) \
1347 M(UnresolvedStaticFieldSet, Instruction) \
David Brazdil74eb1b22015-12-14 11:44:01 +00001348 M(Select, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001349 M(Sub, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 M(SuspendCheck, Instruction) \
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00001351 M(Throw, Instruction) \
David Brazdilfc6a86a2015-06-26 10:33:45 +00001352 M(TryBoundary, Instruction) \
Roland Levillaindff1f282014-11-05 14:15:05 +00001353 M(TypeConversion, Instruction) \
Calin Juravle9aec02f2014-11-18 23:06:35 +00001354 M(UShr, BinaryOperation) \
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00001355 M(Xor, BinaryOperation) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001356
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001357/*
1358 * Instructions, shared across several (not all) architectures.
1359 */
1360#if !defined(ART_ENABLE_CODEGEN_arm) && !defined(ART_ENABLE_CODEGEN_arm64)
1361#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M)
1362#else
1363#define FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Artem Serov7fc63502016-02-09 17:15:29 +00001364 M(BitwiseNegatedRight, Instruction) \
Anton Kirilov74234da2017-01-13 14:42:47 +00001365 M(DataProcWithShifterOp, Instruction) \
Artem Serov328429f2016-07-06 16:23:04 +01001366 M(MultiplyAccumulate, Instruction) \
1367 M(IntermediateAddress, Instruction)
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001368#endif
1369
Vladimir Markob4536b72015-11-24 13:45:23 +00001370#ifndef ART_ENABLE_CODEGEN_arm
Alexandre Ramesef20f712015-06-09 10:29:30 +01001371#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M)
Vladimir Markob4536b72015-11-24 13:45:23 +00001372#else
1373#define FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1374 M(ArmDexCacheArraysBase, Instruction)
1375#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001376
1377#define FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M)
1378
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001379#ifndef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001380#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001381#else
1382#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
1383 M(MipsComputeBaseMethodAddress, Instruction) \
Alexey Frunze96b66822016-09-10 02:32:44 -07001384 M(MipsDexCacheArraysBase, Instruction) \
1385 M(MipsPackedSwitch, Instruction)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001386#endif
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001387
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001388#define FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M)
1389
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001390#ifndef ART_ENABLE_CODEGEN_x86
1391#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M)
1392#else
Mark Mendell0616ae02015-04-17 12:49:27 -04001393#define FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1394 M(X86ComputeBaseMethodAddress, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001395 M(X86LoadFromConstantTable, Instruction) \
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001396 M(X86FPNeg, Instruction) \
Mark Mendell805b3b52015-09-18 14:10:29 -04001397 M(X86PackedSwitch, Instruction)
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001398#endif
Alexandre Ramesef20f712015-06-09 10:29:30 +01001399
1400#define FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1401
1402#define FOR_EACH_CONCRETE_INSTRUCTION(M) \
1403 FOR_EACH_CONCRETE_INSTRUCTION_COMMON(M) \
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001404 FOR_EACH_CONCRETE_INSTRUCTION_SHARED(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001405 FOR_EACH_CONCRETE_INSTRUCTION_ARM(M) \
1406 FOR_EACH_CONCRETE_INSTRUCTION_ARM64(M) \
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001407 FOR_EACH_CONCRETE_INSTRUCTION_MIPS(M) \
Alexandre Ramesf39e0642015-06-23 11:33:45 +01001408 FOR_EACH_CONCRETE_INSTRUCTION_MIPS64(M) \
Alexandre Ramesef20f712015-06-09 10:29:30 +01001409 FOR_EACH_CONCRETE_INSTRUCTION_X86(M) \
1410 FOR_EACH_CONCRETE_INSTRUCTION_X86_64(M)
1411
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001412#define FOR_EACH_ABSTRACT_INSTRUCTION(M) \
1413 M(Condition, BinaryOperation) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001414 M(Constant, Instruction) \
Roland Levillain88cb1752014-10-20 16:36:47 +01001415 M(UnaryOperation, Instruction) \
Calin Juravle34bacdf2014-10-07 20:23:36 +01001416 M(BinaryOperation, Instruction) \
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001417 M(Invoke, Instruction)
Dave Allison20dfc792014-06-16 20:44:29 -07001418
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001419#define FOR_EACH_INSTRUCTION(M) \
1420 FOR_EACH_CONCRETE_INSTRUCTION(M) \
1421 FOR_EACH_ABSTRACT_INSTRUCTION(M)
1422
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001423#define FORWARD_DECLARATION(type, super) class H##type;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001424FOR_EACH_INSTRUCTION(FORWARD_DECLARATION)
1425#undef FORWARD_DECLARATION
1426
Vladimir Marko372f10e2016-05-17 16:30:10 +01001427#define DECLARE_INSTRUCTION(type) \
1428 InstructionKind GetKindInternal() const OVERRIDE { return k##type; } \
1429 const char* DebugName() const OVERRIDE { return #type; } \
1430 bool InstructionTypeEquals(const HInstruction* other) const OVERRIDE { \
1431 return other->Is##type(); \
1432 } \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00001433 void Accept(HGraphVisitor* visitor) OVERRIDE
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001434
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001435#define DECLARE_ABSTRACT_INSTRUCTION(type) \
1436 bool Is##type() const { return As##type() != nullptr; } \
1437 const H##type* As##type() const { return this; } \
1438 H##type* As##type() { return this; }
1439
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001440template <typename T>
Vladimir Markof9f64412015-09-02 14:05:49 +01001441class HUseListNode : public ArenaObject<kArenaAllocUseListNode> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001442 public:
David Brazdiled596192015-01-23 10:39:45 +00001443 T GetUser() const { return user_; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001444 size_t GetIndex() const { return index_; }
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001445 void SetIndex(size_t index) { index_ = index; }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001446
Vladimir Marko46817b82016-03-29 12:21:58 +01001447 // Hook for the IntrusiveForwardList<>.
1448 // TODO: Hide this better.
1449 IntrusiveForwardListHook hook;
1450
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001451 private:
David Brazdiled596192015-01-23 10:39:45 +00001452 HUseListNode(T user, size_t index)
Vladimir Marko46817b82016-03-29 12:21:58 +01001453 : user_(user), index_(index) {}
David Brazdiled596192015-01-23 10:39:45 +00001454
1455 T const user_;
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001456 size_t index_;
David Brazdiled596192015-01-23 10:39:45 +00001457
Vladimir Marko46817b82016-03-29 12:21:58 +01001458 friend class HInstruction;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001459
1460 DISALLOW_COPY_AND_ASSIGN(HUseListNode);
1461};
1462
David Brazdiled596192015-01-23 10:39:45 +00001463template <typename T>
Vladimir Marko46817b82016-03-29 12:21:58 +01001464using HUseList = IntrusiveForwardList<HUseListNode<T>>;
David Brazdiled596192015-01-23 10:39:45 +00001465
David Brazdil1abb4192015-02-17 18:33:36 +00001466// This class is used by HEnvironment and HInstruction classes to record the
1467// instructions they use and pointers to the corresponding HUseListNodes kept
1468// by the used instructions.
1469template <typename T>
Vladimir Marko76c92ac2015-09-17 15:39:16 +01001470class HUserRecord : public ValueObject {
David Brazdil1abb4192015-02-17 18:33:36 +00001471 public:
Vladimir Marko46817b82016-03-29 12:21:58 +01001472 HUserRecord() : instruction_(nullptr), before_use_node_() {}
1473 explicit HUserRecord(HInstruction* instruction) : instruction_(instruction), before_use_node_() {}
David Brazdil1abb4192015-02-17 18:33:36 +00001474
Vladimir Marko46817b82016-03-29 12:21:58 +01001475 HUserRecord(const HUserRecord<T>& old_record, typename HUseList<T>::iterator before_use_node)
1476 : HUserRecord(old_record.instruction_, before_use_node) {}
1477 HUserRecord(HInstruction* instruction, typename HUseList<T>::iterator before_use_node)
1478 : instruction_(instruction), before_use_node_(before_use_node) {
David Brazdil1abb4192015-02-17 18:33:36 +00001479 DCHECK(instruction_ != nullptr);
David Brazdil1abb4192015-02-17 18:33:36 +00001480 }
1481
1482 HInstruction* GetInstruction() const { return instruction_; }
Vladimir Marko46817b82016-03-29 12:21:58 +01001483 typename HUseList<T>::iterator GetBeforeUseNode() const { return before_use_node_; }
1484 typename HUseList<T>::iterator GetUseNode() const { return ++GetBeforeUseNode(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001485
1486 private:
1487 // Instruction used by the user.
1488 HInstruction* instruction_;
1489
Vladimir Marko46817b82016-03-29 12:21:58 +01001490 // Iterator before the corresponding entry in the use list kept by 'instruction_'.
1491 typename HUseList<T>::iterator before_use_node_;
David Brazdil1abb4192015-02-17 18:33:36 +00001492};
1493
Vladimir Markoe9004912016-06-16 16:50:52 +01001494// Helper class that extracts the input instruction from HUserRecord<HInstruction*>.
1495// This is used for HInstruction::GetInputs() to return a container wrapper providing
1496// HInstruction* values even though the underlying container has HUserRecord<>s.
1497struct HInputExtractor {
1498 HInstruction* operator()(HUserRecord<HInstruction*>& record) const {
1499 return record.GetInstruction();
1500 }
1501 const HInstruction* operator()(const HUserRecord<HInstruction*>& record) const {
1502 return record.GetInstruction();
1503 }
1504};
1505
1506using HInputsRef = TransformArrayRef<HUserRecord<HInstruction*>, HInputExtractor>;
1507using HConstInputsRef = TransformArrayRef<const HUserRecord<HInstruction*>, HInputExtractor>;
1508
Aart Bik854a02b2015-07-14 16:07:00 -07001509/**
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001510 * Side-effects representation.
Aart Bik854a02b2015-07-14 16:07:00 -07001511 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001512 * For write/read dependences on fields/arrays, the dependence analysis uses
1513 * type disambiguation (e.g. a float field write cannot modify the value of an
1514 * integer field read) and the access type (e.g. a reference array write cannot
1515 * modify the value of a reference field read [although it may modify the
1516 * reference fetch prior to reading the field, which is represented by its own
1517 * write/read dependence]). The analysis makes conservative points-to
1518 * assumptions on reference types (e.g. two same typed arrays are assumed to be
1519 * the same, and any reference read depends on any reference read without
1520 * further regard of its type).
Aart Bik854a02b2015-07-14 16:07:00 -07001521 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001522 * The internal representation uses 38-bit and is described in the table below.
1523 * The first line indicates the side effect, and for field/array accesses the
1524 * second line indicates the type of the access (in the order of the
1525 * Primitive::Type enum).
1526 * The two numbered lines below indicate the bit position in the bitfield (read
1527 * vertically).
Aart Bik854a02b2015-07-14 16:07:00 -07001528 *
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001529 * |Depends on GC|ARRAY-R |FIELD-R |Can trigger GC|ARRAY-W |FIELD-W |
1530 * +-------------+---------+---------+--------------+---------+---------+
1531 * | |DFJISCBZL|DFJISCBZL| |DFJISCBZL|DFJISCBZL|
1532 * | 3 |333333322|222222221| 1 |111111110|000000000|
1533 * | 7 |654321098|765432109| 8 |765432109|876543210|
1534 *
1535 * Note that, to ease the implementation, 'changes' bits are least significant
1536 * bits, while 'dependency' bits are most significant bits.
Aart Bik854a02b2015-07-14 16:07:00 -07001537 */
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001538class SideEffects : public ValueObject {
1539 public:
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001540 SideEffects() : flags_(0) {}
1541
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001542 static SideEffects None() {
1543 return SideEffects(0);
1544 }
1545
1546 static SideEffects All() {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001547 return SideEffects(kAllChangeBits | kAllDependOnBits);
1548 }
1549
1550 static SideEffects AllChanges() {
1551 return SideEffects(kAllChangeBits);
1552 }
1553
1554 static SideEffects AllDependencies() {
1555 return SideEffects(kAllDependOnBits);
1556 }
1557
1558 static SideEffects AllExceptGCDependency() {
1559 return AllWritesAndReads().Union(SideEffects::CanTriggerGC());
1560 }
1561
1562 static SideEffects AllWritesAndReads() {
Aart Bik854a02b2015-07-14 16:07:00 -07001563 return SideEffects(kAllWrites | kAllReads);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001564 }
1565
Aart Bik34c3ba92015-07-20 14:08:59 -07001566 static SideEffects AllWrites() {
1567 return SideEffects(kAllWrites);
1568 }
1569
1570 static SideEffects AllReads() {
1571 return SideEffects(kAllReads);
1572 }
1573
1574 static SideEffects FieldWriteOfType(Primitive::Type type, bool is_volatile) {
1575 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001576 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001577 : SideEffects(TypeFlag(type, kFieldWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001578 }
1579
Aart Bik854a02b2015-07-14 16:07:00 -07001580 static SideEffects ArrayWriteOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001581 return SideEffects(TypeFlag(type, kArrayWriteOffset));
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001582 }
1583
Aart Bik34c3ba92015-07-20 14:08:59 -07001584 static SideEffects FieldReadOfType(Primitive::Type type, bool is_volatile) {
1585 return is_volatile
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001586 ? AllWritesAndReads()
Aart Bik18b36ab2016-04-13 16:41:35 -07001587 : SideEffects(TypeFlag(type, kFieldReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001588 }
1589
1590 static SideEffects ArrayReadOfType(Primitive::Type type) {
Aart Bik18b36ab2016-04-13 16:41:35 -07001591 return SideEffects(TypeFlag(type, kArrayReadOffset));
Aart Bik854a02b2015-07-14 16:07:00 -07001592 }
1593
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001594 static SideEffects CanTriggerGC() {
1595 return SideEffects(1ULL << kCanTriggerGCBit);
1596 }
1597
1598 static SideEffects DependsOnGC() {
1599 return SideEffects(1ULL << kDependsOnGCBit);
1600 }
1601
Aart Bik854a02b2015-07-14 16:07:00 -07001602 // Combines the side-effects of this and the other.
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001603 SideEffects Union(SideEffects other) const {
1604 return SideEffects(flags_ | other.flags_);
1605 }
1606
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001607 SideEffects Exclusion(SideEffects other) const {
1608 return SideEffects(flags_ & ~other.flags_);
1609 }
1610
Alexandre Ramese6dbf482015-10-19 10:10:41 +01001611 void Add(SideEffects other) {
1612 flags_ |= other.flags_;
1613 }
1614
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001615 bool Includes(SideEffects other) const {
1616 return (other.flags_ & flags_) == other.flags_;
1617 }
1618
1619 bool HasSideEffects() const {
1620 return (flags_ & kAllChangeBits);
1621 }
1622
1623 bool HasDependencies() const {
1624 return (flags_ & kAllDependOnBits);
1625 }
1626
1627 // Returns true if there are no side effects or dependencies.
1628 bool DoesNothing() const {
1629 return flags_ == 0;
1630 }
1631
Aart Bik854a02b2015-07-14 16:07:00 -07001632 // Returns true if something is written.
1633 bool DoesAnyWrite() const {
1634 return (flags_ & kAllWrites);
Roland Levillain72bceff2014-09-15 18:29:00 +01001635 }
1636
Aart Bik854a02b2015-07-14 16:07:00 -07001637 // Returns true if something is read.
1638 bool DoesAnyRead() const {
1639 return (flags_ & kAllReads);
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001640 }
1641
Aart Bik854a02b2015-07-14 16:07:00 -07001642 // Returns true if potentially everything is written and read
1643 // (every type and every kind of access).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001644 bool DoesAllReadWrite() const {
1645 return (flags_ & (kAllWrites | kAllReads)) == (kAllWrites | kAllReads);
1646 }
1647
Aart Bik854a02b2015-07-14 16:07:00 -07001648 bool DoesAll() const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001649 return flags_ == (kAllChangeBits | kAllDependOnBits);
Aart Bik854a02b2015-07-14 16:07:00 -07001650 }
1651
Roland Levillain0d5a2812015-11-13 10:07:31 +00001652 // Returns true if `this` may read something written by `other`.
Aart Bik854a02b2015-07-14 16:07:00 -07001653 bool MayDependOn(SideEffects other) const {
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001654 const uint64_t depends_on_flags = (flags_ & kAllDependOnBits) >> kChangeBits;
1655 return (other.flags_ & depends_on_flags);
Aart Bik854a02b2015-07-14 16:07:00 -07001656 }
1657
1658 // Returns string representation of flags (for debugging only).
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001659 // Format: |x|DFJISCBZL|DFJISCBZL|y|DFJISCBZL|DFJISCBZL|
Aart Bik854a02b2015-07-14 16:07:00 -07001660 std::string ToString() const {
Aart Bik854a02b2015-07-14 16:07:00 -07001661 std::string flags = "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001662 for (int s = kLastBit; s >= 0; s--) {
1663 bool current_bit_is_set = ((flags_ >> s) & 1) != 0;
1664 if ((s == kDependsOnGCBit) || (s == kCanTriggerGCBit)) {
1665 // This is a bit for the GC side effect.
1666 if (current_bit_is_set) {
1667 flags += "GC";
1668 }
Aart Bik854a02b2015-07-14 16:07:00 -07001669 flags += "|";
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001670 } else {
1671 // This is a bit for the array/field analysis.
1672 // The underscore character stands for the 'can trigger GC' bit.
1673 static const char *kDebug = "LZBCSIJFDLZBCSIJFD_LZBCSIJFDLZBCSIJFD";
1674 if (current_bit_is_set) {
1675 flags += kDebug[s];
1676 }
1677 if ((s == kFieldWriteOffset) || (s == kArrayWriteOffset) ||
1678 (s == kFieldReadOffset) || (s == kArrayReadOffset)) {
1679 flags += "|";
1680 }
1681 }
Aart Bik854a02b2015-07-14 16:07:00 -07001682 }
1683 return flags;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001684 }
1685
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001686 bool Equals(const SideEffects& other) const { return flags_ == other.flags_; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001687
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001688 private:
1689 static constexpr int kFieldArrayAnalysisBits = 9;
1690
1691 static constexpr int kFieldWriteOffset = 0;
1692 static constexpr int kArrayWriteOffset = kFieldWriteOffset + kFieldArrayAnalysisBits;
1693 static constexpr int kLastBitForWrites = kArrayWriteOffset + kFieldArrayAnalysisBits - 1;
1694 static constexpr int kCanTriggerGCBit = kLastBitForWrites + 1;
1695
1696 static constexpr int kChangeBits = kCanTriggerGCBit + 1;
1697
1698 static constexpr int kFieldReadOffset = kCanTriggerGCBit + 1;
1699 static constexpr int kArrayReadOffset = kFieldReadOffset + kFieldArrayAnalysisBits;
1700 static constexpr int kLastBitForReads = kArrayReadOffset + kFieldArrayAnalysisBits - 1;
1701 static constexpr int kDependsOnGCBit = kLastBitForReads + 1;
1702
1703 static constexpr int kLastBit = kDependsOnGCBit;
1704 static constexpr int kDependOnBits = kLastBit + 1 - kChangeBits;
1705
1706 // Aliases.
1707
1708 static_assert(kChangeBits == kDependOnBits,
1709 "the 'change' bits should match the 'depend on' bits.");
1710
1711 static constexpr uint64_t kAllChangeBits = ((1ULL << kChangeBits) - 1);
1712 static constexpr uint64_t kAllDependOnBits = ((1ULL << kDependOnBits) - 1) << kChangeBits;
1713 static constexpr uint64_t kAllWrites =
1714 ((1ULL << (kLastBitForWrites + 1 - kFieldWriteOffset)) - 1) << kFieldWriteOffset;
1715 static constexpr uint64_t kAllReads =
1716 ((1ULL << (kLastBitForReads + 1 - kFieldReadOffset)) - 1) << kFieldReadOffset;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001717
Aart Bik854a02b2015-07-14 16:07:00 -07001718 // Translates type to bit flag.
1719 static uint64_t TypeFlag(Primitive::Type type, int offset) {
1720 CHECK_NE(type, Primitive::kPrimVoid);
1721 const uint64_t one = 1;
1722 const int shift = type; // 0-based consecutive enum
1723 DCHECK_LE(kFieldWriteOffset, shift);
1724 DCHECK_LT(shift, kArrayWriteOffset);
1725 return one << (type + offset);
1726 }
1727
1728 // Private constructor on direct flags value.
1729 explicit SideEffects(uint64_t flags) : flags_(flags) {}
1730
1731 uint64_t flags_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001732};
1733
David Brazdiled596192015-01-23 10:39:45 +00001734// A HEnvironment object contains the values of virtual registers at a given location.
Vladimir Markof9f64412015-09-02 14:05:49 +01001735class HEnvironment : public ArenaObject<kArenaAllocEnvironment> {
David Brazdiled596192015-01-23 10:39:45 +00001736 public:
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001737 HEnvironment(ArenaAllocator* arena,
1738 size_t number_of_vregs,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001739 ArtMethod* method,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001740 uint32_t dex_pc,
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001741 HInstruction* holder)
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001742 : vregs_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentVRegs)),
1743 locations_(number_of_vregs, arena->Adapter(kArenaAllocEnvironmentLocations)),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001744 parent_(nullptr),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001745 method_(method),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001746 dex_pc_(dex_pc),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001747 holder_(holder) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001748 }
1749
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001750 HEnvironment(ArenaAllocator* arena, const HEnvironment& to_copy, HInstruction* holder)
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001751 : HEnvironment(arena,
1752 to_copy.Size(),
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001753 to_copy.GetMethod(),
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001754 to_copy.GetDexPc(),
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001755 holder) {}
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001756
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001757 void SetAndCopyParentChain(ArenaAllocator* allocator, HEnvironment* parent) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001758 if (parent_ != nullptr) {
1759 parent_->SetAndCopyParentChain(allocator, parent);
1760 } else {
1761 parent_ = new (allocator) HEnvironment(allocator, *parent, holder_);
1762 parent_->CopyFrom(parent);
1763 if (parent->GetParent() != nullptr) {
1764 parent_->SetAndCopyParentChain(allocator, parent->GetParent());
1765 }
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001766 }
David Brazdiled596192015-01-23 10:39:45 +00001767 }
1768
Vladimir Marko71bf8092015-09-15 15:33:14 +01001769 void CopyFrom(const ArenaVector<HInstruction*>& locals);
Nicolas Geoffray8c0c91a2015-05-07 11:46:05 +01001770 void CopyFrom(HEnvironment* environment);
1771
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001772 // Copy from `env`. If it's a loop phi for `loop_header`, copy the first
1773 // input to the loop phi instead. This is for inserting instructions that
1774 // require an environment (like HDeoptimization) in the loop pre-header.
1775 void CopyFromWithLoopPhiAdjustment(HEnvironment* env, HBasicBlock* loop_header);
David Brazdiled596192015-01-23 10:39:45 +00001776
1777 void SetRawEnvAt(size_t index, HInstruction* instruction) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001778 vregs_[index] = HUserRecord<HEnvironment*>(instruction);
David Brazdiled596192015-01-23 10:39:45 +00001779 }
1780
1781 HInstruction* GetInstructionAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001782 return vregs_[index].GetInstruction();
David Brazdiled596192015-01-23 10:39:45 +00001783 }
1784
David Brazdil1abb4192015-02-17 18:33:36 +00001785 void RemoveAsUserOfInput(size_t index) const;
David Brazdiled596192015-01-23 10:39:45 +00001786
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001787 size_t Size() const { return vregs_.size(); }
David Brazdiled596192015-01-23 10:39:45 +00001788
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001789 HEnvironment* GetParent() const { return parent_; }
1790
1791 void SetLocationAt(size_t index, Location location) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001792 locations_[index] = location;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001793 }
1794
1795 Location GetLocationAt(size_t index) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001796 return locations_[index];
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001797 }
1798
1799 uint32_t GetDexPc() const {
1800 return dex_pc_;
1801 }
1802
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001803 ArtMethod* GetMethod() const {
1804 return method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001805 }
1806
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001807 HInstruction* GetHolder() const {
1808 return holder_;
1809 }
1810
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00001811
1812 bool IsFromInlinedInvoke() const {
1813 return GetParent() != nullptr;
1814 }
1815
David Brazdiled596192015-01-23 10:39:45 +00001816 private:
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001817 ArenaVector<HUserRecord<HEnvironment*>> vregs_;
1818 ArenaVector<Location> locations_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001819 HEnvironment* parent_;
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00001820 ArtMethod* method_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001821 const uint32_t dex_pc_;
David Brazdiled596192015-01-23 10:39:45 +00001822
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01001823 // The instruction that holds this environment.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01001824 HInstruction* const holder_;
1825
Nicolas Geoffray5d7b7f82015-04-28 00:52:43 +01001826 friend class HInstruction;
David Brazdiled596192015-01-23 10:39:45 +00001827
1828 DISALLOW_COPY_AND_ASSIGN(HEnvironment);
1829};
1830
Vladimir Markof9f64412015-09-02 14:05:49 +01001831class HInstruction : public ArenaObject<kArenaAllocInstruction> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001832 public:
Calin Juravle154746b2015-10-06 15:46:54 +01001833 HInstruction(SideEffects side_effects, uint32_t dex_pc)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834 : previous_(nullptr),
1835 next_(nullptr),
1836 block_(nullptr),
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001837 dex_pc_(dex_pc),
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838 id_(-1),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001839 ssa_index_(-1),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001840 packed_fields_(0u),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001841 environment_(nullptr),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001842 locations_(nullptr),
1843 live_interval_(nullptr),
Nicolas Geoffray065bf772014-09-03 14:51:22 +01001844 lifetime_position_(kNoLifetime),
Calin Juravleb1498f62015-02-16 13:13:29 +00001845 side_effects_(side_effects),
Vladimir Markoa1de9182016-02-25 11:37:38 +00001846 reference_type_handle_(ReferenceTypeInfo::CreateInvalid().GetTypeHandle()) {
1847 SetPackedFlag<kFlagReferenceTypeIsExact>(ReferenceTypeInfo::CreateInvalid().IsExact());
1848 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001849
Dave Allison20dfc792014-06-16 20:44:29 -07001850 virtual ~HInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001851
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001852#define DECLARE_KIND(type, super) k##type,
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001853 enum InstructionKind {
1854 FOR_EACH_INSTRUCTION(DECLARE_KIND)
1855 };
1856#undef DECLARE_KIND
1857
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001858 HInstruction* GetNext() const { return next_; }
1859 HInstruction* GetPrevious() const { return previous_; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001860
Calin Juravle77520bc2015-01-12 18:45:46 +00001861 HInstruction* GetNextDisregardingMoves() const;
1862 HInstruction* GetPreviousDisregardingMoves() const;
1863
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 HBasicBlock* GetBlock() const { return block_; }
Roland Levillain9867bc72015-08-05 10:21:34 +01001865 ArenaAllocator* GetArena() const { return block_->GetGraph()->GetArena(); }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001866 void SetBlock(HBasicBlock* block) { block_ = block; }
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +01001867 bool IsInBlock() const { return block_ != nullptr; }
1868 bool IsInLoop() const { return block_->IsInLoop(); }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001869 bool IsLoopHeaderPhi() const { return IsPhi() && block_->IsLoopHeader(); }
1870 bool IsIrreducibleLoopHeaderPhi() const {
1871 return IsLoopHeaderPhi() && GetBlock()->GetLoopInformation()->IsIrreducible();
1872 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001873
Vladimir Marko372f10e2016-05-17 16:30:10 +01001874 virtual ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() = 0;
1875
1876 ArrayRef<const HUserRecord<HInstruction*>> GetInputRecords() const {
1877 // One virtual method is enough, just const_cast<> and then re-add the const.
1878 return ArrayRef<const HUserRecord<HInstruction*>>(
1879 const_cast<HInstruction*>(this)->GetInputRecords());
1880 }
1881
Vladimir Markoe9004912016-06-16 16:50:52 +01001882 HInputsRef GetInputs() {
1883 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001884 }
1885
Vladimir Markoe9004912016-06-16 16:50:52 +01001886 HConstInputsRef GetInputs() const {
1887 return MakeTransformArrayRef(GetInputRecords(), HInputExtractor());
Vladimir Marko372f10e2016-05-17 16:30:10 +01001888 }
1889
1890 size_t InputCount() const { return GetInputRecords().size(); }
David Brazdil1abb4192015-02-17 18:33:36 +00001891 HInstruction* InputAt(size_t i) const { return InputRecordAt(i).GetInstruction(); }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001892
Aart Bik2767f4b2016-10-28 15:03:53 -07001893 bool HasInput(HInstruction* input) const {
1894 for (const HInstruction* i : GetInputs()) {
1895 if (i == input) {
1896 return true;
1897 }
1898 }
1899 return false;
1900 }
1901
Vladimir Marko372f10e2016-05-17 16:30:10 +01001902 void SetRawInputAt(size_t index, HInstruction* input) {
1903 SetRawInputRecordAt(index, HUserRecord<HInstruction*>(input));
1904 }
1905
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001906 virtual void Accept(HGraphVisitor* visitor) = 0;
1907 virtual const char* DebugName() const = 0;
1908
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001909 virtual Primitive::Type GetType() const { return Primitive::kPrimVoid; }
1910
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001911 virtual bool NeedsEnvironment() const { return false; }
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06001912
1913 uint32_t GetDexPc() const { return dex_pc_; }
1914
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01001915 virtual bool IsControlFlow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001916
Roland Levillaine161a2a2014-10-03 12:45:18 +01001917 virtual bool CanThrow() const { return false; }
David Brazdilec16f792015-08-19 15:04:01 +01001918 bool CanThrowIntoCatchBlock() const { return CanThrow() && block_->IsTryBlock(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001919
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001920 bool HasSideEffects() const { return side_effects_.HasSideEffects(); }
Aart Bik854a02b2015-07-14 16:07:00 -07001921 bool DoesAnyWrite() const { return side_effects_.DoesAnyWrite(); }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001922
Calin Juravle10e244f2015-01-26 18:54:32 +00001923 // Does not apply for all instructions, but having this at top level greatly
1924 // simplifies the null check elimination.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00001925 // TODO: Consider merging can_be_null into ReferenceTypeInfo.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001926 virtual bool CanBeNull() const {
1927 DCHECK_EQ(GetType(), Primitive::kPrimNot) << "CanBeNull only applies to reference types";
1928 return true;
1929 }
Calin Juravle10e244f2015-01-26 18:54:32 +00001930
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001931 virtual bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const {
Calin Juravle641547a2015-04-21 22:08:51 +01001932 return false;
1933 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001934
Nicolas Geoffraya3eca2d2016-01-12 16:03:16 +00001935 virtual bool IsActualObject() const {
1936 return GetType() == Primitive::kPrimNot;
1937 }
1938
Calin Juravle2e768302015-07-28 14:41:11 +00001939 void SetReferenceTypeInfo(ReferenceTypeInfo rti);
Calin Juravleacf735c2015-02-12 15:25:22 +00001940
Calin Juravle61d544b2015-02-23 16:46:57 +00001941 ReferenceTypeInfo GetReferenceTypeInfo() const {
1942 DCHECK_EQ(GetType(), Primitive::kPrimNot);
Vladimir Markoa1de9182016-02-25 11:37:38 +00001943 return ReferenceTypeInfo::CreateUnchecked(reference_type_handle_,
Vladimir Marko456307a2016-04-19 14:12:13 +00001944 GetPackedFlag<kFlagReferenceTypeIsExact>());
Calin Juravle61d544b2015-02-23 16:46:57 +00001945 }
Calin Juravleacf735c2015-02-12 15:25:22 +00001946
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001947 void AddUseAt(HInstruction* user, size_t index) {
David Brazdil1abb4192015-02-17 18:33:36 +00001948 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001949 // Note: fixup_end remains valid across push_front().
1950 auto fixup_end = uses_.empty() ? uses_.begin() : ++uses_.begin();
1951 HUseListNode<HInstruction*>* new_node =
1952 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HInstruction*>(user, index);
1953 uses_.push_front(*new_node);
1954 FixUpUserRecordsAfterUseInsertion(fixup_end);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001955 }
1956
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001957 void AddEnvUseAt(HEnvironment* user, size_t index) {
Nicolas Geoffray724c9632014-09-22 12:27:27 +01001958 DCHECK(user != nullptr);
Vladimir Marko46817b82016-03-29 12:21:58 +01001959 // Note: env_fixup_end remains valid across push_front().
1960 auto env_fixup_end = env_uses_.empty() ? env_uses_.begin() : ++env_uses_.begin();
1961 HUseListNode<HEnvironment*>* new_node =
1962 new (GetBlock()->GetGraph()->GetArena()) HUseListNode<HEnvironment*>(user, index);
1963 env_uses_.push_front(*new_node);
1964 FixUpUserRecordsAfterEnvUseInsertion(env_fixup_end);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001965 }
1966
David Brazdil1abb4192015-02-17 18:33:36 +00001967 void RemoveAsUserOfInput(size_t input) {
1968 HUserRecord<HInstruction*> input_use = InputRecordAt(input);
Vladimir Marko46817b82016-03-29 12:21:58 +01001969 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1970 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1971 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
David Brazdil1abb4192015-02-17 18:33:36 +00001972 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001973
Vladimir Marko372f10e2016-05-17 16:30:10 +01001974 void RemoveAsUserOfAllInputs() {
1975 for (const HUserRecord<HInstruction*>& input_use : GetInputRecords()) {
1976 HUseList<HInstruction*>::iterator before_use_node = input_use.GetBeforeUseNode();
1977 input_use.GetInstruction()->uses_.erase_after(before_use_node);
1978 input_use.GetInstruction()->FixUpUserRecordsAfterUseRemoval(before_use_node);
1979 }
1980 }
1981
David Brazdil1abb4192015-02-17 18:33:36 +00001982 const HUseList<HInstruction*>& GetUses() const { return uses_; }
1983 const HUseList<HEnvironment*>& GetEnvUses() const { return env_uses_; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001984
Vladimir Marko46817b82016-03-29 12:21:58 +01001985 bool HasUses() const { return !uses_.empty() || !env_uses_.empty(); }
1986 bool HasEnvironmentUses() const { return !env_uses_.empty(); }
1987 bool HasNonEnvironmentUses() const { return !uses_.empty(); }
Alexandre Rames188d4312015-04-09 18:30:21 +01001988 bool HasOnlyOneNonEnvironmentUse() const {
Vladimir Marko46817b82016-03-29 12:21:58 +01001989 return !HasEnvironmentUses() && GetUses().HasExactlyOneElement();
Alexandre Rames188d4312015-04-09 18:30:21 +01001990 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001991
Aart Bikcc42be02016-10-20 16:14:16 -07001992 bool IsRemovable() const {
Aart Bik482095d2016-10-10 15:39:10 -07001993 return
Aart Bikff7d89c2016-11-07 08:49:28 -08001994 !DoesAnyWrite() &&
Aart Bik482095d2016-10-10 15:39:10 -07001995 !CanThrow() &&
1996 !IsSuspendCheck() &&
1997 !IsControlFlow() &&
1998 !IsNativeDebugInfo() &&
1999 !IsParameterValue() &&
Aart Bik482095d2016-10-10 15:39:10 -07002000 // If we added an explicit barrier then we should keep it.
2001 !IsMemoryBarrier();
2002 }
2003
Aart Bikcc42be02016-10-20 16:14:16 -07002004 bool IsDeadAndRemovable() const {
2005 return IsRemovable() && !HasUses();
2006 }
2007
Roland Levillain6c82d402014-10-13 16:10:27 +01002008 // Does this instruction strictly dominate `other_instruction`?
2009 // Returns false if this instruction and `other_instruction` are the same.
2010 // Aborts if this instruction and `other_instruction` are both phis.
2011 bool StrictlyDominates(HInstruction* other_instruction) const;
Roland Levillainccc07a92014-09-16 14:48:16 +01002012
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002013 int GetId() const { return id_; }
2014 void SetId(int id) { id_ = id; }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002015
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002016 int GetSsaIndex() const { return ssa_index_; }
2017 void SetSsaIndex(int ssa_index) { ssa_index_ = ssa_index; }
2018 bool HasSsaIndex() const { return ssa_index_ != -1; }
2019
2020 bool HasEnvironment() const { return environment_ != nullptr; }
2021 HEnvironment* GetEnvironment() const { return environment_; }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002022 // Set the `environment_` field. Raw because this method does not
2023 // update the uses lists.
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002024 void SetRawEnvironment(HEnvironment* environment) {
2025 DCHECK(environment_ == nullptr);
2026 DCHECK_EQ(environment->GetHolder(), this);
2027 environment_ = environment;
2028 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002029
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002030 void InsertRawEnvironment(HEnvironment* environment) {
2031 DCHECK(environment_ != nullptr);
2032 DCHECK_EQ(environment->GetHolder(), this);
2033 DCHECK(environment->GetParent() == nullptr);
2034 environment->parent_ = environment_;
2035 environment_ = environment;
2036 }
2037
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002038 void RemoveEnvironment();
2039
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002040 // Set the environment of this instruction, copying it from `environment`. While
2041 // copying, the uses lists are being updated.
2042 void CopyEnvironmentFrom(HEnvironment* environment) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002043 DCHECK(environment_ == nullptr);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002044 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002045 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002046 environment_->CopyFrom(environment);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002047 if (environment->GetParent() != nullptr) {
2048 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2049 }
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01002050 }
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002051
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002052 void CopyEnvironmentFromWithLoopPhiAdjustment(HEnvironment* environment,
2053 HBasicBlock* block) {
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002054 DCHECK(environment_ == nullptr);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002055 ArenaAllocator* allocator = GetBlock()->GetGraph()->GetArena();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01002056 environment_ = new (allocator) HEnvironment(allocator, *environment, this);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01002057 environment_->CopyFromWithLoopPhiAdjustment(environment, block);
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01002058 if (environment->GetParent() != nullptr) {
2059 environment_->SetAndCopyParentChain(allocator, environment->GetParent());
2060 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07002061 }
2062
Nicolas Geoffray39468442014-09-02 15:17:15 +01002063 // Returns the number of entries in the environment. Typically, that is the
2064 // number of dex registers in a method. It could be more in case of inlining.
2065 size_t EnvironmentSize() const;
2066
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002067 LocationSummary* GetLocations() const { return locations_; }
2068 void SetLocations(LocationSummary* locations) { locations_ = locations; }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002069
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002070 void ReplaceWith(HInstruction* instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002071 void ReplaceInput(HInstruction* replacement, size_t index);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002072
Alexandre Rames188d4312015-04-09 18:30:21 +01002073 // This is almost the same as doing `ReplaceWith()`. But in this helper, the
2074 // uses of this instruction by `other` are *not* updated.
2075 void ReplaceWithExceptInReplacementAtIndex(HInstruction* other, size_t use_index) {
2076 ReplaceWith(other);
2077 other->ReplaceInput(this, use_index);
2078 }
2079
Alexandre Rames22aa54b2016-10-18 09:32:29 +01002080 // Move `this` instruction before `cursor`
2081 void MoveBefore(HInstruction* cursor, bool do_checks = true);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002082
Vladimir Markofb337ea2015-11-25 15:25:10 +00002083 // Move `this` before its first user and out of any loops. If there is no
2084 // out-of-loop user that dominates all other users, move the instruction
2085 // to the end of the out-of-loop common dominator of the user's blocks.
2086 //
2087 // This can be used only on non-throwing instructions with no side effects that
2088 // have at least one use but no environment uses.
2089 void MoveBeforeFirstUserAndOutOfLoops();
2090
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002091#define INSTRUCTION_TYPE_CHECK(type, super) \
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 bool Is##type() const; \
2093 const H##type* As##type() const; \
2094 H##type* As##type();
2095
2096 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
2097#undef INSTRUCTION_TYPE_CHECK
2098
2099#define INSTRUCTION_TYPE_CHECK(type, super) \
Roland Levillainccc07a92014-09-16 14:48:16 +01002100 bool Is##type() const { return (As##type() != nullptr); } \
2101 virtual const H##type* As##type() const { return nullptr; } \
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002102 virtual H##type* As##type() { return nullptr; }
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002103 FOR_EACH_ABSTRACT_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002104#undef INSTRUCTION_TYPE_CHECK
2105
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002106 // Returns whether the instruction can be moved within the graph.
Aart Bik71bf7b42016-11-16 10:17:46 -08002107 // TODO: this method is used by LICM and GVN with possibly different
2108 // meanings? split and rename?
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002109 virtual bool CanBeMoved() const { return false; }
2110
2111 // Returns whether the two instructions are of the same kind.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002112 virtual bool InstructionTypeEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002113 return false;
2114 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002115
2116 // Returns whether any data encoded in the two instructions is equal.
2117 // This method does not look at the inputs. Both instructions must be
2118 // of the same type, otherwise the method has undefined behavior.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002119 virtual bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002120 return false;
2121 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002122
2123 // Returns whether two instructions are equal, that is:
Calin Juravleddb7df22014-11-25 20:56:51 +00002124 // 1) They have the same type and contain the same data (InstructionDataEquals).
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002125 // 2) Their inputs are identical.
Vladimir Marko372f10e2016-05-17 16:30:10 +01002126 bool Equals(const HInstruction* other) const;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002127
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 // TODO: Remove this indirection when the [[pure]] attribute proposal (n3744)
2129 // is adopted and implemented by our C++ compiler(s). Fow now, we need to hide
2130 // the virtual function because the __attribute__((__pure__)) doesn't really
2131 // apply the strong requirement for virtual functions, preventing optimizations.
2132 InstructionKind GetKind() const PURE;
2133 virtual InstructionKind GetKindInternal() const = 0;
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002134
2135 virtual size_t ComputeHashCode() const {
2136 size_t result = GetKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01002137 for (const HInstruction* input : GetInputs()) {
2138 result = (result * 31) + input->GetId();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002139 }
2140 return result;
2141 }
2142
2143 SideEffects GetSideEffects() const { return side_effects_; }
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00002144 void SetSideEffects(SideEffects other) { side_effects_ = other; }
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002145 void AddSideEffects(SideEffects other) { side_effects_.Add(other); }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01002146
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002147 size_t GetLifetimePosition() const { return lifetime_position_; }
2148 void SetLifetimePosition(size_t position) { lifetime_position_ = position; }
2149 LiveInterval* GetLiveInterval() const { return live_interval_; }
2150 void SetLiveInterval(LiveInterval* interval) { live_interval_ = interval; }
2151 bool HasLiveInterval() const { return live_interval_ != nullptr; }
2152
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002153 bool IsSuspendCheckEntry() const { return IsSuspendCheck() && GetBlock()->IsEntryBlock(); }
2154
2155 // Returns whether the code generation of the instruction will require to have access
2156 // to the current method. Such instructions are:
2157 // (1): Instructions that require an environment, as calling the runtime requires
2158 // to walk the stack and have the current method stored at a specific stack address.
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002159 // (2): HCurrentMethod, potentially used by HInvokeStaticOrDirect, HLoadString, or HLoadClass
2160 // to access the dex cache.
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002161 bool NeedsCurrentMethod() const {
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002162 return NeedsEnvironment() || IsCurrentMethod();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002163 }
2164
Vladimir Markodc151b22015-10-15 18:02:30 +01002165 // Returns whether the code generation of the instruction will require to have access
2166 // to the dex cache of the current method's declaring class via the current method.
2167 virtual bool NeedsDexCacheOfDeclaringClass() const { return false; }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00002168
Mark Mendellc4701932015-04-10 13:18:51 -04002169 // Does this instruction have any use in an environment before
2170 // control flow hits 'other'?
2171 bool HasAnyEnvironmentUseBefore(HInstruction* other);
2172
2173 // Remove all references to environment uses of this instruction.
2174 // The caller must ensure that this is safe to do.
2175 void RemoveEnvironmentUsers();
2176
Vladimir Markoa1de9182016-02-25 11:37:38 +00002177 bool IsEmittedAtUseSite() const { return GetPackedFlag<kFlagEmittedAtUseSite>(); }
2178 void MarkEmittedAtUseSite() { SetPackedFlag<kFlagEmittedAtUseSite>(true); }
David Brazdilb3e773e2016-01-26 11:28:37 +00002179
David Brazdil1abb4192015-02-17 18:33:36 +00002180 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002181 // If set, the machine code for this instruction is assumed to be generated by
2182 // its users. Used by liveness analysis to compute use positions accordingly.
2183 static constexpr size_t kFlagEmittedAtUseSite = 0u;
2184 static constexpr size_t kFlagReferenceTypeIsExact = kFlagEmittedAtUseSite + 1;
2185 static constexpr size_t kNumberOfGenericPackedBits = kFlagReferenceTypeIsExact + 1;
2186 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
2187
Vladimir Marko372f10e2016-05-17 16:30:10 +01002188 const HUserRecord<HInstruction*> InputRecordAt(size_t i) const {
2189 return GetInputRecords()[i];
2190 }
2191
2192 void SetRawInputRecordAt(size_t index, const HUserRecord<HInstruction*>& input) {
2193 ArrayRef<HUserRecord<HInstruction*>> input_records = GetInputRecords();
2194 input_records[index] = input;
2195 }
David Brazdil1abb4192015-02-17 18:33:36 +00002196
Vladimir Markoa1de9182016-02-25 11:37:38 +00002197 uint32_t GetPackedFields() const {
2198 return packed_fields_;
2199 }
2200
2201 template <size_t flag>
2202 bool GetPackedFlag() const {
2203 return (packed_fields_ & (1u << flag)) != 0u;
2204 }
2205
2206 template <size_t flag>
2207 void SetPackedFlag(bool value = true) {
2208 packed_fields_ = (packed_fields_ & ~(1u << flag)) | ((value ? 1u : 0u) << flag);
2209 }
2210
2211 template <typename BitFieldType>
2212 typename BitFieldType::value_type GetPackedField() const {
2213 return BitFieldType::Decode(packed_fields_);
2214 }
2215
2216 template <typename BitFieldType>
2217 void SetPackedField(typename BitFieldType::value_type value) {
2218 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
2219 packed_fields_ = BitFieldType::Update(value, packed_fields_);
2220 }
2221
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002222 private:
Vladimir Marko46817b82016-03-29 12:21:58 +01002223 void FixUpUserRecordsAfterUseInsertion(HUseList<HInstruction*>::iterator fixup_end) {
2224 auto before_use_node = uses_.before_begin();
2225 for (auto use_node = uses_.begin(); use_node != fixup_end; ++use_node) {
2226 HInstruction* user = use_node->GetUser();
2227 size_t input_index = use_node->GetIndex();
2228 user->SetRawInputRecordAt(input_index, HUserRecord<HInstruction*>(this, before_use_node));
2229 before_use_node = use_node;
2230 }
2231 }
2232
2233 void FixUpUserRecordsAfterUseRemoval(HUseList<HInstruction*>::iterator before_use_node) {
2234 auto next = ++HUseList<HInstruction*>::iterator(before_use_node);
2235 if (next != uses_.end()) {
2236 HInstruction* next_user = next->GetUser();
2237 size_t next_index = next->GetIndex();
2238 DCHECK(next_user->InputRecordAt(next_index).GetInstruction() == this);
2239 next_user->SetRawInputRecordAt(next_index, HUserRecord<HInstruction*>(this, before_use_node));
2240 }
2241 }
2242
2243 void FixUpUserRecordsAfterEnvUseInsertion(HUseList<HEnvironment*>::iterator env_fixup_end) {
2244 auto before_env_use_node = env_uses_.before_begin();
2245 for (auto env_use_node = env_uses_.begin(); env_use_node != env_fixup_end; ++env_use_node) {
2246 HEnvironment* user = env_use_node->GetUser();
2247 size_t input_index = env_use_node->GetIndex();
2248 user->vregs_[input_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2249 before_env_use_node = env_use_node;
2250 }
2251 }
2252
2253 void FixUpUserRecordsAfterEnvUseRemoval(HUseList<HEnvironment*>::iterator before_env_use_node) {
2254 auto next = ++HUseList<HEnvironment*>::iterator(before_env_use_node);
2255 if (next != env_uses_.end()) {
2256 HEnvironment* next_user = next->GetUser();
2257 size_t next_index = next->GetIndex();
2258 DCHECK(next_user->vregs_[next_index].GetInstruction() == this);
2259 next_user->vregs_[next_index] = HUserRecord<HEnvironment*>(this, before_env_use_node);
2260 }
2261 }
David Brazdil1abb4192015-02-17 18:33:36 +00002262
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002263 HInstruction* previous_;
2264 HInstruction* next_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002265 HBasicBlock* block_;
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002266 const uint32_t dex_pc_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002267
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002268 // An instruction gets an id when it is added to the graph.
2269 // It reflects creation order. A negative id means the instruction
Nicolas Geoffray39468442014-09-02 15:17:15 +01002270 // has not been added to the graph.
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002271 int id_;
2272
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002273 // When doing liveness analysis, instructions that have uses get an SSA index.
2274 int ssa_index_;
2275
Vladimir Markoa1de9182016-02-25 11:37:38 +00002276 // Packed fields.
2277 uint32_t packed_fields_;
David Brazdilb3e773e2016-01-26 11:28:37 +00002278
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002279 // List of instructions that have this instruction as input.
David Brazdiled596192015-01-23 10:39:45 +00002280 HUseList<HInstruction*> uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002281
2282 // List of environments that contain this instruction.
David Brazdiled596192015-01-23 10:39:45 +00002283 HUseList<HEnvironment*> env_uses_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002284
Nicolas Geoffray39468442014-09-02 15:17:15 +01002285 // The environment associated with this instruction. Not null if the instruction
2286 // might jump out of the method.
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002287 HEnvironment* environment_;
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002288
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002289 // Set by the code generator.
2290 LocationSummary* locations_;
2291
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002292 // Set by the liveness analysis.
2293 LiveInterval* live_interval_;
2294
2295 // Set by the liveness analysis, this is the position in a linear
2296 // order of blocks where this instruction's live interval start.
2297 size_t lifetime_position_;
2298
Alexandre Ramese6dbf482015-10-19 10:10:41 +01002299 SideEffects side_effects_;
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002300
Vladimir Markoa1de9182016-02-25 11:37:38 +00002301 // The reference handle part of the reference type info.
2302 // The IsExact() flag is stored in packed fields.
Calin Juravleacf735c2015-02-12 15:25:22 +00002303 // TODO: for primitive types this should be marked as invalid.
Vladimir Markoa1de9182016-02-25 11:37:38 +00002304 ReferenceTypeInfo::TypeHandle reference_type_handle_;
Calin Juravleacf735c2015-02-12 15:25:22 +00002305
David Brazdil1abb4192015-02-17 18:33:36 +00002306 friend class GraphChecker;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002307 friend class HBasicBlock;
David Brazdil1abb4192015-02-17 18:33:36 +00002308 friend class HEnvironment;
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002309 friend class HGraph;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002310 friend class HInstructionList;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002311
2312 DISALLOW_COPY_AND_ASSIGN(HInstruction);
2313};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002314std::ostream& operator<<(std::ostream& os, const HInstruction::InstructionKind& rhs);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002315
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002316// Iterates over the instructions, while preserving the next instruction
2317// in case the current instruction gets removed from the list by the user
2318// of this iterator.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002319class HInstructionIterator : public ValueObject {
2320 public:
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002321 explicit HInstructionIterator(const HInstructionList& instructions)
2322 : instruction_(instructions.first_instruction_) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002323 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002324 }
2325
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002326 bool Done() const { return instruction_ == nullptr; }
2327 HInstruction* Current() const { return instruction_; }
2328 void Advance() {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002329 instruction_ = next_;
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002330 next_ = Done() ? nullptr : instruction_->GetNext();
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002331 }
2332
2333 private:
2334 HInstruction* instruction_;
2335 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002336
2337 DISALLOW_COPY_AND_ASSIGN(HInstructionIterator);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002338};
2339
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00002340// Iterates over the instructions without saving the next instruction,
2341// therefore handling changes in the graph potentially made by the user
2342// of this iterator.
2343class HInstructionIteratorHandleChanges : public ValueObject {
2344 public:
2345 explicit HInstructionIteratorHandleChanges(const HInstructionList& instructions)
2346 : instruction_(instructions.first_instruction_) {
2347 }
2348
2349 bool Done() const { return instruction_ == nullptr; }
2350 HInstruction* Current() const { return instruction_; }
2351 void Advance() {
2352 instruction_ = instruction_->GetNext();
2353 }
2354
2355 private:
2356 HInstruction* instruction_;
2357
2358 DISALLOW_COPY_AND_ASSIGN(HInstructionIteratorHandleChanges);
2359};
2360
2361
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002362class HBackwardInstructionIterator : public ValueObject {
2363 public:
2364 explicit HBackwardInstructionIterator(const HInstructionList& instructions)
2365 : instruction_(instructions.last_instruction_) {
2366 next_ = Done() ? nullptr : instruction_->GetPrevious();
2367 }
2368
2369 bool Done() const { return instruction_ == nullptr; }
2370 HInstruction* Current() const { return instruction_; }
2371 void Advance() {
2372 instruction_ = next_;
2373 next_ = Done() ? nullptr : instruction_->GetPrevious();
2374 }
2375
2376 private:
2377 HInstruction* instruction_;
2378 HInstruction* next_;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01002379
2380 DISALLOW_COPY_AND_ASSIGN(HBackwardInstructionIterator);
Nicolas Geoffray804d0932014-05-02 08:46:00 +01002381};
2382
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002383class HVariableInputSizeInstruction : public HInstruction {
2384 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002385 using HInstruction::GetInputRecords; // Keep the const version visible.
2386 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
2387 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
2388 }
2389
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002390 void AddInput(HInstruction* input);
2391 void InsertInputAt(size_t index, HInstruction* input);
2392 void RemoveInputAt(size_t index);
2393
2394 protected:
2395 HVariableInputSizeInstruction(SideEffects side_effects,
2396 uint32_t dex_pc,
2397 ArenaAllocator* arena,
2398 size_t number_of_inputs,
2399 ArenaAllocKind kind)
2400 : HInstruction(side_effects, dex_pc),
2401 inputs_(number_of_inputs, arena->Adapter(kind)) {}
2402
2403 ArenaVector<HUserRecord<HInstruction*>> inputs_;
2404
2405 private:
2406 DISALLOW_COPY_AND_ASSIGN(HVariableInputSizeInstruction);
2407};
2408
Vladimir Markof9f64412015-09-02 14:05:49 +01002409template<size_t N>
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002410class HTemplateInstruction: public HInstruction {
2411 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002412 HTemplateInstruction<N>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002413 : HInstruction(side_effects, dex_pc), inputs_() {}
Dave Allison20dfc792014-06-16 20:44:29 -07002414 virtual ~HTemplateInstruction() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002415
Vladimir Marko372f10e2016-05-17 16:30:10 +01002416 using HInstruction::GetInputRecords; // Keep the const version visible.
2417 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2418 return ArrayRef<HUserRecord<HInstruction*>>(inputs_);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002419 }
2420
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002421 private:
Vladimir Markof9f64412015-09-02 14:05:49 +01002422 std::array<HUserRecord<HInstruction*>, N> inputs_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01002423
2424 friend class SsaBuilder;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002425};
2426
Vladimir Markof9f64412015-09-02 14:05:49 +01002427// HTemplateInstruction specialization for N=0.
2428template<>
2429class HTemplateInstruction<0>: public HInstruction {
2430 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002431 explicit HTemplateInstruction<0>(SideEffects side_effects, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002432 : HInstruction(side_effects, dex_pc) {}
2433
Vladimir Markof9f64412015-09-02 14:05:49 +01002434 virtual ~HTemplateInstruction() {}
2435
Vladimir Marko372f10e2016-05-17 16:30:10 +01002436 using HInstruction::GetInputRecords; // Keep the const version visible.
2437 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
2438 return ArrayRef<HUserRecord<HInstruction*>>();
Vladimir Markof9f64412015-09-02 14:05:49 +01002439 }
2440
2441 private:
2442 friend class SsaBuilder;
2443};
2444
Dave Allison20dfc792014-06-16 20:44:29 -07002445template<intptr_t N>
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002446class HExpression : public HTemplateInstruction<N> {
Dave Allison20dfc792014-06-16 20:44:29 -07002447 public:
Calin Juravle154746b2015-10-06 15:46:54 +01002448 HExpression<N>(Primitive::Type type, SideEffects side_effects, uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002449 : HTemplateInstruction<N>(side_effects, dex_pc) {
2450 this->template SetPackedField<TypeField>(type);
2451 }
Dave Allison20dfc792014-06-16 20:44:29 -07002452 virtual ~HExpression() {}
2453
Vladimir Markoa1de9182016-02-25 11:37:38 +00002454 Primitive::Type GetType() const OVERRIDE {
2455 return TypeField::Decode(this->GetPackedFields());
2456 }
Dave Allison20dfc792014-06-16 20:44:29 -07002457
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002458 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002459 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2460 static constexpr size_t kFieldTypeSize =
2461 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2462 static constexpr size_t kNumberOfExpressionPackedBits = kFieldType + kFieldTypeSize;
2463 static_assert(kNumberOfExpressionPackedBits <= HInstruction::kMaxNumberOfPackedBits,
2464 "Too many packed fields.");
2465 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
Dave Allison20dfc792014-06-16 20:44:29 -07002466};
2467
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002468// Represents dex's RETURN_VOID opcode. A HReturnVoid is a control flow
2469// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002470class HReturnVoid FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002471 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002472 explicit HReturnVoid(uint32_t dex_pc = kNoDexPc)
2473 : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002475 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002476
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002477 DECLARE_INSTRUCTION(ReturnVoid);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002478
2479 private:
2480 DISALLOW_COPY_AND_ASSIGN(HReturnVoid);
2481};
2482
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002483// Represents dex's RETURN opcodes. A HReturn is a control flow
2484// instruction that branches to the exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002485class HReturn FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002486 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002487 explicit HReturn(HInstruction* value, uint32_t dex_pc = kNoDexPc)
2488 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002489 SetRawInputAt(0, value);
2490 }
2491
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002492 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002493
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002494 DECLARE_INSTRUCTION(Return);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002495
2496 private:
2497 DISALLOW_COPY_AND_ASSIGN(HReturn);
2498};
2499
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002500class HPhi FINAL : public HVariableInputSizeInstruction {
David Brazdildee58d62016-04-07 09:54:26 +00002501 public:
2502 HPhi(ArenaAllocator* arena,
2503 uint32_t reg_number,
2504 size_t number_of_inputs,
2505 Primitive::Type type,
2506 uint32_t dex_pc = kNoDexPc)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08002507 : HVariableInputSizeInstruction(
2508 SideEffects::None(),
2509 dex_pc,
2510 arena,
2511 number_of_inputs,
2512 kArenaAllocPhiInputs),
David Brazdildee58d62016-04-07 09:54:26 +00002513 reg_number_(reg_number) {
2514 SetPackedField<TypeField>(ToPhiType(type));
2515 DCHECK_NE(GetType(), Primitive::kPrimVoid);
2516 // Phis are constructed live and marked dead if conflicting or unused.
2517 // Individual steps of SsaBuilder should assume that if a phi has been
2518 // marked dead, it can be ignored and will be removed by SsaPhiElimination.
2519 SetPackedFlag<kFlagIsLive>(true);
2520 SetPackedFlag<kFlagCanBeNull>(true);
2521 }
2522
2523 // Returns a type equivalent to the given `type`, but that a `HPhi` can hold.
2524 static Primitive::Type ToPhiType(Primitive::Type type) {
2525 return Primitive::PrimitiveKind(type);
2526 }
2527
2528 bool IsCatchPhi() const { return GetBlock()->IsCatchBlock(); }
2529
David Brazdildee58d62016-04-07 09:54:26 +00002530 Primitive::Type GetType() const OVERRIDE { return GetPackedField<TypeField>(); }
2531 void SetType(Primitive::Type new_type) {
2532 // Make sure that only valid type changes occur. The following are allowed:
2533 // (1) int -> float/ref (primitive type propagation),
2534 // (2) long -> double (primitive type propagation).
2535 DCHECK(GetType() == new_type ||
2536 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimFloat) ||
2537 (GetType() == Primitive::kPrimInt && new_type == Primitive::kPrimNot) ||
2538 (GetType() == Primitive::kPrimLong && new_type == Primitive::kPrimDouble));
2539 SetPackedField<TypeField>(new_type);
2540 }
2541
2542 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
2543 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
2544
2545 uint32_t GetRegNumber() const { return reg_number_; }
2546
2547 void SetDead() { SetPackedFlag<kFlagIsLive>(false); }
2548 void SetLive() { SetPackedFlag<kFlagIsLive>(true); }
2549 bool IsDead() const { return !IsLive(); }
2550 bool IsLive() const { return GetPackedFlag<kFlagIsLive>(); }
2551
Vladimir Markoe9004912016-06-16 16:50:52 +01002552 bool IsVRegEquivalentOf(const HInstruction* other) const {
David Brazdildee58d62016-04-07 09:54:26 +00002553 return other != nullptr
2554 && other->IsPhi()
2555 && other->AsPhi()->GetBlock() == GetBlock()
2556 && other->AsPhi()->GetRegNumber() == GetRegNumber();
2557 }
2558
2559 // Returns the next equivalent phi (starting from the current one) or null if there is none.
2560 // An equivalent phi is a phi having the same dex register and type.
2561 // It assumes that phis with the same dex register are adjacent.
2562 HPhi* GetNextEquivalentPhiWithSameType() {
2563 HInstruction* next = GetNext();
2564 while (next != nullptr && next->AsPhi()->GetRegNumber() == reg_number_) {
2565 if (next->GetType() == GetType()) {
2566 return next->AsPhi();
2567 }
2568 next = next->GetNext();
2569 }
2570 return nullptr;
2571 }
2572
2573 DECLARE_INSTRUCTION(Phi);
2574
David Brazdildee58d62016-04-07 09:54:26 +00002575 private:
2576 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
2577 static constexpr size_t kFieldTypeSize =
2578 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
2579 static constexpr size_t kFlagIsLive = kFieldType + kFieldTypeSize;
2580 static constexpr size_t kFlagCanBeNull = kFlagIsLive + 1;
2581 static constexpr size_t kNumberOfPhiPackedBits = kFlagCanBeNull + 1;
2582 static_assert(kNumberOfPhiPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
2583 using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
2584
David Brazdildee58d62016-04-07 09:54:26 +00002585 const uint32_t reg_number_;
2586
2587 DISALLOW_COPY_AND_ASSIGN(HPhi);
2588};
2589
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002590// The exit instruction is the only instruction of the exit block.
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002591// Instructions aborting the method (HThrow and HReturn) must branch to the
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002592// exit block.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002593class HExit FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002594 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002595 explicit HExit(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +01002596
Alexandre Rames2ed20af2015-03-06 13:55:35 +00002597 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002598
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002599 DECLARE_INSTRUCTION(Exit);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002600
2601 private:
2602 DISALLOW_COPY_AND_ASSIGN(HExit);
2603};
2604
2605// Jumps from one block to another.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002606class HGoto FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002607 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002608 explicit HGoto(uint32_t dex_pc = kNoDexPc) : HTemplateInstruction(SideEffects::None(), dex_pc) {}
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002609
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002610 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002611
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002612 HBasicBlock* GetSuccessor() const {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002613 return GetBlock()->GetSingleSuccessor();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002614 }
2615
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002616 DECLARE_INSTRUCTION(Goto);
Nicolas Geoffray818f2102014-02-18 16:43:35 +00002617
2618 private:
2619 DISALLOW_COPY_AND_ASSIGN(HGoto);
2620};
2621
Roland Levillain9867bc72015-08-05 10:21:34 +01002622class HConstant : public HExpression<0> {
2623 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002624 explicit HConstant(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2625 : HExpression(type, SideEffects::None(), dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002626
2627 bool CanBeMoved() const OVERRIDE { return true; }
2628
Roland Levillain1a653882016-03-18 18:05:57 +00002629 // Is this constant -1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002630 virtual bool IsMinusOne() const { return false; }
Roland Levillain1a653882016-03-18 18:05:57 +00002631 // Is this constant 0 in the arithmetic sense?
2632 virtual bool IsArithmeticZero() const { return false; }
2633 // Is this constant a 0-bit pattern?
2634 virtual bool IsZeroBitPattern() const { return false; }
2635 // Is this constant 1 in the arithmetic sense?
Roland Levillain9867bc72015-08-05 10:21:34 +01002636 virtual bool IsOne() const { return false; }
2637
David Brazdil77a48ae2015-09-15 12:34:04 +00002638 virtual uint64_t GetValueAsUint64() const = 0;
2639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002640 DECLARE_ABSTRACT_INSTRUCTION(Constant);
Roland Levillain9867bc72015-08-05 10:21:34 +01002641
2642 private:
2643 DISALLOW_COPY_AND_ASSIGN(HConstant);
2644};
2645
Vladimir Markofcb503c2016-05-18 12:48:17 +01002646class HNullConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002647 public:
Vladimir Marko372f10e2016-05-17 16:30:10 +01002648 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01002649 return true;
2650 }
2651
David Brazdil77a48ae2015-09-15 12:34:04 +00002652 uint64_t GetValueAsUint64() const OVERRIDE { return 0; }
2653
Roland Levillain9867bc72015-08-05 10:21:34 +01002654 size_t ComputeHashCode() const OVERRIDE { return 0; }
2655
Roland Levillain1a653882016-03-18 18:05:57 +00002656 // The null constant representation is a 0-bit pattern.
2657 virtual bool IsZeroBitPattern() const { return true; }
2658
Roland Levillain9867bc72015-08-05 10:21:34 +01002659 DECLARE_INSTRUCTION(NullConstant);
2660
2661 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002662 explicit HNullConstant(uint32_t dex_pc = kNoDexPc) : HConstant(Primitive::kPrimNot, dex_pc) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002663
2664 friend class HGraph;
2665 DISALLOW_COPY_AND_ASSIGN(HNullConstant);
2666};
2667
2668// Constants of the type int. Those can be from Dex instructions, or
2669// synthesized (for example with the if-eqz instruction).
Vladimir Markofcb503c2016-05-18 12:48:17 +01002670class HIntConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002671 public:
2672 int32_t GetValue() const { return value_; }
2673
David Brazdil9f389d42015-10-01 14:32:56 +01002674 uint64_t GetValueAsUint64() const OVERRIDE {
2675 return static_cast<uint64_t>(static_cast<uint32_t>(value_));
2676 }
David Brazdil77a48ae2015-09-15 12:34:04 +00002677
Vladimir Marko372f10e2016-05-17 16:30:10 +01002678 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002679 DCHECK(other->IsIntConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002680 return other->AsIntConstant()->value_ == value_;
2681 }
2682
2683 size_t ComputeHashCode() const OVERRIDE { return GetValue(); }
2684
2685 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002686 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2687 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002688 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2689
Roland Levillain1a653882016-03-18 18:05:57 +00002690 // Integer constants are used to encode Boolean values as well,
2691 // where 1 means true and 0 means false.
2692 bool IsTrue() const { return GetValue() == 1; }
2693 bool IsFalse() const { return GetValue() == 0; }
2694
Roland Levillain9867bc72015-08-05 10:21:34 +01002695 DECLARE_INSTRUCTION(IntConstant);
2696
2697 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002698 explicit HIntConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2699 : HConstant(Primitive::kPrimInt, dex_pc), value_(value) {}
2700 explicit HIntConstant(bool value, uint32_t dex_pc = kNoDexPc)
2701 : HConstant(Primitive::kPrimInt, dex_pc), value_(value ? 1 : 0) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002702
2703 const int32_t value_;
2704
2705 friend class HGraph;
2706 ART_FRIEND_TEST(GraphTest, InsertInstructionBefore);
2707 ART_FRIEND_TYPED_TEST(ParallelMoveTest, ConstantLast);
2708 DISALLOW_COPY_AND_ASSIGN(HIntConstant);
2709};
2710
Vladimir Markofcb503c2016-05-18 12:48:17 +01002711class HLongConstant FINAL : public HConstant {
Roland Levillain9867bc72015-08-05 10:21:34 +01002712 public:
2713 int64_t GetValue() const { return value_; }
2714
David Brazdil77a48ae2015-09-15 12:34:04 +00002715 uint64_t GetValueAsUint64() const OVERRIDE { return value_; }
2716
Vladimir Marko372f10e2016-05-17 16:30:10 +01002717 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002718 DCHECK(other->IsLongConstant()) << other->DebugName();
Roland Levillain9867bc72015-08-05 10:21:34 +01002719 return other->AsLongConstant()->value_ == value_;
2720 }
2721
2722 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2723
2724 bool IsMinusOne() const OVERRIDE { return GetValue() == -1; }
Roland Levillain1a653882016-03-18 18:05:57 +00002725 bool IsArithmeticZero() const OVERRIDE { return GetValue() == 0; }
2726 bool IsZeroBitPattern() const OVERRIDE { return GetValue() == 0; }
Roland Levillain9867bc72015-08-05 10:21:34 +01002727 bool IsOne() const OVERRIDE { return GetValue() == 1; }
2728
2729 DECLARE_INSTRUCTION(LongConstant);
2730
2731 private:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002732 explicit HLongConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2733 : HConstant(Primitive::kPrimLong, dex_pc), value_(value) {}
Roland Levillain9867bc72015-08-05 10:21:34 +01002734
2735 const int64_t value_;
2736
2737 friend class HGraph;
2738 DISALLOW_COPY_AND_ASSIGN(HLongConstant);
2739};
Dave Allison20dfc792014-06-16 20:44:29 -07002740
Vladimir Markofcb503c2016-05-18 12:48:17 +01002741class HFloatConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002742 public:
2743 float GetValue() const { return value_; }
2744
2745 uint64_t GetValueAsUint64() const OVERRIDE {
2746 return static_cast<uint64_t>(bit_cast<uint32_t, float>(value_));
2747 }
2748
Vladimir Marko372f10e2016-05-17 16:30:10 +01002749 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002750 DCHECK(other->IsFloatConstant()) << other->DebugName();
2751 return other->AsFloatConstant()->GetValueAsUint64() == GetValueAsUint64();
2752 }
2753
2754 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2755
2756 bool IsMinusOne() const OVERRIDE {
2757 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>((-1.0f));
2758 }
Roland Levillain1a653882016-03-18 18:05:57 +00002759 bool IsArithmeticZero() const OVERRIDE {
2760 return std::fpclassify(value_) == FP_ZERO;
2761 }
2762 bool IsArithmeticPositiveZero() const {
2763 return IsArithmeticZero() && !std::signbit(value_);
2764 }
2765 bool IsArithmeticNegativeZero() const {
2766 return IsArithmeticZero() && std::signbit(value_);
2767 }
2768 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002769 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(0.0f);
Roland Levillain31dd3d62016-02-16 12:21:02 +00002770 }
2771 bool IsOne() const OVERRIDE {
2772 return bit_cast<uint32_t, float>(value_) == bit_cast<uint32_t, float>(1.0f);
2773 }
2774 bool IsNaN() const {
2775 return std::isnan(value_);
2776 }
2777
2778 DECLARE_INSTRUCTION(FloatConstant);
2779
2780 private:
2781 explicit HFloatConstant(float value, uint32_t dex_pc = kNoDexPc)
2782 : HConstant(Primitive::kPrimFloat, dex_pc), value_(value) {}
2783 explicit HFloatConstant(int32_t value, uint32_t dex_pc = kNoDexPc)
2784 : HConstant(Primitive::kPrimFloat, dex_pc), value_(bit_cast<float, int32_t>(value)) {}
2785
2786 const float value_;
2787
2788 // Only the SsaBuilder and HGraph can create floating-point constants.
2789 friend class SsaBuilder;
2790 friend class HGraph;
2791 DISALLOW_COPY_AND_ASSIGN(HFloatConstant);
2792};
2793
Vladimir Markofcb503c2016-05-18 12:48:17 +01002794class HDoubleConstant FINAL : public HConstant {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002795 public:
2796 double GetValue() const { return value_; }
2797
2798 uint64_t GetValueAsUint64() const OVERRIDE { return bit_cast<uint64_t, double>(value_); }
2799
Vladimir Marko372f10e2016-05-17 16:30:10 +01002800 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00002801 DCHECK(other->IsDoubleConstant()) << other->DebugName();
2802 return other->AsDoubleConstant()->GetValueAsUint64() == GetValueAsUint64();
2803 }
2804
2805 size_t ComputeHashCode() const OVERRIDE { return static_cast<size_t>(GetValue()); }
2806
2807 bool IsMinusOne() const OVERRIDE {
2808 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((-1.0));
2809 }
Roland Levillain1a653882016-03-18 18:05:57 +00002810 bool IsArithmeticZero() const OVERRIDE {
2811 return std::fpclassify(value_) == FP_ZERO;
2812 }
2813 bool IsArithmeticPositiveZero() const {
2814 return IsArithmeticZero() && !std::signbit(value_);
2815 }
2816 bool IsArithmeticNegativeZero() const {
2817 return IsArithmeticZero() && std::signbit(value_);
2818 }
2819 bool IsZeroBitPattern() const OVERRIDE {
Nicolas Geoffray949e54d2016-03-15 16:23:04 +00002820 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>((0.0));
Roland Levillain31dd3d62016-02-16 12:21:02 +00002821 }
2822 bool IsOne() const OVERRIDE {
2823 return bit_cast<uint64_t, double>(value_) == bit_cast<uint64_t, double>(1.0);
2824 }
2825 bool IsNaN() const {
2826 return std::isnan(value_);
2827 }
2828
2829 DECLARE_INSTRUCTION(DoubleConstant);
2830
2831 private:
2832 explicit HDoubleConstant(double value, uint32_t dex_pc = kNoDexPc)
2833 : HConstant(Primitive::kPrimDouble, dex_pc), value_(value) {}
2834 explicit HDoubleConstant(int64_t value, uint32_t dex_pc = kNoDexPc)
2835 : HConstant(Primitive::kPrimDouble, dex_pc), value_(bit_cast<double, int64_t>(value)) {}
2836
2837 const double value_;
2838
2839 // Only the SsaBuilder and HGraph can create floating-point constants.
2840 friend class SsaBuilder;
2841 friend class HGraph;
2842 DISALLOW_COPY_AND_ASSIGN(HDoubleConstant);
2843};
2844
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002845// Conditional branch. A block ending with an HIf instruction must have
2846// two successors.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002847class HIf FINAL : public HTemplateInstruction<1> {
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002848 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002849 explicit HIf(HInstruction* input, uint32_t dex_pc = kNoDexPc)
2850 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002851 SetRawInputAt(0, input);
2852 }
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002853
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00002854 bool IsControlFlow() const OVERRIDE { return true; }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01002855
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002856 HBasicBlock* IfTrueSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002857 return GetBlock()->GetSuccessors()[0];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002858 }
2859
2860 HBasicBlock* IfFalseSuccessor() const {
Vladimir Markoec7802a2015-10-01 20:57:57 +01002861 return GetBlock()->GetSuccessors()[1];
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002862 }
2863
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002864 DECLARE_INSTRUCTION(If);
Nicolas Geoffraybe9a92a2014-02-25 14:22:56 +00002865
2866 private:
2867 DISALLOW_COPY_AND_ASSIGN(HIf);
2868};
2869
David Brazdilfc6a86a2015-06-26 10:33:45 +00002870
2871// Abstract instruction which marks the beginning and/or end of a try block and
2872// links it to the respective exception handlers. Behaves the same as a Goto in
2873// non-exceptional control flow.
2874// Normal-flow successor is stored at index zero, exception handlers under
2875// higher indices in no particular order.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002876class HTryBoundary FINAL : public HTemplateInstruction<0> {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002877 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002878 enum class BoundaryKind {
David Brazdil56e1acc2015-06-30 15:41:36 +01002879 kEntry,
2880 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00002881 kLast = kExit
David Brazdil56e1acc2015-06-30 15:41:36 +01002882 };
2883
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002884 explicit HTryBoundary(BoundaryKind kind, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00002885 : HTemplateInstruction(SideEffects::None(), dex_pc) {
2886 SetPackedField<BoundaryKindField>(kind);
2887 }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002888
2889 bool IsControlFlow() const OVERRIDE { return true; }
2890
2891 // Returns the block's non-exceptional successor (index zero).
Vladimir Markoec7802a2015-10-01 20:57:57 +01002892 HBasicBlock* GetNormalFlowSuccessor() const { return GetBlock()->GetSuccessors()[0]; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002893
David Brazdild26a4112015-11-10 11:07:31 +00002894 ArrayRef<HBasicBlock* const> GetExceptionHandlers() const {
2895 return ArrayRef<HBasicBlock* const>(GetBlock()->GetSuccessors()).SubArray(1u);
2896 }
2897
David Brazdilfc6a86a2015-06-26 10:33:45 +00002898 // Returns whether `handler` is among its exception handlers (non-zero index
2899 // successors).
David Brazdilffee3d32015-07-06 11:48:53 +01002900 bool HasExceptionHandler(const HBasicBlock& handler) const {
2901 DCHECK(handler.IsCatchBlock());
Vladimir Marko60584552015-09-03 13:35:12 +00002902 return GetBlock()->HasSuccessor(&handler, 1u /* Skip first successor. */);
David Brazdilfc6a86a2015-06-26 10:33:45 +00002903 }
2904
2905 // If not present already, adds `handler` to its block's list of exception
2906 // handlers.
2907 void AddExceptionHandler(HBasicBlock* handler) {
David Brazdilffee3d32015-07-06 11:48:53 +01002908 if (!HasExceptionHandler(*handler)) {
David Brazdilfc6a86a2015-06-26 10:33:45 +00002909 GetBlock()->AddSuccessor(handler);
2910 }
2911 }
2912
Vladimir Markoa1de9182016-02-25 11:37:38 +00002913 BoundaryKind GetBoundaryKind() const { return GetPackedField<BoundaryKindField>(); }
2914 bool IsEntry() const { return GetBoundaryKind() == BoundaryKind::kEntry; }
David Brazdilfc6a86a2015-06-26 10:33:45 +00002915
David Brazdilffee3d32015-07-06 11:48:53 +01002916 bool HasSameExceptionHandlersAs(const HTryBoundary& other) const;
2917
David Brazdilfc6a86a2015-06-26 10:33:45 +00002918 DECLARE_INSTRUCTION(TryBoundary);
2919
2920 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00002921 static constexpr size_t kFieldBoundaryKind = kNumberOfGenericPackedBits;
2922 static constexpr size_t kFieldBoundaryKindSize =
2923 MinimumBitsToStore(static_cast<size_t>(BoundaryKind::kLast));
2924 static constexpr size_t kNumberOfTryBoundaryPackedBits =
2925 kFieldBoundaryKind + kFieldBoundaryKindSize;
2926 static_assert(kNumberOfTryBoundaryPackedBits <= kMaxNumberOfPackedBits,
2927 "Too many packed fields.");
2928 using BoundaryKindField = BitField<BoundaryKind, kFieldBoundaryKind, kFieldBoundaryKindSize>;
David Brazdilfc6a86a2015-06-26 10:33:45 +00002929
2930 DISALLOW_COPY_AND_ASSIGN(HTryBoundary);
2931};
2932
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002933// Deoptimize to interpreter, upon checking a condition.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002934class HDeoptimize FINAL : public HTemplateInstruction<1> {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002935 public:
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002936 // We set CanTriggerGC to prevent any intermediate address to be live
2937 // at the point of the `HDeoptimize`.
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002938 HDeoptimize(HInstruction* cond, uint32_t dex_pc)
Nicolas Geoffray1cde0582016-01-13 13:56:20 +00002939 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002940 SetRawInputAt(0, cond);
2941 }
2942
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002943 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01002944 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray73be1e82015-09-17 15:22:56 +01002945 return true;
2946 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002947 bool NeedsEnvironment() const OVERRIDE { return true; }
2948 bool CanThrow() const OVERRIDE { return true; }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002949
2950 DECLARE_INSTRUCTION(Deoptimize);
2951
2952 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002953 DISALLOW_COPY_AND_ASSIGN(HDeoptimize);
2954};
2955
Mingyao Yang063fc772016-08-02 11:02:54 -07002956// Represents a should_deoptimize flag. Currently used for CHA-based devirtualization.
2957// The compiled code checks this flag value in a guard before devirtualized call and
2958// if it's true, starts to do deoptimization.
2959// It has a 4-byte slot on stack.
2960// TODO: allocate a register for this flag.
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002961class HShouldDeoptimizeFlag FINAL : public HVariableInputSizeInstruction {
Mingyao Yang063fc772016-08-02 11:02:54 -07002962 public:
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002963 // CHA guards are only optimized in a separate pass and it has no side effects
2964 // with regard to other passes.
2965 HShouldDeoptimizeFlag(ArenaAllocator* arena, uint32_t dex_pc)
2966 : HVariableInputSizeInstruction(SideEffects::None(), dex_pc, arena, 0, kArenaAllocCHA) {
Mingyao Yang063fc772016-08-02 11:02:54 -07002967 }
2968
Mingyao Yangb0b051a2016-11-17 09:04:53 -08002969 Primitive::Type GetType() const OVERRIDE { return Primitive::kPrimInt; }
2970
2971 // We do all CHA guard elimination/motion in a single pass, after which there is no
2972 // further guard elimination/motion since a guard might have been used for justification
2973 // of the elimination of another guard. Therefore, we pretend this guard cannot be moved
2974 // to avoid other optimizations trying to move it.
Mingyao Yang063fc772016-08-02 11:02:54 -07002975 bool CanBeMoved() const OVERRIDE { return false; }
2976
2977 DECLARE_INSTRUCTION(ShouldDeoptimizeFlag);
2978
2979 private:
2980 DISALLOW_COPY_AND_ASSIGN(HShouldDeoptimizeFlag);
2981};
2982
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002983// Represents the ArtMethod that was passed as a first argument to
2984// the method. It is used by instructions that depend on it, like
2985// instructions that work with the dex cache.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002986class HCurrentMethod FINAL : public HExpression<0> {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002987 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06002988 explicit HCurrentMethod(Primitive::Type type, uint32_t dex_pc = kNoDexPc)
2989 : HExpression(type, SideEffects::None(), dex_pc) {}
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01002990
2991 DECLARE_INSTRUCTION(CurrentMethod);
2992
2993 private:
2994 DISALLOW_COPY_AND_ASSIGN(HCurrentMethod);
2995};
2996
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00002997// Fetches an ArtMethod from the virtual table or the interface method table
2998// of a class.
Vladimir Markofcb503c2016-05-18 12:48:17 +01002999class HClassTableGet FINAL : public HExpression<1> {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003000 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003001 enum class TableKind {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003002 kVTable,
3003 kIMTable,
Vladimir Markoa1de9182016-02-25 11:37:38 +00003004 kLast = kIMTable
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003005 };
3006 HClassTableGet(HInstruction* cls,
3007 Primitive::Type type,
3008 TableKind kind,
3009 size_t index,
3010 uint32_t dex_pc)
3011 : HExpression(type, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003012 index_(index) {
3013 SetPackedField<TableKindField>(kind);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003014 SetRawInputAt(0, cls);
3015 }
3016
3017 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003018 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003019 return other->AsClassTableGet()->GetIndex() == index_ &&
Vladimir Markoa1de9182016-02-25 11:37:38 +00003020 other->AsClassTableGet()->GetPackedFields() == GetPackedFields();
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003021 }
3022
Vladimir Markoa1de9182016-02-25 11:37:38 +00003023 TableKind GetTableKind() const { return GetPackedField<TableKindField>(); }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003024 size_t GetIndex() const { return index_; }
3025
3026 DECLARE_INSTRUCTION(ClassTableGet);
3027
3028 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003029 static constexpr size_t kFieldTableKind = kNumberOfExpressionPackedBits;
3030 static constexpr size_t kFieldTableKindSize =
3031 MinimumBitsToStore(static_cast<size_t>(TableKind::kLast));
3032 static constexpr size_t kNumberOfClassTableGetPackedBits = kFieldTableKind + kFieldTableKindSize;
3033 static_assert(kNumberOfClassTableGetPackedBits <= kMaxNumberOfPackedBits,
3034 "Too many packed fields.");
3035 using TableKindField = BitField<TableKind, kFieldTableKind, kFieldTableKind>;
3036
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003037 // The index of the ArtMethod in the table.
3038 const size_t index_;
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003039
3040 DISALLOW_COPY_AND_ASSIGN(HClassTableGet);
3041};
3042
Mark Mendellfe57faa2015-09-18 09:26:15 -04003043// PackedSwitch (jump table). A block ending with a PackedSwitch instruction will
3044// have one successor for each entry in the switch table, and the final successor
3045// will be the block containing the next Dex opcode.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003046class HPackedSwitch FINAL : public HTemplateInstruction<1> {
Mark Mendellfe57faa2015-09-18 09:26:15 -04003047 public:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003048 HPackedSwitch(int32_t start_value,
3049 uint32_t num_entries,
3050 HInstruction* input,
Mark Mendellfe57faa2015-09-18 09:26:15 -04003051 uint32_t dex_pc = kNoDexPc)
3052 : HTemplateInstruction(SideEffects::None(), dex_pc),
3053 start_value_(start_value),
3054 num_entries_(num_entries) {
3055 SetRawInputAt(0, input);
3056 }
3057
3058 bool IsControlFlow() const OVERRIDE { return true; }
3059
3060 int32_t GetStartValue() const { return start_value_; }
3061
Vladimir Marko211c2112015-09-24 16:52:33 +01003062 uint32_t GetNumEntries() const { return num_entries_; }
Mark Mendellfe57faa2015-09-18 09:26:15 -04003063
3064 HBasicBlock* GetDefaultBlock() const {
3065 // Last entry is the default block.
Vladimir Markoec7802a2015-10-01 20:57:57 +01003066 return GetBlock()->GetSuccessors()[num_entries_];
Mark Mendellfe57faa2015-09-18 09:26:15 -04003067 }
3068 DECLARE_INSTRUCTION(PackedSwitch);
3069
3070 private:
Mark Mendell3b9f3042015-09-24 08:43:40 -04003071 const int32_t start_value_;
3072 const uint32_t num_entries_;
Mark Mendellfe57faa2015-09-18 09:26:15 -04003073
3074 DISALLOW_COPY_AND_ASSIGN(HPackedSwitch);
3075};
3076
Roland Levillain88cb1752014-10-20 16:36:47 +01003077class HUnaryOperation : public HExpression<1> {
3078 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003079 HUnaryOperation(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
3080 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillain88cb1752014-10-20 16:36:47 +01003081 SetRawInputAt(0, input);
3082 }
3083
3084 HInstruction* GetInput() const { return InputAt(0); }
3085 Primitive::Type GetResultType() const { return GetType(); }
3086
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003087 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003088 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003089 return true;
3090 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003091
Roland Levillain31dd3d62016-02-16 12:21:02 +00003092 // Try to statically evaluate `this` and return a HConstant
3093 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003094 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003095 HConstant* TryStaticEvaluation() const;
3096
3097 // Apply this operation to `x`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003098 virtual HConstant* Evaluate(HIntConstant* x) const = 0;
3099 virtual HConstant* Evaluate(HLongConstant* x) const = 0;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003100 virtual HConstant* Evaluate(HFloatConstant* x) const = 0;
3101 virtual HConstant* Evaluate(HDoubleConstant* x) const = 0;
Roland Levillain9240d6a2014-10-20 16:47:04 +01003102
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003103 DECLARE_ABSTRACT_INSTRUCTION(UnaryOperation);
Roland Levillain88cb1752014-10-20 16:36:47 +01003104
3105 private:
3106 DISALLOW_COPY_AND_ASSIGN(HUnaryOperation);
3107};
3108
Dave Allison20dfc792014-06-16 20:44:29 -07003109class HBinaryOperation : public HExpression<2> {
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003110 public:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003111 HBinaryOperation(Primitive::Type result_type,
3112 HInstruction* left,
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003113 HInstruction* right,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003114 SideEffects side_effects = SideEffects::None(),
3115 uint32_t dex_pc = kNoDexPc)
3116 : HExpression(result_type, side_effects, dex_pc) {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003117 SetRawInputAt(0, left);
3118 SetRawInputAt(1, right);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003119 }
3120
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003121 HInstruction* GetLeft() const { return InputAt(0); }
3122 HInstruction* GetRight() const { return InputAt(1); }
Dave Allison20dfc792014-06-16 20:44:29 -07003123 Primitive::Type GetResultType() const { return GetType(); }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003124
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003125 virtual bool IsCommutative() const { return false; }
3126
3127 // Put constant on the right.
3128 // Returns whether order is changed.
3129 bool OrderInputsWithConstantOnTheRight() {
3130 HInstruction* left = InputAt(0);
3131 HInstruction* right = InputAt(1);
3132 if (left->IsConstant() && !right->IsConstant()) {
3133 ReplaceInput(right, 0);
3134 ReplaceInput(left, 1);
3135 return true;
3136 }
3137 return false;
3138 }
3139
3140 // Order inputs by instruction id, but favor constant on the right side.
3141 // This helps GVN for commutative ops.
3142 void OrderInputs() {
3143 DCHECK(IsCommutative());
3144 HInstruction* left = InputAt(0);
3145 HInstruction* right = InputAt(1);
3146 if (left == right || (!left->IsConstant() && right->IsConstant())) {
3147 return;
3148 }
3149 if (OrderInputsWithConstantOnTheRight()) {
3150 return;
3151 }
3152 // Order according to instruction id.
3153 if (left->GetId() > right->GetId()) {
3154 ReplaceInput(right, 0);
3155 ReplaceInput(left, 1);
3156 }
3157 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003158
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003159 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01003160 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003161 return true;
3162 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01003163
Roland Levillain31dd3d62016-02-16 12:21:02 +00003164 // Try to statically evaluate `this` and return a HConstant
3165 // containing the result of this evaluation. If `this` cannot
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003166 // be evaluated as a constant, return null.
Roland Levillain9240d6a2014-10-20 16:47:04 +01003167 HConstant* TryStaticEvaluation() const;
Roland Levillain556c3d12014-09-18 15:25:07 +01003168
3169 // Apply this operation to `x` and `y`.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003170 virtual HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3171 HNullConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003172 LOG(FATAL) << DebugName() << " is not defined for the (null, null) case.";
3173 UNREACHABLE();
Roland Levillain31dd3d62016-02-16 12:21:02 +00003174 }
Roland Levillain9867bc72015-08-05 10:21:34 +01003175 virtual HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const = 0;
3176 virtual HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const = 0;
Roland Levillain9867bc72015-08-05 10:21:34 +01003177 virtual HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED,
3178 HIntConstant* y ATTRIBUTE_UNUSED) const {
Roland Levillaine53bd812016-02-24 14:54:18 +00003179 LOG(FATAL) << DebugName() << " is not defined for the (long, int) case.";
3180 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01003181 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003182 virtual HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const = 0;
3183 virtual HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const = 0;
Roland Levillain556c3d12014-09-18 15:25:07 +01003184
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003185 // Returns an input that can legally be used as the right input and is
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003186 // constant, or null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003187 HConstant* GetConstantRight() const;
3188
3189 // If `GetConstantRight()` returns one of the input, this returns the other
Mathieu Chartier2cebb242015-04-21 16:50:40 -07003190 // one. Otherwise it returns null.
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00003191 HInstruction* GetLeastConstantLeft() const;
3192
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003193 DECLARE_ABSTRACT_INSTRUCTION(BinaryOperation);
Roland Levillainccc07a92014-09-16 14:48:16 +01003194
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003195 private:
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003196 DISALLOW_COPY_AND_ASSIGN(HBinaryOperation);
3197};
3198
Mark Mendellc4701932015-04-10 13:18:51 -04003199// The comparison bias applies for floating point operations and indicates how NaN
3200// comparisons are treated:
Roland Levillain4fa13f62015-07-06 18:11:54 +01003201enum class ComparisonBias {
Mark Mendellc4701932015-04-10 13:18:51 -04003202 kNoBias, // bias is not applicable (i.e. for long operation)
3203 kGtBias, // return 1 for NaN comparisons
3204 kLtBias, // return -1 for NaN comparisons
Vladimir Markoa1de9182016-02-25 11:37:38 +00003205 kLast = kLtBias
Mark Mendellc4701932015-04-10 13:18:51 -04003206};
3207
Roland Levillain31dd3d62016-02-16 12:21:02 +00003208std::ostream& operator<<(std::ostream& os, const ComparisonBias& rhs);
3209
Dave Allison20dfc792014-06-16 20:44:29 -07003210class HCondition : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003211 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003212 HCondition(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00003213 : HBinaryOperation(Primitive::kPrimBoolean, first, second, SideEffects::None(), dex_pc) {
3214 SetPackedField<ComparisonBiasField>(ComparisonBias::kNoBias);
3215 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003216
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003217 // For code generation purposes, returns whether this instruction is just before
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003218 // `instruction`, and disregard moves in between.
3219 bool IsBeforeWhenDisregardMoves(HInstruction* instruction) const;
Nicolas Geoffray18efde52014-09-22 15:51:11 +01003220
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003221 DECLARE_ABSTRACT_INSTRUCTION(Condition);
Dave Allison20dfc792014-06-16 20:44:29 -07003222
3223 virtual IfCondition GetCondition() const = 0;
3224
Mark Mendellc4701932015-04-10 13:18:51 -04003225 virtual IfCondition GetOppositeCondition() const = 0;
3226
Vladimir Markoa1de9182016-02-25 11:37:38 +00003227 bool IsGtBias() const { return GetBias() == ComparisonBias::kGtBias; }
Anton Shaminbdd79352016-02-15 12:48:36 +06003228 bool IsLtBias() const { return GetBias() == ComparisonBias::kLtBias; }
3229
Vladimir Markoa1de9182016-02-25 11:37:38 +00003230 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
3231 void SetBias(ComparisonBias bias) { SetPackedField<ComparisonBiasField>(bias); }
Mark Mendellc4701932015-04-10 13:18:51 -04003232
Vladimir Marko372f10e2016-05-17 16:30:10 +01003233 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003234 return GetPackedFields() == other->AsCondition()->GetPackedFields();
Mark Mendellc4701932015-04-10 13:18:51 -04003235 }
3236
Roland Levillain4fa13f62015-07-06 18:11:54 +01003237 bool IsFPConditionTrueIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003238 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003239 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003240 if (if_cond == kCondNE) {
3241 return true;
3242 } else if (if_cond == kCondEQ) {
3243 return false;
3244 }
3245 return ((if_cond == kCondGT) || (if_cond == kCondGE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003246 }
3247
3248 bool IsFPConditionFalseIfNaN() const {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003249 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003250 IfCondition if_cond = GetCondition();
Anton Shaminbdd79352016-02-15 12:48:36 +06003251 if (if_cond == kCondEQ) {
3252 return true;
3253 } else if (if_cond == kCondNE) {
3254 return false;
3255 }
3256 return ((if_cond == kCondLT) || (if_cond == kCondLE)) && IsGtBias();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003257 }
3258
Roland Levillain31dd3d62016-02-16 12:21:02 +00003259 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003260 // Needed if we merge a HCompare into a HCondition.
3261 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3262 static constexpr size_t kFieldComparisonBiasSize =
3263 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3264 static constexpr size_t kNumberOfConditionPackedBits =
3265 kFieldComparisonBias + kFieldComparisonBiasSize;
3266 static_assert(kNumberOfConditionPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3267 using ComparisonBiasField =
3268 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3269
Roland Levillain31dd3d62016-02-16 12:21:02 +00003270 template <typename T>
3271 int32_t Compare(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3272
3273 template <typename T>
3274 int32_t CompareFP(T x, T y) const {
3275 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3276 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3277 // Handle the bias.
3278 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compare(x, y);
3279 }
3280
3281 // Return an integer constant containing the result of a condition evaluated at compile time.
3282 HIntConstant* MakeConstantCondition(bool value, uint32_t dex_pc) const {
3283 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3284 }
3285
Dave Allison20dfc792014-06-16 20:44:29 -07003286 private:
3287 DISALLOW_COPY_AND_ASSIGN(HCondition);
3288};
3289
3290// Instruction to check if two inputs are equal to each other.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003291class HEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003292 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003293 HEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3294 : HCondition(first, second, dex_pc) {}
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003295
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003296 bool IsCommutative() const OVERRIDE { return true; }
3297
Vladimir Marko9e23df52015-11-10 17:14:35 +00003298 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3299 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003300 return MakeConstantCondition(true, GetDexPc());
3301 }
3302 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3303 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3304 }
3305 // In the following Evaluate methods, a HCompare instruction has
3306 // been merged into this HEqual instruction; evaluate it as
3307 // `Compare(x, y) == 0`.
3308 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3309 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0),
3310 GetDexPc());
3311 }
3312 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3313 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3314 }
3315 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3316 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003317 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003318
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01003319 DECLARE_INSTRUCTION(Equal);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003320
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003321 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003322 return kCondEQ;
3323 }
3324
Mark Mendellc4701932015-04-10 13:18:51 -04003325 IfCondition GetOppositeCondition() const OVERRIDE {
3326 return kCondNE;
3327 }
3328
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003329 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003330 template <typename T> static bool Compute(T x, T y) { return x == y; }
Aart Bike9f37602015-10-09 11:15:55 -07003331
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003332 DISALLOW_COPY_AND_ASSIGN(HEqual);
3333};
3334
Vladimir Markofcb503c2016-05-18 12:48:17 +01003335class HNotEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003336 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003337 HNotEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3338 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003339
Mingyao Yangdc5ac732015-02-25 11:28:05 -08003340 bool IsCommutative() const OVERRIDE { return true; }
3341
Vladimir Marko9e23df52015-11-10 17:14:35 +00003342 HConstant* Evaluate(HNullConstant* x ATTRIBUTE_UNUSED,
3343 HNullConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003344 return MakeConstantCondition(false, GetDexPc());
3345 }
3346 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
3347 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3348 }
3349 // In the following Evaluate methods, a HCompare instruction has
3350 // been merged into this HNotEqual instruction; evaluate it as
3351 // `Compare(x, y) != 0`.
3352 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
3353 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3354 }
3355 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3356 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3357 }
3358 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3359 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Vladimir Marko9e23df52015-11-10 17:14:35 +00003360 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003361
Dave Allison20dfc792014-06-16 20:44:29 -07003362 DECLARE_INSTRUCTION(NotEqual);
3363
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003364 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003365 return kCondNE;
3366 }
3367
Mark Mendellc4701932015-04-10 13:18:51 -04003368 IfCondition GetOppositeCondition() const OVERRIDE {
3369 return kCondEQ;
3370 }
3371
Dave Allison20dfc792014-06-16 20:44:29 -07003372 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003373 template <typename T> static bool Compute(T x, T y) { return x != y; }
Aart Bike9f37602015-10-09 11:15:55 -07003374
Dave Allison20dfc792014-06-16 20:44:29 -07003375 DISALLOW_COPY_AND_ASSIGN(HNotEqual);
3376};
3377
Vladimir Markofcb503c2016-05-18 12:48:17 +01003378class HLessThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003379 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003380 HLessThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3381 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003382
Roland Levillain9867bc72015-08-05 10:21:34 +01003383 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003384 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003385 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003386 // In the following Evaluate methods, a HCompare instruction has
3387 // been merged into this HLessThan instruction; evaluate it as
3388 // `Compare(x, y) < 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003389 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003390 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3391 }
3392 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3393 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3394 }
3395 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3396 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003397 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003398
Dave Allison20dfc792014-06-16 20:44:29 -07003399 DECLARE_INSTRUCTION(LessThan);
3400
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003401 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003402 return kCondLT;
3403 }
3404
Mark Mendellc4701932015-04-10 13:18:51 -04003405 IfCondition GetOppositeCondition() const OVERRIDE {
3406 return kCondGE;
3407 }
3408
Dave Allison20dfc792014-06-16 20:44:29 -07003409 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003410 template <typename T> static bool Compute(T x, T y) { return x < y; }
Aart Bike9f37602015-10-09 11:15:55 -07003411
Dave Allison20dfc792014-06-16 20:44:29 -07003412 DISALLOW_COPY_AND_ASSIGN(HLessThan);
3413};
3414
Vladimir Markofcb503c2016-05-18 12:48:17 +01003415class HLessThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003416 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003417 HLessThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3418 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003419
Roland Levillain9867bc72015-08-05 10:21:34 +01003420 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003421 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003422 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003423 // In the following Evaluate methods, a HCompare instruction has
3424 // been merged into this HLessThanOrEqual instruction; evaluate it as
3425 // `Compare(x, y) <= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003426 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003427 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3428 }
3429 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3430 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3431 }
3432 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3433 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003434 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003435
Dave Allison20dfc792014-06-16 20:44:29 -07003436 DECLARE_INSTRUCTION(LessThanOrEqual);
3437
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003438 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003439 return kCondLE;
3440 }
3441
Mark Mendellc4701932015-04-10 13:18:51 -04003442 IfCondition GetOppositeCondition() const OVERRIDE {
3443 return kCondGT;
3444 }
3445
Dave Allison20dfc792014-06-16 20:44:29 -07003446 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003447 template <typename T> static bool Compute(T x, T y) { return x <= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003448
Dave Allison20dfc792014-06-16 20:44:29 -07003449 DISALLOW_COPY_AND_ASSIGN(HLessThanOrEqual);
3450};
3451
Vladimir Markofcb503c2016-05-18 12:48:17 +01003452class HGreaterThan FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003453 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003454 HGreaterThan(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3455 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003456
Roland Levillain9867bc72015-08-05 10:21:34 +01003457 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003458 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003459 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003460 // In the following Evaluate methods, a HCompare instruction has
3461 // been merged into this HGreaterThan instruction; evaluate it as
3462 // `Compare(x, y) > 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003463 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003464 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3465 }
3466 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3467 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3468 }
3469 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3470 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003471 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003472
Dave Allison20dfc792014-06-16 20:44:29 -07003473 DECLARE_INSTRUCTION(GreaterThan);
3474
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003475 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003476 return kCondGT;
3477 }
3478
Mark Mendellc4701932015-04-10 13:18:51 -04003479 IfCondition GetOppositeCondition() const OVERRIDE {
3480 return kCondLE;
3481 }
3482
Dave Allison20dfc792014-06-16 20:44:29 -07003483 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003484 template <typename T> static bool Compute(T x, T y) { return x > y; }
Aart Bike9f37602015-10-09 11:15:55 -07003485
Dave Allison20dfc792014-06-16 20:44:29 -07003486 DISALLOW_COPY_AND_ASSIGN(HGreaterThan);
3487};
3488
Vladimir Markofcb503c2016-05-18 12:48:17 +01003489class HGreaterThanOrEqual FINAL : public HCondition {
Dave Allison20dfc792014-06-16 20:44:29 -07003490 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003491 HGreaterThanOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3492 : HCondition(first, second, dex_pc) {}
Dave Allison20dfc792014-06-16 20:44:29 -07003493
Roland Levillain9867bc72015-08-05 10:21:34 +01003494 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003495 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003496 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00003497 // In the following Evaluate methods, a HCompare instruction has
3498 // been merged into this HGreaterThanOrEqual instruction; evaluate it as
3499 // `Compare(x, y) >= 0`.
Roland Levillain9867bc72015-08-05 10:21:34 +01003500 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003501 return MakeConstantCondition(Compute(Compare(x->GetValue(), y->GetValue()), 0), GetDexPc());
3502 }
3503 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3504 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
3505 }
3506 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3507 return MakeConstantCondition(Compute(CompareFP(x->GetValue(), y->GetValue()), 0), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01003508 }
Roland Levillain556c3d12014-09-18 15:25:07 +01003509
Dave Allison20dfc792014-06-16 20:44:29 -07003510 DECLARE_INSTRUCTION(GreaterThanOrEqual);
3511
Alexandre Rames2ed20af2015-03-06 13:55:35 +00003512 IfCondition GetCondition() const OVERRIDE {
Dave Allison20dfc792014-06-16 20:44:29 -07003513 return kCondGE;
3514 }
3515
Mark Mendellc4701932015-04-10 13:18:51 -04003516 IfCondition GetOppositeCondition() const OVERRIDE {
3517 return kCondLT;
3518 }
3519
Dave Allison20dfc792014-06-16 20:44:29 -07003520 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003521 template <typename T> static bool Compute(T x, T y) { return x >= y; }
Aart Bike9f37602015-10-09 11:15:55 -07003522
Dave Allison20dfc792014-06-16 20:44:29 -07003523 DISALLOW_COPY_AND_ASSIGN(HGreaterThanOrEqual);
3524};
3525
Vladimir Markofcb503c2016-05-18 12:48:17 +01003526class HBelow FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003527 public:
3528 HBelow(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3529 : HCondition(first, second, dex_pc) {}
3530
3531 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003532 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003533 }
3534 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003535 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3536 }
3537 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3538 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3539 LOG(FATAL) << DebugName() << " is not defined for float values";
3540 UNREACHABLE();
3541 }
3542 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3543 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3544 LOG(FATAL) << DebugName() << " is not defined for double values";
3545 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003546 }
3547
3548 DECLARE_INSTRUCTION(Below);
3549
3550 IfCondition GetCondition() const OVERRIDE {
3551 return kCondB;
3552 }
3553
3554 IfCondition GetOppositeCondition() const OVERRIDE {
3555 return kCondAE;
3556 }
3557
3558 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003559 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003560 return MakeUnsigned(x) < MakeUnsigned(y);
3561 }
Aart Bike9f37602015-10-09 11:15:55 -07003562
3563 DISALLOW_COPY_AND_ASSIGN(HBelow);
3564};
3565
Vladimir Markofcb503c2016-05-18 12:48:17 +01003566class HBelowOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003567 public:
3568 HBelowOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3569 : HCondition(first, second, dex_pc) {}
3570
3571 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003572 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003573 }
3574 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003575 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3576 }
3577 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3578 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3579 LOG(FATAL) << DebugName() << " is not defined for float values";
3580 UNREACHABLE();
3581 }
3582 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3583 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3584 LOG(FATAL) << DebugName() << " is not defined for double values";
3585 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003586 }
3587
3588 DECLARE_INSTRUCTION(BelowOrEqual);
3589
3590 IfCondition GetCondition() const OVERRIDE {
3591 return kCondBE;
3592 }
3593
3594 IfCondition GetOppositeCondition() const OVERRIDE {
3595 return kCondA;
3596 }
3597
3598 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003599 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003600 return MakeUnsigned(x) <= MakeUnsigned(y);
3601 }
Aart Bike9f37602015-10-09 11:15:55 -07003602
3603 DISALLOW_COPY_AND_ASSIGN(HBelowOrEqual);
3604};
3605
Vladimir Markofcb503c2016-05-18 12:48:17 +01003606class HAbove FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003607 public:
3608 HAbove(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3609 : HCondition(first, second, dex_pc) {}
3610
3611 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003612 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003613 }
3614 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003615 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3616 }
3617 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3618 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3619 LOG(FATAL) << DebugName() << " is not defined for float values";
3620 UNREACHABLE();
3621 }
3622 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3623 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3624 LOG(FATAL) << DebugName() << " is not defined for double values";
3625 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003626 }
3627
3628 DECLARE_INSTRUCTION(Above);
3629
3630 IfCondition GetCondition() const OVERRIDE {
3631 return kCondA;
3632 }
3633
3634 IfCondition GetOppositeCondition() const OVERRIDE {
3635 return kCondBE;
3636 }
3637
3638 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003639 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003640 return MakeUnsigned(x) > MakeUnsigned(y);
3641 }
Aart Bike9f37602015-10-09 11:15:55 -07003642
3643 DISALLOW_COPY_AND_ASSIGN(HAbove);
3644};
3645
Vladimir Markofcb503c2016-05-18 12:48:17 +01003646class HAboveOrEqual FINAL : public HCondition {
Aart Bike9f37602015-10-09 11:15:55 -07003647 public:
3648 HAboveOrEqual(HInstruction* first, HInstruction* second, uint32_t dex_pc = kNoDexPc)
3649 : HCondition(first, second, dex_pc) {}
3650
3651 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003652 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Aart Bike9f37602015-10-09 11:15:55 -07003653 }
3654 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003655 return MakeConstantCondition(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3656 }
3657 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
3658 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3659 LOG(FATAL) << DebugName() << " is not defined for float values";
3660 UNREACHABLE();
3661 }
3662 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
3663 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
3664 LOG(FATAL) << DebugName() << " is not defined for double values";
3665 UNREACHABLE();
Aart Bike9f37602015-10-09 11:15:55 -07003666 }
3667
3668 DECLARE_INSTRUCTION(AboveOrEqual);
3669
3670 IfCondition GetCondition() const OVERRIDE {
3671 return kCondAE;
3672 }
3673
3674 IfCondition GetOppositeCondition() const OVERRIDE {
3675 return kCondB;
3676 }
3677
3678 private:
Anton Kirilov7fe30f92016-06-29 17:03:40 +01003679 template <typename T> static bool Compute(T x, T y) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003680 return MakeUnsigned(x) >= MakeUnsigned(y);
3681 }
Aart Bike9f37602015-10-09 11:15:55 -07003682
3683 DISALLOW_COPY_AND_ASSIGN(HAboveOrEqual);
3684};
Dave Allison20dfc792014-06-16 20:44:29 -07003685
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003686// Instruction to check how two inputs compare to each other.
3687// Result is 0 if input0 == input1, 1 if input0 > input1, or -1 if input0 < input1.
Vladimir Markofcb503c2016-05-18 12:48:17 +01003688class HCompare FINAL : public HBinaryOperation {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003689 public:
Roland Levillaina5c4a402016-03-15 15:02:50 +00003690 // Note that `comparison_type` is the type of comparison performed
3691 // between the comparison's inputs, not the type of the instantiated
3692 // HCompare instruction (which is always Primitive::kPrimInt).
3693 HCompare(Primitive::Type comparison_type,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003694 HInstruction* first,
3695 HInstruction* second,
Mark Mendellc4701932015-04-10 13:18:51 -04003696 ComparisonBias bias,
Alexey Frunze4dda3372015-06-01 18:31:49 -07003697 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06003698 : HBinaryOperation(Primitive::kPrimInt,
3699 first,
3700 second,
Roland Levillaina5c4a402016-03-15 15:02:50 +00003701 SideEffectsForArchRuntimeCalls(comparison_type),
Vladimir Markoa1de9182016-02-25 11:37:38 +00003702 dex_pc) {
3703 SetPackedField<ComparisonBiasField>(bias);
Roland Levillain5b5b9312016-03-22 14:57:31 +00003704 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(first->GetType()));
3705 DCHECK_EQ(comparison_type, Primitive::PrimitiveKind(second->GetType()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003706 }
3707
Roland Levillain9867bc72015-08-05 10:21:34 +01003708 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00003709 int32_t Compute(T x, T y) const { return x > y ? 1 : (x < y ? -1 : 0); }
3710
3711 template <typename T>
3712 int32_t ComputeFP(T x, T y) const {
3713 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
3714 DCHECK_NE(GetBias(), ComparisonBias::kNoBias);
3715 // Handle the bias.
3716 return std::isunordered(x, y) ? (IsGtBias() ? 1 : -1) : Compute(x, y);
3717 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003718
Roland Levillain9867bc72015-08-05 10:21:34 +01003719 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003720 // Note that there is no "cmp-int" Dex instruction so we shouldn't
3721 // reach this code path when processing a freshly built HIR
3722 // graph. However HCompare integer instructions can be synthesized
3723 // by the instruction simplifier to implement IntegerCompare and
3724 // IntegerSignum intrinsics, so we have to handle this case.
3725 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01003726 }
3727 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00003728 return MakeConstantComparison(Compute(x->GetValue(), y->GetValue()), GetDexPc());
3729 }
3730 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
3731 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
3732 }
3733 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
3734 return MakeConstantComparison(ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain556c3d12014-09-18 15:25:07 +01003735 }
3736
Vladimir Marko372f10e2016-05-17 16:30:10 +01003737 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003738 return GetPackedFields() == other->AsCompare()->GetPackedFields();
Calin Juravleddb7df22014-11-25 20:56:51 +00003739 }
3740
Vladimir Markoa1de9182016-02-25 11:37:38 +00003741 ComparisonBias GetBias() const { return GetPackedField<ComparisonBiasField>(); }
Mark Mendellc4701932015-04-10 13:18:51 -04003742
Roland Levillain31dd3d62016-02-16 12:21:02 +00003743 // Does this compare instruction have a "gt bias" (vs an "lt bias")?
Vladimir Markoa1de9182016-02-25 11:37:38 +00003744 // Only meaningful for floating-point comparisons.
Roland Levillain31dd3d62016-02-16 12:21:02 +00003745 bool IsGtBias() const {
3746 DCHECK(Primitive::IsFloatingPointType(InputAt(0)->GetType())) << InputAt(0)->GetType();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003747 return GetBias() == ComparisonBias::kGtBias;
Roland Levillain31dd3d62016-02-16 12:21:02 +00003748 }
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003749
Roland Levillain1693a1f2016-03-15 14:57:31 +00003750 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type type ATTRIBUTE_UNUSED) {
3751 // Comparisons do not require a runtime call in any back end.
3752 return SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01003753 }
Alexey Frunze4dda3372015-06-01 18:31:49 -07003754
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003755 DECLARE_INSTRUCTION(Compare);
3756
Roland Levillain31dd3d62016-02-16 12:21:02 +00003757 protected:
Vladimir Markoa1de9182016-02-25 11:37:38 +00003758 static constexpr size_t kFieldComparisonBias = kNumberOfExpressionPackedBits;
3759 static constexpr size_t kFieldComparisonBiasSize =
3760 MinimumBitsToStore(static_cast<size_t>(ComparisonBias::kLast));
3761 static constexpr size_t kNumberOfComparePackedBits =
3762 kFieldComparisonBias + kFieldComparisonBiasSize;
3763 static_assert(kNumberOfComparePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
3764 using ComparisonBiasField =
3765 BitField<ComparisonBias, kFieldComparisonBias, kFieldComparisonBiasSize>;
3766
Roland Levillain31dd3d62016-02-16 12:21:02 +00003767 // Return an integer constant containing the result of a comparison evaluated at compile time.
3768 HIntConstant* MakeConstantComparison(int32_t value, uint32_t dex_pc) const {
3769 DCHECK(value == -1 || value == 0 || value == 1) << value;
3770 return GetBlock()->GetGraph()->GetIntConstant(value, dex_pc);
3771 }
3772
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003773 private:
3774 DISALLOW_COPY_AND_ASSIGN(HCompare);
3775};
3776
Nicolas Geoffray2b615ba2017-01-06 14:40:07 +00003777class HNewInstance FINAL : public HExpression<1> {
David Brazdil6de19382016-01-08 17:37:10 +00003778 public:
3779 HNewInstance(HInstruction* cls,
David Brazdil6de19382016-01-08 17:37:10 +00003780 uint32_t dex_pc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08003781 dex::TypeIndex type_index,
David Brazdil6de19382016-01-08 17:37:10 +00003782 const DexFile& dex_file,
David Brazdil6de19382016-01-08 17:37:10 +00003783 bool finalizable,
3784 QuickEntrypointEnum entrypoint)
3785 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc),
3786 type_index_(type_index),
3787 dex_file_(dex_file),
David Brazdil6de19382016-01-08 17:37:10 +00003788 entrypoint_(entrypoint) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003789 SetPackedFlag<kFlagFinalizable>(finalizable);
David Brazdil6de19382016-01-08 17:37:10 +00003790 SetRawInputAt(0, cls);
David Brazdil6de19382016-01-08 17:37:10 +00003791 }
3792
Andreas Gampea5b09a62016-11-17 15:21:22 -08003793 dex::TypeIndex GetTypeIndex() const { return type_index_; }
David Brazdil6de19382016-01-08 17:37:10 +00003794 const DexFile& GetDexFile() const { return dex_file_; }
3795
3796 // Calls runtime so needs an environment.
3797 bool NeedsEnvironment() const OVERRIDE { return true; }
3798
Mingyao Yang062157f2016-03-02 10:15:36 -08003799 // Can throw errors when out-of-memory or if it's not instantiable/accessible.
3800 bool CanThrow() const OVERRIDE { return true; }
3801
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003802 bool NeedsChecks() const {
3803 return entrypoint_ == kQuickAllocObjectWithChecks;
3804 }
David Brazdil6de19382016-01-08 17:37:10 +00003805
Vladimir Markoa1de9182016-02-25 11:37:38 +00003806 bool IsFinalizable() const { return GetPackedFlag<kFlagFinalizable>(); }
David Brazdil6de19382016-01-08 17:37:10 +00003807
3808 bool CanBeNull() const OVERRIDE { return false; }
3809
3810 QuickEntrypointEnum GetEntrypoint() const { return entrypoint_; }
3811
3812 void SetEntrypoint(QuickEntrypointEnum entrypoint) {
3813 entrypoint_ = entrypoint;
3814 }
3815
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003816 HLoadClass* GetLoadClass() const {
3817 HInstruction* input = InputAt(0);
3818 if (input->IsClinitCheck()) {
3819 input = input->InputAt(0);
3820 }
3821 DCHECK(input->IsLoadClass());
3822 return input->AsLoadClass();
3823 }
3824
David Brazdil6de19382016-01-08 17:37:10 +00003825 bool IsStringAlloc() const;
3826
3827 DECLARE_INSTRUCTION(NewInstance);
3828
3829 private:
Nicolas Geoffray5247c082017-01-13 14:17:29 +00003830 static constexpr size_t kFlagFinalizable = kNumberOfExpressionPackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003831 static constexpr size_t kNumberOfNewInstancePackedBits = kFlagFinalizable + 1;
3832 static_assert(kNumberOfNewInstancePackedBits <= kMaxNumberOfPackedBits,
3833 "Too many packed fields.");
3834
Andreas Gampea5b09a62016-11-17 15:21:22 -08003835 const dex::TypeIndex type_index_;
David Brazdil6de19382016-01-08 17:37:10 +00003836 const DexFile& dex_file_;
David Brazdil6de19382016-01-08 17:37:10 +00003837 QuickEntrypointEnum entrypoint_;
3838
3839 DISALLOW_COPY_AND_ASSIGN(HNewInstance);
3840};
3841
Agi Csaki05f20562015-08-19 14:58:14 -07003842enum IntrinsicNeedsEnvironmentOrCache {
3843 kNoEnvironmentOrCache, // Intrinsic does not require an environment or dex cache.
3844 kNeedsEnvironmentOrCache // Intrinsic requires an environment or requires a dex cache.
agicsaki57b81ec2015-08-11 17:39:37 -07003845};
3846
Aart Bik5d75afe2015-12-14 11:57:01 -08003847enum IntrinsicSideEffects {
3848 kNoSideEffects, // Intrinsic does not have any heap memory side effects.
3849 kReadSideEffects, // Intrinsic may read heap memory.
3850 kWriteSideEffects, // Intrinsic may write heap memory.
3851 kAllSideEffects // Intrinsic may read or write heap memory, or trigger GC.
3852};
3853
3854enum IntrinsicExceptions {
3855 kNoThrow, // Intrinsic does not throw any exceptions.
3856 kCanThrow // Intrinsic may throw exceptions.
3857};
3858
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003859class HInvoke : public HVariableInputSizeInstruction {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003860 public:
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003861 bool NeedsEnvironment() const OVERRIDE;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003862
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01003863 void SetArgumentAt(size_t index, HInstruction* argument) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003864 SetRawInputAt(index, argument);
3865 }
3866
Roland Levillain3e3d7332015-04-28 11:00:54 +01003867 // Return the number of arguments. This number can be lower than
3868 // the number of inputs returned by InputCount(), as some invoke
3869 // instructions (e.g. HInvokeStaticOrDirect) can have non-argument
3870 // inputs at the end of their list of inputs.
3871 uint32_t GetNumberOfArguments() const { return number_of_arguments_; }
3872
Vladimir Markoa1de9182016-02-25 11:37:38 +00003873 Primitive::Type GetType() const OVERRIDE { return GetPackedField<ReturnTypeField>(); }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003874
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003875 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
3876
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003877 InvokeType GetInvokeType() const {
3878 return GetPackedField<InvokeTypeField>();
Vladimir Markoa1de9182016-02-25 11:37:38 +00003879 }
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003880
Nicolas Geoffray1ba19812015-04-21 09:12:40 +01003881 Intrinsics GetIntrinsic() const {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003882 return intrinsic_;
3883 }
3884
Aart Bik5d75afe2015-12-14 11:57:01 -08003885 void SetIntrinsic(Intrinsics intrinsic,
3886 IntrinsicNeedsEnvironmentOrCache needs_env_or_cache,
3887 IntrinsicSideEffects side_effects,
3888 IntrinsicExceptions exceptions);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003889
Nicolas Geoffray78f4fa72015-06-12 09:35:05 +01003890 bool IsFromInlinedInvoke() const {
Nicolas Geoffray8e1ef532015-11-23 12:04:37 +00003891 return GetEnvironment()->IsFromInlinedInvoke();
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003892 }
3893
Aart Bikff7d89c2016-11-07 08:49:28 -08003894 void SetCanThrow(bool can_throw) { SetPackedFlag<kFlagCanThrow>(can_throw); }
3895
Vladimir Markoa1de9182016-02-25 11:37:38 +00003896 bool CanThrow() const OVERRIDE { return GetPackedFlag<kFlagCanThrow>(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003897
Aart Bik71bf7b42016-11-16 10:17:46 -08003898 bool CanBeMoved() const OVERRIDE { return IsIntrinsic() && !DoesAnyWrite(); }
Aart Bik5d75afe2015-12-14 11:57:01 -08003899
Vladimir Marko372f10e2016-05-17 16:30:10 +01003900 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bik5d75afe2015-12-14 11:57:01 -08003901 return intrinsic_ != Intrinsics::kNone && intrinsic_ == other->AsInvoke()->intrinsic_;
3902 }
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +01003903
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003904 uint32_t* GetIntrinsicOptimizations() {
3905 return &intrinsic_optimizations_;
3906 }
3907
3908 const uint32_t* GetIntrinsicOptimizations() const {
3909 return &intrinsic_optimizations_;
3910 }
3911
3912 bool IsIntrinsic() const { return intrinsic_ != Intrinsics::kNone; }
3913
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003914 ArtMethod* GetResolvedMethod() const { return resolved_method_; }
3915
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003916 DECLARE_ABSTRACT_INSTRUCTION(Invoke);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01003917
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003918 protected:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003919 static constexpr size_t kFieldInvokeType = kNumberOfGenericPackedBits;
3920 static constexpr size_t kFieldInvokeTypeSize =
Vladimir Markoa1de9182016-02-25 11:37:38 +00003921 MinimumBitsToStore(static_cast<size_t>(kMaxInvokeType));
3922 static constexpr size_t kFieldReturnType =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003923 kFieldInvokeType + kFieldInvokeTypeSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003924 static constexpr size_t kFieldReturnTypeSize =
3925 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
3926 static constexpr size_t kFlagCanThrow = kFieldReturnType + kFieldReturnTypeSize;
3927 static constexpr size_t kNumberOfInvokePackedBits = kFlagCanThrow + 1;
3928 static_assert(kNumberOfInvokePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003929 using InvokeTypeField = BitField<InvokeType, kFieldInvokeType, kFieldInvokeTypeSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00003930 using ReturnTypeField = BitField<Primitive::Type, kFieldReturnType, kFieldReturnTypeSize>;
3931
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003932 HInvoke(ArenaAllocator* arena,
3933 uint32_t number_of_arguments,
Roland Levillain3e3d7332015-04-28 11:00:54 +01003934 uint32_t number_of_other_inputs,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003935 Primitive::Type return_type,
3936 uint32_t dex_pc,
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01003937 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003938 ArtMethod* resolved_method,
3939 InvokeType invoke_type)
Mingyao Yanga9dbe832016-12-15 12:02:53 -08003940 : HVariableInputSizeInstruction(
3941 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
3942 dex_pc,
3943 arena,
3944 number_of_arguments + number_of_other_inputs,
3945 kArenaAllocInvokeInputs),
Roland Levillain3e3d7332015-04-28 11:00:54 +01003946 number_of_arguments_(number_of_arguments),
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003947 resolved_method_(resolved_method),
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003948 dex_method_index_(dex_method_index),
agicsaki57b81ec2015-08-11 17:39:37 -07003949 intrinsic_(Intrinsics::kNone),
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003950 intrinsic_optimizations_(0) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00003951 SetPackedField<ReturnTypeField>(return_type);
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003952 SetPackedField<InvokeTypeField>(invoke_type);
Vladimir Markoa1de9182016-02-25 11:37:38 +00003953 SetPackedFlag<kFlagCanThrow>(true);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003954 }
3955
Roland Levillain3e3d7332015-04-28 11:00:54 +01003956 uint32_t number_of_arguments_;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003957 ArtMethod* const resolved_method_;
Andreas Gampe71fb52f2014-12-29 17:43:08 -08003958 const uint32_t dex_method_index_;
3959 Intrinsics intrinsic_;
Nicolas Geoffraya83a54d2015-10-02 17:30:26 +01003960
3961 // A magic word holding optimizations for intrinsics. See intrinsics.h.
3962 uint32_t intrinsic_optimizations_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003963
3964 private:
3965 DISALLOW_COPY_AND_ASSIGN(HInvoke);
3966};
3967
Vladimir Markofcb503c2016-05-18 12:48:17 +01003968class HInvokeUnresolved FINAL : public HInvoke {
Calin Juravle175dc732015-08-25 15:42:32 +01003969 public:
3970 HInvokeUnresolved(ArenaAllocator* arena,
3971 uint32_t number_of_arguments,
3972 Primitive::Type return_type,
3973 uint32_t dex_pc,
3974 uint32_t dex_method_index,
3975 InvokeType invoke_type)
3976 : HInvoke(arena,
3977 number_of_arguments,
3978 0u /* number_of_other_inputs */,
3979 return_type,
3980 dex_pc,
3981 dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01003982 nullptr,
Calin Juravle175dc732015-08-25 15:42:32 +01003983 invoke_type) {
3984 }
3985
3986 DECLARE_INSTRUCTION(InvokeUnresolved);
3987
3988 private:
3989 DISALLOW_COPY_AND_ASSIGN(HInvokeUnresolved);
3990};
3991
Orion Hodsonac141392017-01-13 11:53:47 +00003992class HInvokePolymorphic FINAL : public HInvoke {
3993 public:
3994 HInvokePolymorphic(ArenaAllocator* arena,
3995 uint32_t number_of_arguments,
3996 Primitive::Type return_type,
3997 uint32_t dex_pc,
3998 uint32_t dex_method_index)
3999 : HInvoke(arena,
4000 number_of_arguments,
4001 0u /* number_of_other_inputs */,
4002 return_type,
4003 dex_pc,
4004 dex_method_index,
4005 nullptr,
4006 kVirtual) {}
4007
4008 DECLARE_INSTRUCTION(InvokePolymorphic);
4009
4010 private:
4011 DISALLOW_COPY_AND_ASSIGN(HInvokePolymorphic);
4012};
4013
Vladimir Markofcb503c2016-05-18 12:48:17 +01004014class HInvokeStaticOrDirect FINAL : public HInvoke {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004015 public:
Roland Levillain4c0eb422015-04-24 16:43:49 +01004016 // Requirements of this method call regarding the class
4017 // initialization (clinit) check of its declaring class.
4018 enum class ClinitCheckRequirement {
4019 kNone, // Class already initialized.
4020 kExplicit, // Static call having explicit clinit check as last input.
4021 kImplicit, // Static call implicitly requiring a clinit check.
Vladimir Markoa1de9182016-02-25 11:37:38 +00004022 kLast = kImplicit
Roland Levillain4c0eb422015-04-24 16:43:49 +01004023 };
4024
Vladimir Marko58155012015-08-19 12:49:41 +00004025 // Determines how to load the target ArtMethod*.
4026 enum class MethodLoadKind {
4027 // Use a String init ArtMethod* loaded from Thread entrypoints.
4028 kStringInit,
4029
4030 // Use the method's own ArtMethod* loaded by the register allocator.
4031 kRecursive,
4032
4033 // Use ArtMethod* at a known address, embed the direct address in the code.
4034 // Used for app->boot calls with non-relocatable image and for JIT-compiled calls.
4035 kDirectAddress,
4036
Vladimir Markoa1de9182016-02-25 11:37:38 +00004037 // Load from resolved methods array in the dex cache using a PC-relative load.
Vladimir Marko58155012015-08-19 12:49:41 +00004038 // Used when we need to use the dex cache, for example for invoke-static that
4039 // may cause class initialization (the entry may point to a resolution method),
4040 // and we know that we can access the dex cache arrays using a PC-relative load.
4041 kDexCachePcRelative,
4042
4043 // Use ArtMethod* from the resolved methods of the compiled method's own ArtMethod*.
4044 // Used for JIT when we need to use the dex cache. This is also the last-resort-kind
4045 // used when other kinds are unavailable (say, dex cache arrays are not PC-relative)
4046 // or unimplemented or impractical (i.e. slow) on a particular architecture.
4047 kDexCacheViaMethod,
4048 };
4049
4050 // Determines the location of the code pointer.
4051 enum class CodePtrLocation {
4052 // Recursive call, use local PC-relative call instruction.
4053 kCallSelf,
4054
Vladimir Marko58155012015-08-19 12:49:41 +00004055 // Use code pointer from the ArtMethod*.
4056 // Used when we don't know the target code. This is also the last-resort-kind used when
4057 // other kinds are unimplemented or impractical (i.e. slow) on a particular architecture.
4058 kCallArtMethod,
4059 };
4060
4061 struct DispatchInfo {
Vladimir Markodc151b22015-10-15 18:02:30 +01004062 MethodLoadKind method_load_kind;
4063 CodePtrLocation code_ptr_location;
Vladimir Marko58155012015-08-19 12:49:41 +00004064 // The method load data holds
4065 // - thread entrypoint offset for kStringInit method if this is a string init invoke.
4066 // Note that there are multiple string init methods, each having its own offset.
4067 // - the method address for kDirectAddress
4068 // - the dex cache arrays offset for kDexCachePcRel.
Vladimir Markodc151b22015-10-15 18:02:30 +01004069 uint64_t method_load_data;
Vladimir Marko58155012015-08-19 12:49:41 +00004070 };
4071
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004072 HInvokeStaticOrDirect(ArenaAllocator* arena,
4073 uint32_t number_of_arguments,
4074 Primitive::Type return_type,
4075 uint32_t dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004076 uint32_t method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004077 ArtMethod* resolved_method,
Vladimir Marko58155012015-08-19 12:49:41 +00004078 DispatchInfo dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004079 InvokeType invoke_type,
4080 MethodReference target_method,
Roland Levillain4c0eb422015-04-24 16:43:49 +01004081 ClinitCheckRequirement clinit_check_requirement)
Roland Levillain3e3d7332015-04-28 11:00:54 +01004082 : HInvoke(arena,
4083 number_of_arguments,
Vladimir Markob554b5a2015-11-06 12:57:55 +00004084 // There is potentially one extra argument for the HCurrentMethod node, and
4085 // potentially one other if the clinit check is explicit, and potentially
4086 // one other if the method is a string factory.
4087 (NeedsCurrentMethodInput(dispatch_info.method_load_kind) ? 1u : 0u) +
David Brazdildee58d62016-04-07 09:54:26 +00004088 (clinit_check_requirement == ClinitCheckRequirement::kExplicit ? 1u : 0u),
Roland Levillain3e3d7332015-04-28 11:00:54 +01004089 return_type,
4090 dex_pc,
Vladimir Marko58155012015-08-19 12:49:41 +00004091 method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004092 resolved_method,
4093 invoke_type),
Vladimir Marko58155012015-08-19 12:49:41 +00004094 target_method_(target_method),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004095 dispatch_info_(dispatch_info) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004096 SetPackedField<ClinitCheckRequirementField>(clinit_check_requirement);
4097 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004098
Vladimir Markodc151b22015-10-15 18:02:30 +01004099 void SetDispatchInfo(const DispatchInfo& dispatch_info) {
Vladimir Markob554b5a2015-11-06 12:57:55 +00004100 bool had_current_method_input = HasCurrentMethodInput();
4101 bool needs_current_method_input = NeedsCurrentMethodInput(dispatch_info.method_load_kind);
4102
4103 // Using the current method is the default and once we find a better
4104 // method load kind, we should not go back to using the current method.
4105 DCHECK(had_current_method_input || !needs_current_method_input);
4106
4107 if (had_current_method_input && !needs_current_method_input) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004108 DCHECK_EQ(InputAt(GetSpecialInputIndex()), GetBlock()->GetGraph()->GetCurrentMethod());
4109 RemoveInputAt(GetSpecialInputIndex());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004110 }
Vladimir Markodc151b22015-10-15 18:02:30 +01004111 dispatch_info_ = dispatch_info;
4112 }
4113
Vladimir Markoc53c0792015-11-19 15:48:33 +00004114 void AddSpecialInput(HInstruction* input) {
4115 // We allow only one special input.
4116 DCHECK(!IsStringInit() && !HasCurrentMethodInput());
4117 DCHECK(InputCount() == GetSpecialInputIndex() ||
4118 (InputCount() == GetSpecialInputIndex() + 1 && IsStaticWithExplicitClinitCheck()));
4119 InsertInputAt(GetSpecialInputIndex(), input);
4120 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004121
Vladimir Marko372f10e2016-05-17 16:30:10 +01004122 using HInstruction::GetInputRecords; // Keep the const version visible.
4123 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE {
4124 ArrayRef<HUserRecord<HInstruction*>> input_records = HInvoke::GetInputRecords();
4125 if (kIsDebugBuild && IsStaticWithExplicitClinitCheck()) {
4126 DCHECK(!input_records.empty());
4127 DCHECK_GT(input_records.size(), GetNumberOfArguments());
4128 HInstruction* last_input = input_records.back().GetInstruction();
4129 // Note: `last_input` may be null during arguments setup.
4130 if (last_input != nullptr) {
4131 // `last_input` is the last input of a static invoke marked as having
4132 // an explicit clinit check. It must either be:
4133 // - an art::HClinitCheck instruction, set by art::HGraphBuilder; or
4134 // - an art::HLoadClass instruction, set by art::PrepareForRegisterAllocation.
4135 DCHECK(last_input->IsClinitCheck() || last_input->IsLoadClass()) << last_input->DebugName();
4136 }
4137 }
4138 return input_records;
4139 }
4140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004141 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004142 // We access the method via the dex cache so we can't do an implicit null check.
4143 // TODO: for intrinsics we can generate implicit null checks.
4144 return false;
4145 }
4146
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004147 bool CanBeNull() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004148 return GetPackedField<ReturnTypeField>() == Primitive::kPrimNot && !IsStringInit();
Nicolas Geoffrayefa84682015-08-12 18:28:14 -07004149 }
4150
Vladimir Markoc53c0792015-11-19 15:48:33 +00004151 // Get the index of the special input, if any.
4152 //
David Brazdil6de19382016-01-08 17:37:10 +00004153 // If the invoke HasCurrentMethodInput(), the "special input" is the current
4154 // method pointer; otherwise there may be one platform-specific special input,
4155 // such as PC-relative addressing base.
Vladimir Markoc53c0792015-11-19 15:48:33 +00004156 uint32_t GetSpecialInputIndex() const { return GetNumberOfArguments(); }
Nicolas Geoffray97793072016-02-16 15:33:54 +00004157 bool HasSpecialInput() const { return GetNumberOfArguments() != InputCount(); }
Vladimir Markoc53c0792015-11-19 15:48:33 +00004158
Vladimir Marko58155012015-08-19 12:49:41 +00004159 MethodLoadKind GetMethodLoadKind() const { return dispatch_info_.method_load_kind; }
4160 CodePtrLocation GetCodePtrLocation() const { return dispatch_info_.code_ptr_location; }
4161 bool IsRecursive() const { return GetMethodLoadKind() == MethodLoadKind::kRecursive; }
Vladimir Markodc151b22015-10-15 18:02:30 +01004162 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE;
Vladimir Marko58155012015-08-19 12:49:41 +00004163 bool IsStringInit() const { return GetMethodLoadKind() == MethodLoadKind::kStringInit; }
Vladimir Marko58155012015-08-19 12:49:41 +00004164 bool HasMethodAddress() const { return GetMethodLoadKind() == MethodLoadKind::kDirectAddress; }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004165 bool HasPcRelativeDexCache() const {
Vladimir Markodc151b22015-10-15 18:02:30 +01004166 return GetMethodLoadKind() == MethodLoadKind::kDexCachePcRelative;
4167 }
Vladimir Markob554b5a2015-11-06 12:57:55 +00004168 bool HasCurrentMethodInput() const {
4169 // This function can be called only after the invoke has been fully initialized by the builder.
4170 if (NeedsCurrentMethodInput(GetMethodLoadKind())) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004171 DCHECK(InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004172 return true;
4173 } else {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004174 DCHECK(InputCount() == GetSpecialInputIndex() ||
4175 !InputAt(GetSpecialInputIndex())->IsCurrentMethod());
Vladimir Markob554b5a2015-11-06 12:57:55 +00004176 return false;
4177 }
4178 }
Vladimir Marko58155012015-08-19 12:49:41 +00004179
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004180 QuickEntrypointEnum GetStringInitEntryPoint() const {
Vladimir Marko58155012015-08-19 12:49:41 +00004181 DCHECK(IsStringInit());
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004182 return static_cast<QuickEntrypointEnum>(dispatch_info_.method_load_data);
Vladimir Marko58155012015-08-19 12:49:41 +00004183 }
4184
4185 uint64_t GetMethodAddress() const {
4186 DCHECK(HasMethodAddress());
4187 return dispatch_info_.method_load_data;
4188 }
4189
4190 uint32_t GetDexCacheArrayOffset() const {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004191 DCHECK(HasPcRelativeDexCache());
Vladimir Marko58155012015-08-19 12:49:41 +00004192 return dispatch_info_.method_load_data;
4193 }
4194
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00004195 const DexFile& GetDexFileForPcRelativeDexCache() const;
4196
Vladimir Markoa1de9182016-02-25 11:37:38 +00004197 ClinitCheckRequirement GetClinitCheckRequirement() const {
4198 return GetPackedField<ClinitCheckRequirementField>();
4199 }
Calin Juravle68ad6492015-08-18 17:08:12 +01004200
Roland Levillain4c0eb422015-04-24 16:43:49 +01004201 // Is this instruction a call to a static method?
4202 bool IsStatic() const {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004203 return GetInvokeType() == kStatic;
4204 }
4205
4206 MethodReference GetTargetMethod() const {
4207 return target_method_;
Roland Levillain4c0eb422015-04-24 16:43:49 +01004208 }
4209
Vladimir Markofbb184a2015-11-13 14:47:00 +00004210 // Remove the HClinitCheck or the replacement HLoadClass (set as last input by
4211 // PrepareForRegisterAllocation::VisitClinitCheck() in lieu of the initial HClinitCheck)
4212 // instruction; only relevant for static calls with explicit clinit check.
4213 void RemoveExplicitClinitCheck(ClinitCheckRequirement new_requirement) {
Roland Levillain4c0eb422015-04-24 16:43:49 +01004214 DCHECK(IsStaticWithExplicitClinitCheck());
Vladimir Marko372f10e2016-05-17 16:30:10 +01004215 size_t last_input_index = inputs_.size() - 1u;
4216 HInstruction* last_input = inputs_.back().GetInstruction();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004217 DCHECK(last_input != nullptr);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004218 DCHECK(last_input->IsLoadClass() || last_input->IsClinitCheck()) << last_input->DebugName();
Roland Levillain4c0eb422015-04-24 16:43:49 +01004219 RemoveAsUserOfInput(last_input_index);
Vladimir Markofa6b93c2015-09-15 10:15:55 +01004220 inputs_.pop_back();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004221 SetPackedField<ClinitCheckRequirementField>(new_requirement);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004222 DCHECK(!IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01004223 }
4224
4225 // Is this a call to a static method whose declaring class has an
Vladimir Markofbb184a2015-11-13 14:47:00 +00004226 // explicit initialization check in the graph?
Roland Levillain4c0eb422015-04-24 16:43:49 +01004227 bool IsStaticWithExplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004228 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kExplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004229 }
4230
4231 // Is this a call to a static method whose declaring class has an
4232 // implicit intialization check requirement?
4233 bool IsStaticWithImplicitClinitCheck() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00004234 return IsStatic() && (GetClinitCheckRequirement() == ClinitCheckRequirement::kImplicit);
Roland Levillain4c0eb422015-04-24 16:43:49 +01004235 }
4236
Vladimir Markob554b5a2015-11-06 12:57:55 +00004237 // Does this method load kind need the current method as an input?
4238 static bool NeedsCurrentMethodInput(MethodLoadKind kind) {
4239 return kind == MethodLoadKind::kRecursive || kind == MethodLoadKind::kDexCacheViaMethod;
4240 }
4241
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004242 DECLARE_INSTRUCTION(InvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004243
4244 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004245 static constexpr size_t kFieldClinitCheckRequirement = kNumberOfInvokePackedBits;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004246 static constexpr size_t kFieldClinitCheckRequirementSize =
4247 MinimumBitsToStore(static_cast<size_t>(ClinitCheckRequirement::kLast));
4248 static constexpr size_t kNumberOfInvokeStaticOrDirectPackedBits =
4249 kFieldClinitCheckRequirement + kFieldClinitCheckRequirementSize;
4250 static_assert(kNumberOfInvokeStaticOrDirectPackedBits <= kMaxNumberOfPackedBits,
4251 "Too many packed fields.");
Vladimir Markoa1de9182016-02-25 11:37:38 +00004252 using ClinitCheckRequirementField = BitField<ClinitCheckRequirement,
4253 kFieldClinitCheckRequirement,
4254 kFieldClinitCheckRequirementSize>;
4255
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004256 // Cached values of the resolved method, to avoid needing the mutator lock.
Vladimir Marko58155012015-08-19 12:49:41 +00004257 MethodReference target_method_;
4258 DispatchInfo dispatch_info_;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004259
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00004260 DISALLOW_COPY_AND_ASSIGN(HInvokeStaticOrDirect);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004261};
Vladimir Markof64242a2015-12-01 14:58:23 +00004262std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::MethodLoadKind rhs);
Vladimir Markofbb184a2015-11-13 14:47:00 +00004263std::ostream& operator<<(std::ostream& os, HInvokeStaticOrDirect::ClinitCheckRequirement rhs);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00004264
Vladimir Markofcb503c2016-05-18 12:48:17 +01004265class HInvokeVirtual FINAL : public HInvoke {
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004266 public:
4267 HInvokeVirtual(ArenaAllocator* arena,
4268 uint32_t number_of_arguments,
4269 Primitive::Type return_type,
4270 uint32_t dex_pc,
Andreas Gampe71fb52f2014-12-29 17:43:08 -08004271 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004272 ArtMethod* resolved_method,
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004273 uint32_t vtable_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004274 : HInvoke(arena,
4275 number_of_arguments,
4276 0u,
4277 return_type,
4278 dex_pc,
4279 dex_method_index,
4280 resolved_method,
4281 kVirtual),
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004282 vtable_index_(vtable_index) {}
4283
Aart Bik71bf7b42016-11-16 10:17:46 -08004284 bool CanBeNull() const OVERRIDE {
4285 switch (GetIntrinsic()) {
4286 case Intrinsics::kThreadCurrentThread:
4287 case Intrinsics::kStringBufferAppend:
4288 case Intrinsics::kStringBufferToString:
4289 case Intrinsics::kStringBuilderAppend:
4290 case Intrinsics::kStringBuilderToString:
4291 return false;
4292 default:
4293 return HInvoke::CanBeNull();
4294 }
4295 }
4296
Calin Juravle641547a2015-04-21 22:08:51 +01004297 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004298 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004299 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004300 }
4301
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004302 uint32_t GetVTableIndex() const { return vtable_index_; }
4303
4304 DECLARE_INSTRUCTION(InvokeVirtual);
4305
4306 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004307 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01004308 const uint32_t vtable_index_;
4309
4310 DISALLOW_COPY_AND_ASSIGN(HInvokeVirtual);
4311};
4312
Vladimir Markofcb503c2016-05-18 12:48:17 +01004313class HInvokeInterface FINAL : public HInvoke {
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004314 public:
4315 HInvokeInterface(ArenaAllocator* arena,
4316 uint32_t number_of_arguments,
4317 Primitive::Type return_type,
4318 uint32_t dex_pc,
4319 uint32_t dex_method_index,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004320 ArtMethod* resolved_method,
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004321 uint32_t imt_index)
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004322 : HInvoke(arena,
4323 number_of_arguments,
4324 0u,
4325 return_type,
4326 dex_pc,
4327 dex_method_index,
4328 resolved_method,
4329 kInterface),
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004330 imt_index_(imt_index) {}
4331
Calin Juravle641547a2015-04-21 22:08:51 +01004332 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00004333 // TODO: Add implicit null checks in intrinsics.
Calin Juravle641547a2015-04-21 22:08:51 +01004334 return (obj == InputAt(0)) && !GetLocations()->Intrinsified();
Calin Juravle77520bc2015-01-12 18:45:46 +00004335 }
4336
Nicolas Geoffrayfbdfa6d2017-02-03 10:43:13 +00004337 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
4338 // The assembly stub currently needs it.
4339 return true;
4340 }
4341
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004342 uint32_t GetImtIndex() const { return imt_index_; }
4343 uint32_t GetDexMethodIndex() const { return dex_method_index_; }
4344
4345 DECLARE_INSTRUCTION(InvokeInterface);
4346
4347 private:
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004348 // Cached value of the resolved method, to avoid needing the mutator lock.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00004349 const uint32_t imt_index_;
4350
4351 DISALLOW_COPY_AND_ASSIGN(HInvokeInterface);
4352};
4353
Vladimir Markofcb503c2016-05-18 12:48:17 +01004354class HNeg FINAL : public HUnaryOperation {
Roland Levillain88cb1752014-10-20 16:36:47 +01004355 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004356 HNeg(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
Roland Levillain937e6cd2016-03-22 11:54:37 +00004357 : HUnaryOperation(result_type, input, dex_pc) {
4358 DCHECK_EQ(result_type, Primitive::PrimitiveKind(input->GetType()));
4359 }
Roland Levillain88cb1752014-10-20 16:36:47 +01004360
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004361 template <typename T> static T Compute(T x) { return -x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004362
4363 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004364 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004365 }
4366 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004367 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004368 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004369 HConstant* Evaluate(HFloatConstant* x) const OVERRIDE {
4370 return GetBlock()->GetGraph()->GetFloatConstant(Compute(x->GetValue()), GetDexPc());
4371 }
4372 HConstant* Evaluate(HDoubleConstant* x) const OVERRIDE {
4373 return GetBlock()->GetGraph()->GetDoubleConstant(Compute(x->GetValue()), GetDexPc());
4374 }
Roland Levillain9240d6a2014-10-20 16:47:04 +01004375
Roland Levillain88cb1752014-10-20 16:36:47 +01004376 DECLARE_INSTRUCTION(Neg);
4377
4378 private:
4379 DISALLOW_COPY_AND_ASSIGN(HNeg);
4380};
4381
Vladimir Markofcb503c2016-05-18 12:48:17 +01004382class HNewArray FINAL : public HExpression<2> {
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004383 public:
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004384 HNewArray(HInstruction* cls, HInstruction* length, uint32_t dex_pc)
4385 : HExpression(Primitive::kPrimNot, SideEffects::CanTriggerGC(), dex_pc) {
4386 SetRawInputAt(0, cls);
4387 SetRawInputAt(1, length);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004388 }
4389
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004390 // Calls runtime so needs an environment.
Calin Juravle10e244f2015-01-26 18:54:32 +00004391 bool NeedsEnvironment() const OVERRIDE { return true; }
4392
Mingyao Yang0c365e62015-03-31 15:09:29 -07004393 // May throw NegativeArraySizeException, OutOfMemoryError, etc.
4394 bool CanThrow() const OVERRIDE { return true; }
4395
Calin Juravle10e244f2015-01-26 18:54:32 +00004396 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004397
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004398 HLoadClass* GetLoadClass() const {
4399 DCHECK(InputAt(0)->IsLoadClass());
4400 return InputAt(0)->AsLoadClass();
4401 }
4402
4403 HInstruction* GetLength() const {
4404 return InputAt(1);
4405 }
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +00004406
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004407 DECLARE_INSTRUCTION(NewArray);
4408
4409 private:
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004410 DISALLOW_COPY_AND_ASSIGN(HNewArray);
4411};
4412
Vladimir Markofcb503c2016-05-18 12:48:17 +01004413class HAdd FINAL : public HBinaryOperation {
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004414 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004415 HAdd(Primitive::Type result_type,
4416 HInstruction* left,
4417 HInstruction* right,
4418 uint32_t dex_pc = kNoDexPc)
4419 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004420
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004421 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004422
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004423 template <typename T> static T Compute(T x, T y) { return x + y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004424
4425 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004426 return GetBlock()->GetGraph()->GetIntConstant(
4427 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004428 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004429 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004430 return GetBlock()->GetGraph()->GetLongConstant(
4431 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004432 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004433 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4434 return GetBlock()->GetGraph()->GetFloatConstant(
4435 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4436 }
4437 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4438 return GetBlock()->GetGraph()->GetDoubleConstant(
4439 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4440 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004441
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004442 DECLARE_INSTRUCTION(Add);
4443
4444 private:
4445 DISALLOW_COPY_AND_ASSIGN(HAdd);
4446};
4447
Vladimir Markofcb503c2016-05-18 12:48:17 +01004448class HSub FINAL : public HBinaryOperation {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004449 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004450 HSub(Primitive::Type result_type,
4451 HInstruction* left,
4452 HInstruction* right,
4453 uint32_t dex_pc = kNoDexPc)
4454 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004455
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004456 template <typename T> static T Compute(T x, T y) { return x - y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004457
4458 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004459 return GetBlock()->GetGraph()->GetIntConstant(
4460 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004461 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004462 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004463 return GetBlock()->GetGraph()->GetLongConstant(
4464 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain93445682014-10-06 19:24:02 +01004465 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004466 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4467 return GetBlock()->GetGraph()->GetFloatConstant(
4468 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4469 }
4470 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4471 return GetBlock()->GetGraph()->GetDoubleConstant(
4472 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4473 }
Roland Levillain556c3d12014-09-18 15:25:07 +01004474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004475 DECLARE_INSTRUCTION(Sub);
4476
4477 private:
4478 DISALLOW_COPY_AND_ASSIGN(HSub);
4479};
4480
Vladimir Markofcb503c2016-05-18 12:48:17 +01004481class HMul FINAL : public HBinaryOperation {
Calin Juravle34bacdf2014-10-07 20:23:36 +01004482 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004483 HMul(Primitive::Type result_type,
4484 HInstruction* left,
4485 HInstruction* right,
4486 uint32_t dex_pc = kNoDexPc)
4487 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle34bacdf2014-10-07 20:23:36 +01004488
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004489 bool IsCommutative() const OVERRIDE { return true; }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004490
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004491 template <typename T> static T Compute(T x, T y) { return x * y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004492
4493 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004494 return GetBlock()->GetGraph()->GetIntConstant(
4495 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004496 }
4497 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004498 return GetBlock()->GetGraph()->GetLongConstant(
4499 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004500 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004501 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4502 return GetBlock()->GetGraph()->GetFloatConstant(
4503 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4504 }
4505 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4506 return GetBlock()->GetGraph()->GetDoubleConstant(
4507 Compute(x->GetValue(), y->GetValue()), GetDexPc());
4508 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004509
4510 DECLARE_INSTRUCTION(Mul);
4511
4512 private:
4513 DISALLOW_COPY_AND_ASSIGN(HMul);
4514};
4515
Vladimir Markofcb503c2016-05-18 12:48:17 +01004516class HDiv FINAL : public HBinaryOperation {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004517 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004518 HDiv(Primitive::Type result_type,
4519 HInstruction* left,
4520 HInstruction* right,
4521 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004522 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravle7c4954d2014-10-28 16:57:40 +00004523
Roland Levillain9867bc72015-08-05 10:21:34 +01004524 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004525 T ComputeIntegral(T x, T y) const {
4526 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004527 // Our graph structure ensures we never have 0 for `y` during
4528 // constant folding.
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004529 DCHECK_NE(y, 0);
Calin Juravlebacfec32014-11-14 15:54:36 +00004530 // Special case -1 to avoid getting a SIGFPE on x86(_64).
Nicolas Geoffraycd2de0c2014-11-06 15:59:38 +00004531 return (y == -1) ? -x : x / y;
4532 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004533
Roland Levillain31dd3d62016-02-16 12:21:02 +00004534 template <typename T>
4535 T ComputeFP(T x, T y) const {
4536 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4537 return x / y;
4538 }
4539
Roland Levillain9867bc72015-08-05 10:21:34 +01004540 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004541 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004542 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004543 }
4544 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004545 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004546 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
4547 }
4548 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4549 return GetBlock()->GetGraph()->GetFloatConstant(
4550 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4551 }
4552 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4553 return GetBlock()->GetGraph()->GetDoubleConstant(
4554 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004555 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004556
4557 DECLARE_INSTRUCTION(Div);
4558
4559 private:
4560 DISALLOW_COPY_AND_ASSIGN(HDiv);
4561};
4562
Vladimir Markofcb503c2016-05-18 12:48:17 +01004563class HRem FINAL : public HBinaryOperation {
Calin Juravlebacfec32014-11-14 15:54:36 +00004564 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004565 HRem(Primitive::Type result_type,
4566 HInstruction* left,
4567 HInstruction* right,
4568 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01004569 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Calin Juravlebacfec32014-11-14 15:54:36 +00004570
Roland Levillain9867bc72015-08-05 10:21:34 +01004571 template <typename T>
Roland Levillain31dd3d62016-02-16 12:21:02 +00004572 T ComputeIntegral(T x, T y) const {
4573 DCHECK(!Primitive::IsFloatingPointType(GetType())) << GetType();
Roland Levillain9867bc72015-08-05 10:21:34 +01004574 // Our graph structure ensures we never have 0 for `y` during
4575 // constant folding.
Calin Juravlebacfec32014-11-14 15:54:36 +00004576 DCHECK_NE(y, 0);
4577 // Special case -1 to avoid getting a SIGFPE on x86(_64).
4578 return (y == -1) ? 0 : x % y;
4579 }
4580
Roland Levillain31dd3d62016-02-16 12:21:02 +00004581 template <typename T>
4582 T ComputeFP(T x, T y) const {
4583 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
4584 return std::fmod(x, y);
4585 }
4586
Roland Levillain9867bc72015-08-05 10:21:34 +01004587 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004588 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004589 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004590 }
4591 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004592 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain31dd3d62016-02-16 12:21:02 +00004593 ComputeIntegral(x->GetValue(), y->GetValue()), GetDexPc());
Calin Juravlebacfec32014-11-14 15:54:36 +00004594 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004595 HConstant* Evaluate(HFloatConstant* x, HFloatConstant* y) const OVERRIDE {
4596 return GetBlock()->GetGraph()->GetFloatConstant(
4597 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4598 }
4599 HConstant* Evaluate(HDoubleConstant* x, HDoubleConstant* y) const OVERRIDE {
4600 return GetBlock()->GetGraph()->GetDoubleConstant(
4601 ComputeFP(x->GetValue(), y->GetValue()), GetDexPc());
4602 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004603
4604 DECLARE_INSTRUCTION(Rem);
4605
4606 private:
Calin Juravlebacfec32014-11-14 15:54:36 +00004607 DISALLOW_COPY_AND_ASSIGN(HRem);
4608};
4609
Vladimir Markofcb503c2016-05-18 12:48:17 +01004610class HDivZeroCheck FINAL : public HExpression<1> {
Calin Juravled0d48522014-11-04 16:40:20 +00004611 public:
Alexandre Rames780aece2016-01-13 14:34:39 +00004612 // `HDivZeroCheck` can trigger GC, as it may call the `ArithmeticException`
4613 // constructor.
Calin Juravled0d48522014-11-04 16:40:20 +00004614 HDivZeroCheck(HInstruction* value, uint32_t dex_pc)
Alexandre Rames780aece2016-01-13 14:34:39 +00004615 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Calin Juravled0d48522014-11-04 16:40:20 +00004616 SetRawInputAt(0, value);
4617 }
4618
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004619 Primitive::Type GetType() const OVERRIDE { return InputAt(0)->GetType(); }
4620
Calin Juravled0d48522014-11-04 16:40:20 +00004621 bool CanBeMoved() const OVERRIDE { return true; }
4622
Vladimir Marko372f10e2016-05-17 16:30:10 +01004623 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +00004624 return true;
4625 }
4626
4627 bool NeedsEnvironment() const OVERRIDE { return true; }
4628 bool CanThrow() const OVERRIDE { return true; }
4629
Calin Juravled0d48522014-11-04 16:40:20 +00004630 DECLARE_INSTRUCTION(DivZeroCheck);
4631
4632 private:
Calin Juravled0d48522014-11-04 16:40:20 +00004633 DISALLOW_COPY_AND_ASSIGN(HDivZeroCheck);
4634};
4635
Vladimir Markofcb503c2016-05-18 12:48:17 +01004636class HShl FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004637 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004638 HShl(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004639 HInstruction* value,
4640 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004641 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004642 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4643 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4644 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004645 }
4646
Roland Levillain5b5b9312016-03-22 14:57:31 +00004647 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004648 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004649 return value << (distance & max_shift_distance);
4650 }
4651
4652 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004653 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004654 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004655 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004656 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004657 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004658 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004659 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004660 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4661 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4662 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4663 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004664 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004665 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4666 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004667 LOG(FATAL) << DebugName() << " is not defined for float values";
4668 UNREACHABLE();
4669 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004670 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4671 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004672 LOG(FATAL) << DebugName() << " is not defined for double values";
4673 UNREACHABLE();
4674 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004675
4676 DECLARE_INSTRUCTION(Shl);
4677
4678 private:
4679 DISALLOW_COPY_AND_ASSIGN(HShl);
4680};
4681
Vladimir Markofcb503c2016-05-18 12:48:17 +01004682class HShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004683 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004684 HShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004685 HInstruction* value,
4686 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004687 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004688 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4689 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4690 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain9867bc72015-08-05 10:21:34 +01004691 }
4692
Roland Levillain5b5b9312016-03-22 14:57:31 +00004693 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004694 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004695 return value >> (distance & max_shift_distance);
4696 }
4697
4698 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004699 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004700 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004701 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004703 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004705 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004706 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4707 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4708 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4709 UNREACHABLE();
Roland Levillain9867bc72015-08-05 10:21:34 +01004710 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004711 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4712 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004713 LOG(FATAL) << DebugName() << " is not defined for float values";
4714 UNREACHABLE();
4715 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004716 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4717 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004718 LOG(FATAL) << DebugName() << " is not defined for double values";
4719 UNREACHABLE();
4720 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004721
4722 DECLARE_INSTRUCTION(Shr);
4723
4724 private:
4725 DISALLOW_COPY_AND_ASSIGN(HShr);
4726};
4727
Vladimir Markofcb503c2016-05-18 12:48:17 +01004728class HUShr FINAL : public HBinaryOperation {
Calin Juravle9aec02f2014-11-18 23:06:35 +00004729 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004730 HUShr(Primitive::Type result_type,
Roland Levillain5b5b9312016-03-22 14:57:31 +00004731 HInstruction* value,
4732 HInstruction* distance,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004733 uint32_t dex_pc = kNoDexPc)
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 : HBinaryOperation(result_type, value, distance, SideEffects::None(), dex_pc) {
4735 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4736 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004737 }
4738
Roland Levillain5b5b9312016-03-22 14:57:31 +00004739 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004740 static T Compute(T value, int32_t distance, int32_t max_shift_distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004741 typedef typename std::make_unsigned<T>::type V;
4742 V ux = static_cast<V>(value);
4743 return static_cast<T>(ux >> (distance & max_shift_distance));
4744 }
4745
4746 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004747 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004749 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004750 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Roland Levillain9867bc72015-08-05 10:21:34 +01004751 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004753 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004754 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4755 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4756 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4757 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004758 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004759 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4760 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004761 LOG(FATAL) << DebugName() << " is not defined for float values";
4762 UNREACHABLE();
4763 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004764 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4765 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004766 LOG(FATAL) << DebugName() << " is not defined for double values";
4767 UNREACHABLE();
4768 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004769
4770 DECLARE_INSTRUCTION(UShr);
4771
4772 private:
4773 DISALLOW_COPY_AND_ASSIGN(HUShr);
4774};
4775
Vladimir Markofcb503c2016-05-18 12:48:17 +01004776class HAnd FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004777 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004778 HAnd(Primitive::Type result_type,
4779 HInstruction* left,
4780 HInstruction* right,
4781 uint32_t dex_pc = kNoDexPc)
4782 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004783
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004784 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004785
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004786 template <typename T> static T Compute(T x, T y) { return x & y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004787
4788 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004789 return GetBlock()->GetGraph()->GetIntConstant(
4790 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004791 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004792 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004793 return GetBlock()->GetGraph()->GetLongConstant(
4794 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004795 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004796 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4797 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4798 LOG(FATAL) << DebugName() << " is not defined for float values";
4799 UNREACHABLE();
4800 }
4801 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4802 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4803 LOG(FATAL) << DebugName() << " is not defined for double values";
4804 UNREACHABLE();
4805 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004806
4807 DECLARE_INSTRUCTION(And);
4808
4809 private:
4810 DISALLOW_COPY_AND_ASSIGN(HAnd);
4811};
4812
Vladimir Markofcb503c2016-05-18 12:48:17 +01004813class HOr FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004814 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004815 HOr(Primitive::Type result_type,
4816 HInstruction* left,
4817 HInstruction* right,
4818 uint32_t dex_pc = kNoDexPc)
4819 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004820
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004821 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004822
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004823 template <typename T> static T Compute(T x, T y) { return x | y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004824
4825 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004826 return GetBlock()->GetGraph()->GetIntConstant(
4827 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004828 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004829 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004830 return GetBlock()->GetGraph()->GetLongConstant(
4831 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004832 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004833 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4834 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4835 LOG(FATAL) << DebugName() << " is not defined for float values";
4836 UNREACHABLE();
4837 }
4838 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4839 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4840 LOG(FATAL) << DebugName() << " is not defined for double values";
4841 UNREACHABLE();
4842 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004843
4844 DECLARE_INSTRUCTION(Or);
4845
4846 private:
4847 DISALLOW_COPY_AND_ASSIGN(HOr);
4848};
4849
Vladimir Markofcb503c2016-05-18 12:48:17 +01004850class HXor FINAL : public HBinaryOperation {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004851 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004852 HXor(Primitive::Type result_type,
4853 HInstruction* left,
4854 HInstruction* right,
4855 uint32_t dex_pc = kNoDexPc)
4856 : HBinaryOperation(result_type, left, right, SideEffects::None(), dex_pc) {}
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004857
Mingyao Yangdc5ac732015-02-25 11:28:05 -08004858 bool IsCommutative() const OVERRIDE { return true; }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004859
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004860 template <typename T> static T Compute(T x, T y) { return x ^ y; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004861
4862 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004863 return GetBlock()->GetGraph()->GetIntConstant(
4864 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004865 }
Roland Levillain9867bc72015-08-05 10:21:34 +01004866 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004867 return GetBlock()->GetGraph()->GetLongConstant(
4868 Compute(x->GetValue(), y->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004869 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00004870 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
4871 HFloatConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4872 LOG(FATAL) << DebugName() << " is not defined for float values";
4873 UNREACHABLE();
4874 }
4875 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
4876 HDoubleConstant* y ATTRIBUTE_UNUSED) const OVERRIDE {
4877 LOG(FATAL) << DebugName() << " is not defined for double values";
4878 UNREACHABLE();
4879 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00004880
4881 DECLARE_INSTRUCTION(Xor);
4882
4883 private:
4884 DISALLOW_COPY_AND_ASSIGN(HXor);
4885};
4886
Vladimir Markofcb503c2016-05-18 12:48:17 +01004887class HRor FINAL : public HBinaryOperation {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004888 public:
4889 HRor(Primitive::Type result_type, HInstruction* value, HInstruction* distance)
Roland Levillain22c49222016-03-18 14:04:28 +00004890 : HBinaryOperation(result_type, value, distance) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004891 DCHECK_EQ(result_type, Primitive::PrimitiveKind(value->GetType()));
4892 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(distance->GetType()));
Roland Levillain22c49222016-03-18 14:04:28 +00004893 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004894
Roland Levillain5b5b9312016-03-22 14:57:31 +00004895 template <typename T>
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004896 static T Compute(T value, int32_t distance, int32_t max_shift_value) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004897 typedef typename std::make_unsigned<T>::type V;
4898 V ux = static_cast<V>(value);
4899 if ((distance & max_shift_value) == 0) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004900 return static_cast<T>(ux);
4901 } else {
4902 const V reg_bits = sizeof(T) * 8;
Roland Levillain5b5b9312016-03-22 14:57:31 +00004903 return static_cast<T>(ux >> (distance & max_shift_value)) |
4904 (value << (reg_bits - (distance & max_shift_value)));
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004905 }
4906 }
4907
Roland Levillain5b5b9312016-03-22 14:57:31 +00004908 HConstant* Evaluate(HIntConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004909 return GetBlock()->GetGraph()->GetIntConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004910 Compute(value->GetValue(), distance->GetValue(), kMaxIntShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004911 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004912 HConstant* Evaluate(HLongConstant* value, HIntConstant* distance) const OVERRIDE {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004913 return GetBlock()->GetGraph()->GetLongConstant(
Roland Levillain5b5b9312016-03-22 14:57:31 +00004914 Compute(value->GetValue(), distance->GetValue(), kMaxLongShiftDistance), GetDexPc());
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004915 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004916 HConstant* Evaluate(HLongConstant* value ATTRIBUTE_UNUSED,
4917 HLongConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
4918 LOG(FATAL) << DebugName() << " is not defined for the (long, long) case.";
4919 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004920 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004921 HConstant* Evaluate(HFloatConstant* value ATTRIBUTE_UNUSED,
4922 HFloatConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004923 LOG(FATAL) << DebugName() << " is not defined for float values";
4924 UNREACHABLE();
4925 }
Roland Levillain5b5b9312016-03-22 14:57:31 +00004926 HConstant* Evaluate(HDoubleConstant* value ATTRIBUTE_UNUSED,
4927 HDoubleConstant* distance ATTRIBUTE_UNUSED) const OVERRIDE {
Roland Levillain31dd3d62016-02-16 12:21:02 +00004928 LOG(FATAL) << DebugName() << " is not defined for double values";
4929 UNREACHABLE();
4930 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004931
4932 DECLARE_INSTRUCTION(Ror);
4933
4934 private:
4935 DISALLOW_COPY_AND_ASSIGN(HRor);
4936};
4937
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004938// The value of a parameter in this method. Its location depends on
4939// the calling convention.
Vladimir Markofcb503c2016-05-18 12:48:17 +01004940class HParameterValue FINAL : public HExpression<0> {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004941 public:
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004942 HParameterValue(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08004943 dex::TypeIndex type_index,
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004944 uint8_t index,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004945 Primitive::Type parameter_type,
4946 bool is_this = false)
4947 : HExpression(parameter_type, SideEffects::None(), kNoDexPc),
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004948 dex_file_(dex_file),
4949 type_index_(type_index),
Vladimir Markoa1de9182016-02-25 11:37:38 +00004950 index_(index) {
4951 SetPackedFlag<kFlagIsThis>(is_this);
4952 SetPackedFlag<kFlagCanBeNull>(!is_this);
4953 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004954
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004955 const DexFile& GetDexFile() const { return dex_file_; }
Andreas Gampea5b09a62016-11-17 15:21:22 -08004956 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004957 uint8_t GetIndex() const { return index_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00004958 bool IsThis() const { return GetPackedFlag<kFlagIsThis>(); }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004959
Vladimir Markoa1de9182016-02-25 11:37:38 +00004960 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
4961 void SetCanBeNull(bool can_be_null) { SetPackedFlag<kFlagCanBeNull>(can_be_null); }
Calin Juravle10e244f2015-01-26 18:54:32 +00004962
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004963 DECLARE_INSTRUCTION(ParameterValue);
4964
4965 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00004966 // Whether or not the parameter value corresponds to 'this' argument.
4967 static constexpr size_t kFlagIsThis = kNumberOfExpressionPackedBits;
4968 static constexpr size_t kFlagCanBeNull = kFlagIsThis + 1;
4969 static constexpr size_t kNumberOfParameterValuePackedBits = kFlagCanBeNull + 1;
4970 static_assert(kNumberOfParameterValuePackedBits <= kMaxNumberOfPackedBits,
4971 "Too many packed fields.");
4972
Calin Juravlee6e3bea2015-10-14 13:53:10 +00004973 const DexFile& dex_file_;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004974 const dex::TypeIndex type_index_;
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004975 // The index of this parameter in the parameters list. Must be less
Calin Juravle10e244f2015-01-26 18:54:32 +00004976 // than HGraph::number_of_in_vregs_.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004977 const uint8_t index_;
4978
4979 DISALLOW_COPY_AND_ASSIGN(HParameterValue);
4980};
4981
Vladimir Markofcb503c2016-05-18 12:48:17 +01004982class HNot FINAL : public HUnaryOperation {
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004983 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004984 HNot(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc = kNoDexPc)
4985 : HUnaryOperation(result_type, input, dex_pc) {}
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004986
Alexandre Rames2ed20af2015-03-06 13:55:35 +00004987 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01004988 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004989 return true;
4990 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01004991
Anton Kirilov7fe30f92016-06-29 17:03:40 +01004992 template <typename T> static T Compute(T x) { return ~x; }
Roland Levillain9867bc72015-08-05 10:21:34 +01004993
4994 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004995 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004996 }
4997 HConstant* Evaluate(HLongConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06004998 return GetBlock()->GetGraph()->GetLongConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01004999 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005000 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5001 LOG(FATAL) << DebugName() << " is not defined for float values";
5002 UNREACHABLE();
5003 }
5004 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5005 LOG(FATAL) << DebugName() << " is not defined for double values";
5006 UNREACHABLE();
5007 }
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005008
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005009 DECLARE_INSTRUCTION(Not);
5010
5011 private:
5012 DISALLOW_COPY_AND_ASSIGN(HNot);
5013};
5014
Vladimir Markofcb503c2016-05-18 12:48:17 +01005015class HBooleanNot FINAL : public HUnaryOperation {
David Brazdil66d126e2015-04-03 16:02:44 +01005016 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005017 explicit HBooleanNot(HInstruction* input, uint32_t dex_pc = kNoDexPc)
5018 : HUnaryOperation(Primitive::Type::kPrimBoolean, input, dex_pc) {}
David Brazdil66d126e2015-04-03 16:02:44 +01005019
5020 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005021 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
David Brazdil66d126e2015-04-03 16:02:44 +01005022 return true;
5023 }
5024
Anton Kirilov7fe30f92016-06-29 17:03:40 +01005025 template <typename T> static bool Compute(T x) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005026 DCHECK(IsUint<1>(x)) << x;
David Brazdil66d126e2015-04-03 16:02:44 +01005027 return !x;
5028 }
5029
Roland Levillain9867bc72015-08-05 10:21:34 +01005030 HConstant* Evaluate(HIntConstant* x) const OVERRIDE {
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005031 return GetBlock()->GetGraph()->GetIntConstant(Compute(x->GetValue()), GetDexPc());
Roland Levillain9867bc72015-08-05 10:21:34 +01005032 }
5033 HConstant* Evaluate(HLongConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5034 LOG(FATAL) << DebugName() << " is not defined for long values";
David Brazdil66d126e2015-04-03 16:02:44 +01005035 UNREACHABLE();
5036 }
Roland Levillain31dd3d62016-02-16 12:21:02 +00005037 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5038 LOG(FATAL) << DebugName() << " is not defined for float values";
5039 UNREACHABLE();
5040 }
5041 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED) const OVERRIDE {
5042 LOG(FATAL) << DebugName() << " is not defined for double values";
5043 UNREACHABLE();
5044 }
David Brazdil66d126e2015-04-03 16:02:44 +01005045
5046 DECLARE_INSTRUCTION(BooleanNot);
5047
5048 private:
5049 DISALLOW_COPY_AND_ASSIGN(HBooleanNot);
5050};
5051
Vladimir Markofcb503c2016-05-18 12:48:17 +01005052class HTypeConversion FINAL : public HExpression<1> {
Roland Levillaindff1f282014-11-05 14:15:05 +00005053 public:
5054 // Instantiate a type conversion of `input` to `result_type`.
Roland Levillain624279f2014-12-04 11:54:28 +00005055 HTypeConversion(Primitive::Type result_type, HInstruction* input, uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005056 : HExpression(result_type, SideEffects::None(), dex_pc) {
Roland Levillaindff1f282014-11-05 14:15:05 +00005057 SetRawInputAt(0, input);
Roland Levillainf355c3f2016-03-30 19:09:03 +01005058 // Invariant: We should never generate a conversion to a Boolean value.
5059 DCHECK_NE(Primitive::kPrimBoolean, result_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00005060 }
5061
5062 HInstruction* GetInput() const { return InputAt(0); }
5063 Primitive::Type GetInputType() const { return GetInput()->GetType(); }
5064 Primitive::Type GetResultType() const { return GetType(); }
5065
5066 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005067 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
5068 return true;
5069 }
Roland Levillaindff1f282014-11-05 14:15:05 +00005070
Mark Mendelle82549b2015-05-06 10:55:34 -04005071 // Try to statically evaluate the conversion and return a HConstant
5072 // containing the result. If the input cannot be converted, return nullptr.
5073 HConstant* TryStaticEvaluation() const;
5074
Roland Levillaindff1f282014-11-05 14:15:05 +00005075 DECLARE_INSTRUCTION(TypeConversion);
5076
5077 private:
5078 DISALLOW_COPY_AND_ASSIGN(HTypeConversion);
5079};
5080
Nicolas Geoffray276d9da2015-02-02 18:24:11 +00005081static constexpr uint32_t kNoRegNumber = -1;
5082
Vladimir Markofcb503c2016-05-18 12:48:17 +01005083class HNullCheck FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005084 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005085 // `HNullCheck` can trigger GC, as it may call the `NullPointerException`
5086 // constructor.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005087 HNullCheck(HInstruction* value, uint32_t dex_pc)
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005088 : HExpression(value->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005089 SetRawInputAt(0, value);
5090 }
5091
Calin Juravle10e244f2015-01-26 18:54:32 +00005092 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005093 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005094 return true;
5095 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005096
Calin Juravle10e244f2015-01-26 18:54:32 +00005097 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005098
Calin Juravle10e244f2015-01-26 18:54:32 +00005099 bool CanThrow() const OVERRIDE { return true; }
5100
5101 bool CanBeNull() const OVERRIDE { return false; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005102
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005103
5104 DECLARE_INSTRUCTION(NullCheck);
5105
5106 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005107 DISALLOW_COPY_AND_ASSIGN(HNullCheck);
5108};
5109
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005110// Embeds an ArtField and all the information required by the compiler. We cache
5111// that information to avoid requiring the mutator lock every time we need it.
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005112class FieldInfo : public ValueObject {
5113 public:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005114 FieldInfo(ArtField* field,
5115 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005116 Primitive::Type field_type,
5117 bool is_volatile,
5118 uint32_t index,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005119 uint16_t declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005120 const DexFile& dex_file)
5121 : field_(field),
5122 field_offset_(field_offset),
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005123 field_type_(field_type),
5124 is_volatile_(is_volatile),
5125 index_(index),
Mingyao Yang8df69d42015-10-22 15:40:58 -07005126 declaring_class_def_index_(declaring_class_def_index),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005127 dex_file_(dex_file) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005128
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005129 ArtField* GetField() const { return field_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005130 MemberOffset GetFieldOffset() const { return field_offset_; }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005131 Primitive::Type GetFieldType() const { return field_type_; }
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005132 uint32_t GetFieldIndex() const { return index_; }
Mingyao Yang8df69d42015-10-22 15:40:58 -07005133 uint16_t GetDeclaringClassDefIndex() const { return declaring_class_def_index_;}
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005134 const DexFile& GetDexFile() const { return dex_file_; }
Calin Juravle52c48962014-12-16 17:02:57 +00005135 bool IsVolatile() const { return is_volatile_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005136
5137 private:
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005138 ArtField* const field_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139 const MemberOffset field_offset_;
Nicolas Geoffray39468442014-09-02 15:17:15 +01005140 const Primitive::Type field_type_;
Calin Juravle52c48962014-12-16 17:02:57 +00005141 const bool is_volatile_;
Mathieu Chartier736b5602015-09-02 14:54:11 -07005142 const uint32_t index_;
Mingyao Yang8df69d42015-10-22 15:40:58 -07005143 const uint16_t declaring_class_def_index_;
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005144 const DexFile& dex_file_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005145};
5146
Vladimir Markofcb503c2016-05-18 12:48:17 +01005147class HInstanceFieldGet FINAL : public HExpression<1> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005148 public:
5149 HInstanceFieldGet(HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005150 ArtField* field,
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005152 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005153 bool is_volatile,
5154 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005155 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005156 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005157 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005158 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005159 field_info_(field,
5160 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005161 field_type,
5162 is_volatile,
5163 field_idx,
5164 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005165 dex_file) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166 SetRawInputAt(0, value);
5167 }
5168
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005169 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005170
Vladimir Marko372f10e2016-05-17 16:30:10 +01005171 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5172 const HInstanceFieldGet* other_get = other->AsInstanceFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005173 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005174 }
5175
Calin Juravle641547a2015-04-21 22:08:51 +01005176 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005177 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005178 }
5179
5180 size_t ComputeHashCode() const OVERRIDE {
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01005181 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5182 }
5183
Calin Juravle52c48962014-12-16 17:02:57 +00005184 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005186 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005187 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005188
5189 DECLARE_INSTRUCTION(InstanceFieldGet);
5190
5191 private:
5192 const FieldInfo field_info_;
5193
5194 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldGet);
5195};
5196
Vladimir Markofcb503c2016-05-18 12:48:17 +01005197class HInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005198 public:
5199 HInstanceFieldSet(HInstruction* object,
5200 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005201 ArtField* field,
Nicolas Geoffray39468442014-09-02 15:17:15 +01005202 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005203 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005204 bool is_volatile,
5205 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005206 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005207 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005208 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005209 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005210 field_info_(field,
5211 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005212 field_type,
5213 is_volatile,
5214 field_idx,
5215 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005216 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005217 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005218 SetRawInputAt(0, object);
5219 SetRawInputAt(1, value);
5220 }
5221
Calin Juravle641547a2015-04-21 22:08:51 +01005222 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +01005223 return (obj == InputAt(0)) && art::CanDoImplicitNullCheckOn(GetFieldOffset().Uint32Value());
Calin Juravle77520bc2015-01-12 18:45:46 +00005224 }
5225
Calin Juravle52c48962014-12-16 17:02:57 +00005226 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005227 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005228 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005229 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005230 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005231 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5232 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005233
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005234 DECLARE_INSTRUCTION(InstanceFieldSet);
5235
5236 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005237 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
5238 static constexpr size_t kNumberOfInstanceFieldSetPackedBits = kFlagValueCanBeNull + 1;
5239 static_assert(kNumberOfInstanceFieldSetPackedBits <= kMaxNumberOfPackedBits,
5240 "Too many packed fields.");
5241
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005242 const FieldInfo field_info_;
5243
5244 DISALLOW_COPY_AND_ASSIGN(HInstanceFieldSet);
5245};
5246
Vladimir Markofcb503c2016-05-18 12:48:17 +01005247class HArrayGet FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005249 HArrayGet(HInstruction* array,
5250 HInstruction* index,
5251 Primitive::Type type,
5252 uint32_t dex_pc,
5253 bool is_string_char_at = false)
Aart Bik18b36ab2016-04-13 16:41:35 -07005254 : HExpression(type, SideEffects::ArrayReadOfType(type), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005255 SetPackedFlag<kFlagIsStringCharAt>(is_string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 SetRawInputAt(0, array);
5257 SetRawInputAt(1, index);
5258 }
5259
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005260 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005261 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005262 return true;
5263 }
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005264 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005265 // TODO: We can be smarter here.
5266 // Currently, the array access is always preceded by an ArrayLength or a NullCheck
5267 // which generates the implicit null check. There are cases when these can be removed
5268 // to produce better code. If we ever add optimizations to do so we should allow an
5269 // implicit check here (as long as the address falls in the first page).
5270 return false;
5271 }
5272
David Brazdil4833f5a2015-12-16 10:37:39 +00005273 bool IsEquivalentOf(HArrayGet* other) const {
5274 bool result = (GetDexPc() == other->GetDexPc());
5275 if (kIsDebugBuild && result) {
5276 DCHECK_EQ(GetBlock(), other->GetBlock());
5277 DCHECK_EQ(GetArray(), other->GetArray());
5278 DCHECK_EQ(GetIndex(), other->GetIndex());
5279 if (Primitive::IsIntOrLongType(GetType())) {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005280 DCHECK(Primitive::IsFloatingPointType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005281 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00005282 DCHECK(Primitive::IsFloatingPointType(GetType())) << GetType();
5283 DCHECK(Primitive::IsIntOrLongType(other->GetType())) << other->GetType();
David Brazdil4833f5a2015-12-16 10:37:39 +00005284 }
5285 }
5286 return result;
5287 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005288
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005289 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
5290
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005291 HInstruction* GetArray() const { return InputAt(0); }
5292 HInstruction* GetIndex() const { return InputAt(1); }
5293
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 DECLARE_INSTRUCTION(ArrayGet);
5295
5296 private:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005297 // We treat a String as an array, creating the HArrayGet from String.charAt()
5298 // intrinsic in the instruction simplifier. We can always determine whether
5299 // a particular HArrayGet is actually a String.charAt() by looking at the type
5300 // of the input but that requires holding the mutator lock, so we prefer to use
5301 // a flag, so that code generators don't need to do the locking.
5302 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
5303 static constexpr size_t kNumberOfArrayGetPackedBits = kFlagIsStringCharAt + 1;
5304 static_assert(kNumberOfArrayGetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5305 "Too many packed fields.");
5306
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 DISALLOW_COPY_AND_ASSIGN(HArrayGet);
5308};
5309
Vladimir Markofcb503c2016-05-18 12:48:17 +01005310class HArraySet FINAL : public HTemplateInstruction<3> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311 public:
5312 HArraySet(HInstruction* array,
5313 HInstruction* index,
5314 HInstruction* value,
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005315 Primitive::Type expected_component_type,
Aart Bik18b36ab2016-04-13 16:41:35 -07005316 uint32_t dex_pc)
5317 : HTemplateInstruction(SideEffects::None(), dex_pc) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005318 SetPackedField<ExpectedComponentTypeField>(expected_component_type);
5319 SetPackedFlag<kFlagNeedsTypeCheck>(value->GetType() == Primitive::kPrimNot);
5320 SetPackedFlag<kFlagValueCanBeNull>(true);
5321 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(false);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322 SetRawInputAt(0, array);
5323 SetRawInputAt(1, index);
5324 SetRawInputAt(2, value);
Aart Bik18b36ab2016-04-13 16:41:35 -07005325 // Make a best guess now, may be refined during SSA building.
5326 ComputeSideEffects();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327 }
5328
Calin Juravle77520bc2015-01-12 18:45:46 +00005329 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005330 // We call a runtime method to throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005331 return NeedsTypeCheck();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 }
5333
Mingyao Yang81014cb2015-06-02 03:16:27 -07005334 // Can throw ArrayStoreException.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005335 bool CanThrow() const OVERRIDE { return NeedsTypeCheck(); }
Mingyao Yang81014cb2015-06-02 03:16:27 -07005336
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005337 bool CanDoImplicitNullCheckOn(HInstruction* obj ATTRIBUTE_UNUSED) const OVERRIDE {
Calin Juravle77520bc2015-01-12 18:45:46 +00005338 // TODO: Same as for ArrayGet.
5339 return false;
5340 }
5341
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005342 void ClearNeedsTypeCheck() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005343 SetPackedFlag<kFlagNeedsTypeCheck>(false);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005344 }
5345
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005346 void ClearValueCanBeNull() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005347 SetPackedFlag<kFlagValueCanBeNull>(false);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005348 }
5349
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005350 void SetStaticTypeOfArrayIsObjectArray() {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005351 SetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>(true);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005352 }
5353
Vladimir Markoa1de9182016-02-25 11:37:38 +00005354 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5355 bool NeedsTypeCheck() const { return GetPackedFlag<kFlagNeedsTypeCheck>(); }
5356 bool StaticTypeOfArrayIsObjectArray() const {
5357 return GetPackedFlag<kFlagStaticTypeOfArrayIsObjectArray>();
5358 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005359
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005360 HInstruction* GetArray() const { return InputAt(0); }
5361 HInstruction* GetIndex() const { return InputAt(1); }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005362 HInstruction* GetValue() const { return InputAt(2); }
5363
5364 Primitive::Type GetComponentType() const {
5365 // The Dex format does not type floating point index operations. Since the
5366 // `expected_component_type_` is set during building and can therefore not
5367 // be correct, we also check what is the value type. If it is a floating
5368 // point type, we must use that type.
5369 Primitive::Type value_type = GetValue()->GetType();
5370 return ((value_type == Primitive::kPrimFloat) || (value_type == Primitive::kPrimDouble))
5371 ? value_type
Vladimir Markoa1de9182016-02-25 11:37:38 +00005372 : GetRawExpectedComponentType();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005373 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01005374
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005375 Primitive::Type GetRawExpectedComponentType() const {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005376 return GetPackedField<ExpectedComponentTypeField>();
Alexandre Ramese6dbf482015-10-19 10:10:41 +01005377 }
5378
Aart Bik18b36ab2016-04-13 16:41:35 -07005379 void ComputeSideEffects() {
5380 Primitive::Type type = GetComponentType();
5381 SetSideEffects(SideEffects::ArrayWriteOfType(type).Union(
5382 SideEffectsForArchRuntimeCalls(type)));
5383 }
5384
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005385 static SideEffects SideEffectsForArchRuntimeCalls(Primitive::Type value_type) {
5386 return (value_type == Primitive::kPrimNot) ? SideEffects::CanTriggerGC() : SideEffects::None();
5387 }
5388
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389 DECLARE_INSTRUCTION(ArraySet);
5390
5391 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00005392 static constexpr size_t kFieldExpectedComponentType = kNumberOfGenericPackedBits;
5393 static constexpr size_t kFieldExpectedComponentTypeSize =
5394 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
5395 static constexpr size_t kFlagNeedsTypeCheck =
5396 kFieldExpectedComponentType + kFieldExpectedComponentTypeSize;
5397 static constexpr size_t kFlagValueCanBeNull = kFlagNeedsTypeCheck + 1;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 // Cached information for the reference_type_info_ so that codegen
5399 // does not need to inspect the static type.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005400 static constexpr size_t kFlagStaticTypeOfArrayIsObjectArray = kFlagValueCanBeNull + 1;
5401 static constexpr size_t kNumberOfArraySetPackedBits =
5402 kFlagStaticTypeOfArrayIsObjectArray + 1;
5403 static_assert(kNumberOfArraySetPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
5404 using ExpectedComponentTypeField =
5405 BitField<Primitive::Type, kFieldExpectedComponentType, kFieldExpectedComponentTypeSize>;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406
5407 DISALLOW_COPY_AND_ASSIGN(HArraySet);
5408};
5409
Vladimir Markofcb503c2016-05-18 12:48:17 +01005410class HArrayLength FINAL : public HExpression<1> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 public:
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005412 HArrayLength(HInstruction* array, uint32_t dex_pc, bool is_string_length = false)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005413 : HExpression(Primitive::kPrimInt, SideEffects::None(), dex_pc) {
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005414 SetPackedFlag<kFlagIsStringLength>(is_string_length);
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005415 // Note that arrays do not change length, so the instruction does not
5416 // depend on any write.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 SetRawInputAt(0, array);
5418 }
5419
Calin Juravle77520bc2015-01-12 18:45:46 +00005420 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005421 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005422 return true;
5423 }
Calin Juravle641547a2015-04-21 22:08:51 +01005424 bool CanDoImplicitNullCheckOn(HInstruction* obj) const OVERRIDE {
5425 return obj == InputAt(0);
5426 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005427
Vladimir Markodce016e2016-04-28 13:10:02 +01005428 bool IsStringLength() const { return GetPackedFlag<kFlagIsStringLength>(); }
5429
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 DECLARE_INSTRUCTION(ArrayLength);
5431
5432 private:
Vladimir Markodce016e2016-04-28 13:10:02 +01005433 // We treat a String as an array, creating the HArrayLength from String.length()
5434 // or String.isEmpty() intrinsic in the instruction simplifier. We can always
5435 // determine whether a particular HArrayLength is actually a String.length() by
5436 // looking at the type of the input but that requires holding the mutator lock, so
5437 // we prefer to use a flag, so that code generators don't need to do the locking.
5438 static constexpr size_t kFlagIsStringLength = kNumberOfExpressionPackedBits;
5439 static constexpr size_t kNumberOfArrayLengthPackedBits = kFlagIsStringLength + 1;
5440 static_assert(kNumberOfArrayLengthPackedBits <= HInstruction::kMaxNumberOfPackedBits,
5441 "Too many packed fields.");
5442
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005443 DISALLOW_COPY_AND_ASSIGN(HArrayLength);
5444};
5445
Vladimir Markofcb503c2016-05-18 12:48:17 +01005446class HBoundsCheck FINAL : public HExpression<2> {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005447 public:
Nicolas Geoffray1af564e2016-01-13 12:09:39 +00005448 // `HBoundsCheck` can trigger GC, as it may call the `IndexOutOfBoundsException`
5449 // constructor.
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005450 HBoundsCheck(HInstruction* index,
5451 HInstruction* length,
5452 uint32_t dex_pc,
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005453 bool string_char_at = false)
5454 : HExpression(index->GetType(), SideEffects::CanTriggerGC(), dex_pc) {
David Brazdildee58d62016-04-07 09:54:26 +00005455 DCHECK_EQ(Primitive::kPrimInt, Primitive::PrimitiveKind(index->GetType()));
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005456 SetPackedFlag<kFlagIsStringCharAt>(string_char_at);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005457 SetRawInputAt(0, index);
5458 SetRawInputAt(1, length);
5459 }
5460
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005461 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005462 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07005463 return true;
5464 }
Nicolas Geoffray065bf772014-09-03 14:51:22 +01005465
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005466 bool NeedsEnvironment() const OVERRIDE { return true; }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005467
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005468 bool CanThrow() const OVERRIDE { return true; }
Roland Levillaine161a2a2014-10-03 12:45:18 +01005469
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005470 bool IsStringCharAt() const { return GetPackedFlag<kFlagIsStringCharAt>(); }
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005471
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:
Nicolas Geoffray5d37c152017-01-12 13:25:19 +00005477 static constexpr size_t kFlagIsStringCharAt = kNumberOfExpressionPackedBits;
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005478
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005479 DISALLOW_COPY_AND_ASSIGN(HBoundsCheck);
5480};
5481
Vladimir Markofcb503c2016-05-18 12:48:17 +01005482class HSuspendCheck FINAL : public HTemplateInstruction<0> {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005483 public:
David Brazdil86ea7ee2016-02-16 09:26:07 +00005484 explicit HSuspendCheck(uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005485 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc), slow_path_(nullptr) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005486
Alexandre Rames2ed20af2015-03-06 13:55:35 +00005487 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005488 return true;
5489 }
5490
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005491 void SetSlowPath(SlowPathCode* slow_path) { slow_path_ = slow_path; }
5492 SlowPathCode* GetSlowPath() const { return slow_path_; }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005493
5494 DECLARE_INSTRUCTION(SuspendCheck);
5495
5496 private:
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005497 // Only used for code generation, in order to share the same slow path between back edges
5498 // of a same loop.
5499 SlowPathCode* slow_path_;
5500
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005501 DISALLOW_COPY_AND_ASSIGN(HSuspendCheck);
5502};
5503
David Srbecky0cf44932015-12-09 14:09:59 +00005504// Pseudo-instruction which provides the native debugger with mapping information.
5505// It ensures that we can generate line number and local variables at this point.
5506class HNativeDebugInfo : public HTemplateInstruction<0> {
5507 public:
5508 explicit HNativeDebugInfo(uint32_t dex_pc)
5509 : HTemplateInstruction<0>(SideEffects::None(), dex_pc) {}
5510
5511 bool NeedsEnvironment() const OVERRIDE {
5512 return true;
5513 }
5514
5515 DECLARE_INSTRUCTION(NativeDebugInfo);
5516
5517 private:
5518 DISALLOW_COPY_AND_ASSIGN(HNativeDebugInfo);
5519};
5520
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005521/**
5522 * Instruction to load a Class object.
5523 */
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005524class HLoadClass FINAL : public HInstruction {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005525 public:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005526 // Determines how to load the Class.
5527 enum class LoadKind {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005528 // We cannot load this class. See HSharpening::SharpenLoadClass.
5529 kInvalid = -1,
5530
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005531 // Use the Class* from the method's own ArtMethod*.
5532 kReferrersClass,
5533
5534 // Use boot image Class* address that will be known at link time.
5535 // Used for boot image classes referenced by boot image code in non-PIC mode.
5536 kBootImageLinkTimeAddress,
5537
5538 // Use PC-relative boot image Class* address that will be known at link time.
5539 // Used for boot image classes referenced by boot image code in PIC mode.
5540 kBootImageLinkTimePcRelative,
5541
5542 // Use a known boot image Class* address, embedded in the code by the codegen.
5543 // Used for boot image classes referenced by apps in AOT- and JIT-compiled code.
5544 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5545 // GetIncludePatchInformation().
5546 kBootImageAddress,
5547
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005548 // Load from an entry in the .bss section using a PC-relative load.
5549 // Used for classes outside boot image when .bss is accessible with a PC-relative load.
5550 kBssEntry,
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 Geoffray5247c082017-01-13 14:17:29 +00005566 Handle<mirror::Class> klass,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005567 bool is_referrers_class,
Calin Juravle98893e12015-10-02 21:05:03 +01005568 uint32_t dex_pc,
Nicolas Geoffray56876342016-12-16 16:09:08 +00005569 bool needs_access_check)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005570 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5571 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572 type_index_(type_index),
Nicolas Geoffrayd5111bf2015-05-22 15:37:09 +01005573 dex_file_(dex_file),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005574 klass_(klass),
Calin Juravle2e768302015-07-28 14:41:11 +00005575 loaded_class_rti_(ReferenceTypeInfo::CreateInvalid()) {
Calin Juravle4e2a5572015-10-07 18:55:43 +01005576 // Referrers class should not need access check. We never inline unverified
5577 // methods so we can't possibly end up in this situation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00005578 DCHECK(!is_referrers_class || !needs_access_check);
5579
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005580 SetPackedField<LoadKindField>(
5581 is_referrers_class ? LoadKind::kReferrersClass : LoadKind::kDexCacheViaMethod);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005582 SetPackedFlag<kFlagNeedsAccessCheck>(needs_access_check);
Nicolas Geoffray56876342016-12-16 16:09:08 +00005583 SetPackedFlag<kFlagIsInBootImage>(false);
Vladimir Markoa1de9182016-02-25 11:37:38 +00005584 SetPackedFlag<kFlagGenerateClInitCheck>(false);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005585 }
5586
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005587 void SetLoadKind(LoadKind load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005588
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005589 LoadKind GetLoadKind() const {
5590 return GetPackedField<LoadKindField>();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005591 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005592
5593 bool CanBeMoved() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005594
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005595 bool InstructionDataEquals(const HInstruction* other) const;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596
Andreas Gampea5b09a62016-11-17 15:21:22 -08005597 size_t ComputeHashCode() const OVERRIDE { return type_index_.index_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005598
Nicolas Geoffray7d5ea032015-07-02 15:48:27 +01005599 bool CanBeNull() const OVERRIDE { return false; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005601 bool NeedsEnvironment() const OVERRIDE {
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005602 return CanCallRuntime();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603 }
5604
Calin Juravle0ba218d2015-05-19 18:46:01 +01005605 void SetMustGenerateClinitCheck(bool generate_clinit_check) {
Nicolas Geoffrayd9309292015-10-31 22:21:31 +00005606 // The entrypoint the code generator is going to call does not do
5607 // clinit of the class.
5608 DCHECK(!NeedsAccessCheck());
Vladimir Markoa1de9182016-02-25 11:37:38 +00005609 SetPackedFlag<kFlagGenerateClInitCheck>(generate_clinit_check);
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005610 }
5611
5612 bool CanCallRuntime() const {
Vladimir Marko41559982017-01-06 14:04:23 +00005613 return NeedsAccessCheck() ||
5614 MustGenerateClinitCheck() ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005615 GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5616 GetLoadKind() == LoadKind::kBssEntry;
Calin Juravle98893e12015-10-02 21:05:03 +01005617 }
5618
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005619 bool CanThrow() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005620 return NeedsAccessCheck() ||
5621 MustGenerateClinitCheck() ||
5622 // If the class is in the boot image, the lookup in the runtime call cannot throw.
5623 // This keeps CanThrow() consistent between non-PIC (using kBootImageAddress) and
5624 // PIC and subsequently avoids a DCE behavior dependency on the PIC option.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005625 ((GetLoadKind() == LoadKind::kDexCacheViaMethod ||
5626 GetLoadKind() == LoadKind::kBssEntry) &&
5627 !IsInBootImage());
Nicolas Geoffray82091da2015-01-26 10:02:45 +00005628 }
5629
Calin Juravleacf735c2015-02-12 15:25:22 +00005630 ReferenceTypeInfo GetLoadedClassRTI() {
5631 return loaded_class_rti_;
5632 }
5633
5634 void SetLoadedClassRTI(ReferenceTypeInfo rti) {
5635 // Make sure we only set exact types (the loaded class should never be merged).
5636 DCHECK(rti.IsExact());
5637 loaded_class_rti_ = rti;
5638 }
5639
Andreas Gampea5b09a62016-11-17 15:21:22 -08005640 dex::TypeIndex GetTypeIndex() const { return type_index_; }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005641 const DexFile& GetDexFile() const { return dex_file_; }
5642
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005643 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
Vladimir Marko41559982017-01-06 14:04:23 +00005644 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005645 }
Nicolas Geoffray9437b782015-03-25 10:08:51 +00005646
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005647 static SideEffects SideEffectsForArchRuntimeCalls() {
5648 return SideEffects::CanTriggerGC();
5649 }
5650
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005651 bool IsReferrersClass() const { return GetLoadKind() == LoadKind::kReferrersClass; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005652 bool NeedsAccessCheck() const { return GetPackedFlag<kFlagNeedsAccessCheck>(); }
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005653 bool IsInBootImage() const { return GetPackedFlag<kFlagIsInBootImage>(); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005654 bool MustGenerateClinitCheck() const { return GetPackedFlag<kFlagGenerateClInitCheck>(); }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005655
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005656 void MarkInBootImage() {
5657 SetPackedFlag<kFlagIsInBootImage>(true);
5658 }
5659
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005660 void AddSpecialInput(HInstruction* special_input);
5661
5662 using HInstruction::GetInputRecords; // Keep the const version visible.
5663 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5664 return ArrayRef<HUserRecord<HInstruction*>>(
5665 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
5666 }
5667
5668 Primitive::Type GetType() const OVERRIDE {
5669 return Primitive::kPrimNot;
5670 }
5671
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005672 Handle<mirror::Class> GetClass() const {
5673 return klass_;
5674 }
5675
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005676 DECLARE_INSTRUCTION(LoadClass);
5677
5678 private:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005679 static constexpr size_t kFlagNeedsAccessCheck = kNumberOfGenericPackedBits;
Vladimir Marko41559982017-01-06 14:04:23 +00005680 static constexpr size_t kFlagIsInBootImage = kFlagNeedsAccessCheck + 1;
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005681 // Whether this instruction must generate the initialization check.
5682 // Used for code generation.
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005683 static constexpr size_t kFlagGenerateClInitCheck = kFlagIsInBootImage + 1;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005684 static constexpr size_t kFieldLoadKind = kFlagGenerateClInitCheck + 1;
5685 static constexpr size_t kFieldLoadKindSize =
5686 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5687 static constexpr size_t kNumberOfLoadClassPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005688 static_assert(kNumberOfLoadClassPackedBits < kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005689 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
5690
5691 static bool HasTypeReference(LoadKind load_kind) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005692 return load_kind == LoadKind::kReferrersClass ||
5693 load_kind == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005694 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005695 load_kind == LoadKind::kBssEntry ||
5696 load_kind == LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005697 }
5698
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005699 void SetLoadKindInternal(LoadKind load_kind);
5700
5701 // The special input is the HCurrentMethod for kDexCacheViaMethod or kReferrersClass.
5702 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005703 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005704 HUserRecord<HInstruction*> special_input_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005705
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005706 // A type index and dex file where the class can be accessed. The dex file can be:
5707 // - The compiling method's dex file if the class is defined there too.
5708 // - The compiling method's dex file if the class is referenced there.
5709 // - The dex file where the class is defined. When the load kind can only be
5710 // kBssEntry or kDexCacheViaMethod, we cannot emit code for this `HLoadClass`.
Andreas Gampea5b09a62016-11-17 15:21:22 -08005711 const dex::TypeIndex type_index_;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005712 const DexFile& dex_file_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005713
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005714 Handle<mirror::Class> klass_;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715
Calin Juravleacf735c2015-02-12 15:25:22 +00005716 ReferenceTypeInfo loaded_class_rti_;
5717
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005718 DISALLOW_COPY_AND_ASSIGN(HLoadClass);
5719};
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005720std::ostream& operator<<(std::ostream& os, HLoadClass::LoadKind rhs);
5721
5722// Note: defined outside class to see operator<<(., HLoadClass::LoadKind).
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005723inline void HLoadClass::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005724 // The special input is used for PC-relative loads on some architectures,
5725 // including literal pool loads, which are PC-relative too.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005726 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005727 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005728 GetLoadKind() == LoadKind::kBootImageAddress ||
5729 GetLoadKind() == LoadKind::kBssEntry) << GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005730 DCHECK(special_input_.GetInstruction() == nullptr);
5731 special_input_ = HUserRecord<HInstruction*>(special_input);
5732 special_input->AddUseAt(this, 0);
5733}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734
Vladimir Marko372f10e2016-05-17 16:30:10 +01005735class HLoadString FINAL : public HInstruction {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005736 public:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005737 // Determines how to load the String.
5738 enum class LoadKind {
5739 // Use boot image String* address that will be known at link time.
5740 // Used for boot image strings referenced by boot image code in non-PIC mode.
5741 kBootImageLinkTimeAddress,
5742
5743 // Use PC-relative boot image String* address that will be known at link time.
5744 // Used for boot image strings referenced by boot image code in PIC mode.
5745 kBootImageLinkTimePcRelative,
5746
5747 // Use a known boot image String* address, embedded in the code by the codegen.
5748 // Used for boot image strings referenced by apps in AOT- and JIT-compiled code.
5749 // Note: codegen needs to emit a linker patch if indicated by compiler options'
5750 // GetIncludePatchInformation().
5751 kBootImageAddress,
5752
Vladimir Markoaad75c62016-10-03 08:46:48 +00005753 // Load from an entry in the .bss section using a PC-relative load.
5754 // Used for strings outside boot image when .bss is accessible with a PC-relative load.
5755 kBssEntry,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005756
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005757 // Load from the root table associated with the JIT compiled method.
5758 kJitTableAddress,
5759
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005760 // Load from resolved strings array accessed through the class loaded from
5761 // the compiled method's own ArtMethod*. This is the default access type when
5762 // all other types are unavailable.
5763 kDexCacheViaMethod,
5764
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005765 kLast = kDexCacheViaMethod,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005766 };
5767
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005768 HLoadString(HCurrentMethod* current_method,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005769 dex::StringIndex string_index,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005770 const DexFile& dex_file,
5771 uint32_t dex_pc)
Vladimir Marko372f10e2016-05-17 16:30:10 +01005772 : HInstruction(SideEffectsForArchRuntimeCalls(), dex_pc),
5773 special_input_(HUserRecord<HInstruction*>(current_method)),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005774 string_index_(string_index),
5775 dex_file_(dex_file) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005776 SetPackedField<LoadKindField>(LoadKind::kDexCacheViaMethod);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005777 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005778
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005779 void SetLoadKind(LoadKind load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005780
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005781 LoadKind GetLoadKind() const {
5782 return GetPackedField<LoadKindField>();
5783 }
5784
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005785 const DexFile& GetDexFile() const {
5786 return dex_file_;
5787 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005788
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005789 dex::StringIndex GetStringIndex() const {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005790 return string_index_;
5791 }
5792
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005793 Handle<mirror::String> GetString() const {
5794 return string_;
5795 }
5796
5797 void SetString(Handle<mirror::String> str) {
5798 string_ = str;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005799 }
5800
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005801 bool CanBeMoved() const OVERRIDE { return true; }
5802
Vladimir Marko372f10e2016-05-17 16:30:10 +01005803 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005804
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005805 size_t ComputeHashCode() const OVERRIDE { return string_index_.index_; }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005806
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005807 // Will call the runtime if we need to load the string through
5808 // the dex cache and the string is not guaranteed to be there yet.
5809 bool NeedsEnvironment() const OVERRIDE {
5810 LoadKind load_kind = GetLoadKind();
5811 if (load_kind == LoadKind::kBootImageLinkTimeAddress ||
5812 load_kind == LoadKind::kBootImageLinkTimePcRelative ||
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005813 load_kind == LoadKind::kBootImageAddress ||
5814 load_kind == LoadKind::kJitTableAddress) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005815 return false;
5816 }
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005817 return true;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005818 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005819
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005820 bool NeedsDexCacheOfDeclaringClass() const OVERRIDE {
5821 return GetLoadKind() == LoadKind::kDexCacheViaMethod;
5822 }
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00005823
Nicolas Geoffraye418dda2015-08-11 20:03:09 -07005824 bool CanBeNull() const OVERRIDE { return false; }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005825 bool CanThrow() const OVERRIDE { return NeedsEnvironment(); }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005826
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005827 static SideEffects SideEffectsForArchRuntimeCalls() {
5828 return SideEffects::CanTriggerGC();
5829 }
5830
Vladimir Marko372f10e2016-05-17 16:30:10 +01005831 void AddSpecialInput(HInstruction* special_input);
5832
5833 using HInstruction::GetInputRecords; // Keep the const version visible.
5834 ArrayRef<HUserRecord<HInstruction*>> GetInputRecords() OVERRIDE FINAL {
5835 return ArrayRef<HUserRecord<HInstruction*>>(
5836 &special_input_, (special_input_.GetInstruction() != nullptr) ? 1u : 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005837 }
5838
Vladimir Marko372f10e2016-05-17 16:30:10 +01005839 Primitive::Type GetType() const OVERRIDE {
5840 return Primitive::kPrimNot;
5841 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005842
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005843 DECLARE_INSTRUCTION(LoadString);
5844
5845 private:
Vladimir Marko4d1be4922017-01-06 14:43:11 +00005846 static constexpr size_t kFieldLoadKind = kNumberOfGenericPackedBits;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005847 static constexpr size_t kFieldLoadKindSize =
5848 MinimumBitsToStore(static_cast<size_t>(LoadKind::kLast));
5849 static constexpr size_t kNumberOfLoadStringPackedBits = kFieldLoadKind + kFieldLoadKindSize;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005850 static_assert(kNumberOfLoadStringPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005851 using LoadKindField = BitField<LoadKind, kFieldLoadKind, kFieldLoadKindSize>;
Vladimir Markoa1de9182016-02-25 11:37:38 +00005852
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005853 void SetLoadKindInternal(LoadKind load_kind);
5854
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 // The special input is the HCurrentMethod for kDexCacheViaMethod.
5856 // For other load kinds it's empty or possibly some architecture-specific instruction
Vladimir Marko48886c22017-01-06 11:45:47 +00005857 // for PC-relative loads, i.e. kBssEntry or kBootImageLinkTimePcRelative.
Vladimir Marko372f10e2016-05-17 16:30:10 +01005858 HUserRecord<HInstruction*> special_input_;
5859
Andreas Gampe8a0128a2016-11-28 07:38:35 -08005860 dex::StringIndex string_index_;
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005861 const DexFile& dex_file_;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005862
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005863 Handle<mirror::String> string_;
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005864
5865 DISALLOW_COPY_AND_ASSIGN(HLoadString);
5866};
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005867std::ostream& operator<<(std::ostream& os, HLoadString::LoadKind rhs);
5868
5869// Note: defined outside class to see operator<<(., HLoadString::LoadKind).
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005870inline void HLoadString::AddSpecialInput(HInstruction* special_input) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07005871 // The special input is used for PC-relative loads on some architectures,
5872 // including literal pool loads, which are PC-relative too.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005873 DCHECK(GetLoadKind() == LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00005874 GetLoadKind() == LoadKind::kBssEntry ||
Alexey Frunze06a46c42016-07-19 15:00:40 -07005875 GetLoadKind() == LoadKind::kBootImageLinkTimeAddress ||
5876 GetLoadKind() == LoadKind::kBootImageAddress) << GetLoadKind();
Vladimir Marko372f10e2016-05-17 16:30:10 +01005877 // HLoadString::GetInputRecords() returns an empty array at this point,
5878 // so use the GetInputRecords() from the base class to set the input record.
5879 DCHECK(special_input_.GetInstruction() == nullptr);
5880 special_input_ = HUserRecord<HInstruction*>(special_input);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005881 special_input->AddUseAt(this, 0);
5882}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005883
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005884/**
5885 * Performs an initialization check on its Class object input.
5886 */
Vladimir Markofcb503c2016-05-18 12:48:17 +01005887class HClinitCheck FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005888 public:
Roland Levillain3887c462015-08-12 18:15:42 +01005889 HClinitCheck(HLoadClass* constant, uint32_t dex_pc)
Aart Bik854a02b2015-07-14 16:07:00 -07005890 : HExpression(
Alexandre Rames78e3ef62015-08-12 13:43:29 +01005891 Primitive::kPrimNot,
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06005892 SideEffects::AllChanges(), // Assume write/read on all fields/arrays.
5893 dex_pc) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005894 SetRawInputAt(0, constant);
5895 }
5896
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005897 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01005898 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005899 return true;
5900 }
5901
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005902 bool NeedsEnvironment() const OVERRIDE {
5903 // May call runtime to initialize the class.
5904 return true;
5905 }
5906
Nicolas Geoffray729645a2015-11-19 13:29:02 +00005907 bool CanThrow() const OVERRIDE { return true; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005908
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005909 HLoadClass* GetLoadClass() const {
5910 DCHECK(InputAt(0)->IsLoadClass());
5911 return InputAt(0)->AsLoadClass();
5912 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005913
5914 DECLARE_INSTRUCTION(ClinitCheck);
5915
5916 private:
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917 DISALLOW_COPY_AND_ASSIGN(HClinitCheck);
5918};
5919
Vladimir Markofcb503c2016-05-18 12:48:17 +01005920class HStaticFieldGet FINAL : public HExpression<1> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005921 public:
5922 HStaticFieldGet(HInstruction* cls,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005923 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005924 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005925 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005926 bool is_volatile,
5927 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005928 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005929 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005930 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005931 : HExpression(field_type, SideEffects::FieldReadOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005932 field_info_(field,
5933 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005934 field_type,
5935 is_volatile,
5936 field_idx,
5937 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005938 dex_file) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005939 SetRawInputAt(0, cls);
5940 }
5941
Calin Juravle52c48962014-12-16 17:02:57 +00005942
Calin Juravle10c9cbe2014-12-19 10:50:19 +00005943 bool CanBeMoved() const OVERRIDE { return !IsVolatile(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005944
Vladimir Marko372f10e2016-05-17 16:30:10 +01005945 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
5946 const HStaticFieldGet* other_get = other->AsStaticFieldGet();
Calin Juravle52c48962014-12-16 17:02:57 +00005947 return GetFieldOffset().SizeValue() == other_get->GetFieldOffset().SizeValue();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005948 }
5949
5950 size_t ComputeHashCode() const OVERRIDE {
5951 return (HInstruction::ComputeHashCode() << 7) | GetFieldOffset().SizeValue();
5952 }
5953
Calin Juravle52c48962014-12-16 17:02:57 +00005954 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005955 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5956 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005957 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005958
5959 DECLARE_INSTRUCTION(StaticFieldGet);
5960
5961 private:
5962 const FieldInfo field_info_;
5963
5964 DISALLOW_COPY_AND_ASSIGN(HStaticFieldGet);
5965};
5966
Vladimir Markofcb503c2016-05-18 12:48:17 +01005967class HStaticFieldSet FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005968 public:
5969 HStaticFieldSet(HInstruction* cls,
5970 HInstruction* value,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005971 ArtField* field,
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005972 Primitive::Type field_type,
Calin Juravle52c48962014-12-16 17:02:57 +00005973 MemberOffset field_offset,
Guillaume "Vermeille" Sanchez104fd8a2015-05-20 17:52:13 +01005974 bool is_volatile,
5975 uint32_t field_idx,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005976 uint16_t declaring_class_def_index,
Mathieu Chartier736b5602015-09-02 14:54:11 -07005977 const DexFile& dex_file,
Calin Juravle154746b2015-10-06 15:46:54 +01005978 uint32_t dex_pc)
Alexandre Rames91a65162016-09-19 13:54:30 +01005979 : HTemplateInstruction(SideEffects::FieldWriteOfType(field_type, is_volatile), dex_pc),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005980 field_info_(field,
5981 field_offset,
Mingyao Yang8df69d42015-10-22 15:40:58 -07005982 field_type,
5983 is_volatile,
5984 field_idx,
5985 declaring_class_def_index,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +00005986 dex_file) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00005987 SetPackedFlag<kFlagValueCanBeNull>(true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005988 SetRawInputAt(0, cls);
5989 SetRawInputAt(1, value);
5990 }
5991
Calin Juravle52c48962014-12-16 17:02:57 +00005992 const FieldInfo& GetFieldInfo() const { return field_info_; }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005993 MemberOffset GetFieldOffset() const { return field_info_.GetFieldOffset(); }
5994 Primitive::Type GetFieldType() const { return field_info_.GetFieldType(); }
Calin Juravle52c48962014-12-16 17:02:57 +00005995 bool IsVolatile() const { return field_info_.IsVolatile(); }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005996
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005997 HInstruction* GetValue() const { return InputAt(1); }
Vladimir Markoa1de9182016-02-25 11:37:38 +00005998 bool GetValueCanBeNull() const { return GetPackedFlag<kFlagValueCanBeNull>(); }
5999 void ClearValueCanBeNull() { SetPackedFlag<kFlagValueCanBeNull>(false); }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006000
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006001 DECLARE_INSTRUCTION(StaticFieldSet);
6002
6003 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006004 static constexpr size_t kFlagValueCanBeNull = kNumberOfGenericPackedBits;
6005 static constexpr size_t kNumberOfStaticFieldSetPackedBits = kFlagValueCanBeNull + 1;
6006 static_assert(kNumberOfStaticFieldSetPackedBits <= kMaxNumberOfPackedBits,
6007 "Too many packed fields.");
6008
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006009 const FieldInfo field_info_;
6010
6011 DISALLOW_COPY_AND_ASSIGN(HStaticFieldSet);
6012};
6013
Vladimir Markofcb503c2016-05-18 12:48:17 +01006014class HUnresolvedInstanceFieldGet FINAL : public HExpression<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006015 public:
6016 HUnresolvedInstanceFieldGet(HInstruction* obj,
6017 Primitive::Type field_type,
6018 uint32_t field_index,
6019 uint32_t dex_pc)
6020 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6021 field_index_(field_index) {
6022 SetRawInputAt(0, obj);
6023 }
6024
6025 bool NeedsEnvironment() const OVERRIDE { return true; }
6026 bool CanThrow() const OVERRIDE { return true; }
6027
6028 Primitive::Type GetFieldType() const { return GetType(); }
6029 uint32_t GetFieldIndex() const { return field_index_; }
6030
6031 DECLARE_INSTRUCTION(UnresolvedInstanceFieldGet);
6032
6033 private:
6034 const uint32_t field_index_;
6035
6036 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldGet);
6037};
6038
Vladimir Markofcb503c2016-05-18 12:48:17 +01006039class HUnresolvedInstanceFieldSet FINAL : public HTemplateInstruction<2> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006040 public:
6041 HUnresolvedInstanceFieldSet(HInstruction* obj,
6042 HInstruction* value,
6043 Primitive::Type field_type,
6044 uint32_t field_index,
6045 uint32_t dex_pc)
6046 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006047 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006048 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006049 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006050 SetRawInputAt(0, obj);
6051 SetRawInputAt(1, value);
6052 }
6053
6054 bool NeedsEnvironment() const OVERRIDE { return true; }
6055 bool CanThrow() const OVERRIDE { return true; }
6056
Vladimir Markoa1de9182016-02-25 11:37:38 +00006057 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006058 uint32_t GetFieldIndex() const { return field_index_; }
6059
6060 DECLARE_INSTRUCTION(UnresolvedInstanceFieldSet);
6061
6062 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006063 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6064 static constexpr size_t kFieldFieldTypeSize =
6065 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6066 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6067 kFieldFieldType + kFieldFieldTypeSize;
6068 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6069 "Too many packed fields.");
6070 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6071
Calin Juravlee460d1d2015-09-29 04:52:17 +01006072 const uint32_t field_index_;
6073
6074 DISALLOW_COPY_AND_ASSIGN(HUnresolvedInstanceFieldSet);
6075};
6076
Vladimir Markofcb503c2016-05-18 12:48:17 +01006077class HUnresolvedStaticFieldGet FINAL : public HExpression<0> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006078 public:
6079 HUnresolvedStaticFieldGet(Primitive::Type field_type,
6080 uint32_t field_index,
6081 uint32_t dex_pc)
6082 : HExpression(field_type, SideEffects::AllExceptGCDependency(), dex_pc),
6083 field_index_(field_index) {
6084 }
6085
6086 bool NeedsEnvironment() const OVERRIDE { return true; }
6087 bool CanThrow() const OVERRIDE { return true; }
6088
6089 Primitive::Type GetFieldType() const { return GetType(); }
6090 uint32_t GetFieldIndex() const { return field_index_; }
6091
6092 DECLARE_INSTRUCTION(UnresolvedStaticFieldGet);
6093
6094 private:
6095 const uint32_t field_index_;
6096
6097 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldGet);
6098};
6099
Vladimir Markofcb503c2016-05-18 12:48:17 +01006100class HUnresolvedStaticFieldSet FINAL : public HTemplateInstruction<1> {
Calin Juravlee460d1d2015-09-29 04:52:17 +01006101 public:
6102 HUnresolvedStaticFieldSet(HInstruction* value,
6103 Primitive::Type field_type,
6104 uint32_t field_index,
6105 uint32_t dex_pc)
6106 : HTemplateInstruction(SideEffects::AllExceptGCDependency(), dex_pc),
Calin Juravlee460d1d2015-09-29 04:52:17 +01006107 field_index_(field_index) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006108 SetPackedField<FieldTypeField>(field_type);
David Brazdildee58d62016-04-07 09:54:26 +00006109 DCHECK_EQ(Primitive::PrimitiveKind(field_type), Primitive::PrimitiveKind(value->GetType()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01006110 SetRawInputAt(0, value);
6111 }
6112
6113 bool NeedsEnvironment() const OVERRIDE { return true; }
6114 bool CanThrow() const OVERRIDE { return true; }
6115
Vladimir Markoa1de9182016-02-25 11:37:38 +00006116 Primitive::Type GetFieldType() const { return GetPackedField<FieldTypeField>(); }
Calin Juravlee460d1d2015-09-29 04:52:17 +01006117 uint32_t GetFieldIndex() const { return field_index_; }
6118
6119 DECLARE_INSTRUCTION(UnresolvedStaticFieldSet);
6120
6121 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006122 static constexpr size_t kFieldFieldType = HInstruction::kNumberOfGenericPackedBits;
6123 static constexpr size_t kFieldFieldTypeSize =
6124 MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
6125 static constexpr size_t kNumberOfUnresolvedStaticFieldSetPackedBits =
6126 kFieldFieldType + kFieldFieldTypeSize;
6127 static_assert(kNumberOfUnresolvedStaticFieldSetPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6128 "Too many packed fields.");
6129 using FieldTypeField = BitField<Primitive::Type, kFieldFieldType, kFieldFieldTypeSize>;
6130
Calin Juravlee460d1d2015-09-29 04:52:17 +01006131 const uint32_t field_index_;
6132
6133 DISALLOW_COPY_AND_ASSIGN(HUnresolvedStaticFieldSet);
6134};
6135
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006136// Implement the move-exception DEX instruction.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006137class HLoadException FINAL : public HExpression<0> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006138 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006139 explicit HLoadException(uint32_t dex_pc = kNoDexPc)
6140 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc) {}
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006141
David Brazdilbbd733e2015-08-18 17:48:17 +01006142 bool CanBeNull() const OVERRIDE { return false; }
6143
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006144 DECLARE_INSTRUCTION(LoadException);
6145
6146 private:
6147 DISALLOW_COPY_AND_ASSIGN(HLoadException);
6148};
6149
David Brazdilcb1c0552015-08-04 16:22:25 +01006150// Implicit part of move-exception which clears thread-local exception storage.
6151// Must not be removed because the runtime expects the TLS to get cleared.
Vladimir Markofcb503c2016-05-18 12:48:17 +01006152class HClearException FINAL : public HTemplateInstruction<0> {
David Brazdilcb1c0552015-08-04 16:22:25 +01006153 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006154 explicit HClearException(uint32_t dex_pc = kNoDexPc)
6155 : HTemplateInstruction(SideEffects::AllWrites(), dex_pc) {}
David Brazdilcb1c0552015-08-04 16:22:25 +01006156
6157 DECLARE_INSTRUCTION(ClearException);
6158
6159 private:
6160 DISALLOW_COPY_AND_ASSIGN(HClearException);
6161};
6162
Vladimir Markofcb503c2016-05-18 12:48:17 +01006163class HThrow FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006164 public:
6165 HThrow(HInstruction* exception, uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006166 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006167 SetRawInputAt(0, exception);
6168 }
6169
6170 bool IsControlFlow() const OVERRIDE { return true; }
6171
6172 bool NeedsEnvironment() const OVERRIDE { return true; }
6173
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006174 bool CanThrow() const OVERRIDE { return true; }
6175
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006176
6177 DECLARE_INSTRUCTION(Throw);
6178
6179 private:
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006180 DISALLOW_COPY_AND_ASSIGN(HThrow);
6181};
6182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183/**
6184 * Implementation strategies for the code generator of a HInstanceOf
6185 * or `HCheckCast`.
6186 */
6187enum class TypeCheckKind {
Calin Juravle98893e12015-10-02 21:05:03 +01006188 kUnresolvedCheck, // Check against an unresolved type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 kExactCheck, // Can do a single class compare.
6190 kClassHierarchyCheck, // Can just walk the super class chain.
6191 kAbstractClassCheck, // Can just walk the super class chain, starting one up.
6192 kInterfaceCheck, // No optimization yet when checking against an interface.
6193 kArrayObjectCheck, // Can just check if the array is not primitive.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006194 kArrayCheck, // No optimization yet when checking against a generic array.
6195 kLast = kArrayCheck
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196};
6197
Roland Levillain86503782016-02-11 19:07:30 +00006198std::ostream& operator<<(std::ostream& os, TypeCheckKind rhs);
6199
Vladimir Markofcb503c2016-05-18 12:48:17 +01006200class HInstanceOf FINAL : public HExpression<2> {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006201 public:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006202 HInstanceOf(HInstruction* object,
6203 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006205 uint32_t dex_pc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006206 : HExpression(Primitive::kPrimBoolean,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 SideEffectsForArchRuntimeCalls(check_kind),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006208 dex_pc) {
6209 SetPackedField<TypeCheckKindField>(check_kind);
6210 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006211 SetRawInputAt(0, object);
6212 SetRawInputAt(1, constant);
6213 }
6214
6215 bool CanBeMoved() const OVERRIDE { return true; }
6216
Vladimir Marko372f10e2016-05-17 16:30:10 +01006217 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006218 return true;
6219 }
6220
6221 bool NeedsEnvironment() const OVERRIDE {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006222 return CanCallRuntime(GetTypeCheckKind());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006223 }
6224
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006225 // Used only in code generation.
Vladimir Markoa1de9182016-02-25 11:37:38 +00006226 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6227 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6228 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6229 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006230
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006231 static bool CanCallRuntime(TypeCheckKind check_kind) {
6232 // Mips currently does runtime calls for any other checks.
6233 return check_kind != TypeCheckKind::kExactCheck;
6234 }
6235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 static SideEffects SideEffectsForArchRuntimeCalls(TypeCheckKind check_kind) {
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006237 return CanCallRuntime(check_kind) ? SideEffects::CanTriggerGC() : SideEffects::None();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006238 }
6239
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006240 DECLARE_INSTRUCTION(InstanceOf);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006241
6242 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006243 static constexpr size_t kFieldTypeCheckKind = kNumberOfExpressionPackedBits;
6244 static constexpr size_t kFieldTypeCheckKindSize =
6245 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6246 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6247 static constexpr size_t kNumberOfInstanceOfPackedBits = kFlagMustDoNullCheck + 1;
6248 static_assert(kNumberOfInstanceOfPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6249 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006250
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006251 DISALLOW_COPY_AND_ASSIGN(HInstanceOf);
6252};
6253
Vladimir Markofcb503c2016-05-18 12:48:17 +01006254class HBoundType FINAL : public HExpression<1> {
Calin Juravleb1498f62015-02-16 13:13:29 +00006255 public:
Chih-Hung Hsieha5931182016-09-01 15:08:13 -07006256 explicit HBoundType(HInstruction* input, uint32_t dex_pc = kNoDexPc)
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006257 : HExpression(Primitive::kPrimNot, SideEffects::None(), dex_pc),
Vladimir Markoa1de9182016-02-25 11:37:38 +00006258 upper_bound_(ReferenceTypeInfo::CreateInvalid()) {
6259 SetPackedFlag<kFlagUpperCanBeNull>(true);
6260 SetPackedFlag<kFlagCanBeNull>(true);
Calin Juravle61d544b2015-02-23 16:46:57 +00006261 DCHECK_EQ(input->GetType(), Primitive::kPrimNot);
Calin Juravleb1498f62015-02-16 13:13:29 +00006262 SetRawInputAt(0, input);
6263 }
6264
David Brazdilf5552582015-12-27 13:36:12 +00006265 // {Get,Set}Upper* should only be used in reference type propagation.
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006266 const ReferenceTypeInfo& GetUpperBound() const { return upper_bound_; }
Vladimir Markoa1de9182016-02-25 11:37:38 +00006267 bool GetUpperCanBeNull() const { return GetPackedFlag<kFlagUpperCanBeNull>(); }
David Brazdilf5552582015-12-27 13:36:12 +00006268 void SetUpperBound(const ReferenceTypeInfo& upper_bound, bool can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006269
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006270 void SetCanBeNull(bool can_be_null) {
Vladimir Markoa1de9182016-02-25 11:37:38 +00006271 DCHECK(GetUpperCanBeNull() || !can_be_null);
6272 SetPackedFlag<kFlagCanBeNull>(can_be_null);
Calin Juravleb1498f62015-02-16 13:13:29 +00006273 }
6274
Vladimir Markoa1de9182016-02-25 11:37:38 +00006275 bool CanBeNull() const OVERRIDE { return GetPackedFlag<kFlagCanBeNull>(); }
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006276
Calin Juravleb1498f62015-02-16 13:13:29 +00006277 DECLARE_INSTRUCTION(BoundType);
6278
6279 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006280 // Represents the top constraint that can_be_null_ cannot exceed (i.e. if this
6281 // is false then CanBeNull() cannot be true).
6282 static constexpr size_t kFlagUpperCanBeNull = kNumberOfExpressionPackedBits;
6283 static constexpr size_t kFlagCanBeNull = kFlagUpperCanBeNull + 1;
6284 static constexpr size_t kNumberOfBoundTypePackedBits = kFlagCanBeNull + 1;
6285 static_assert(kNumberOfBoundTypePackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6286
Calin Juravleb1498f62015-02-16 13:13:29 +00006287 // Encodes the most upper class that this instruction can have. In other words
Calin Juravlea5ae3c32015-07-28 14:40:50 +00006288 // it is always the case that GetUpperBound().IsSupertypeOf(GetReferenceType()).
6289 // It is used to bound the type in cases like:
6290 // if (x instanceof ClassX) {
6291 // // uper_bound_ will be ClassX
6292 // }
David Brazdilf5552582015-12-27 13:36:12 +00006293 ReferenceTypeInfo upper_bound_;
Calin Juravleb1498f62015-02-16 13:13:29 +00006294
6295 DISALLOW_COPY_AND_ASSIGN(HBoundType);
6296};
6297
Vladimir Markofcb503c2016-05-18 12:48:17 +01006298class HCheckCast FINAL : public HTemplateInstruction<2> {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299 public:
6300 HCheckCast(HInstruction* object,
6301 HLoadClass* constant,
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 TypeCheckKind check_kind,
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006303 uint32_t dex_pc)
Vladimir Markoa1de9182016-02-25 11:37:38 +00006304 : HTemplateInstruction(SideEffects::CanTriggerGC(), dex_pc) {
6305 SetPackedField<TypeCheckKindField>(check_kind);
6306 SetPackedFlag<kFlagMustDoNullCheck>(true);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006307 SetRawInputAt(0, object);
6308 SetRawInputAt(1, constant);
6309 }
6310
6311 bool CanBeMoved() const OVERRIDE { return true; }
6312
Vladimir Marko372f10e2016-05-17 16:30:10 +01006313 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006314 return true;
6315 }
6316
6317 bool NeedsEnvironment() const OVERRIDE {
6318 // Instruction may throw a CheckCastError.
6319 return true;
6320 }
6321
6322 bool CanThrow() const OVERRIDE { return true; }
6323
Vladimir Markoa1de9182016-02-25 11:37:38 +00006324 bool MustDoNullCheck() const { return GetPackedFlag<kFlagMustDoNullCheck>(); }
6325 void ClearMustDoNullCheck() { SetPackedFlag<kFlagMustDoNullCheck>(false); }
6326 TypeCheckKind GetTypeCheckKind() const { return GetPackedField<TypeCheckKindField>(); }
6327 bool IsExactCheck() const { return GetTypeCheckKind() == TypeCheckKind::kExactCheck; }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006328
6329 DECLARE_INSTRUCTION(CheckCast);
6330
6331 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006332 static constexpr size_t kFieldTypeCheckKind = kNumberOfGenericPackedBits;
6333 static constexpr size_t kFieldTypeCheckKindSize =
6334 MinimumBitsToStore(static_cast<size_t>(TypeCheckKind::kLast));
6335 static constexpr size_t kFlagMustDoNullCheck = kFieldTypeCheckKind + kFieldTypeCheckKindSize;
6336 static constexpr size_t kNumberOfCheckCastPackedBits = kFlagMustDoNullCheck + 1;
6337 static_assert(kNumberOfCheckCastPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
6338 using TypeCheckKindField = BitField<TypeCheckKind, kFieldTypeCheckKind, kFieldTypeCheckKindSize>;
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006339
6340 DISALLOW_COPY_AND_ASSIGN(HCheckCast);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006341};
6342
Andreas Gampe26de38b2016-07-27 17:53:11 -07006343/**
6344 * @brief Memory barrier types (see "The JSR-133 Cookbook for Compiler Writers").
6345 * @details We define the combined barrier types that are actually required
6346 * by the Java Memory Model, rather than using exactly the terminology from
6347 * the JSR-133 cookbook. These should, in many cases, be replaced by acquire/release
6348 * primitives. Note that the JSR-133 cookbook generally does not deal with
6349 * store atomicity issues, and the recipes there are not always entirely sufficient.
6350 * The current recipe is as follows:
6351 * -# Use AnyStore ~= (LoadStore | StoreStore) ~= release barrier before volatile store.
6352 * -# Use AnyAny barrier after volatile store. (StoreLoad is as expensive.)
6353 * -# Use LoadAny barrier ~= (LoadLoad | LoadStore) ~= acquire barrier after each volatile load.
6354 * -# Use StoreStore barrier after all stores but before return from any constructor whose
6355 * class has final fields.
6356 * -# Use NTStoreStore to order non-temporal stores with respect to all later
6357 * store-to-memory instructions. Only generated together with non-temporal stores.
6358 */
6359enum MemBarrierKind {
6360 kAnyStore,
6361 kLoadAny,
6362 kStoreStore,
6363 kAnyAny,
6364 kNTStoreStore,
6365 kLastBarrierKind = kNTStoreStore
6366};
6367std::ostream& operator<<(std::ostream& os, const MemBarrierKind& kind);
6368
Vladimir Markofcb503c2016-05-18 12:48:17 +01006369class HMemoryBarrier FINAL : public HTemplateInstruction<0> {
Calin Juravle27df7582015-04-17 19:12:31 +01006370 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006371 explicit HMemoryBarrier(MemBarrierKind barrier_kind, uint32_t dex_pc = kNoDexPc)
Aart Bik34c3ba92015-07-20 14:08:59 -07006372 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006373 SideEffects::AllWritesAndReads(), dex_pc) { // Assume write/read on all fields/arrays.
6374 SetPackedField<BarrierKindField>(barrier_kind);
6375 }
Calin Juravle27df7582015-04-17 19:12:31 +01006376
Vladimir Markoa1de9182016-02-25 11:37:38 +00006377 MemBarrierKind GetBarrierKind() { return GetPackedField<BarrierKindField>(); }
Calin Juravle27df7582015-04-17 19:12:31 +01006378
6379 DECLARE_INSTRUCTION(MemoryBarrier);
6380
6381 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006382 static constexpr size_t kFieldBarrierKind = HInstruction::kNumberOfGenericPackedBits;
6383 static constexpr size_t kFieldBarrierKindSize =
6384 MinimumBitsToStore(static_cast<size_t>(kLastBarrierKind));
6385 static constexpr size_t kNumberOfMemoryBarrierPackedBits =
6386 kFieldBarrierKind + kFieldBarrierKindSize;
6387 static_assert(kNumberOfMemoryBarrierPackedBits <= kMaxNumberOfPackedBits,
6388 "Too many packed fields.");
6389 using BarrierKindField = BitField<MemBarrierKind, kFieldBarrierKind, kFieldBarrierKindSize>;
Calin Juravle27df7582015-04-17 19:12:31 +01006390
6391 DISALLOW_COPY_AND_ASSIGN(HMemoryBarrier);
6392};
6393
Vladimir Markofcb503c2016-05-18 12:48:17 +01006394class HMonitorOperation FINAL : public HTemplateInstruction<1> {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006395 public:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006396 enum class OperationKind {
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006397 kEnter,
6398 kExit,
Vladimir Markoa1de9182016-02-25 11:37:38 +00006399 kLast = kExit
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006400 };
6401
6402 HMonitorOperation(HInstruction* object, OperationKind kind, uint32_t dex_pc)
Alexandre Rames78e3ef62015-08-12 13:43:29 +01006403 : HTemplateInstruction(
Vladimir Markoa1de9182016-02-25 11:37:38 +00006404 SideEffects::AllExceptGCDependency(), // Assume write/read on all fields/arrays.
6405 dex_pc) {
6406 SetPackedField<OperationKindField>(kind);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006407 SetRawInputAt(0, object);
6408 }
6409
Nicolas Geoffray03196cf2016-02-01 12:23:22 +00006410 // Instruction may go into runtime, so we need an environment.
6411 bool NeedsEnvironment() const OVERRIDE { return true; }
David Brazdilbff75032015-07-08 17:26:51 +00006412
6413 bool CanThrow() const OVERRIDE {
6414 // Verifier guarantees that monitor-exit cannot throw.
6415 // This is important because it allows the HGraphBuilder to remove
6416 // a dead throw-catch loop generated for `synchronized` blocks/methods.
6417 return IsEnter();
6418 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006419
Vladimir Markoa1de9182016-02-25 11:37:38 +00006420 OperationKind GetOperationKind() const { return GetPackedField<OperationKindField>(); }
6421 bool IsEnter() const { return GetOperationKind() == OperationKind::kEnter; }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006422
6423 DECLARE_INSTRUCTION(MonitorOperation);
6424
Calin Juravle52c48962014-12-16 17:02:57 +00006425 private:
Vladimir Markoa1de9182016-02-25 11:37:38 +00006426 static constexpr size_t kFieldOperationKind = HInstruction::kNumberOfGenericPackedBits;
6427 static constexpr size_t kFieldOperationKindSize =
6428 MinimumBitsToStore(static_cast<size_t>(OperationKind::kLast));
6429 static constexpr size_t kNumberOfMonitorOperationPackedBits =
6430 kFieldOperationKind + kFieldOperationKindSize;
6431 static_assert(kNumberOfMonitorOperationPackedBits <= HInstruction::kMaxNumberOfPackedBits,
6432 "Too many packed fields.");
6433 using OperationKindField = BitField<OperationKind, kFieldOperationKind, kFieldOperationKindSize>;
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006434
6435 private:
6436 DISALLOW_COPY_AND_ASSIGN(HMonitorOperation);
6437};
6438
Vladimir Markofcb503c2016-05-18 12:48:17 +01006439class HSelect FINAL : public HExpression<3> {
David Brazdil74eb1b22015-12-14 11:44:01 +00006440 public:
6441 HSelect(HInstruction* condition,
6442 HInstruction* true_value,
6443 HInstruction* false_value,
6444 uint32_t dex_pc)
6445 : HExpression(HPhi::ToPhiType(true_value->GetType()), SideEffects::None(), dex_pc) {
6446 DCHECK_EQ(HPhi::ToPhiType(true_value->GetType()), HPhi::ToPhiType(false_value->GetType()));
6447
6448 // First input must be `true_value` or `false_value` to allow codegens to
6449 // use the SameAsFirstInput allocation policy. We make it `false_value`, so
6450 // that architectures which implement HSelect as a conditional move also
6451 // will not need to invert the condition.
6452 SetRawInputAt(0, false_value);
6453 SetRawInputAt(1, true_value);
6454 SetRawInputAt(2, condition);
6455 }
6456
6457 HInstruction* GetFalseValue() const { return InputAt(0); }
6458 HInstruction* GetTrueValue() const { return InputAt(1); }
6459 HInstruction* GetCondition() const { return InputAt(2); }
6460
6461 bool CanBeMoved() const OVERRIDE { return true; }
Vladimir Marko372f10e2016-05-17 16:30:10 +01006462 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const OVERRIDE {
6463 return true;
6464 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006465
6466 bool CanBeNull() const OVERRIDE {
6467 return GetTrueValue()->CanBeNull() || GetFalseValue()->CanBeNull();
6468 }
6469
6470 DECLARE_INSTRUCTION(Select);
6471
6472 private:
6473 DISALLOW_COPY_AND_ASSIGN(HSelect);
6474};
6475
Vladimir Markof9f64412015-09-02 14:05:49 +01006476class MoveOperands : public ArenaObject<kArenaAllocMoveOperands> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006477 public:
Nicolas Geoffray90218252015-04-15 11:56:51 +01006478 MoveOperands(Location source,
6479 Location destination,
6480 Primitive::Type type,
6481 HInstruction* instruction)
6482 : source_(source), destination_(destination), type_(type), instruction_(instruction) {}
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006483
6484 Location GetSource() const { return source_; }
6485 Location GetDestination() const { return destination_; }
6486
6487 void SetSource(Location value) { source_ = value; }
6488 void SetDestination(Location value) { destination_ = value; }
6489
6490 // The parallel move resolver marks moves as "in-progress" by clearing the
6491 // destination (but not the source).
6492 Location MarkPending() {
6493 DCHECK(!IsPending());
6494 Location dest = destination_;
6495 destination_ = Location::NoLocation();
6496 return dest;
6497 }
6498
6499 void ClearPending(Location dest) {
6500 DCHECK(IsPending());
6501 destination_ = dest;
6502 }
6503
6504 bool IsPending() const {
Roland Levillainc9285912015-12-18 10:38:42 +00006505 DCHECK(source_.IsValid() || destination_.IsInvalid());
6506 return destination_.IsInvalid() && source_.IsValid();
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006507 }
6508
6509 // True if this blocks a move from the given location.
6510 bool Blocks(Location loc) const {
Zheng Xuad4450e2015-04-17 18:48:56 +08006511 return !IsEliminated() && source_.OverlapsWith(loc);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006512 }
6513
6514 // A move is redundant if it's been eliminated, if its source and
6515 // destination are the same, or if its destination is unneeded.
6516 bool IsRedundant() const {
6517 return IsEliminated() || destination_.IsInvalid() || source_.Equals(destination_);
6518 }
6519
6520 // We clear both operands to indicate move that's been eliminated.
6521 void Eliminate() {
6522 source_ = destination_ = Location::NoLocation();
6523 }
6524
6525 bool IsEliminated() const {
6526 DCHECK(!source_.IsInvalid() || destination_.IsInvalid());
6527 return source_.IsInvalid();
6528 }
6529
Alexey Frunze4dda3372015-06-01 18:31:49 -07006530 Primitive::Type GetType() const { return type_; }
6531
Nicolas Geoffray90218252015-04-15 11:56:51 +01006532 bool Is64BitMove() const {
6533 return Primitive::Is64BitType(type_);
6534 }
6535
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006536 HInstruction* GetInstruction() const { return instruction_; }
6537
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006538 private:
6539 Location source_;
6540 Location destination_;
Nicolas Geoffray90218252015-04-15 11:56:51 +01006541 // The type this move is for.
6542 Primitive::Type type_;
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006543 // The instruction this move is assocatied with. Null when this move is
6544 // for moving an input in the expected locations of user (including a phi user).
6545 // This is only used in debug mode, to ensure we do not connect interval siblings
6546 // in the same parallel move.
6547 HInstruction* instruction_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006548};
6549
Roland Levillainc9285912015-12-18 10:38:42 +00006550std::ostream& operator<<(std::ostream& os, const MoveOperands& rhs);
6551
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006552static constexpr size_t kDefaultNumberOfMoves = 4;
6553
Vladimir Markofcb503c2016-05-18 12:48:17 +01006554class HParallelMove FINAL : public HTemplateInstruction<0> {
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006555 public:
Yevgeny Rouban3ecfd652015-09-07 17:57:00 +06006556 explicit HParallelMove(ArenaAllocator* arena, uint32_t dex_pc = kNoDexPc)
Vladimir Marko225b6462015-09-28 12:17:40 +01006557 : HTemplateInstruction(SideEffects::None(), dex_pc),
6558 moves_(arena->Adapter(kArenaAllocMoveOperands)) {
6559 moves_.reserve(kDefaultNumberOfMoves);
6560 }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006561
Nicolas Geoffray90218252015-04-15 11:56:51 +01006562 void AddMove(Location source,
6563 Location destination,
6564 Primitive::Type type,
6565 HInstruction* instruction) {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006566 DCHECK(source.IsValid());
6567 DCHECK(destination.IsValid());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006568 if (kIsDebugBuild) {
6569 if (instruction != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006570 for (const MoveOperands& move : moves_) {
6571 if (move.GetInstruction() == instruction) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006572 // Special case the situation where the move is for the spill slot
6573 // of the instruction.
6574 if ((GetPrevious() == instruction)
6575 || ((GetPrevious() == nullptr)
6576 && instruction->IsPhi()
6577 && instruction->GetBlock() == GetBlock())) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006578 DCHECK_NE(destination.GetKind(), move.GetDestination().GetKind())
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006579 << "Doing parallel moves for the same instruction.";
6580 } else {
6581 DCHECK(false) << "Doing parallel moves for the same instruction.";
6582 }
6583 }
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00006584 }
6585 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006586 for (const MoveOperands& move : moves_) {
6587 DCHECK(!destination.OverlapsWith(move.GetDestination()))
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006588 << "Overlapped destination for two moves in a parallel move: "
Vladimir Marko225b6462015-09-28 12:17:40 +01006589 << move.GetSource() << " ==> " << move.GetDestination() << " and "
Guillaume "Vermeille" Sanchez8909baf2015-04-23 21:35:11 +01006590 << source << " ==> " << destination;
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006591 }
Nicolas Geoffray740475d2014-09-29 10:33:25 +01006592 }
Vladimir Marko225b6462015-09-28 12:17:40 +01006593 moves_.emplace_back(source, destination, type, instruction);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006594 }
6595
Vladimir Marko225b6462015-09-28 12:17:40 +01006596 MoveOperands* MoveOperandsAt(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006597 return &moves_[index];
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006598 }
6599
Vladimir Marko225b6462015-09-28 12:17:40 +01006600 size_t NumMoves() const { return moves_.size(); }
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006601
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01006602 DECLARE_INSTRUCTION(ParallelMove);
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006603
6604 private:
Vladimir Marko225b6462015-09-28 12:17:40 +01006605 ArenaVector<MoveOperands> moves_;
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006606
6607 DISALLOW_COPY_AND_ASSIGN(HParallelMove);
6608};
6609
Mark Mendell0616ae02015-04-17 12:49:27 -04006610} // namespace art
6611
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03006612#if defined(ART_ENABLE_CODEGEN_arm) || defined(ART_ENABLE_CODEGEN_arm64)
6613#include "nodes_shared.h"
6614#endif
Vladimir Markob4536b72015-11-24 13:45:23 +00006615#ifdef ART_ENABLE_CODEGEN_arm
6616#include "nodes_arm.h"
6617#endif
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006618#ifdef ART_ENABLE_CODEGEN_mips
6619#include "nodes_mips.h"
6620#endif
Mark Mendell0616ae02015-04-17 12:49:27 -04006621#ifdef ART_ENABLE_CODEGEN_x86
6622#include "nodes_x86.h"
6623#endif
6624
6625namespace art {
6626
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006627class HGraphVisitor : public ValueObject {
6628 public:
Dave Allison20dfc792014-06-16 20:44:29 -07006629 explicit HGraphVisitor(HGraph* graph) : graph_(graph) {}
6630 virtual ~HGraphVisitor() {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006631
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006632 virtual void VisitInstruction(HInstruction* instruction ATTRIBUTE_UNUSED) {}
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006633 virtual void VisitBasicBlock(HBasicBlock* block);
6634
Roland Levillain633021e2014-10-01 14:12:25 +01006635 // Visit the graph following basic block insertion order.
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006636 void VisitInsertionOrder();
6637
Roland Levillain633021e2014-10-01 14:12:25 +01006638 // Visit the graph following dominator tree reverse post-order.
6639 void VisitReversePostOrder();
6640
Nicolas Geoffray787c3072014-03-17 10:20:19 +00006641 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006642
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006643 // Visit functions for instruction classes.
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006644#define DECLARE_VISIT_INSTRUCTION(name, super) \
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006645 virtual void Visit##name(H##name* instr) { VisitInstruction(instr); }
6646
6647 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6648
6649#undef DECLARE_VISIT_INSTRUCTION
6650
6651 private:
Ian Rogerscf7f1912014-10-22 22:06:39 -07006652 HGraph* const graph_;
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006653
6654 DISALLOW_COPY_AND_ASSIGN(HGraphVisitor);
6655};
6656
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006657class HGraphDelegateVisitor : public HGraphVisitor {
6658 public:
6659 explicit HGraphDelegateVisitor(HGraph* graph) : HGraphVisitor(graph) {}
6660 virtual ~HGraphDelegateVisitor() {}
6661
6662 // Visit functions that delegate to to super class.
6663#define DECLARE_VISIT_INSTRUCTION(name, super) \
Alexandre Rames2ed20af2015-03-06 13:55:35 +00006664 void Visit##name(H##name* instr) OVERRIDE { Visit##super(instr); }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01006665
6666 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
6667
6668#undef DECLARE_VISIT_INSTRUCTION
6669
6670 private:
6671 DISALLOW_COPY_AND_ASSIGN(HGraphDelegateVisitor);
6672};
6673
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006674// Iterator over the blocks that art part of the loop. Includes blocks part
6675// of an inner loop. The order in which the blocks are iterated is on their
6676// block id.
6677class HBlocksInLoopIterator : public ValueObject {
6678 public:
6679 explicit HBlocksInLoopIterator(const HLoopInformation& info)
6680 : blocks_in_loop_(info.GetBlocks()),
6681 blocks_(info.GetHeader()->GetGraph()->GetBlocks()),
6682 index_(0) {
6683 if (!blocks_in_loop_.IsBitSet(index_)) {
6684 Advance();
6685 }
6686 }
6687
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006688 bool Done() const { return index_ == blocks_.size(); }
6689 HBasicBlock* Current() const { return blocks_[index_]; }
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006690 void Advance() {
6691 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006692 for (size_t e = blocks_.size(); index_ < e; ++index_) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006693 if (blocks_in_loop_.IsBitSet(index_)) {
6694 break;
6695 }
6696 }
6697 }
6698
6699 private:
6700 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006701 const ArenaVector<HBasicBlock*>& blocks_;
Nicolas Geoffray82091da2015-01-26 10:02:45 +00006702 size_t index_;
6703
6704 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopIterator);
6705};
6706
Mingyao Yang3584bce2015-05-19 16:01:59 -07006707// Iterator over the blocks that art part of the loop. Includes blocks part
6708// of an inner loop. The order in which the blocks are iterated is reverse
6709// post order.
6710class HBlocksInLoopReversePostOrderIterator : public ValueObject {
6711 public:
6712 explicit HBlocksInLoopReversePostOrderIterator(const HLoopInformation& info)
6713 : blocks_in_loop_(info.GetBlocks()),
6714 blocks_(info.GetHeader()->GetGraph()->GetReversePostOrder()),
6715 index_(0) {
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006716 if (!blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006717 Advance();
6718 }
6719 }
6720
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006721 bool Done() const { return index_ == blocks_.size(); }
6722 HBasicBlock* Current() const { return blocks_[index_]; }
Mingyao Yang3584bce2015-05-19 16:01:59 -07006723 void Advance() {
6724 ++index_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006725 for (size_t e = blocks_.size(); index_ < e; ++index_) {
6726 if (blocks_in_loop_.IsBitSet(blocks_[index_]->GetBlockId())) {
Mingyao Yang3584bce2015-05-19 16:01:59 -07006727 break;
6728 }
6729 }
6730 }
6731
6732 private:
6733 const BitVector& blocks_in_loop_;
Vladimir Markofa6b93c2015-09-15 10:15:55 +01006734 const ArenaVector<HBasicBlock*>& blocks_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07006735 size_t index_;
6736
6737 DISALLOW_COPY_AND_ASSIGN(HBlocksInLoopReversePostOrderIterator);
6738};
6739
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006740inline int64_t Int64FromConstant(HConstant* constant) {
David Brazdilc0b601b2016-02-08 14:20:45 +00006741 if (constant->IsIntConstant()) {
6742 return constant->AsIntConstant()->GetValue();
6743 } else if (constant->IsLongConstant()) {
6744 return constant->AsLongConstant()->GetValue();
6745 } else {
Roland Levillain31dd3d62016-02-16 12:21:02 +00006746 DCHECK(constant->IsNullConstant()) << constant->DebugName();
David Brazdilc0b601b2016-02-08 14:20:45 +00006747 return 0;
6748 }
Alexandre Ramesb2fd7bc2015-03-11 16:48:16 +00006749}
6750
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00006751#define INSTRUCTION_TYPE_CHECK(type, super) \
6752 inline bool HInstruction::Is##type() const { return GetKind() == k##type; } \
6753 inline const H##type* HInstruction::As##type() const { \
6754 return Is##type() ? down_cast<const H##type*>(this) : nullptr; \
6755 } \
6756 inline H##type* HInstruction::As##type() { \
6757 return Is##type() ? static_cast<H##type*>(this) : nullptr; \
6758 }
6759
6760 FOR_EACH_CONCRETE_INSTRUCTION(INSTRUCTION_TYPE_CHECK)
6761#undef INSTRUCTION_TYPE_CHECK
6762
Nicolas Geoffray916cc1d2016-02-18 11:12:31 +00006763// Create space in `blocks` for adding `number_of_new_blocks` entries
6764// starting at location `at`. Blocks after `at` are moved accordingly.
6765inline void MakeRoomFor(ArenaVector<HBasicBlock*>* blocks,
6766 size_t number_of_new_blocks,
6767 size_t after) {
6768 DCHECK_LT(after, blocks->size());
6769 size_t old_size = blocks->size();
6770 size_t new_size = old_size + number_of_new_blocks;
6771 blocks->resize(new_size);
6772 std::copy_backward(blocks->begin() + after + 1u, blocks->begin() + old_size, blocks->end());
6773}
6774
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00006775/*
6776 * Hunt "under the hood" of array lengths (leading to array references),
6777 * null checks (also leading to array references), and new arrays
6778 * (leading to the actual length). This makes it more likely related
6779 * instructions become actually comparable.
6780 */
6781inline HInstruction* HuntForDeclaration(HInstruction* instruction) {
6782 while (instruction->IsArrayLength() ||
6783 instruction->IsNullCheck() ||
6784 instruction->IsNewArray()) {
6785 instruction = instruction->IsNewArray()
6786 ? instruction->AsNewArray()->GetLength()
6787 : instruction->InputAt(0);
6788 }
6789 return instruction;
6790}
6791
Nicolas Geoffray818f2102014-02-18 16:43:35 +00006792} // namespace art
6793
6794#endif // ART_COMPILER_OPTIMIZING_NODES_H_